KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > ext > parsers > java_1_2 > SimpleNode


1 /* Generated By:JJTree: Do not edit this line. SimpleNode.java */
2 //
3
// Ejen (code generation system)
4
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
5
//
6
// This file is part of Ejen.
7
//
8
// Ejen is free software; you can redistribute it and/or modify
9
// it under the terms of the GNU General Public License as published by
10
// the Free Software Foundation; either version 2 of the License, or
11
// (at your option) any later version.
12
//
13
// Ejen is distributed in the hope that it will be useful,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
// GNU General Public License for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// along with Ejen; if not, write to the Free Software
20
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
//
22
package org.ejen.ext.parsers.java_1_2;
23
24 // New ---------------------------
25
import org.ejen.util.arl.ArlUtil;
26
27 // --------------------------- New
28
/**
29  * Modification of the <code>SimpleNode</code> class generated by
30  * <a HREF="http://www.webgain.com/products/java_cc/">JavaCC</a>.
31  * @author F. Wolff
32  * @version 1.0
33  */

34 public class SimpleNode implements Node {
35     protected Node parent;
36     protected Node[] children;
37     protected int id;
38     protected JavaParser parser;
39     // New ---------------------------
40
protected Token first, last;
41     // --------------------------- New
42
public SimpleNode(int i) {
43         id = i;
44     }
45
46     public SimpleNode(JavaParser p, int i) {
47         this(i);
48         parser = p;
49     }
50
51     public void jjtOpen() {
52         // New ---------------------------
53
first = parser.getToken(1);
54         // --------------------------- New
55
}
56
57     public void jjtClose() {
58         // New ---------------------------
59
last = parser.getToken(0);
60         // --------------------------- New
61
}
62
63     public void jjtSetParent(Node n) {
64         parent = n;
65     }
66
67     public Node jjtGetParent() {
68         return parent;
69     }
70
71     public void jjtAddChild(Node n, int i) {
72         if (children == null) {
73             children = new Node[i + 1];
74         } else if (i >= children.length) {
75             Node c[] = new Node[i + 1];
76
77             System.arraycopy(children, 0, c, 0, children.length);
78             children = c;
79         }
80         children[i] = n;
81     }
82
83     public Node jjtGetChild(int i) {
84         return children[i];
85     }
86
87     public int jjtGetNumChildren() {
88         return (children == null) ? 0 : children.length;
89     }
90
91     /* You can override these two methods in subclasses of SimpleNode to
92      customize the way the node appears when the tree is dumped. If
93      your output uses more than one line you should override
94      toString(String), otherwise overriding toString() is probably all
95      you need to do. */

96     public String JavaDoc toString() {
97         return JavaParserTreeConstants.jjtNodeName[id];
98     }
99
100     public String JavaDoc toString(String JavaDoc prefix) {
101         return prefix + toString();
102     }
103
104     /* Override this method if you want to customize how the node dumps
105      out its children. */

106     public void dump(String JavaDoc prefix) {
107         System.out.println(toString(prefix));
108         if (children != null) {
109             for (int i = 0; i < children.length; ++i) {
110                 SimpleNode n = (SimpleNode) children[i];
111
112                 if (n != null) {
113                     n.dump(prefix + " ");
114                 }
115             }
116         }
117     }
118
119     // New ---------------------------
120
/**
121      * Creates a new parent DOM node for this SimpleNode, stops visiting or just visits
122      * children nodes of this SimpleNode, depending on the 'nodesMap' parameter.
123      * Tokens DOM nodes are also created except if this SimpleNode is marked for removal.
124      * <p>
125      * <table class="usage"><tr><td class="usage"><pre>
126      *
127      * &lt;CompilationUnit id="0"&gt;
128      * ...
129      * &lt;/CompilationUnit&gt;
130      * </pre></td></tr></table>
131      * <p>
132      * Each SimpleNode is converted to a DOM Node using this rule: the Node name
133      * is the corresponding name of the production (according to the grammar), and
134      * its id attribute is the corresponding integer identifier
135      * ({@link JavaParserTreeConstants#JJTCOMPILATIONUNIT}). You may need to run
136      * JavaCC with the source to read the JavaParserTreeConstants.java file.
137      * <p>
138      * See also {@link org.ejen.util.arl.ArlUtil}.
139      * <p>
140      * @param doc the Document in which the nodes are to be created.
141      * @param parent the DOM node to which the new Node must be appended.
142      * @param nodesMap an <code>int</code> array specifying, for each SimpleNode
143      * id, if this node must accepted (created), crossed (only children
144      * are visited without a node creation for this SimpleNode) or
145      * removed (no node creation, all children are skipped).
146      * @param tokensMap passed to the
147      * {@link org.ejen.ext.parsers.java_1_2.Token#toNode(org.w3c.dom.Document,
148      * org.w3c.dom.Node,
149      * int[],boolean)
150      * Token.toNode(...)} function.
151      * @throws org.w3c.dom.DOMException DOM errors...
152      */

153     public final void toNode(org.w3c.dom.Document JavaDoc doc,
154             org.w3c.dom.Node JavaDoc parent,
155             int nodesMap[],
156             int[] tokensMap,
157             boolean tokensPos)
158         throws org.w3c.dom.DOMException JavaDoc {
159         if (nodesMap[id] == ArlUtil.F_REMOVE) {
160             return;
161         }
162         if (nodesMap[id] == ArlUtil.F_ACCEPT) {
163             org.w3c.dom.Element JavaDoc elt = doc.createElement(JavaParserTreeConstants.jjtNodeName[id]);
164
165             elt.setAttribute("id", String.valueOf(id));
166             parent.appendChild(elt);
167             parent = elt;
168         }
169         Token t = new Token();
170
171         t.next = first;
172         if (children != null) {
173             for (int i = 0; i < children.length; i++) {
174                 SimpleNode cn = (SimpleNode) (children[i]);
175
176                 for (t = t.next; t != null && t != cn.first; t = t.next) {
177                     t.toNode(doc, parent, tokensMap, tokensPos);
178                 }
179                 cn.toNode(doc, parent, nodesMap, tokensMap, tokensPos);
180                 t = cn.last;
181             }
182         }
183         while (t != null && t != last) {
184             t = t.next;
185             if (t != null) {
186                 t.toNode(doc, parent, tokensMap, tokensPos);
187             }
188         }
189     }
190     // --------------------------- New
191
}
192
Popular Tags