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 java.io.IOException;
23 import java.io.Writer;
24
25 import org.apache.velocity.app.event.EventHandlerUtil;
26 import org.apache.velocity.context.InternalContextAdapter;
27 import org.apache.velocity.exception.MethodInvocationException;
28 import org.apache.velocity.exception.TemplateInitException;
29 import org.apache.velocity.runtime.RuntimeConstants;
30 import org.apache.velocity.runtime.parser.Parser;
31 import org.apache.velocity.runtime.parser.ParserVisitor;
32 import org.apache.velocity.util.introspection.Info;
33
34
35
36
37
38
39
40
41 public class ASTSetDirective extends SimpleNode
42 {
43 private String leftReference = "";
44 private Node right = null;
45 private ASTReference left = null;
46 boolean logOnNull = false;
47
48
49
50
51 protected Info uberInfo;
52
53
54
55
56 public ASTSetDirective(int id)
57 {
58 super(id);
59 }
60
61
62
63
64
65 public ASTSetDirective(Parser p, int id)
66 {
67 super(p, id);
68 }
69
70
71
72
73 public Object jjtAccept(ParserVisitor visitor, Object data)
74 {
75 return visitor.visit(this, data);
76 }
77
78
79
80
81
82
83
84
85 public Object init(InternalContextAdapter context, Object data)
86 throws TemplateInitException
87 {
88
89
90
91
92 super.init( context, data );
93
94 uberInfo = new Info(context.getCurrentTemplateName(),
95 getLine(), getColumn());
96
97 right = getRightHandSide();
98 left = getLeftHandSide();
99
100 logOnNull = rsvc.getBoolean(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID, true);
101
102
103
104
105 leftReference = left.getFirstToken().image.substring(1);
106
107 return data;
108 }
109
110
111
112
113
114
115
116
117
118 public boolean render( InternalContextAdapter context, Writer writer)
119 throws IOException, MethodInvocationException
120 {
121
122
123
124
125 Object value = right.value(context);
126
127
128
129
130
131
132 if( !rsvc.getBoolean(RuntimeConstants.SET_NULL_ALLOWED,false) )
133 {
134 if ( value == null )
135 {
136
137
138
139 if(logOnNull)
140 {
141 boolean doit = EventHandlerUtil.shouldLogOnNullSet( rsvc, context, left.literal(), right.literal() );
142
143 if (doit && log.isInfoEnabled())
144 {
145 log.info("RHS of #set statement is null. Context will not be modified. "
146 + context.getCurrentTemplateName() + " [line " + getLine()
147 + ", column " + getColumn() + "]");
148 }
149 }
150
151 String rightReference = null;
152 if (right instanceof ASTExpression)
153 {
154 rightReference = ((ASTExpression) right).getLastToken().image;
155 }
156 EventHandlerUtil.invalidSetMethod(rsvc, context, leftReference, rightReference, uberInfo);
157
158 return false;
159 }
160 }
161
162 if ( value == null )
163 {
164 String rightReference = null;
165 if (right instanceof ASTExpression)
166 {
167 rightReference = ((ASTExpression) right).getLastToken().image;
168 }
169 EventHandlerUtil.invalidSetMethod(rsvc, context, leftReference, rightReference, uberInfo);
170
171
172
173
174
175 context.remove( leftReference );
176
177 return false;
178
179 }
180 else
181 {
182
183
184
185
186
187
188 if (left.jjtGetNumChildren() == 0)
189 {
190 context.put( leftReference, value);
191 }
192 else
193 {
194 left.setValue(context, value);
195 }
196 }
197
198 return true;
199 }
200
201
202
203
204
205
206 private ASTReference getLeftHandSide()
207 {
208 return (ASTReference) jjtGetChild(0);
209 }
210
211
212
213
214
215
216 private Node getRightHandSide()
217 {
218 return jjtGetChild(1);
219 }
220 }