KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > expression > SimpleNode


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.lang.jpath.expression;
18
19 import javax.servlet.jsp.PageContext JavaDoc;
20
21 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext;
22
23 /**
24  * This class provides an abstract implementation of the <tt>Node</tt>
25  * iterface. All actual nodes should extend this class to take advantage
26  * of the default implementations of many of the methods.
27  */

28 public abstract class SimpleNode implements Node {
29
30     protected Node parent;
31     protected Node[] children;
32     protected int id;
33     protected Parser parser;
34     protected String JavaDoc image;
35     protected int beginColumn;
36     protected int endColumn;
37     protected int beginLine;
38     protected int endLine;
39     protected Token firstToken;
40     protected Token lastToken;
41     protected Object JavaDoc val;
42
43     /**
44      * Used to create an instance of the SimpleNode class
45      *
46      *
47      * @param i
48      *
49      */

50     public SimpleNode(int i) {
51         id = i;
52     }
53
54     /**
55      * Used to create an instance of the SimpleNode class
56      *
57      *
58      * @param p
59      * @param i
60      *
61      */

62     public SimpleNode(Parser p, int i) {
63
64         this(i);
65
66         parser = p;
67     }
68
69     /**
70      * The jjtOpen method
71      *
72      *
73      */

74     public void jjtOpen() {}
75
76     /**
77      * The jjtClose method
78      *
79      *
80      */

81     public void jjtClose() {}
82
83     /**
84      * The jjtSetParent method
85      *
86      *
87      * @param n
88      *
89      */

90     public void jjtSetParent(Node n) {
91         parent = n;
92     }
93
94     /**
95      * The jjtGetParent method
96      *
97      *
98      * @return
99      *
100      */

101     public Node jjtGetParent() {
102         return parent;
103     }
104
105     /**
106      * The jjtAddChild method
107      *
108      *
109      * @param n
110      * @param i
111      *
112      */

113     public void jjtAddChild(Node n, int i) {
114
115         if (children == null) {
116             children = new Node[i + 1];
117         } else if (i >= children.length) {
118             Node c[] = new Node[i + 1];
119
120             System.arraycopy(children, 0, c, 0, children.length);
121
122             children = c;
123         }
124
125         children[i] = n;
126     }
127
128     /**
129      * The jjtGetChild method
130      *
131      *
132      * @param i
133      *
134      * @return
135      *
136      */

137     public Node jjtGetChild(int i) {
138         return children[i];
139     }
140
141     /**
142      * The jjtGetNumChildren method
143      *
144      *
145      * @return
146      *
147      */

148     public int jjtGetNumChildren() {
149         return (children == null) ? 0 : children.length;
150     }
151
152     /**
153      * Provides a method to print the name of the node. You can override
154      * this method in subclasses of SimpleNode to
155      * customize the way the node appears when the tree is dumped. If
156      * your output uses more than one line you should override
157      * toString(String), otherwise overriding toString() is probably all
158      * you need to do.
159      *
160      * @return
161      */

162     public String JavaDoc toString() {
163         return ParserTreeConstants.jjtNodeName[id];
164     }
165
166     /**
167      * Provides a method to print the name of the node with a prefix string.
168      * You can override
169      * this method in subclasses of SimpleNode to
170      * customize the way the node appears when the tree is dumped. If
171      * your output uses more than one line you should override
172      * toString(String), otherwise overriding toString() is probably all
173      * you need to do.
174      *
175      * @param prefix
176      *
177      * @return
178      */

179     public String JavaDoc toString(String JavaDoc prefix) {
180         return prefix + toString();
181     }
182
183     /**
184      * Provides a method to dump the entire subtree of nodes to
185      * <tt>System.out</tt>. You should
186      * override this method if you want to customize how the node dumps
187      * out its children.
188      *
189      * @param prefix
190      */

191     public void dump(String JavaDoc prefix) {
192
193         System.out.println(toString(prefix));
194
195         if (children != null) {
196             for (int i = 0; i < children.length; ++i) {
197                 SimpleNode n = (SimpleNode) children[i];
198
199                 if (n != null) {
200                     n.dump(prefix + " ");
201                 }
202             }
203         }
204     }
205
206     /**
207      * Provides a method to dump the entire subtree of nodes to
208      * <tt>System.out</tt> with a prefix string. You should
209      * override this method if you want to customize how the node dumps
210      * out its children.
211      *
212      * @param prefix
213      *
214      * @return
215      */

216     public String JavaDoc stringDump(String JavaDoc prefix) {
217
218         String JavaDoc result = prefix + toString() + "\n";
219
220         System.out.println(toString(prefix));
221
222         if (children != null) {
223             for (int i = 0; i < children.length; ++i) {
224                 SimpleNode n = (SimpleNode) children[i];
225
226                 if (n != null) {
227                     result = result + n.stringDump(prefix + " ");
228                 }
229             }
230         }
231
232         return result;
233     }
234
235     /**
236      * Provides a method to print a normalized version of the original
237      * expression. The normalized version has standardized spacing and
238      * parenthesis, and can be used to compare expressions formatted
239      * in different ways to see if they are actually the same expression.
240      *
241      * @returns The normalized version of the original expression
242      *
243      * @return
244      */

245
246     /**
247      * Provides a method to print a normalized version of the original
248      * expression. The normalized version has standardized spacing and
249      * parenthesis, and can be used to compare expressions formatted
250      * in different ways to see if they are actually the same expression.
251      *
252      *
253      * @return The normalized version of the original expression
254      *
255      */

256     public String JavaDoc toNormalizedString() {
257
258         String JavaDoc normalized = "toNormalizedString() not implemented in ["
259                                 + ParserTreeConstants.jjtNodeName[id] + "]";
260
261         return normalized;
262     }
263
264     /**
265      * Provides a method to print the original version of the
266      * expression from this node downward in the tree.
267      * The original version is an accurate representation
268      * of the original expression as it was received. This can be useful
269      * when formatting error messages.
270      *
271      * @returns The original version of the expression, from this node downward
272      *
273      * @return
274      */

275     public String JavaDoc toOriginalString() {
276
277         String JavaDoc result = "";
278         Token t = firstToken;
279         boolean finished = false;
280
281         while (!finished) {
282             result += getSpecialTokenString(t);
283
284             if (t.image != null) {
285                 while (result.length() < t.beginColumn - 1) {
286                     result += " ";
287                 }
288
289                 result += (t.image);
290             }
291
292             if (t == lastToken) {
293                 finished = true;
294             } else {
295                 t = t.next;
296             }
297         }
298
299         return result;
300     }
301
302     /**
303      * Provides a method to print the original version of the
304      * entire expression.
305      * The original version is an accurate representation
306      * of the original expression as it was received. This can be useful
307      * when formatting error messages.
308      *
309      * @returns The original version of the entire expression
310      *
311      * @return
312      */

313     public String JavaDoc rootOriginalString() {
314
315         SimpleNode current = this;
316
317         while (current.parent != null) {
318             current = (SimpleNode) current.parent;
319         }
320
321         return current.toOriginalString();
322     }
323
324     /**
325      * This method evaluates this node of the expression and all child nodes.
326      * It returns the result of the
327      * evaluation as an <tt>Object</tt>. If any problems are encountered
328      * during the evaluation, an <tt>EvaluationException</tt> is thrown.
329      *
330      * @param pageContext the current JSP PageContext
331      *
332      * @param icontext the Iteration Context of the expression. If there is
333      * no interation context, this should be null.
334      *
335      * @returns the result of the expression evaluation as an object
336      *
337      *
338      * @return
339      * @throws EvaluationException if a problem is encountered during the
340      * evaluation
341      */

342     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
343             throws EvaluationException {
344
345         throw new EvaluationException(this,
346                 "evaluate not supported in ["
347                 + ParserTreeConstants.jjtNodeName[id] + "]");
348     }
349
350     /**
351      * The getTokenImage method
352      *
353      *
354      * @param tokenNumber
355      *
356      * @return
357      *
358      */

359     public String JavaDoc getTokenImage(int tokenNumber) {
360
361         String JavaDoc tokenImage = ParserConstants.tokenImage[tokenNumber];
362
363         return tokenImage.substring(1, tokenImage.length() - 1);
364     }
365
366     /**
367      * The getSpecialTokenString method
368      *
369      *
370      * @param t
371      *
372      * @return
373      *
374      */

375     private String JavaDoc getSpecialTokenString(Token t) {
376
377         String JavaDoc result;
378
379         if (t.specialToken == null) {
380             result = "";
381         } else {
382             result = getSpecialTokenString(t) + t.image;
383         }
384
385         return result;
386     }
387
388     /**
389      * Provides a mechanism to validate an expression. This method
390      * validates this node and all child nodes. The validation is done
391      * without any knowldege of the PageContext, and therefore
392      * even after validation, problems may still be encountered during
393      * evaluation. However, many problems, such as invalid literals,
394      * can be caught during validation.
395      *
396      * @throws ValidationException if a problem is encountered during the
397      */

398     public void validate() throws ValidationException {
399
400         if (children != null) {
401             for (int i = 0; i < children.length; ++i) {
402                 SimpleNode n = (SimpleNode) children[i];
403
404                 if (n != null) {
405                     n.validate();
406                 }
407             }
408         }
409     }
410
411     /**
412      * Provides a mechanism for simplication of the expression. For instance,
413      * large mathematical expressions with number literals can be simplified
414      * for faster subsequent evaluation. Other simplification may be possible
415      * with certain boolean logic expressions. This mechansim may be used in
416      * conjunction with an expression caching strategy.
417      *
418      * @returns the simplified node
419      *
420      *
421      * @return
422      */

423     public Node simplify() {
424
425         if (children != null) {
426             for (int i = 0; i < children.length; ++i) {
427                 SimpleNode n = (SimpleNode) children[i];
428
429                 if (n != null) {
430                     n = (SimpleNode) n.simplify();
431                 }
432             }
433         }
434
435         // Any custom simplification should be done here
436
return this;
437     }
438 }
439
Popular Tags