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