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