KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > eg4DumpVisitor


1
2 /*
3  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
4  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
5  * intellectual property rights relating to technology embodied in the product
6  * that is described in this document. In particular, and without limitation,
7  * these intellectual property rights may include one or more of the U.S.
8  * patents listed at http://www.sun.com/patents and one or more additional
9  * patents or pending patent applications in the U.S. and in other countries.
10  * U.S. Government Rights - Commercial software. Government users are subject
11  * to the Sun Microsystems, Inc. standard license agreement and applicable
12  * provisions of the FAR and its supplements. Use is subject to license terms.
13  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
14  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
15  * product is covered and controlled by U.S. Export Control laws and may be
16  * subject to the export or import laws in other countries. Nuclear, missile,
17  * chemical biological weapons or nuclear maritime end uses or end users,
18  * whether direct or indirect, are strictly prohibited. Export or reexport
19  * to countries subject to U.S. embargo or to entities identified on U.S.
20  * export exclusion lists, including, but not limited to, the denied persons
21  * and specially designated nationals lists is strictly prohibited.
22  */

23
24
25 /* This is an example of how the Visitor pattern might be used to
26    implement the dumping code that comes with SimpleNode. It's a bit
27    long-winded, but it does illustrate a couple of the main points.
28
29    1) the visitor can maintain state between the nodes that it visits
30    (for example the current indentation level).
31
32    2) if you don't implement a jjtAccept() method for a subclass of
33    SimpleNode, then SimpleNode's acceptor will get called.
34
35    3) the utility method childrenAccept() can be useful when
36    implementing preorder or postorder tree walks.
37
38 */

39
40 public class eg4DumpVisitor implements eg4Visitor
41 {
42   private int indent = 0;
43
44   private String JavaDoc indentString() {
45     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
46     for (int i = 0; i < indent; ++i) {
47       sb.append(" ");
48     }
49     return sb.toString();
50   }
51
52   public Object JavaDoc visit(SimpleNode node, Object JavaDoc data) {
53     System.out.println(indentString() + node +
54                ": acceptor not unimplemented in subclass?");
55     ++indent;
56     data = node.childrenAccept(this, data);
57     --indent;
58     return data;
59   }
60
61   public Object JavaDoc visit(ASTStart node, Object JavaDoc data) {
62     System.out.println(indentString() + node);
63     ++indent;
64     data = node.childrenAccept(this, data);
65     --indent;
66     return data;
67   }
68
69   public Object JavaDoc visit(ASTAdd node, Object JavaDoc data) {
70     System.out.println(indentString() + node);
71     ++indent;
72     data = node.childrenAccept(this, data);
73     --indent;
74     return data;
75   }
76
77   public Object JavaDoc visit(ASTMult node, Object JavaDoc data) {
78     System.out.println(indentString() + node);
79     ++indent;
80     data = node.childrenAccept(this, data);
81     --indent;
82     return data;
83   }
84
85   public Object JavaDoc visit(ASTMyOtherID node, Object JavaDoc data) {
86     System.out.println(indentString() + node);
87     ++indent;
88     data = node.childrenAccept(this, data);
89     --indent;
90     return data;
91   }
92
93   public Object JavaDoc visit(ASTInteger node, Object JavaDoc data) {
94     System.out.println(indentString() + node);
95     ++indent;
96     data = node.childrenAccept(this, data);
97     --indent;
98     return data;
99   }
100 }
101
102 /*end*/
103
Popular Tags