Discussion:
[FreeMarker-user] Having TemplateException or NumberFormatException
ven12344
2014-09-10 22:23:29 UTC
Permalink
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)




--
View this message in context: http://freemarker.624813.n4.nabble.com/Having-TemplateException-or-NumberFormatException-tp4655267.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2014-09-10 23:16:58 UTC
Permalink
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
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
Post by ven12344
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
"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
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
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)
Denis Bredelet
2014-09-12 16:23:03 UTC
Permalink
Hi Daniel,
Post by Daniel Dekany
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.
How will it work?
the result of number() is used as index

I think ?number is the correct answer.

-- Denis.
Post by Daniel Dekany
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
Post by ven12344
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
"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
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
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)
Daniel Dekany
2014-09-12 20:35:55 UTC
Permalink
Post by Denis Bredelet
Hi Daniel,
Post by Daniel Dekany
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.
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.
Post by Denis Bredelet
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.
Post by Denis Bredelet
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
Post by Denis Bredelet
-- Denis.
Post by Daniel Dekany
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
Post by ven12344
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
"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
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
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
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Loading...