View Javadoc

1   package org.apache.velocity.runtime.parser.node;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.IOException;
23  import java.io.Writer;
24  
25  import org.apache.commons.lang.builder.ToStringBuilder;
26  import org.apache.velocity.context.InternalContextAdapter;
27  import org.apache.velocity.exception.MethodInvocationException;
28  import org.apache.velocity.exception.ParseErrorException;
29  import org.apache.velocity.exception.ResourceNotFoundException;
30  import org.apache.velocity.exception.TemplateInitException;
31  import org.apache.velocity.runtime.RuntimeServices;
32  import org.apache.velocity.runtime.log.Log;
33  import org.apache.velocity.runtime.parser.Parser;
34  import org.apache.velocity.runtime.parser.ParserVisitor;
35  import org.apache.velocity.runtime.parser.Token;
36  
37  /**
38   *
39   */
40  public class SimpleNode implements Node
41  {
42      /** */
43      protected RuntimeServices rsvc = null;
44  
45      /** */
46      protected Log log = null;
47  
48      /** */
49      protected Node parent;
50  
51      /** */
52      protected Node[] children;
53  
54      /** */
55      protected int id;
56  
57      /** */
58      protected Parser parser;
59  
60      /** */
61      protected int info; // added
62  
63      /** */
64      public boolean state;
65  
66      /** */
67      protected boolean invalid = false;
68  
69      /** */
70      protected Token first;
71  
72      /** */
73      protected Token last;
74  
75      /**
76       * @param i
77       */
78      public SimpleNode(int i)
79      {
80          id = i;
81      }
82  
83      /**
84       * @param p
85       * @param i
86       */
87      public SimpleNode(Parser p, int i)
88      {
89          this(i);
90          parser = p;
91      }
92  
93      /**
94       * @see org.apache.velocity.runtime.parser.node.Node#jjtOpen()
95       */
96      public void jjtOpen()
97      {
98          first = parser.getToken(1); // added
99      }
100 
101     /**
102      * @see org.apache.velocity.runtime.parser.node.Node#jjtClose()
103      */
104     public void jjtClose()
105     {
106         last = parser.getToken(0); // added
107     }
108 
109     /**
110      * @param t
111      */
112     public void setFirstToken(Token t)
113     {
114         this.first = t;
115     }
116 
117     /**
118      * @see org.apache.velocity.runtime.parser.node.Node#getFirstToken()
119      */
120     public Token getFirstToken()
121     {
122         return first;
123     }
124 
125     /**
126      * @see org.apache.velocity.runtime.parser.node.Node#getLastToken()
127      */
128     public Token getLastToken()
129     {
130         return last;
131     }
132 
133     /**
134      * @see org.apache.velocity.runtime.parser.node.Node#jjtSetParent(org.apache.velocity.runtime.parser.node.Node)
135      */
136     public void jjtSetParent(Node n)
137     {
138         parent = n;
139     }
140 
141     /**
142      * @see org.apache.velocity.runtime.parser.node.Node#jjtGetParent()
143      */
144     public Node jjtGetParent()
145     {
146         return parent;
147     }
148 
149     /**
150      * @see org.apache.velocity.runtime.parser.node.Node#jjtAddChild(org.apache.velocity.runtime.parser.node.Node, int)
151      */
152     public void jjtAddChild(Node n, int i)
153     {
154         if (children == null)
155         {
156             children = new Node[i + 1];
157         }
158         else if (i >= children.length)
159         {
160             Node c[] = new Node[i + 1];
161             System.arraycopy(children, 0, c, 0, children.length);
162             children = c;
163         }
164         children[i] = n;
165     }
166 
167     /**
168      * @see org.apache.velocity.runtime.parser.node.Node#jjtGetChild(int)
169      */
170     public Node jjtGetChild(int i)
171     {
172         return children[i];
173     }
174 
175     /**
176      * @see org.apache.velocity.runtime.parser.node.Node#jjtGetNumChildren()
177      */
178     public int jjtGetNumChildren()
179     {
180         return (children == null) ? 0 : children.length;
181     }
182 
183 
184     /**
185      * @see org.apache.velocity.runtime.parser.node.Node#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
186      */
187     public Object jjtAccept(ParserVisitor visitor, Object data)
188     {
189         return visitor.visit(this, data);
190     }
191 
192 
193     /**
194      * @see org.apache.velocity.runtime.parser.node.Node#childrenAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
195      */
196     public Object childrenAccept(ParserVisitor visitor, Object data)
197     {
198         if (children != null)
199         {
200             for (int i = 0; i < children.length; ++i)
201             {
202                 children[i].jjtAccept(visitor, data);
203             }
204         }
205         return data;
206     }
207 
208     /* You can override these two methods in subclasses of SimpleNode to
209         customize the way the node appears when the tree is dumped.  If
210         your output uses more than one line you should override
211         toString(String), otherwise overriding toString() is probably all
212         you need to do. */
213 
214     //    public String toString()
215     // {
216     //    return ParserTreeConstants.jjtNodeName[id];
217     // }
218     /**
219      * @param prefix
220      * @return String representation of this node.
221      */
222     public String toString(String prefix)
223     {
224         return prefix + toString();
225     }
226 
227     /**
228      * Override this method if you want to customize how the node dumps
229      * out its children.
230      *
231      * @param prefix
232      */
233     public void dump(String prefix)
234     {
235         System.out.println(toString(prefix));
236         if (children != null)
237         {
238             for (int i = 0; i < children.length; ++i)
239             {
240                 SimpleNode n = (SimpleNode) children[i];
241                 if (n != null)
242                 {
243                     n.dump(prefix + " ");
244                 }
245             }
246         }
247     }
248 
249     // All additional methods
250 
251     /**
252      * @see org.apache.velocity.runtime.parser.node.Node#literal()
253      */
254     public String literal()
255     {
256         Token t = first;
257         StringBuffer sb = new StringBuffer(t.image);
258 
259         while (t != last)
260         {
261             t = t.next;
262             sb.append(t.image);
263         }
264 
265         return sb.toString();
266     }
267 
268     /**
269      * @throws TemplateInitException 
270      * @see org.apache.velocity.runtime.parser.node.Node#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object)
271      */
272     public Object init( InternalContextAdapter context, Object data) throws TemplateInitException
273     {
274         /*
275          * hold onto the RuntimeServices
276          */
277 
278         rsvc = (RuntimeServices) data;
279         log = rsvc.getLog();
280 
281         int i, k = jjtGetNumChildren();
282 
283         for (i = 0; i < k; i++)
284         {
285             jjtGetChild(i).init( context, data);
286         }
287 
288         return data;
289     }
290 
291     /**
292      * @see org.apache.velocity.runtime.parser.node.Node#evaluate(org.apache.velocity.context.InternalContextAdapter)
293      */
294     public boolean evaluate( InternalContextAdapter  context)
295         throws MethodInvocationException
296     {
297         return false;
298     }
299 
300     /**
301      * @see org.apache.velocity.runtime.parser.node.Node#value(org.apache.velocity.context.InternalContextAdapter)
302      */
303     public Object value( InternalContextAdapter context)
304         throws MethodInvocationException
305     {
306         return null;
307     }
308 
309     /**
310      * @see org.apache.velocity.runtime.parser.node.Node#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
311      */
312     public boolean render( InternalContextAdapter context, Writer writer)
313         throws IOException, MethodInvocationException, ParseErrorException, ResourceNotFoundException
314     {
315         int i, k = jjtGetNumChildren();
316 
317         for (i = 0; i < k; i++)
318             jjtGetChild(i).render(context, writer);
319 
320         return true;
321     }
322 
323     /**
324      * @see org.apache.velocity.runtime.parser.node.Node#execute(java.lang.Object, org.apache.velocity.context.InternalContextAdapter)
325      */
326     public Object execute(Object o, InternalContextAdapter context)
327       throws MethodInvocationException
328     {
329         return null;
330     }
331 
332     /**
333      * @see org.apache.velocity.runtime.parser.node.Node#getType()
334      */
335     public int getType()
336     {
337         return id;
338     }
339 
340     /**
341      * @see org.apache.velocity.runtime.parser.node.Node#setInfo(int)
342      */
343     public void setInfo(int info)
344     {
345         this.info = info;
346     }
347 
348     /**
349      * @see org.apache.velocity.runtime.parser.node.Node#getInfo()
350      */
351     public int getInfo()
352     {
353         return info;
354     }
355 
356     /**
357      * @see org.apache.velocity.runtime.parser.node.Node#setInvalid()
358      */
359     public void setInvalid()
360     {
361         invalid = true;
362     }
363 
364     /**
365      * @see org.apache.velocity.runtime.parser.node.Node#isInvalid()
366      */
367     public boolean isInvalid()
368     {
369         return invalid;
370     }
371 
372     /**
373      * @see org.apache.velocity.runtime.parser.node.Node#getLine()
374      */
375     public int getLine()
376     {
377         return first.beginLine;
378     }
379 
380     /**
381      * @see org.apache.velocity.runtime.parser.node.Node#getColumn()
382      */
383     public int getColumn()
384     {
385         return first.beginColumn;
386     }
387     
388     public String toString()
389     {
390         StringBuffer tokens = new StringBuffer();
391         
392         for (Token t = getFirstToken(); t != null; )
393         {
394             tokens.append("[").append(t.image).append("]");
395             if (t.next != null)
396             {
397                 if (t.equals(getLastToken()))
398                 {
399                     break;
400                 }
401                 else
402                 {
403                     tokens.append(", ");
404                 }
405             }
406             t = t.next;
407         }
408 
409         return new ToStringBuilder(this)
410             .append("id", getType())
411             .append("info", getInfo())
412             .append("invalid", isInvalid())
413             .append("children", jjtGetNumChildren())
414             .append("tokens", tokens)
415             .toString();
416     }
417 }
418