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