1 package org.apache.velocity.runtime.parser.node;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.velocity.context.InternalContextAdapter;
23 import org.apache.velocity.runtime.parser.Parser;
24 import org.apache.velocity.runtime.parser.ParserVisitor;
25
26 import org.apache.velocity.exception.MethodInvocationException;
27
28 import org.apache.velocity.util.TemplateNumber;
29
30
31
32
33
34
35
36
37
38
39 public class ASTNENode extends SimpleNode
40 {
41
42
43
44 public ASTNENode(int id)
45 {
46 super(id);
47 }
48
49
50
51
52
53 public ASTNENode(Parser p, int id)
54 {
55 super(p, id);
56 }
57
58
59
60
61 public Object jjtAccept(ParserVisitor visitor, Object data)
62 {
63 return visitor.visit(this, data);
64 }
65
66
67
68
69 public boolean evaluate( InternalContextAdapter context)
70 throws MethodInvocationException
71 {
72 Object left = jjtGetChild(0).value( context );
73 Object right = jjtGetChild(1).value( context );
74
75
76
77
78
79 if ( left == null || right == null)
80 {
81 log.error((left == null ? "Left" : "Right")
82 + " side ("
83 + jjtGetChild( (left == null? 0 : 1) ).literal()
84 + ") of '!=' operation has null value."
85 + " Operation not possible. "
86 + context.getCurrentTemplateName() + " [line " + getLine()
87 + ", column " + getColumn() + "]");
88 return false;
89
90 }
91
92
93
94
95 if (left instanceof TemplateNumber)
96 {
97 left = ( (TemplateNumber) left).getAsNumber();
98 }
99 if (right instanceof TemplateNumber)
100 {
101 right = ( (TemplateNumber) right).getAsNumber();
102 }
103
104
105
106
107 if (left instanceof Number && right instanceof Number)
108 {
109 return MathUtils.compare ( (Number)left,(Number)right) != 0;
110 }
111
112
113
114
115
116
117
118 if (left.getClass().isAssignableFrom(right.getClass()) ||
119 right.getClass().isAssignableFrom(left.getClass()) )
120 {
121 return !left.equals( right );
122 }
123 else
124 {
125
126
127
128 if ((left.toString() == null) || (right.toString() == null))
129 {
130 boolean culprit = (left.toString() == null);
131 log.error((culprit ? "Left" : "Right")
132 + " string side "
133 + "String representation ("
134 + jjtGetChild((culprit ? 0 : 1) ).literal()
135 + ") of '!=' operation has null value."
136 + " Operation not possible. "
137 + context.getCurrentTemplateName() + " [line " + getLine()
138 + ", column " + getColumn() + "]");
139 return false;
140 }
141
142 else
143 {
144 return !left.toString().equals(right.toString());
145 }
146
147 }
148
149 }
150
151
152
153
154 public Object value(InternalContextAdapter context)
155 throws MethodInvocationException
156 {
157 boolean val = evaluate(context);
158
159 return val ? Boolean.TRUE : Boolean.FALSE;
160 }
161
162 }