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
23
24
25
26
27
28
29
30
31
32
33
34 import org.apache.velocity.context.InternalContextAdapter;
35 import org.apache.velocity.runtime.parser.Parser;
36 import org.apache.velocity.runtime.parser.ParserVisitor;
37
38 import org.apache.velocity.exception.MethodInvocationException;
39
40 import org.apache.velocity.util.TemplateNumber;
41
42
43
44
45 public class ASTAddNode extends SimpleNode
46 {
47
48
49
50 public ASTAddNode(int id)
51 {
52 super(id);
53 }
54
55
56
57
58
59 public ASTAddNode(Parser p, int id)
60 {
61 super(p, id);
62 }
63
64
65
66
67 public Object jjtAccept(ParserVisitor visitor, Object data)
68 {
69 return visitor.visit(this, data);
70 }
71
72
73
74
75
76
77
78 public Object value( InternalContextAdapter context)
79 throws MethodInvocationException
80 {
81
82
83
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 addition operation has null value."
98 + " Operation not possible. "
99 + context.getCurrentTemplateName() + " [line " + getLine()
100 + ", column " + getColumn() + "]");
101 return null;
102 }
103
104
105
106
107 if (left instanceof TemplateNumber)
108 {
109 left = ( (TemplateNumber) left).getAsNumber();
110 }
111 if (right instanceof TemplateNumber)
112 {
113 right = ( (TemplateNumber) right).getAsNumber();
114 }
115
116
117
118
119 if (left instanceof Number && right instanceof Number)
120 {
121 return MathUtils.add((Number)left, (Number)right);
122 }
123
124
125
126
127 if (left instanceof String || right instanceof String)
128 {
129 return left.toString().concat(right.toString());
130 }
131
132
133
134 log.error((!(left instanceof Number || left instanceof String) ? "Left" : "Right")
135 + " side of addition operation is not a valid type. "
136 + "Currently only Strings, numbers (1,2,3...) and Number type are supported. "
137 + context.getCurrentTemplateName() + " [line " + getLine()
138 + ", column " + getColumn() + "]");
139
140 return null;
141 }
142 }
143
144
145
146