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