View Javadoc

1   package org.apache.velocity.runtime.visitor;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.velocity.runtime.parser.Token;
23  import org.apache.velocity.runtime.parser.node.ASTAddNode;
24  import org.apache.velocity.runtime.parser.node.ASTAndNode;
25  import org.apache.velocity.runtime.parser.node.ASTAssignment;
26  import org.apache.velocity.runtime.parser.node.ASTBlock;
27  import org.apache.velocity.runtime.parser.node.ASTDirective;
28  import org.apache.velocity.runtime.parser.node.ASTDivNode;
29  import org.apache.velocity.runtime.parser.node.ASTEQNode;
30  import org.apache.velocity.runtime.parser.node.ASTElseIfStatement;
31  import org.apache.velocity.runtime.parser.node.ASTElseStatement;
32  import org.apache.velocity.runtime.parser.node.ASTEscape;
33  import org.apache.velocity.runtime.parser.node.ASTEscapedDirective;
34  import org.apache.velocity.runtime.parser.node.ASTExpression;
35  import org.apache.velocity.runtime.parser.node.ASTFalse;
36  import org.apache.velocity.runtime.parser.node.ASTFloatingPointLiteral;
37  import org.apache.velocity.runtime.parser.node.ASTGENode;
38  import org.apache.velocity.runtime.parser.node.ASTGTNode;
39  import org.apache.velocity.runtime.parser.node.ASTIdentifier;
40  import org.apache.velocity.runtime.parser.node.ASTIfStatement;
41  import org.apache.velocity.runtime.parser.node.ASTIntegerLiteral;
42  import org.apache.velocity.runtime.parser.node.ASTIntegerRange;
43  import org.apache.velocity.runtime.parser.node.ASTLENode;
44  import org.apache.velocity.runtime.parser.node.ASTLTNode;
45  import org.apache.velocity.runtime.parser.node.ASTMap;
46  import org.apache.velocity.runtime.parser.node.ASTMethod;
47  import org.apache.velocity.runtime.parser.node.ASTModNode;
48  import org.apache.velocity.runtime.parser.node.ASTMulNode;
49  import org.apache.velocity.runtime.parser.node.ASTNENode;
50  import org.apache.velocity.runtime.parser.node.ASTNotNode;
51  import org.apache.velocity.runtime.parser.node.ASTObjectArray;
52  import org.apache.velocity.runtime.parser.node.ASTOrNode;
53  import org.apache.velocity.runtime.parser.node.ASTReference;
54  import org.apache.velocity.runtime.parser.node.ASTSetDirective;
55  import org.apache.velocity.runtime.parser.node.ASTStringLiteral;
56  import org.apache.velocity.runtime.parser.node.ASTSubtractNode;
57  import org.apache.velocity.runtime.parser.node.ASTText;
58  import org.apache.velocity.runtime.parser.node.ASTTrue;
59  import org.apache.velocity.runtime.parser.node.ASTWord;
60  import org.apache.velocity.runtime.parser.node.ASTprocess;
61  import org.apache.velocity.runtime.parser.node.Node;
62  import org.apache.velocity.runtime.parser.node.SimpleNode;
63  
64  /**
65   * This class is simply a visitor implementation
66   * that traverses the AST, produced by the Velocity
67   * parsing process, and creates a visual structure
68   * of the AST. This is primarily used for
69   * debugging, but it useful for documentation
70   * as well.
71   *
72   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
73   * @version $Id: NodeViewMode.java 747106 2009-02-23 19:25:14Z nbubna $
74   */
75  public class NodeViewMode extends BaseVisitor
76  {
77      private int indent = 0;
78      private boolean showTokens = true;
79  
80      /** Indent child nodes to help visually identify
81        *  the structure of the AST.
82        */
83      private String indentString()
84      {
85          StringBuffer sb = new StringBuffer();
86          for (int i = 0; i < indent; ++i)
87          {
88              sb.append("  ");
89          }
90          return sb.toString();
91      }
92  
93      /**
94        * Display the type of nodes and optionally the
95        * first token.
96        */
97      private Object showNode(Node node, Object data)
98      {
99          String tokens = "";
100         String special = "";
101         Token t;
102 
103         if (showTokens)
104         {
105             t = node.getFirstToken();
106 
107             if (t.specialToken != null && ! t.specialToken.image.startsWith("##"))
108                 special = t.specialToken.image;
109 
110             tokens = " -> " + special + t.image;
111         }
112 
113         System.out.println(indentString() + node + tokens);
114         ++indent;
115         data = node.childrenAccept(this, data);
116         --indent;
117         return data;
118     }
119 
120     /**
121      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.SimpleNode, java.lang.Object)
122      */
123     public Object visit(SimpleNode node, Object data)
124     {
125         return showNode(node,data);
126     }
127 
128     /**
129      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTprocess, java.lang.Object)
130      */
131     public Object visit(ASTprocess node, Object data)
132     {
133         return showNode(node,data);
134     }
135 
136     /**
137      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTExpression, java.lang.Object)
138      */
139     public Object visit(ASTExpression node, Object data)
140     {
141         return showNode(node,data);
142     }
143 
144     /**
145      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTAssignment, java.lang.Object)
146      */
147     public Object visit(ASTAssignment node, Object data)
148     {
149         return showNode(node,data);
150     }
151 
152     /**
153      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTOrNode, java.lang.Object)
154      */
155     public Object visit(ASTOrNode node, Object data)
156     {
157         return showNode(node,data);
158     }
159 
160     /**
161      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTAndNode, java.lang.Object)
162      */
163     public Object visit(ASTAndNode node, Object data)
164     {
165         return showNode(node,data);
166     }
167 
168     /**
169      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTEQNode, java.lang.Object)
170      */
171     public Object visit(ASTEQNode node, Object data)
172     {
173         return showNode(node,data);
174     }
175 
176     /**
177      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTNENode, java.lang.Object)
178      */
179     public Object visit(ASTNENode node, Object data)
180     {
181         return showNode(node,data);
182     }
183 
184     /**
185      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTLTNode, java.lang.Object)
186      */
187     public Object visit(ASTLTNode node, Object data)
188     {
189         return showNode(node,data);
190     }
191 
192     /**
193      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTGTNode, java.lang.Object)
194      */
195     public Object visit(ASTGTNode node, Object data)
196     {
197         return showNode(node,data);
198     }
199 
200     /**
201      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTLENode, java.lang.Object)
202      */
203     public Object visit(ASTLENode node, Object data)
204     {
205         return showNode(node,data);
206     }
207 
208     /**
209      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTGENode, java.lang.Object)
210      */
211     public Object visit(ASTGENode node, Object data)
212     {
213         return showNode(node,data);
214     }
215 
216     /**
217      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTAddNode, java.lang.Object)
218      */
219     public Object visit(ASTAddNode node, Object data)
220     {
221         return showNode(node,data);
222     }
223 
224     /**
225      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTSubtractNode, java.lang.Object)
226      */
227     public Object visit(ASTSubtractNode node, Object data)
228     {
229         return showNode(node,data);
230     }
231 
232     /**
233      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTMulNode, java.lang.Object)
234      */
235     public Object visit(ASTMulNode node, Object data)
236     {
237         return showNode(node,data);
238     }
239 
240     /**
241      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTDivNode, java.lang.Object)
242      */
243     public Object visit(ASTDivNode node, Object data)
244     {
245         return showNode(node,data);
246     }
247 
248     /**
249      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTModNode, java.lang.Object)
250      */
251     public Object visit(ASTModNode node, Object data)
252     {
253         return showNode(node,data);
254     }
255 
256     /**
257      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTNotNode, java.lang.Object)
258      */
259     public Object visit(ASTNotNode node, Object data)
260     {
261         return showNode(node,data);
262     }
263 
264     /**
265      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTFloatingPointLiteral, java.lang.Object)
266      */
267     public Object visit(ASTFloatingPointLiteral node, Object data)
268     {
269         return showNode(node,data);
270     }
271 
272     /**
273      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTIntegerLiteral, java.lang.Object)
274      * @since 1.5
275      */
276     public Object visit(ASTIntegerLiteral node, Object data)
277     {
278         return showNode(node,data);
279     }
280 
281     /**
282      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTStringLiteral, java.lang.Object)
283      */
284     public Object visit(ASTStringLiteral node, Object data)
285     {
286         return showNode(node,data);
287     }
288 
289     /**
290      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTIdentifier, java.lang.Object)
291      */
292     public Object visit(ASTIdentifier node, Object data)
293     {
294         return showNode(node,data);
295     }
296 
297     /**
298      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTMethod, java.lang.Object)
299      */
300     public Object visit(ASTMethod node, Object data)
301     {
302         return showNode(node,data);
303     }
304 
305     /**
306      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTReference, java.lang.Object)
307      */
308     public Object visit(ASTReference node, Object data)
309     {
310         return showNode(node,data);
311     }
312 
313     /**
314      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTTrue, java.lang.Object)
315      */
316     public Object visit(ASTTrue node, Object data)
317     {
318         return showNode(node,data);
319     }
320 
321     /**
322      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTFalse, java.lang.Object)
323      */
324     public Object visit(ASTFalse node, Object data)
325     {
326         return showNode(node,data);
327     }
328 
329     /**
330      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTBlock, java.lang.Object)
331      */
332     public Object visit(ASTBlock node, Object data)
333     {
334         return showNode(node,data);
335     }
336 
337     /**
338      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTText, java.lang.Object)
339      */
340     public Object visit(ASTText node, Object data)
341     {
342         return showNode(node,data);
343     }
344 
345     /**
346      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTIfStatement, java.lang.Object)
347      */
348     public Object visit(ASTIfStatement node, Object data)
349     {
350         return showNode(node,data);
351     }
352 
353     /**
354      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTElseStatement, java.lang.Object)
355      */
356     public Object visit(ASTElseStatement node, Object data)
357     {
358         return showNode(node,data);
359     }
360 
361     /**
362      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTElseIfStatement, java.lang.Object)
363      */
364     public Object visit(ASTElseIfStatement node, Object data)
365     {
366         return showNode(node,data);
367     }
368 
369     /**
370      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTObjectArray, java.lang.Object)
371      */
372     public Object visit(ASTObjectArray node, Object data)
373     {
374         return showNode(node,data);
375     }
376 
377     /**
378      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTDirective, java.lang.Object)
379      */
380     public Object visit(ASTDirective node, Object data)
381     {
382         return showNode(node,data);
383     }
384 
385     /**
386      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTWord, java.lang.Object)
387      */
388     public Object visit(ASTWord node, Object data)
389     {
390         return showNode(node,data);
391     }
392 
393     /**
394      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTSetDirective, java.lang.Object)
395      */
396     public Object visit(ASTSetDirective node, Object data)
397     {
398         return showNode(node,data);
399     }
400 
401     /**
402      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTEscapedDirective, java.lang.Object)
403      * @since 1.5
404      */
405     public Object visit(ASTEscapedDirective node, Object data)
406     {
407         return showNode(node,data);
408     }
409 
410     /**
411      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTEscape, java.lang.Object)
412      * @since 1.5
413      */
414     public Object visit(ASTEscape node, Object data)
415     {
416         return showNode(node,data);
417     }
418 
419     /**
420      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTMap, java.lang.Object)
421      * @since 1.5
422      */
423     public Object visit(ASTMap node, Object data)
424     {
425         return showNode(node,data);
426     }
427 
428     /**
429      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTIntegerRange, java.lang.Object)
430      */
431     public Object visit(ASTIntegerRange node, Object data)
432     {
433         return showNode(node,data);
434     }
435 }