View Javadoc

1   /*
2    * Copyright 2002-2006 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.commons.jexl.parser;
18  
19  import org.apache.commons.jexl.JexlContext;
20  
21  /***
22   * A Useful implementation of {@link Node}. Mostly autogenerated by javacc
23   * 
24   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
25   * @version $Id: SimpleNode.java 398328 2006-04-30 12:46:59Z dion $
26   */
27  public class SimpleNode implements Node {
28      /*** parent node. */
29      protected Node parent;
30  
31      /*** children of this node. */
32      protected Node[] children;
33  
34      /*** id of the node. */
35      protected int id;
36  
37      /*** parser that created the node. */
38      protected Parser parser;
39  
40      /***
41       * Create the node given an id.
42       * 
43       * @param i node id.
44       */
45      public SimpleNode(int i) {
46          id = i;
47      }
48  
49      /***
50       * Create a node with the given parser and id.
51       * 
52       * @param p a parser.
53       * @param i node id.
54       */
55      public SimpleNode(Parser p, int i) {
56          this(i);
57          parser = p;
58      }
59  
60      /***
61       * Start of the node.
62       */
63      public void jjtOpen() {
64      }
65  
66      /***
67       * End of the node.
68       */
69      public void jjtClose() {
70      }
71  
72      /*** {@inheritDoc} */
73      public void jjtSetParent(Node n) {
74          parent = n;
75      }
76  
77      /*** {@inheritDoc} */
78      public Node jjtGetParent() {
79          return parent;
80      }
81  
82      /*** {@inheritDoc} */
83      public void jjtAddChild(Node n, int i) {
84          if (children == null) {
85              children = new Node[i + 1];
86          } else if (i >= children.length) {
87              Node[] c = new Node[i + 1];
88              System.arraycopy(children, 0, c, 0, children.length);
89              children = c;
90          }
91  
92          children[i] = n;
93      }
94  
95      /*** {@inheritDoc} */
96      public Node jjtGetChild(int i) {
97          return children[i];
98      }
99  
100     /*** {@inheritDoc} */
101     public int jjtGetNumChildren() {
102         return (children == null) ? 0 : children.length;
103     }
104 
105     /***
106      * Accept the visitor.
107      * 
108      * @param visitor a {@link ParserVisitor}.
109      * @param data data to be passed along to the visitor.
110      * @return the value from visiting.
111      * @see ParserVisitor#visit
112      */
113     public Object jjtAccept(ParserVisitor visitor, Object data) {
114         return visitor.visit(this, data);
115     }
116 
117     /***
118      * Visit all children.
119      * 
120      * @param visitor a {@link ParserVisitor}.
121      * @param data data to be passed along to the visitor.
122      * @return the value from visiting.
123      * @see ParserVisitor#visit
124      */
125     public Object childrenAccept(ParserVisitor visitor, Object data) {
126         if (children != null) {
127             for (int i = 0; i < children.length; ++i) {
128                 children[i].jjtAccept(visitor, data);
129             }
130         }
131         return data;
132     }
133 
134     /***
135      * Gets a string representation of the node.
136      * @return the node name.
137      */
138     public String toString() {
139         return ParserTreeConstants.jjtNodeName[id];
140     }
141 
142     /***
143      * Used during dumping to output the node with a prefix.
144      * @param prefix text to prefix {@link #toString()}
145      * @return text.
146      */
147     public String toString(String prefix) {
148         return prefix + toString();
149     }
150 
151     /***
152      * Dump the node and all children.
153      * @param prefix text to prefix the node output.
154      */
155     public void dump(String prefix) {
156         System.out.println(toString(prefix));
157 
158         if (children != null) {
159             for (int i = 0; i < children.length; ++i) {
160                 SimpleNode n = (SimpleNode) children[i];
161 
162                 if (n != null) {
163                     n.dump(prefix + " ");
164                 }
165             }
166         }
167     }
168 
169     /***
170      * basic interpret - just invoke interpret on all children.
171      * @param pc the {@link JexlContext context} to interpret against.
172      * @return true if interpretation worked.
173      * @throws Exception on any error.
174      */
175     public boolean interpret(JexlContext pc) throws Exception {
176         for (int i = 0; i < jjtGetNumChildren(); i++) {
177             SimpleNode node = (SimpleNode) jjtGetChild(i);
178             if (!node.interpret(pc)) {
179                 return false;
180             }
181         }
182 
183         return true;
184     }
185 
186     /***
187      * Gets the value of this node.
188      * 
189      * @param context the context to retrieve values from.
190      * @return the value of the node.
191      * @throws Exception when evaluating the operands fails.
192      */
193     public Object value(JexlContext context) throws Exception {
194         return null;
195     }
196 
197     /***
198      * Sets the value for the node - again, only makes sense for some nodes but
199      * lazyness tempts me to put it here. Keeps things simple.
200      * 
201      * @param context the context to retrieve values from.
202      * @param value the value.
203      * @return the result.
204      * @throws Exception when evaluating the operands fails.
205      */
206     public Object setValue(JexlContext context, Object value) throws Exception {
207         return null;
208     }
209 
210     /***
211      * Used to let a node calcuate it's value..
212      * @param o the object to calculate with.
213      * @param ctx the context to retrieve values from.
214      * @throws Exception when calculating the value fails.
215      * @return the result of the calculation.
216      */
217     public Object execute(Object o, JexlContext ctx) throws Exception {
218         return null;
219     }
220 }