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