KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > template > parser > JagBlockImpl


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.template.parser;
19
20 import com.finalist.jag.util.*;
21
22 import java.io.*;
23
24
25 /**
26  * Class JagBlockImpl
27  *
28  *
29  * @author Wendel D. de Witte
30  * @version %I%, %G%
31  */

32 public class JagBlockImpl implements JagBlock, Serializable {
33
34    /** Field down */
35    private JagBlockImpl down;
36
37    /** Field right */
38    private JagBlockImpl right;
39
40    /** Field text */
41    private String JavaDoc text = "";
42
43    /** Field type */
44    private int type;
45
46    /** Field startPos */
47    private int startPos;
48
49    /** Field endPos */
50    private int endPos;
51
52    /** Field verboseStringConversion */
53    private static boolean verboseStringConversion = false;
54
55    /** Field tokenNames */
56    private static String JavaDoc[] tokenNames = null;
57
58
59    /**
60     * Add a node to the end of the child list for this node
61     *
62     * @param node
63     */

64    public void addChild(JagBlock node) {
65       if (node == null) return;
66
67       JagBlockImpl t = this.down;
68
69       if (t != null) {
70          while (t.right != null) {
71             t = t.right;
72          }
73
74          t.right = (JagBlockImpl) node;
75       }
76       else {
77          this.down = (JagBlockImpl) node;
78       }
79    }
80
81
82    /**
83     * Is node t equal to this in terms of token type and text?
84     *
85     * @param t
86     *
87     * @return
88     */

89    public boolean equals(JagBlock t) {
90       if (t == null) return false;
91
92       return this.getText().equals(t.getText())
93          && (this.getType() == t.getType());
94    }
95
96
97    /**
98     * Get the first child of this node; null if not children
99     *
100     * @return
101     */

102    public JagBlock getFirstChild() {
103       return down;
104    }
105
106
107    /**
108     * Get the next sibling in line after this one
109     *
110     * @return
111     */

112    public JagBlock getNextSibling() {
113       return right;
114    }
115
116
117    /**
118     * Get the token text for this node
119     *
120     * @return
121     */

122    public String JavaDoc getText() {
123       return text;
124    }
125
126
127    /**
128     * Get the token type for this node
129     *
130     * @return
131     */

132    public int getType() {
133       return type;
134    }
135
136
137    /**
138     * Set the startposition of the token
139     *
140     * @return
141     */

142    public int getStartPos() {
143       return startPos;
144    }
145
146
147    /**
148     * Set the endposition of the token
149     *
150     * @return
151     */

152    public int getEndPos() {
153       return endPos;
154    }
155
156
157    /**
158     * Method initialize
159     *
160     *
161     * @param t
162     * @param txt
163     *
164     */

165    public void initialize(int t, String JavaDoc txt) {
166       setType(t);
167       setText(txt);
168    }
169
170
171    /**
172     * Method initialize
173     *
174     *
175     * @param t
176     *
177     */

178    public void initialize(JagBlock t) {
179       initialize(t.getType(), t.getText());
180    }
181
182
183    /** Remove all children */
184    public void removeChildren() {
185       down = null;
186    }
187
188
189    /**
190     * Set the first child of a node.
191     *
192     * @param c
193     */

194    public void setFirstChild(JagBlock c) {
195       down = (JagBlockImpl) c;
196    }
197
198
199    /**
200     * Method setNextSibling
201     *
202     *
203     * @param n
204     *
205     */

206    public void setNextSibling(JagBlock n) {
207       right = (JagBlockImpl) n;
208    }
209
210
211    /**
212     * Set the token text for this node
213     *
214     * @param text
215     */

216    public void setText(String JavaDoc text) {
217       this.text = text;
218    }
219
220
221    /**
222     * Set the token type for this node
223     *
224     * @param type
225     */

226    public void setType(int type) {
227       this.type = type;
228    }
229
230
231    /**
232     * Set the startposition of the token
233     *
234     * @param startPos
235     */

236    public void setStartPos(int startPos) {
237       this.startPos = startPos;
238    }
239
240
241    /**
242     * Set the endposition of the token
243     *
244     * @param endPos
245     */

246    public void setEndPos(int endPos) {
247       this.endPos = endPos;
248    }
249
250
251    /**
252     * Method setVerboseStringConversion
253     *
254     *
255     * @param verbose
256     * @param names
257     *
258     */

259    public static void setVerboseStringConversion(boolean verbose, String JavaDoc[] names) {
260       verboseStringConversion = verbose;
261       tokenNames = names;
262    }
263
264
265    /**
266     * Method toString
267     *
268     *
269     * @return
270     *
271     */

272    public String JavaDoc toString() {
273       StringBuffer JavaDoc b = new StringBuffer JavaDoc();
274
275       // if verbose and type name not same as text(keyword probably)
276
if (verboseStringConversion && !getText()
277          .equalsIgnoreCase(tokenNames[getType()]) && !getText()
278          .equalsIgnoreCase(Tools
279          .stripFrontBack(tokenNames[getType()], "\"", "\""))) {
280          b.append('[');
281          b.append(getText());
282          b.append(",<");
283          b.append(tokenNames[getType()]);
284          b.append(">]");
285          return b.toString();
286       }
287       return getText();
288    }
289 }
290
291 ;
292
Popular Tags