001: /*
002: * Copyright 2002-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.jexl.parser;
018:
019: import org.apache.commons.jexl.JexlContext;
020:
021: /**
022: * A Useful implementation of {@link Node}. Mostly autogenerated by javacc
023: *
024: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
025: * @version $Id: SimpleNode.java 398328 2006-04-30 12:46:59Z dion $
026: */
027: public class SimpleNode implements Node {
028: /** parent node. */
029: protected Node parent;
030:
031: /** children of this node. */
032: protected Node[] children;
033:
034: /** id of the node. */
035: protected int id;
036:
037: /** parser that created the node. */
038: protected Parser parser;
039:
040: /**
041: * Create the node given an id.
042: *
043: * @param i node id.
044: */
045: public SimpleNode(int i) {
046: id = i;
047: }
048:
049: /**
050: * Create a node with the given parser and id.
051: *
052: * @param p a parser.
053: * @param i node id.
054: */
055: public SimpleNode(Parser p, int i) {
056: this (i);
057: parser = p;
058: }
059:
060: /**
061: * Start of the node.
062: */
063: public void jjtOpen() {
064: }
065:
066: /**
067: * End of the node.
068: */
069: public void jjtClose() {
070: }
071:
072: /** {@inheritDoc} */
073: public void jjtSetParent(Node n) {
074: parent = n;
075: }
076:
077: /** {@inheritDoc} */
078: public Node jjtGetParent() {
079: return parent;
080: }
081:
082: /** {@inheritDoc} */
083: public void jjtAddChild(Node n, int i) {
084: if (children == null) {
085: children = new Node[i + 1];
086: } else if (i >= children.length) {
087: Node[] c = new Node[i + 1];
088: System.arraycopy(children, 0, c, 0, children.length);
089: children = c;
090: }
091:
092: children[i] = n;
093: }
094:
095: /** {@inheritDoc} */
096: public Node jjtGetChild(int i) {
097: return children[i];
098: }
099:
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)
207: throws Exception {
208: return null;
209: }
210:
211: /**
212: * Used to let a node calcuate it's value..
213: * @param o the object to calculate with.
214: * @param ctx the context to retrieve values from.
215: * @throws Exception when calculating the value fails.
216: * @return the result of the calculation.
217: */
218: public Object execute(Object o, JexlContext ctx) throws Exception {
219: return null;
220: }
221: }
|