1 /***************************************************************************************
2 * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- *
5 * The software in this package is published under the terms of the LGPL license *
6 * a copy of which has been included with this distribution in the license.txt file. *
7 **************************************************************************************/
8 package org.codehaus.aspectwerkz.expression;
9
10 import org.codehaus.aspectwerkz.expression.ast.ASTAnd;
11 import org.codehaus.aspectwerkz.expression.ast.ASTAttribute;
12 import org.codehaus.aspectwerkz.expression.ast.ASTCall;
13 import org.codehaus.aspectwerkz.expression.ast.ASTCflow;
14 import org.codehaus.aspectwerkz.expression.ast.ASTCflowBelow;
15 import org.codehaus.aspectwerkz.expression.ast.ASTClassPattern;
16 import org.codehaus.aspectwerkz.expression.ast.ASTConstructorPattern;
17 import org.codehaus.aspectwerkz.expression.ast.ASTExecution;
18 import org.codehaus.aspectwerkz.expression.ast.ASTExpression;
19 import org.codehaus.aspectwerkz.expression.ast.ASTFieldPattern;
20 import org.codehaus.aspectwerkz.expression.ast.ASTGet;
21 import org.codehaus.aspectwerkz.expression.ast.ASTHandler;
22 import org.codehaus.aspectwerkz.expression.ast.ASTMethodPattern;
23 import org.codehaus.aspectwerkz.expression.ast.ASTModifier;
24 import org.codehaus.aspectwerkz.expression.ast.ASTNot;
25 import org.codehaus.aspectwerkz.expression.ast.ASTOr;
26 import org.codehaus.aspectwerkz.expression.ast.ASTParameter;
27 import org.codehaus.aspectwerkz.expression.ast.ASTPointcutReference;
28 import org.codehaus.aspectwerkz.expression.ast.ASTRoot;
29 import org.codehaus.aspectwerkz.expression.ast.ASTSet;
30 import org.codehaus.aspectwerkz.expression.ast.ASTStaticInitialization;
31 import org.codehaus.aspectwerkz.expression.ast.ASTWithin;
32 import org.codehaus.aspectwerkz.expression.ast.ASTWithinCode;
33 import org.codehaus.aspectwerkz.expression.ast.ExpressionParserVisitor;
34 import org.codehaus.aspectwerkz.expression.ast.SimpleNode;
35 import org.codehaus.aspectwerkz.expression.ast.ASTArgs;
36 import org.codehaus.aspectwerkz.expression.ast.ASTArgParameter;
37 import org.codehaus.aspectwerkz.expression.ast.ASTHasField;
38 import org.codehaus.aspectwerkz.expression.ast.ASTHasMethod;
39
40 /***
41 * TODO: do we need that, there is a dump() method in jjtree API
42 *
43 * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
44 * @author Michael Nascimento
45 */
46 public class DumpVisitor implements ExpressionParserVisitor {
47 private ASTRoot m_root;
48
49 private int indent = 0;
50
51 private DumpVisitor(final ASTRoot root) {
52 m_root = root;
53 }
54
55 public static void dumpAST(final ASTRoot root) {
56 DumpVisitor dumper = new DumpVisitor(root);
57 dumper.visit(dumper.m_root, null);
58 }
59
60 public Object visit(SimpleNode node, Object data) {
61 System.out.println(indentString() + node);
62 ++indent;
63 data = node.jjtGetChild(0).jjtAccept(this, data);
64 --indent;
65 return data;
66 }
67
68 public Object visit(ASTRoot node, Object data) {
69 System.out.println(indentString() + node);
70 ++indent;
71 data = node.jjtGetChild(0).jjtAccept(this, data);
72 --indent;
73 return data;
74 }
75
76 public Object visit(ASTExpression node, Object data) {
77 System.out.println(indentString() + node);
78 ++indent;
79 data = node.jjtGetChild(0).jjtAccept(this, data);
80 --indent;
81 return data;
82 }
83
84 public Object visit(ASTOr node, Object data) {
85 System.out.println(indentString() + node);
86 ++indent;
87 for (int i = 0; i < node.jjtGetNumChildren(); i++) {
88 data = node.jjtGetChild(i).jjtAccept(this, data);
89 }
90 --indent;
91 return data;
92 }
93
94 public Object visit(ASTAnd node, Object data) {
95 System.out.println(indentString() + node);
96 ++indent;
97 for (int i = 0; i < node.jjtGetNumChildren(); i++) {
98 data = node.jjtGetChild(i).jjtAccept(this, data);
99 }
100 --indent;
101 return data;
102 }
103
104 public Object visit(ASTNot node, Object data) {
105 System.out.println(indentString() + node);
106 ++indent;
107 data = node.jjtGetChild(0).jjtAccept(this, data);
108 --indent;
109 return data;
110 }
111
112 public Object visit(ASTExecution node, Object data) {
113 System.out.println(indentString() + node);
114 ++indent;
115 data = node.jjtGetChild(0).jjtAccept(this, data);
116 --indent;
117 return data;
118 }
119
120 public Object visit(ASTCall node, Object data) {
121 System.out.println(indentString() + node);
122 ++indent;
123 data = node.jjtGetChild(0).jjtAccept(this, data);
124 --indent;
125 return data;
126 }
127
128 public Object visit(ASTSet node, Object data) {
129 System.out.println(indentString() + node);
130 ++indent;
131 data = node.jjtGetChild(0).jjtAccept(this, data);
132 --indent;
133 return data;
134 }
135
136 public Object visit(ASTGet node, Object data) {
137 System.out.println(indentString() + node);
138 ++indent;
139 data = node.jjtGetChild(0).jjtAccept(this, data);
140 --indent;
141 return data;
142 }
143
144 public Object visit(ASTHandler node, Object data) {
145 System.out.println(indentString() + node);
146 ++indent;
147 data = node.jjtGetChild(0).jjtAccept(this, data);
148 --indent;
149 return data;
150 }
151
152 public Object visit(ASTWithin node, Object data) {
153 System.out.println(indentString() + node);
154 ++indent;
155 data = node.jjtGetChild(0).jjtAccept(this, data);
156 --indent;
157 return data;
158 }
159
160 public Object visit(ASTWithinCode node, Object data) {
161 System.out.println(indentString() + node);
162 ++indent;
163 data = node.jjtGetChild(0).jjtAccept(this, data);
164 --indent;
165 return data;
166 }
167
168 public Object visit(ASTStaticInitialization node, Object data) {
169 System.out.println(indentString() + node);
170 ++indent;
171 data = node.jjtGetChild(0).jjtAccept(this, data);
172 --indent;
173 return data;
174 }
175
176 public Object visit(ASTCflow node, Object data) {
177 System.out.println(indentString() + node);
178 ++indent;
179 data = node.jjtGetChild(0).jjtAccept(this, data);
180 --indent;
181 return data;
182 }
183
184 public Object visit(ASTCflowBelow node, Object data) {
185 System.out.println(indentString() + node);
186 ++indent;
187 data = node.jjtGetChild(0).jjtAccept(this, data);
188 --indent;
189 return data;
190 }
191
192
193 public Object visit(ASTHasMethod node, Object data) {
194 System.out.println(indentString() + node);
195 ++indent;
196 data = node.jjtGetChild(0).jjtAccept(this, data);
197 --indent;
198 return data;
199 }
200
201
202 public Object visit(ASTHasField node, Object data) {
203 System.out.println(indentString() + node);
204 ++indent;
205 data = node.jjtGetChild(0).jjtAccept(this, data);
206 --indent;
207 return data;
208 }
209
210 public Object visit(ASTClassPattern node, Object data) {
211 System.out.println(indentString() + node);
212 ++indent;
213 int nr = node.jjtGetNumChildren();
214 for (int i = 0; i < nr; i++) {
215 data = node.jjtGetChild(i).jjtAccept(this, data);
216 }
217 --indent;
218 return data;
219 }
220
221 public Object visit(ASTMethodPattern node, Object data) {
222 System.out.println(indentString() + node);
223 ++indent;
224 int nr = node.jjtGetNumChildren();
225 for (int i = 0; i < nr; i++) {
226 data = node.jjtGetChild(i).jjtAccept(this, data);
227 }
228 --indent;
229 return data;
230 }
231
232 public Object visit(ASTConstructorPattern node, Object data) {
233 System.out.println(indentString() + node);
234 ++indent;
235 int nr = node.jjtGetNumChildren();
236 for (int i = 0; i < nr; i++) {
237 data = node.jjtGetChild(i).jjtAccept(this, data);
238 }
239 --indent;
240 return data;
241 }
242
243 public Object visit(ASTFieldPattern node, Object data) {
244 System.out.println(indentString() + node);
245 ++indent;
246 int nr = node.jjtGetNumChildren();
247 for (int i = 0; i < nr; i++) {
248 data = node.jjtGetChild(i).jjtAccept(this, data);
249 }
250 --indent;
251 return data;
252 }
253
254 public Object visit(ASTPointcutReference node, Object data) {
255 System.out.println(indentString() + node);
256 return data;
257 }
258
259 public Object visit(ASTParameter node, Object data) {
260 System.out.println(indentString() + node);
261 return data;
262 }
263
264 public Object visit(ASTArgs node, Object data) {
265 System.out.println(indentString() + node);
266 ++indent;
267 if (node.jjtGetNumChildren() > 0) {
268 data = node.jjtGetChild(0).jjtAccept(this, data);
269 }
270 --indent;
271 return data;
272 }
273
274 public Object visit(ASTArgParameter node, Object data) {
275 System.out.println(indentString() + node);
276 return data;
277 }
278
279 public Object visit(ASTAttribute node, Object data) {
280 System.out.println(indentString() + node);
281 return data;
282 }
283
284 public Object visit(ASTModifier node, Object data) {
285 System.out.println(indentString() + node);
286 return data;
287 }
288
289 private String indentString() {
290 StringBuffer sb = new StringBuffer();
291 for (int i = 0; i < indent; ++i) {
292 sb.append(" ");
293 }
294 return sb.toString();
295 }
296 }