KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > parser > node > SimpleNode


1 /*
2  * Copyright 2000-2001,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.velocity.runtime.parser.node;
18
19 import java.io.Writer JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import org.apache.velocity.context.InternalContextAdapter;
23 import org.apache.velocity.runtime.RuntimeServices;
24 import org.apache.velocity.runtime.exception.ReferenceException;
25 import org.apache.velocity.runtime.parser.Parser;
26 import org.apache.velocity.runtime.parser.Token;
27
28 import org.apache.velocity.exception.MethodInvocationException;
29 import org.apache.velocity.exception.ParseErrorException;
30 import org.apache.velocity.exception.ResourceNotFoundException;
31
32
33 public class SimpleNode implements Node
34 {
35     protected RuntimeServices rsvc = null;
36
37     protected Node parent;
38     protected Node[] children;
39     protected int id;
40     protected Parser parser;
41     
42     protected int info; // added
43
public boolean state;
44     protected boolean invalid = false;
45     
46     /* Added */
47     protected Token first, last;
48
49     public SimpleNode(int i)
50     {
51         id = i;
52     }
53
54     public SimpleNode(Parser p, int i)
55     {
56         this(i);
57         parser = p;
58     }
59
60     public void jjtOpen()
61     {
62         first = parser.getToken(1); // added
63
}
64
65     public void jjtClose()
66     {
67         last = parser.getToken(0); // added
68
}
69
70     public void setFirstToken(Token t)
71     {
72         this.first = t;
73     }
74
75     public Token getFirstToken()
76     {
77         return first;
78     }
79     public Token getLastToken()
80     {
81         return last;
82     }
83
84     public void jjtSetParent(Node n)
85     {
86         parent = n;
87     }
88     public Node jjtGetParent()
89     {
90         return parent;
91     }
92
93     public void jjtAddChild(Node n, int i)
94     {
95         if (children == null)
96         {
97             children = new Node[i + 1];
98         }
99         else if (i >= children.length)
100         {
101             Node c[] = new Node[i + 1];
102             System.arraycopy(children, 0, c, 0, children.length);
103             children = c;
104         }
105         children[i] = n;
106     }
107
108     public Node jjtGetChild(int i)
109     {
110         return children[i];
111     }
112
113     public int jjtGetNumChildren()
114     {
115         return (children == null) ? 0 : children.length;
116     }
117
118     /** Accept the visitor. **/
119     public Object JavaDoc jjtAccept(ParserVisitor visitor, Object JavaDoc data)
120     {
121         return visitor.visit(this, data);
122     }
123
124     /** Accept the visitor. **/
125     public Object JavaDoc childrenAccept(ParserVisitor visitor, Object JavaDoc data)
126     {
127         if (children != null)
128         {
129             for (int i = 0; i < children.length; ++i)
130             {
131                 children[i].jjtAccept(visitor, data);
132             }
133         }
134         return data;
135     }
136
137     /* You can override these two methods in subclasses of SimpleNode to
138         customize the way the node appears when the tree is dumped. If
139         your output uses more than one line you should override
140         toString(String), otherwise overriding toString() is probably all
141         you need to do. */

142
143     // public String toString()
144
// {
145
// return ParserTreeConstants.jjtNodeName[id];
146
// }
147
public String JavaDoc toString(String JavaDoc prefix)
148     {
149         return prefix + toString();
150     }
151
152     /* Override this method if you want to customize how the node dumps
153         out its children. */

154
155     public void dump(String JavaDoc prefix)
156     {
157         System.out.println(toString(prefix));
158         if (children != null)
159         {
160             for (int i = 0; i < children.length; ++i)
161             {
162                 SimpleNode n = (SimpleNode) children[i];
163                 if (n != null)
164                 {
165                     n.dump(prefix + " ");
166                 }
167             }
168         }
169     }
170
171     // All additional methods
172

173     public String JavaDoc literal()
174     {
175         Token t = first;
176         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(t.image);
177         
178         while (t != last)
179         {
180             t = t.next;
181             sb.append(t.image);
182         }
183         
184         return sb.toString();
185     }
186
187     public Object JavaDoc init( InternalContextAdapter context, Object JavaDoc data) throws Exception JavaDoc
188     {
189         /*
190          * hold onto the RuntimeServices
191          */

192
193         rsvc = (RuntimeServices) data;
194
195         int i, k = jjtGetNumChildren();
196
197         for (i = 0; i < k; i++)
198         {
199             try
200             {
201                 jjtGetChild(i).init( context, data);
202             }
203             catch (ReferenceException re)
204             {
205                 rsvc.error(re);
206             }
207         }
208     
209         return data;
210     }
211
212     public boolean evaluate( InternalContextAdapter context)
213         throws MethodInvocationException
214     {
215         return false;
216     }
217
218     public Object JavaDoc value( InternalContextAdapter context)
219         throws MethodInvocationException
220     {
221         return null;
222     }
223
224     public boolean render( InternalContextAdapter context, Writer JavaDoc writer)
225         throws IOException JavaDoc, MethodInvocationException, ParseErrorException, ResourceNotFoundException
226     {
227         int i, k = jjtGetNumChildren();
228
229         for (i = 0; i < k; i++)
230             jjtGetChild(i).render(context, writer);
231     
232         return true;
233     }
234
235     public Object JavaDoc execute(Object JavaDoc o, InternalContextAdapter context)
236       throws MethodInvocationException
237     {
238         return null;
239     }
240
241     public int getType()
242     {
243         return id;
244     }
245
246     public void setInfo(int info)
247     {
248         this.info = info;
249     }
250     
251     public int getInfo()
252     {
253         return info;
254     }
255
256     public void setInvalid()
257     {
258         invalid = true;
259     }
260
261     public boolean isInvalid()
262     {
263         return invalid;
264     }
265
266     public int getLine()
267     {
268         return first.beginLine;
269     }
270     
271     public int getColumn()
272     {
273         return first.beginColumn;
274     }
275 }
276
277
Popular Tags