KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > util > javasource > jjt > SimpleNode


1 /* Generated By:JJTree: Do not edit this line. SimpleNode.java */
2
3 package de.gulden.util.javasource.jjt;
4
5 public class SimpleNode implements Node {
6   protected Node parent;
7   protected Node[] children;
8   protected int id;
9   protected Parser parser;
10
11   public SimpleNode(int i) {
12     id = i;
13   }
14
15   public SimpleNode(Parser p, int i) {
16     this(i);
17     parser = p;
18   }
19
20   public void jjtOpen() {
21   }
22
23   public void jjtClose() {
24   }
25   
26   public void jjtSetParent(Node n) { parent = n; }
27   public Node jjtGetParent() { return parent; }
28
29   public void jjtAddChild(Node n, int i) {
30     if (children == null) {
31       children = new Node[i + 1];
32     } else if (i >= children.length) {
33       Node c[] = new Node[i + 1];
34       System.arraycopy(children, 0, c, 0, children.length);
35       children = c;
36     }
37     children[i] = n;
38   }
39
40   public Node jjtGetChild(int i) {
41     return children[i];
42   }
43
44   public int jjtGetNumChildren() {
45     return (children == null) ? 0 : children.length;
46   }
47
48   /* You can override these two methods in subclasses of SimpleNode to
49      customize the way the node appears when the tree is dumped. If
50      your output uses more than one line you should override
51      toString(String), otherwise overriding toString() is probably all
52      you need to do. */

53
54   public String JavaDoc toString() { return ParserTreeConstants.jjtNodeName[id]; }
55   public String JavaDoc toString(String JavaDoc prefix) { return prefix + toString(); }
56
57   /* Override this method if you want to customize how the node dumps
58      out its children. */

59
60   public void dump(String JavaDoc prefix) {
61     System.out.println(toString(prefix));
62     if (children != null) {
63       for (int i = 0; i < children.length; ++i) {
64     SimpleNode n = (SimpleNode)children[i];
65     if (n != null) {
66       n.dump(prefix + " ");
67     }
68       }
69     }
70   }
71
72
73   /*** added by Jens *********************************************************/
74
75   private final static Node[] EMPTY=new Node[0];
76   
77   protected String JavaDoc value;
78   protected Token[] tokenRange=new Token[2];
79   protected TextImage textImage;
80   protected String JavaDoc source;
81
82   public int getId()
83   {
84     return id;
85   }
86   
87   /**
88    * Get first child with id.
89    */

90   public Node getChild(int id)
91   {
92     if (children!=null)
93       {
94       for (int i=0;i<children.length;i++)
95       {
96         if (children[i].getId()==id)
97         {
98           return children[i];
99         }
100       }
101     }
102     return null;
103   }
104   
105   public boolean hasChild(int id)
106   {
107     return (getChild(id)!=null);
108   }
109   
110   public Node[] getChildren(int id)
111   {
112     if (children!=null)
113     {
114       java.util.Vector JavaDoc v=new java.util.Vector JavaDoc(5);
115       for (int i=0;i<children.length;i++)
116       {
117         if (children[i].getId()==id)
118         {
119           v.addElement(children[i]);
120         }
121       }
122       Node[] n=new Node[v.size()];
123       v.copyInto(n);
124       return n;
125     }
126     else
127     {
128       return EMPTY;
129     }
130   }
131   
132   public Node[] getAllChildren()
133   {
134     if (children!=null)
135     {
136       return children;
137     }
138     else
139     {
140       return EMPTY;
141     }
142   }
143   
144   public String JavaDoc getValue()
145   {
146     return value;
147   }
148   
149   public void setValue(String JavaDoc v)
150   {
151 //System.out.print(v+" ");
152
value=v;
153   }
154   
155   public String JavaDoc getName()
156   {
157     Node node=getChild(ParserTreeConstants.JJT_NAME);
158     if (node!=null)
159     {
160       return node.retrieveName();
161     }
162     else
163     {
164       return null;
165     }
166   }
167   
168   public String JavaDoc retrieveName()
169   {
170     String JavaDoc name;
171     int num=jjtGetNumChildren();
172     if (num==1) // optimization: single child node _IDENTIFIER
173
{
174       Node n=jjtGetChild(0);
175       name=n.getValue();
176     }
177     else // general case: name identifiers joined by dots
178
{
179       StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
180       for (int i=0;i<num;i+=2)
181       {
182         //Node child=node.jjtGetChild(i).getValue();
183
String JavaDoc n=jjtGetChild(i).getValue();
184         sb.append(n);
185         if (i+1<num)
186         {
187           // next one must be _DOT
188
sb.append(".");
189         }
190       }
191       name=sb.toString();
192     }
193     return name;
194   }
195   
196   public void setStartToken(Token t)
197   {
198     tokenRange[0]=t;
199   }
200   
201   public void setEndToken(Token t)
202   {
203     tokenRange[1]=t;
204   }
205   
206   public Token getStartToken()
207   {
208     return tokenRange[0];
209   }
210   
211   public Token getEndToken()
212   {
213     return tokenRange[1];
214   }
215   
216   public void setTextImage(TextImage ti)
217   {
218     textImage=ti;
219   }
220   
221   public TextImage getTextImage()
222   {
223     return textImage;
224   }
225   
226   public String JavaDoc getSource()
227   {
228     return source;
229   }
230   
231   public void setSource(String JavaDoc source)
232   {
233     this.source=source;
234   }
235   
236   public int[] getSourcePosition()
237   {
238     int[] pos={getStartToken().beginLine,getStartToken().beginColumn};
239     return pos;
240   }
241 }
242
243
Popular Tags