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.commons.lang.builder.ToStringBuilder;
26 import org.apache.velocity.context.InternalContextAdapter;
27 import org.apache.velocity.exception.MethodInvocationException;
28 import org.apache.velocity.exception.ParseErrorException;
29 import org.apache.velocity.exception.ResourceNotFoundException;
30 import org.apache.velocity.exception.TemplateInitException;
31 import org.apache.velocity.runtime.RuntimeServices;
32 import org.apache.velocity.runtime.log.Log;
33 import org.apache.velocity.runtime.parser.Parser;
34 import org.apache.velocity.runtime.parser.ParserVisitor;
35 import org.apache.velocity.runtime.parser.Token;
36
37
38
39
40 public class SimpleNode implements Node
41 {
42
43 protected RuntimeServices rsvc = null;
44
45
46 protected Log log = null;
47
48
49 protected Node parent;
50
51
52 protected Node[] children;
53
54
55 protected int id;
56
57
58 protected Parser parser;
59
60
61 protected int info;
62
63
64 public boolean state;
65
66
67 protected boolean invalid = false;
68
69
70 protected Token first;
71
72
73 protected Token last;
74
75
76
77
78 public SimpleNode(int i)
79 {
80 id = i;
81 }
82
83
84
85
86
87 public SimpleNode(Parser p, int i)
88 {
89 this(i);
90 parser = p;
91 }
92
93
94
95
96 public void jjtOpen()
97 {
98 first = parser.getToken(1);
99 }
100
101
102
103
104 public void jjtClose()
105 {
106 last = parser.getToken(0);
107 }
108
109
110
111
112 public void setFirstToken(Token t)
113 {
114 this.first = t;
115 }
116
117
118
119
120 public Token getFirstToken()
121 {
122 return first;
123 }
124
125
126
127
128 public Token getLastToken()
129 {
130 return last;
131 }
132
133
134
135
136 public void jjtSetParent(Node n)
137 {
138 parent = n;
139 }
140
141
142
143
144 public Node jjtGetParent()
145 {
146 return parent;
147 }
148
149
150
151
152 public void jjtAddChild(Node n, int i)
153 {
154 if (children == null)
155 {
156 children = new Node[i + 1];
157 }
158 else if (i >= children.length)
159 {
160 Node c[] = new Node[i + 1];
161 System.arraycopy(children, 0, c, 0, children.length);
162 children = c;
163 }
164 children[i] = n;
165 }
166
167
168
169
170 public Node jjtGetChild(int i)
171 {
172 return children[i];
173 }
174
175
176
177
178 public int jjtGetNumChildren()
179 {
180 return (children == null) ? 0 : children.length;
181 }
182
183
184
185
186
187 public Object jjtAccept(ParserVisitor visitor, Object data)
188 {
189 return visitor.visit(this, data);
190 }
191
192
193
194
195
196 public Object childrenAccept(ParserVisitor visitor, Object data)
197 {
198 if (children != null)
199 {
200 for (int i = 0; i < children.length; ++i)
201 {
202 children[i].jjtAccept(visitor, data);
203 }
204 }
205 return data;
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 public String toString(String prefix)
223 {
224 return prefix + toString();
225 }
226
227
228
229
230
231
232
233 public void dump(String prefix)
234 {
235 System.out.println(toString(prefix));
236 if (children != null)
237 {
238 for (int i = 0; i < children.length; ++i)
239 {
240 SimpleNode n = (SimpleNode) children[i];
241 if (n != null)
242 {
243 n.dump(prefix + " ");
244 }
245 }
246 }
247 }
248
249
250
251
252
253
254 public String literal()
255 {
256 Token t = first;
257 StringBuffer sb = new StringBuffer(t.image);
258
259 while (t != last)
260 {
261 t = t.next;
262 sb.append(t.image);
263 }
264
265 return sb.toString();
266 }
267
268
269
270
271
272 public Object init( InternalContextAdapter context, Object data) throws TemplateInitException
273 {
274
275
276
277
278 rsvc = (RuntimeServices) data;
279 log = rsvc.getLog();
280
281 int i, k = jjtGetNumChildren();
282
283 for (i = 0; i < k; i++)
284 {
285 jjtGetChild(i).init( context, data);
286 }
287
288 return data;
289 }
290
291
292
293
294 public boolean evaluate( InternalContextAdapter context)
295 throws MethodInvocationException
296 {
297 return false;
298 }
299
300
301
302
303 public Object value( InternalContextAdapter context)
304 throws MethodInvocationException
305 {
306 return null;
307 }
308
309
310
311
312 public boolean render( InternalContextAdapter context, Writer writer)
313 throws IOException, MethodInvocationException, ParseErrorException, ResourceNotFoundException
314 {
315 int i, k = jjtGetNumChildren();
316
317 for (i = 0; i < k; i++)
318 jjtGetChild(i).render(context, writer);
319
320 return true;
321 }
322
323
324
325
326 public Object execute(Object o, InternalContextAdapter context)
327 throws MethodInvocationException
328 {
329 return null;
330 }
331
332
333
334
335 public int getType()
336 {
337 return id;
338 }
339
340
341
342
343 public void setInfo(int info)
344 {
345 this.info = info;
346 }
347
348
349
350
351 public int getInfo()
352 {
353 return info;
354 }
355
356
357
358
359 public void setInvalid()
360 {
361 invalid = true;
362 }
363
364
365
366
367 public boolean isInvalid()
368 {
369 return invalid;
370 }
371
372
373
374
375 public int getLine()
376 {
377 return first.beginLine;
378 }
379
380
381
382
383 public int getColumn()
384 {
385 return first.beginColumn;
386 }
387
388 public String toString()
389 {
390 StringBuffer tokens = new StringBuffer();
391
392 for (Token t = getFirstToken(); t != null; )
393 {
394 tokens.append("[").append(t.image).append("]");
395 if (t.next != null)
396 {
397 if (t.equals(getLastToken()))
398 {
399 break;
400 }
401 else
402 {
403 tokens.append(", ");
404 }
405 }
406 t = t.next;
407 }
408
409 return new ToStringBuilder(this)
410 .append("id", getType())
411 .append("info", getInfo())
412 .append("invalid", isInvalid())
413 .append("children", jjtGetNumChildren())
414 .append("tokens", tokens)
415 .toString();
416 }
417 }
418