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.BufferedReader;
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.io.StringWriter;
26
27 import org.apache.velocity.context.InternalContextAdapter;
28 import org.apache.velocity.exception.MethodInvocationException;
29 import org.apache.velocity.exception.ParseErrorException;
30 import org.apache.velocity.exception.ResourceNotFoundException;
31 import org.apache.velocity.exception.TemplateInitException;
32 import org.apache.velocity.runtime.RuntimeConstants;
33 import org.apache.velocity.runtime.parser.ParseException;
34 import org.apache.velocity.runtime.parser.Parser;
35 import org.apache.velocity.runtime.parser.ParserVisitor;
36
37
38
39
40
41
42
43
44 public class ASTStringLiteral extends SimpleNode
45 {
46
47 private boolean interpolate = true;
48 private SimpleNode nodeTree = null;
49 private String image = "";
50 private String interpolateimage = "";
51
52
53 private boolean containsLineComment;
54
55
56
57
58 public ASTStringLiteral(int id)
59 {
60 super(id);
61 }
62
63
64
65
66
67 public ASTStringLiteral(Parser p, int id)
68 {
69 super(p, id);
70 }
71
72
73
74
75
76
77
78
79
80 public Object init(InternalContextAdapter context, Object data)
81 throws TemplateInitException
82 {
83
84
85
86
87 super.init(context, data);
88
89
90
91
92
93
94
95
96
97
98
99
100 interpolate = rsvc.getBoolean(RuntimeConstants.INTERPOLATE_STRINGLITERALS , true)
101 && getFirstToken().image.startsWith("\"")
102 && ((getFirstToken().image.indexOf('$') != -1)
103 || (getFirstToken().image.indexOf('#') != -1));
104
105
106
107
108
109 image = getFirstToken().image.substring(1,
110 getFirstToken().image.length() - 1);
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 containsLineComment = (image.indexOf("##") != -1);
129
130
131
132
133
134 if (!containsLineComment)
135 {
136 interpolateimage = image + " ";
137 }
138 else
139 {
140 interpolateimage = image;
141 }
142
143 if (interpolate)
144 {
145
146
147
148 BufferedReader br = new BufferedReader(new StringReader(interpolateimage));
149
150
151
152
153
154
155
156
157 try
158 {
159 nodeTree = rsvc.parse(br, (context != null) ?
160 context.getCurrentTemplateName() : "StringLiteral", false);
161 }
162 catch (ParseException e)
163 {
164 throw new TemplateInitException("Problem parsing String literal.",
165 e,
166 (context != null) ? context.getCurrentTemplateName() : "StringLiteral",
167 getColumn(),
168 getLine() );
169 }
170
171
172
173
174
175 nodeTree.init(context, rsvc);
176 }
177
178 return data;
179 }
180
181
182
183
184 public Object jjtAccept(ParserVisitor visitor, Object data)
185 {
186 return visitor.visit(this, data);
187 }
188
189
190
191
192
193
194
195
196
197 public Object value(InternalContextAdapter context)
198 {
199 if (interpolate)
200 {
201 try
202 {
203
204
205
206
207 StringWriter writer = new StringWriter();
208 nodeTree.render(context, writer);
209
210
211
212
213
214 String ret = writer.toString();
215
216
217
218
219
220 if (!containsLineComment && ret.length() > 0)
221 {
222 return ret.substring(0, ret.length() - 1);
223 }
224 else
225 {
226 return ret;
227 }
228 }
229
230
231
232
233
234 catch( ParseErrorException e )
235 {
236 log.error("Error in interpolating string literal", e);
237 }
238 catch( MethodInvocationException e )
239 {
240 log.error("Error in interpolating string literal", e);
241 }
242 catch( ResourceNotFoundException e )
243 {
244 log.error("Error in interpolating string literal", e);
245 }
246
247
248
249
250 catch( RuntimeException e )
251 {
252 throw e;
253 }
254
255 catch( IOException e )
256 {
257 log.error("Error in interpolating string literal", e);
258 }
259
260 }
261
262
263
264
265
266
267
268 return image;
269 }
270 }