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 import java.io.IOException;
21 import java.io.StringReader;
22 import java.io.StringWriter;
23
24 import org.apache.commons.lang.text.StrBuilder;
25 import org.apache.velocity.context.InternalContextAdapter;
26 import org.apache.velocity.exception.MethodInvocationException;
27 import org.apache.velocity.exception.ParseErrorException;
28 import org.apache.velocity.exception.ResourceNotFoundException;
29 import org.apache.velocity.exception.TemplateInitException;
30 import org.apache.velocity.exception.VelocityException;
31 import org.apache.velocity.runtime.RuntimeConstants;
32 import org.apache.velocity.runtime.log.Log;
33 import org.apache.velocity.runtime.parser.ParseException;
34 import org.apache.velocity.runtime.parser.Parser;
35 import org.apache.velocity.runtime.parser.Token;
36 import org.apache.velocity.runtime.visitor.BaseVisitor;
37
38
39
40
41
42
43
44
45 public class ASTStringLiteral extends SimpleNode
46 {
47
48 private boolean interpolate = true;
49
50 private SimpleNode nodeTree = null;
51
52 private String image = "";
53
54 private String interpolateimage = "";
55
56
57 private boolean containsLineComment;
58
59
60
61
62 public ASTStringLiteral(int id)
63 {
64 super(id);
65 }
66
67
68
69
70
71 public ASTStringLiteral(Parser p, int id)
72 {
73 super(p, id);
74 }
75
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
95
96
97
98
99
100
101
102
103
104 interpolate = rsvc.getBoolean(
105 RuntimeConstants.INTERPOLATE_STRINGLITERALS, true)
106 && getFirstToken().image.startsWith("\"")
107 && ((getFirstToken().image.indexOf('$') != -1) || (getFirstToken().image
108 .indexOf('#') != -1));
109
110
111
112
113
114 image = getFirstToken().image.substring(1, getFirstToken().image
115 .length() - 1);
116 if (getFirstToken().image.startsWith("\""))
117 {
118 image = unescape(image);
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 containsLineComment = (image.indexOf("##") != -1);
136
137
138
139
140
141 if (!containsLineComment)
142 {
143 interpolateimage = image + " ";
144 }
145 else
146 {
147 interpolateimage = image;
148 }
149
150 if (interpolate)
151 {
152
153
154
155 StringReader br = new StringReader(interpolateimage);
156
157
158
159
160
161
162
163
164 String templateName =
165 (context != null) ? context.getCurrentTemplateName() : "StringLiteral";
166 try
167 {
168 nodeTree = rsvc.parse(br, templateName, false);
169 }
170 catch (ParseException e)
171 {
172 String msg = "Failed to parse String literal at "+
173 Log.formatFileString(templateName, getLine(), getColumn());
174 throw new TemplateInitException(msg, e, templateName, getColumn(), getLine());
175 }
176
177 adjTokenLineNums(nodeTree);
178
179
180
181
182
183 nodeTree.init(context, rsvc);
184 }
185
186 return data;
187 }
188
189
190
191
192
193
194
195
196
197 public void adjTokenLineNums(Node node)
198 {
199 Token tok = node.getFirstToken();
200
201 while(tok != null && tok != node.getLastToken())
202 {
203
204
205
206 if (tok.beginLine == 1)
207 tok.beginColumn += getColumn();
208
209 if (tok.endLine == 1)
210 tok.endColumn += getColumn();
211
212 tok.beginLine += getLine()- 1;
213 tok.endLine += getLine() - 1;
214 tok = tok.next;
215 }
216 }
217
218
219
220
221
222 public static String unescape(final String string)
223 {
224 int u = string.indexOf("\\u");
225 if (u < 0) return string;
226
227 StrBuilder result = new StrBuilder();
228
229 int lastCopied = 0;
230
231 for (;;)
232 {
233 result.append(string.substring(lastCopied, u));
234
235
236
237 char c = (char) Integer.parseInt(string.substring(u + 2, u + 6), 16);
238 result.append(c);
239
240 lastCopied = u + 6;
241
242 u = string.indexOf("\\u", lastCopied);
243 if (u < 0)
244 {
245 result.append(string.substring(lastCopied));
246 return result.toString();
247 }
248 }
249 }
250
251
252
253
254
255
256 public Object jjtAccept(ParserVisitor visitor, Object data)
257 {
258 return visitor.visit(this, data);
259 }
260
261
262
263
264
265
266 public boolean isConstant()
267 {
268 return !interpolate;
269 }
270
271
272
273
274
275
276
277
278
279 public Object value(InternalContextAdapter context)
280 {
281 if (interpolate)
282 {
283 try
284 {
285
286
287
288
289 StringWriter writer = new StringWriter();
290 nodeTree.render(context, writer);
291
292
293
294
295
296 String ret = writer.toString();
297
298
299
300
301
302 if (!containsLineComment && ret.length() > 0)
303 {
304 return ret.substring(0, ret.length() - 1);
305 }
306 else
307 {
308 return ret;
309 }
310 }
311
312
313
314
315 catch (RuntimeException e)
316 {
317 throw e;
318 }
319
320 catch (IOException e)
321 {
322 String msg = "Error in interpolating string literal";
323 log.error(msg, e);
324 throw new VelocityException(msg, e);
325 }
326
327 }
328
329
330
331
332
333
334 return image;
335 }
336 }