KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > visitor > NodeViewMode


1 package org.apache.velocity.runtime.visitor;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.velocity.runtime.parser.Token;
20 import org.apache.velocity.runtime.parser.node.*;
21
22 /**
23  * This class is simply a visitor implementation
24  * that traverses the AST, produced by the Velocity
25  * parsing process, and creates a visual structure
26  * of the AST. This is primarily used for
27  * debugging, but it useful for documentation
28  * as well.
29  *
30  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
31  * @version $Id: NodeViewMode.java,v 1.6.4.1 2004/03/03 23:23:03 geirm Exp $
32  */

33 public class NodeViewMode extends BaseVisitor
34 {
35     private int indent = 0;
36     private boolean showTokens = true;
37
38     /** Indent child nodes to help visually identify
39       * the structure of the AST.
40       */

41     private String JavaDoc indentString()
42     {
43         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
44         for (int i = 0; i < indent; ++i)
45         {
46             sb.append(" ");
47         }
48         return sb.toString();
49     }
50
51     /**
52       * Display the type of nodes and optionally the
53       * first token.
54       */

55     private Object JavaDoc showNode(Node node, Object JavaDoc data)
56     {
57         String JavaDoc tokens = "";
58         String JavaDoc special = "";
59         Token t;
60         
61         if (showTokens)
62         {
63             t = node.getFirstToken();
64             
65             if (t.specialToken != null && ! t.specialToken.image.startsWith("##"))
66                 special = t.specialToken.image;
67             
68             tokens = " -> " + special + t.image;
69         }
70         
71         System.out.println(indentString() + node + tokens);
72         ++indent;
73         data = node.childrenAccept(this, data);
74         --indent;
75         return data;
76     }
77
78     /** Display a SimpleNode */
79     public Object JavaDoc visit(SimpleNode node, Object JavaDoc data)
80     {
81         return showNode(node,data);
82     }
83     
84     /** Display an ASTprocess node */
85     public Object JavaDoc visit(ASTprocess node, Object JavaDoc data)
86     {
87         return showNode(node,data);
88     }
89     
90     /** Display an ASTExpression node */
91     public Object JavaDoc visit(ASTExpression node, Object JavaDoc data)
92     {
93         return showNode(node,data);
94     }
95     
96     /** Display an ASTAssignment node ( = ) */
97     public Object JavaDoc visit(ASTAssignment node, Object JavaDoc data)
98     {
99         return showNode(node,data);
100     }
101     
102     /** Display an ASTOrNode ( || ) */
103     public Object JavaDoc visit(ASTOrNode node, Object JavaDoc data)
104     {
105         return showNode(node,data);
106     }
107     
108     /** Display an ASTAndNode ( && ) */
109     public Object JavaDoc visit(ASTAndNode node, Object JavaDoc data)
110     {
111         return showNode(node,data);
112     }
113     
114     /** Display an ASTEQNode ( == ) */
115     public Object JavaDoc visit(ASTEQNode node, Object JavaDoc data)
116     {
117         return showNode(node,data);
118     }
119     
120     /** Display an ASTNENode ( != ) */
121     public Object JavaDoc visit(ASTNENode node, Object JavaDoc data)
122     {
123         return showNode(node,data);
124     }
125     
126     /** Display an ASTLTNode ( < ) */
127     public Object JavaDoc visit(ASTLTNode node, Object JavaDoc data)
128     {
129         return showNode(node,data);
130     }
131     
132     /** Display an ASTGTNode ( > ) */
133     public Object JavaDoc visit(ASTGTNode node, Object JavaDoc data)
134     {
135         return showNode(node,data);
136     }
137     
138     /** Display an ASTLENode ( <= ) */
139     public Object JavaDoc visit(ASTLENode node, Object JavaDoc data)
140     {
141         return showNode(node,data);
142     }
143     
144     /** Display an ASTGENode ( >= ) */
145     public Object JavaDoc visit(ASTGENode node, Object JavaDoc data)
146     {
147         return showNode(node,data);
148     }
149     
150     /** Display an ASTAddNode ( + ) */
151     public Object JavaDoc visit(ASTAddNode node, Object JavaDoc data)
152     {
153         return showNode(node,data);
154     }
155     
156     /** Display an ASTSubtractNode ( - ) */
157     public Object JavaDoc visit(ASTSubtractNode node, Object JavaDoc data)
158     {
159         return showNode(node,data);
160     }
161     
162     /** Display an ASTMulNode ( * ) */
163     public Object JavaDoc visit(ASTMulNode node, Object JavaDoc data)
164     {
165         return showNode(node,data);
166     }
167     
168     /** Display an ASTDivNode ( / ) */
169     public Object JavaDoc visit(ASTDivNode node, Object JavaDoc data)
170     {
171         return showNode(node,data);
172     }
173     
174     /** Display an ASTModNode ( % ) */
175     public Object JavaDoc visit(ASTModNode node, Object JavaDoc data)
176     {
177         return showNode(node,data);
178     }
179     
180     /** Display an ASTNotNode ( ! ) */
181     public Object JavaDoc visit(ASTNotNode node, Object JavaDoc data)
182     {
183         return showNode(node,data);
184     }
185     
186     /** Display an ASTNumberLiteral node */
187     public Object JavaDoc visit(ASTNumberLiteral node, Object JavaDoc data)
188     {
189         return showNode(node,data);
190     }
191     
192     /** Display an ASTStringLiteral node */
193     public Object JavaDoc visit(ASTStringLiteral node, Object JavaDoc data)
194     {
195         return showNode(node,data);
196     }
197     
198     /** Display an ASTIdentifier node */
199     public Object JavaDoc visit(ASTIdentifier node, Object JavaDoc data)
200     {
201         return showNode(node,data);
202     }
203     
204     /** Display an ASTMethod node */
205     public Object JavaDoc visit(ASTMethod node, Object JavaDoc data)
206     {
207         return showNode(node,data);
208     }
209     
210     /** Display an ASTReference node */
211     public Object JavaDoc visit(ASTReference node, Object JavaDoc data)
212     {
213         return showNode(node,data);
214     }
215     
216     /** Display an ASTTrue node */
217     public Object JavaDoc visit(ASTTrue node, Object JavaDoc data)
218     {
219         return showNode(node,data);
220     }
221     
222     /** Display an ASTFalse node */
223     public Object JavaDoc visit(ASTFalse node, Object JavaDoc data)
224     {
225         return showNode(node,data);
226     }
227     
228     /** Display an ASTBlock node */
229     public Object JavaDoc visit(ASTBlock node, Object JavaDoc data)
230     {
231         return showNode(node,data);
232     }
233     
234     /** Display an ASTText node */
235     public Object JavaDoc visit(ASTText node, Object JavaDoc data)
236     {
237         return showNode(node,data);
238     }
239     
240     /** Display an ASTIfStatement node */
241     public Object JavaDoc visit(ASTIfStatement node, Object JavaDoc data)
242     {
243         return showNode(node,data);
244     }
245     
246     /** Display an ASTElseStatement node */
247     public Object JavaDoc visit(ASTElseStatement node, Object JavaDoc data)
248     {
249         return showNode(node,data);
250     }
251     
252     /** Display an ASTElseIfStatement node */
253     public Object JavaDoc visit(ASTElseIfStatement node, Object JavaDoc data)
254     {
255         return showNode(node,data);
256     }
257     
258     public Object JavaDoc visit(ASTObjectArray node, Object JavaDoc data)
259     {
260         return showNode(node,data);
261     }
262
263     public Object JavaDoc visit(ASTDirective node, Object JavaDoc data)
264     {
265         return showNode(node,data);
266     }
267
268     public Object JavaDoc visit(ASTWord node, Object JavaDoc data)
269     {
270         return showNode(node,data);
271     }
272     
273     public Object JavaDoc visit(ASTSetDirective node, Object JavaDoc data)
274     {
275         return showNode(node,data);
276     }
277 }
278
Popular Tags