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.ASTStop;
56  import org.apache.velocity.runtime.parser.node.ASTStringLiteral;
57  import org.apache.velocity.runtime.parser.node.ASTSubtractNode;
58  import org.apache.velocity.runtime.parser.node.ASTText;
59  import org.apache.velocity.runtime.parser.node.ASTTrue;
60  import org.apache.velocity.runtime.parser.node.ASTWord;
61  import org.apache.velocity.runtime.parser.node.ASTprocess;
62  import org.apache.velocity.runtime.parser.node.Node;
63  import org.apache.velocity.runtime.parser.node.SimpleNode;
64  
65  /**
66   * This class is simply a visitor implementation
67   * that traverses the AST, produced by the Velocity
68   * parsing process, and creates a visual structure
69   * of the AST. This is primarily used for
70   * debugging, but it useful for documentation
71   * as well.
72   *
73   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
74   * @version $Id: NodeViewMode.java 463298 2006-10-12 16:10:32Z henning $
75   */
76  public class NodeViewMode extends BaseVisitor
77  {
78      private int indent = 0;
79      private boolean showTokens = true;
80  
81      /** Indent child nodes to help visually identify
82        *  the structure of the AST.
83        */
84      private String indentString()
85      {
86          StringBuffer sb = new StringBuffer();
87          for (int i = 0; i < indent; ++i)
88          {
89              sb.append("  ");
90          }
91          return sb.toString();
92      }
93  
94      /**
95        * Display the type of nodes and optionally the
96        * first token.
97        */
98      private Object showNode(Node node, Object data)
99      {
100         String tokens = "";
101         String special = "";
102         Token t;
103 
104         if (showTokens)
105         {
106             t = node.getFirstToken();
107 
108             if (t.specialToken != null && ! t.specialToken.image.startsWith("##"))
109                 special = t.specialToken.image;
110 
111             tokens = " -> " + special + t.image;
112         }
113 
114         System.out.println(indentString() + node + tokens);
115         ++indent;
116         data = node.childrenAccept(this, data);
117         --indent;
118         return data;
119     }
120 
121     /**
122      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.SimpleNode, java.lang.Object)
123      */
124     public Object visit(SimpleNode node, Object data)
125     {
126         return showNode(node,data);
127     }
128 
129     /**
130      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTprocess, java.lang.Object)
131      */
132     public Object visit(ASTprocess node, Object data)
133     {
134         return showNode(node,data);
135     }
136 
137     /**
138      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTExpression, java.lang.Object)
139      */
140     public Object visit(ASTExpression node, Object data)
141     {
142         return showNode(node,data);
143     }
144 
145     /**
146      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTAssignment, java.lang.Object)
147      */
148     public Object visit(ASTAssignment node, Object data)
149     {
150         return showNode(node,data);
151     }
152 
153     /**
154      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTOrNode, java.lang.Object)
155      */
156     public Object visit(ASTOrNode node, Object data)
157     {
158         return showNode(node,data);
159     }
160 
161     /**
162      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTAndNode, java.lang.Object)
163      */
164     public Object visit(ASTAndNode node, Object data)
165     {
166         return showNode(node,data);
167     }
168 
169     /**
170      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTEQNode, java.lang.Object)
171      */
172     public Object visit(ASTEQNode node, Object data)
173     {
174         return showNode(node,data);
175     }
176 
177     /**
178      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTNENode, java.lang.Object)
179      */
180     public Object visit(ASTNENode node, Object data)
181     {
182         return showNode(node,data);
183     }
184 
185     /**
186      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTLTNode, java.lang.Object)
187      */
188     public Object visit(ASTLTNode node, Object data)
189     {
190         return showNode(node,data);
191     }
192 
193     /**
194      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTGTNode, java.lang.Object)
195      */
196     public Object visit(ASTGTNode node, Object data)
197     {
198         return showNode(node,data);
199     }
200 
201     /**
202      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTLENode, java.lang.Object)
203      */
204     public Object visit(ASTLENode node, Object data)
205     {
206         return showNode(node,data);
207     }
208 
209     /**
210      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTGENode, java.lang.Object)
211      */
212     public Object visit(ASTGENode node, Object data)
213     {
214         return showNode(node,data);
215     }
216 
217     /**
218      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTAddNode, java.lang.Object)
219      */
220     public Object visit(ASTAddNode node, Object data)
221     {
222         return showNode(node,data);
223     }
224 
225     /**
226      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTSubtractNode, java.lang.Object)
227      */
228     public Object visit(ASTSubtractNode node, Object data)
229     {
230         return showNode(node,data);
231     }
232 
233     /**
234      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTMulNode, java.lang.Object)
235      */
236     public Object visit(ASTMulNode node, Object data)
237     {
238         return showNode(node,data);
239     }
240 
241     /**
242      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTDivNode, java.lang.Object)
243      */
244     public Object visit(ASTDivNode node, Object data)
245     {
246         return showNode(node,data);
247     }
248 
249     /**
250      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTModNode, java.lang.Object)
251      */
252     public Object visit(ASTModNode node, Object data)
253     {
254         return showNode(node,data);
255     }
256 
257     /**
258      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTNotNode, java.lang.Object)
259      */
260     public Object visit(ASTNotNode node, Object data)
261     {
262         return showNode(node,data);
263     }
264 
265     /**
266      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTFloatingPointLiteral, java.lang.Object)
267      */
268     public Object visit(ASTFloatingPointLiteral node, Object data)
269     {
270         return showNode(node,data);
271     }
272 
273     /**
274      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTIntegerLiteral, java.lang.Object)
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      */
404     public Object visit(ASTEscapedDirective node, Object data)
405     {
406         return showNode(node,data);
407     }
408 
409     /**
410      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTEscape, java.lang.Object)
411      */
412     public Object visit(ASTEscape node, Object data)
413     {
414         return showNode(node,data);
415     }
416 
417     /**
418      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTMap, java.lang.Object)
419      */
420     public Object visit(ASTMap node, Object data)
421     {
422         return showNode(node,data);
423     }
424 
425     /**
426      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTIntegerRange, java.lang.Object)
427      */
428     public Object visit(ASTIntegerRange node, Object data)
429     {
430         return showNode(node,data);
431     }
432 
433     /**
434      * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTStop, java.lang.Object)
435      */
436     public Object visit(ASTStop node, Object data)
437     {
438         return showNode(node,data);
439     }
440 }