001: package org.apache.velocity.runtime.visitor;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.velocity.runtime.parser.Token;
023: import org.apache.velocity.runtime.parser.node.ASTAddNode;
024: import org.apache.velocity.runtime.parser.node.ASTAndNode;
025: import org.apache.velocity.runtime.parser.node.ASTAssignment;
026: import org.apache.velocity.runtime.parser.node.ASTBlock;
027: import org.apache.velocity.runtime.parser.node.ASTDirective;
028: import org.apache.velocity.runtime.parser.node.ASTDivNode;
029: import org.apache.velocity.runtime.parser.node.ASTEQNode;
030: import org.apache.velocity.runtime.parser.node.ASTElseIfStatement;
031: import org.apache.velocity.runtime.parser.node.ASTElseStatement;
032: import org.apache.velocity.runtime.parser.node.ASTEscape;
033: import org.apache.velocity.runtime.parser.node.ASTEscapedDirective;
034: import org.apache.velocity.runtime.parser.node.ASTExpression;
035: import org.apache.velocity.runtime.parser.node.ASTFalse;
036: import org.apache.velocity.runtime.parser.node.ASTFloatingPointLiteral;
037: import org.apache.velocity.runtime.parser.node.ASTGENode;
038: import org.apache.velocity.runtime.parser.node.ASTGTNode;
039: import org.apache.velocity.runtime.parser.node.ASTIdentifier;
040: import org.apache.velocity.runtime.parser.node.ASTIfStatement;
041: import org.apache.velocity.runtime.parser.node.ASTIntegerLiteral;
042: import org.apache.velocity.runtime.parser.node.ASTIntegerRange;
043: import org.apache.velocity.runtime.parser.node.ASTLENode;
044: import org.apache.velocity.runtime.parser.node.ASTLTNode;
045: import org.apache.velocity.runtime.parser.node.ASTMap;
046: import org.apache.velocity.runtime.parser.node.ASTMethod;
047: import org.apache.velocity.runtime.parser.node.ASTModNode;
048: import org.apache.velocity.runtime.parser.node.ASTMulNode;
049: import org.apache.velocity.runtime.parser.node.ASTNENode;
050: import org.apache.velocity.runtime.parser.node.ASTNotNode;
051: import org.apache.velocity.runtime.parser.node.ASTObjectArray;
052: import org.apache.velocity.runtime.parser.node.ASTOrNode;
053: import org.apache.velocity.runtime.parser.node.ASTReference;
054: import org.apache.velocity.runtime.parser.node.ASTSetDirective;
055: import org.apache.velocity.runtime.parser.node.ASTStop;
056: import org.apache.velocity.runtime.parser.node.ASTStringLiteral;
057: import org.apache.velocity.runtime.parser.node.ASTSubtractNode;
058: import org.apache.velocity.runtime.parser.node.ASTText;
059: import org.apache.velocity.runtime.parser.node.ASTTrue;
060: import org.apache.velocity.runtime.parser.node.ASTWord;
061: import org.apache.velocity.runtime.parser.node.ASTprocess;
062: import org.apache.velocity.runtime.parser.node.Node;
063: import org.apache.velocity.runtime.parser.node.SimpleNode;
064:
065: /**
066: * This class is simply a visitor implementation
067: * that traverses the AST, produced by the Velocity
068: * parsing process, and creates a visual structure
069: * of the AST. This is primarily used for
070: * debugging, but it useful for documentation
071: * as well.
072: *
073: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
074: * @version $Id: NodeViewMode.java 463298 2006-10-12 16:10:32Z henning $
075: */
076: public class NodeViewMode extends BaseVisitor {
077: private int indent = 0;
078: private boolean showTokens = true;
079:
080: /** Indent child nodes to help visually identify
081: * the structure of the AST.
082: */
083: private String indentString() {
084: StringBuffer sb = new StringBuffer();
085: for (int i = 0; i < indent; ++i) {
086: sb.append(" ");
087: }
088: return sb.toString();
089: }
090:
091: /**
092: * Display the type of nodes and optionally the
093: * first token.
094: */
095: private Object showNode(Node node, Object data) {
096: String tokens = "";
097: String special = "";
098: Token t;
099:
100: if (showTokens) {
101: t = node.getFirstToken();
102:
103: if (t.specialToken != null
104: && !t.specialToken.image.startsWith("##"))
105: special = t.specialToken.image;
106:
107: tokens = " -> " + special + t.image;
108: }
109:
110: System.out.println(indentString() + node + tokens);
111: ++indent;
112: data = node.childrenAccept(this , data);
113: --indent;
114: return data;
115: }
116:
117: /**
118: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.SimpleNode, java.lang.Object)
119: */
120: public Object visit(SimpleNode node, Object data) {
121: return showNode(node, data);
122: }
123:
124: /**
125: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTprocess, java.lang.Object)
126: */
127: public Object visit(ASTprocess node, Object data) {
128: return showNode(node, data);
129: }
130:
131: /**
132: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTExpression, java.lang.Object)
133: */
134: public Object visit(ASTExpression node, Object data) {
135: return showNode(node, data);
136: }
137:
138: /**
139: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTAssignment, java.lang.Object)
140: */
141: public Object visit(ASTAssignment node, Object data) {
142: return showNode(node, data);
143: }
144:
145: /**
146: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTOrNode, java.lang.Object)
147: */
148: public Object visit(ASTOrNode node, Object data) {
149: return showNode(node, data);
150: }
151:
152: /**
153: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTAndNode, java.lang.Object)
154: */
155: public Object visit(ASTAndNode node, Object data) {
156: return showNode(node, data);
157: }
158:
159: /**
160: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTEQNode, java.lang.Object)
161: */
162: public Object visit(ASTEQNode node, Object data) {
163: return showNode(node, data);
164: }
165:
166: /**
167: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTNENode, java.lang.Object)
168: */
169: public Object visit(ASTNENode node, Object data) {
170: return showNode(node, data);
171: }
172:
173: /**
174: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTLTNode, java.lang.Object)
175: */
176: public Object visit(ASTLTNode node, Object data) {
177: return showNode(node, data);
178: }
179:
180: /**
181: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTGTNode, java.lang.Object)
182: */
183: public Object visit(ASTGTNode node, Object data) {
184: return showNode(node, data);
185: }
186:
187: /**
188: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTLENode, java.lang.Object)
189: */
190: public Object visit(ASTLENode node, Object data) {
191: return showNode(node, data);
192: }
193:
194: /**
195: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTGENode, java.lang.Object)
196: */
197: public Object visit(ASTGENode node, Object data) {
198: return showNode(node, data);
199: }
200:
201: /**
202: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTAddNode, java.lang.Object)
203: */
204: public Object visit(ASTAddNode node, Object data) {
205: return showNode(node, data);
206: }
207:
208: /**
209: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTSubtractNode, java.lang.Object)
210: */
211: public Object visit(ASTSubtractNode node, Object data) {
212: return showNode(node, data);
213: }
214:
215: /**
216: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTMulNode, java.lang.Object)
217: */
218: public Object visit(ASTMulNode node, Object data) {
219: return showNode(node, data);
220: }
221:
222: /**
223: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTDivNode, java.lang.Object)
224: */
225: public Object visit(ASTDivNode node, Object data) {
226: return showNode(node, data);
227: }
228:
229: /**
230: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTModNode, java.lang.Object)
231: */
232: public Object visit(ASTModNode node, Object data) {
233: return showNode(node, data);
234: }
235:
236: /**
237: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTNotNode, java.lang.Object)
238: */
239: public Object visit(ASTNotNode node, Object data) {
240: return showNode(node, data);
241: }
242:
243: /**
244: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTFloatingPointLiteral, java.lang.Object)
245: */
246: public Object visit(ASTFloatingPointLiteral node, Object data) {
247: return showNode(node, data);
248: }
249:
250: /**
251: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTIntegerLiteral, java.lang.Object)
252: */
253: public Object visit(ASTIntegerLiteral node, Object data) {
254: return showNode(node, data);
255: }
256:
257: /**
258: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTStringLiteral, java.lang.Object)
259: */
260: public Object visit(ASTStringLiteral node, Object data) {
261: return showNode(node, data);
262: }
263:
264: /**
265: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTIdentifier, java.lang.Object)
266: */
267: public Object visit(ASTIdentifier node, Object data) {
268: return showNode(node, data);
269: }
270:
271: /**
272: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTMethod, java.lang.Object)
273: */
274: public Object visit(ASTMethod node, Object data) {
275: return showNode(node, data);
276: }
277:
278: /**
279: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTReference, java.lang.Object)
280: */
281: public Object visit(ASTReference node, Object data) {
282: return showNode(node, data);
283: }
284:
285: /**
286: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTTrue, java.lang.Object)
287: */
288: public Object visit(ASTTrue node, Object data) {
289: return showNode(node, data);
290: }
291:
292: /**
293: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTFalse, java.lang.Object)
294: */
295: public Object visit(ASTFalse node, Object data) {
296: return showNode(node, data);
297: }
298:
299: /**
300: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTBlock, java.lang.Object)
301: */
302: public Object visit(ASTBlock node, Object data) {
303: return showNode(node, data);
304: }
305:
306: /**
307: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTText, java.lang.Object)
308: */
309: public Object visit(ASTText node, Object data) {
310: return showNode(node, data);
311: }
312:
313: /**
314: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTIfStatement, java.lang.Object)
315: */
316: public Object visit(ASTIfStatement node, Object data) {
317: return showNode(node, data);
318: }
319:
320: /**
321: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTElseStatement, java.lang.Object)
322: */
323: public Object visit(ASTElseStatement node, Object data) {
324: return showNode(node, data);
325: }
326:
327: /**
328: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTElseIfStatement, java.lang.Object)
329: */
330: public Object visit(ASTElseIfStatement node, Object data) {
331: return showNode(node, data);
332: }
333:
334: /**
335: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTObjectArray, java.lang.Object)
336: */
337: public Object visit(ASTObjectArray node, Object data) {
338: return showNode(node, data);
339: }
340:
341: /**
342: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTDirective, java.lang.Object)
343: */
344: public Object visit(ASTDirective node, Object data) {
345: return showNode(node, data);
346: }
347:
348: /**
349: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTWord, java.lang.Object)
350: */
351: public Object visit(ASTWord node, Object data) {
352: return showNode(node, data);
353: }
354:
355: /**
356: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTSetDirective, java.lang.Object)
357: */
358: public Object visit(ASTSetDirective node, Object data) {
359: return showNode(node, data);
360: }
361:
362: /**
363: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTEscapedDirective, java.lang.Object)
364: */
365: public Object visit(ASTEscapedDirective node, Object data) {
366: return showNode(node, data);
367: }
368:
369: /**
370: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTEscape, java.lang.Object)
371: */
372: public Object visit(ASTEscape node, Object data) {
373: return showNode(node, data);
374: }
375:
376: /**
377: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTMap, java.lang.Object)
378: */
379: public Object visit(ASTMap node, Object data) {
380: return showNode(node, data);
381: }
382:
383: /**
384: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTIntegerRange, java.lang.Object)
385: */
386: public Object visit(ASTIntegerRange node, Object data) {
387: return showNode(node, data);
388: }
389:
390: /**
391: * @see org.apache.velocity.runtime.visitor.BaseVisitor#visit(org.apache.velocity.runtime.parser.node.ASTStop, java.lang.Object)
392: */
393: public Object visit(ASTStop node, Object data) {
394: return showNode(node, data);
395: }
396: }
|