Re: Having TemplateException or NumberFormatException

Daniel Dekany <[email protected]> Fri, 12 Sep 2014 22:35:55 +0200
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Friday, September 12, 2014, 6:23:03 PM, Denis Bredelet wrote:

> Hi Daniel,
>
> On 11 Sep 2014, at 00:16, Daniel Dekany wrote:
>
>> XML stores string values, not numbers or dates or such (OK, not
>> exactly since Schema, but still). So you have to tell FreeMarker to
>> convert that string to a number, if you want to treat it as a number,
>> or else it's just a string, no mater how it looks.
>> 
>>  <#local level = doc["XPath..../@level"]?number>.
>> 
>> However, I guess "number(XPath..../@level)" should work too, and the
>
> How will it work?

XPath expressions normally evaluate to a set of matching nodes, but
with an XPath function like that we get a Number from the XPath
implementation. So then FreeMarker will wrap it as an FTL number.

>>> <#local level = doc["number(XPath..../@level)"]>
>
> the result of number() is used as index

No, it's the "number(XPath..../@level)" *string* that's used as an
index. And it's the result of doc[someIndex] that's a number in this
case.

> I think ?number is the correct answer.

That's the FreeMarker-ish way at least. (Although before 2.3.21 it
haven't understood "NaN"/"INF"/"-INF".)

-- 
Thanks,
 Daniel Dekany


> -- Denis.
>
>> resulting arithmetic exception is odd. What number is that, that
>> BigDecimal can't parse? A NaN maybe? (BTW FreeMarker 2.3.21 RC1 will
>> show the problematic number in the error message.)
>> 
>> -- 
>> Thanks,
>> Daniel Dekany
>> 
>> 
>> Thursday, September 11, 2014, 12:23:29 AM, ven12344 wrote:
>> 
>>> Hi, All
>>> 
>>> Need help with Freemarker local variable comparison with a constant or local
>>> constant variable, tried both but did not work
>>> 
>>> *FIRST APPROACH*
>>> 
>>> I have code like this
>>> 
>>> <#local level = doc["XPath..../@level"]>
>>> 
>>> "doc" here is the passed in XML document to freemarker and I am getting the
>>> level (this is attribute in XML which will give the level of the element
>>> like 1,2,3...) info using XPath and comparing to a constant local variable
>>> like this 
>>> 
>>> <#if (symbolLevel > levelTest)><#t>
>>> 
>>> where
>>> 
>>> <#local levelTest = 4>
>>> 
>>> For this bit of code I got this exception
>>> 
>>> freemarker.template.TemplateException: The only legal comparisons are
>>> between two numbers, two strings, or two dates.
>>> Left  hand operand is a freemarker.ext.dom.AttributeNodeModel
>>> Right hand operand is a freemarker.template.SimpleNumber
>>> 
>>> 
>>> *SECOND APPROACH*
>>> 
>>> I have code like this
>>> 
>>> <#local level = doc["number(XPath..../@level)"]>
>>> 
>>> and I am getting the level info using XPath's *number function* and
>>> comparing to a constant local variable like this 
>>> 
>>> <#if (symbolLevel > 4)><#t>
>>> 
>>> For this bit of code I got the same exception as the third approach.
>>> 
>>> 
>>> *THIRD APPROACH*
>>> 
>>> I have code like this
>>> 
>>> <#local level = doc["number(XPath..../@level)"]>
>>> 
>>> and I am getting the level info using XPath's *number function* and
>>> comparing to a constant local variable like this 
>>> 
>>> <#if (symbolLevel > levelTest)><#t>
>>> 
>>> For this bit of code I got this exception
>>> 
>>> java.lang.NumberFormatException
>>>        at java.math.BigDecimal.<init>(BigDecimal.java:459)
>>>        at java.math.BigDecimal.<init>(BigDecimal.java:728)
>>>        at
>>> freemarker.core.ArithmeticEngine.toBigDecimal(ArithmeticEngine.java:529)
>>>        at
>>> freemarker.core.ArithmeticEngine.access$000(ArithmeticEngine.java:68)
>>>        at
>>> freemarker.core.ArithmeticEngine$BigDecimalEngine.compareNumbers(ArithmeticEngine.java:142)
>>>        at
>>> freemarker.core.ComparisonExpression.isTrue(ComparisonExpression.java:131)
>>>        at
>>> freemarker.core.ParentheticalExpression.isTrue(ParentheticalExpression.java:66)
>>>        at freemarker.core.IfBlock.accept(IfBlock.java:80)
>>>        at freemarker.core.Environment.visit(Environment.java:208)
>>>        at freemarker.core.MixedContent.accept(MixedContent.java:92)
>>>        at freemarker.core.Environment.visit(Environment.java:208)
>>>        at
>>> freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:179)
>>>        at freemarker.core.Environment.visit(Environment.java:415)
>>>        at
>>> freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
>>>        at freemarker.core.Environment.visit(Environment.java:208)
>>>        at freemarker.core.MixedContent.accept(MixedContent.java:92)
>>>        at freemarker.core.Environment.visit(Environment.java:208)
>>>        at freemarker.core.Environment.visit(Environment.java:297)
>>>        at freemarker.core.UnifiedCall.accept(UnifiedCall.java:130)
>>>        at freemarker.core.Environment.visit(Environment.java:208)
>>>        at freemarker.core.Macro$Context.runMacro(Macro.java:163)
>>>        at freemarker.core.Environment.visit(Environment.java:601)
>>>        at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
>>>        at freemarker.core.Environment.visit(Environment.java:208)
>>>        at freemarker.core.MixedContent.accept(MixedContent.java:92)
>>>        at freemarker.core.Environment.visit(Environment.java:208)
>>>        at freemarker.core.Environment.process(Environment.java:188)
>>>        at freemarker.template.Template.process(Template.java:237)
>
>
> ------------------------------------------------------------------------------
> Want excitement?
> Manually upgrade your production database.
> When you want reliability, choose Perforce
> Perforce version control. Predictably reliable.
> http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>


------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk