KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > exp > parser > SimpleNode


1 /* Generated By:JJTree: Do not edit this line. SimpleNode.java */
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 package org.apache.cayenne.exp.parser;
23
24 import java.io.PrintWriter JavaDoc;
25
26 import org.apache.cayenne.ObjectId;
27 import org.apache.cayenne.Persistent;
28 import org.apache.cayenne.exp.Expression;
29 import org.apache.cayenne.exp.ExpressionException;
30 import org.apache.cayenne.util.Util;
31
32 /**
33  * Superclass of AST* expressions that implements Node interface defined by JavaCC
34  * framework.
35  * <p>
36  * Some parts of the parser are based on OGNL parser, copyright (c) 2002, Drew Davidson
37  * and Luke Blanshard.
38  * </p>
39  *
40  * @since 1.1
41  */

42 public abstract class SimpleNode extends Expression implements Node {
43
44     protected Node parent;
45     protected Node[] children;
46     protected int id;
47
48     /**
49      * Utility method that encodes an object that is not an expression Node to String.
50      */

51     protected static void encodeScalarAsString(PrintWriter JavaDoc pw, Object JavaDoc scalar) {
52         boolean quote = scalar instanceof String JavaDoc;
53
54         if (quote) {
55             pw.print('\"');
56         }
57
58         // encode only ObjectId for Persistent, ensure that the order of keys is
59
// predictable....
60
if (scalar instanceof Persistent) {
61             ObjectId id = ((Persistent) scalar).getObjectId();
62             if(id != null) {
63                 scalar = id;
64             }
65         }
66
67         encodeAsEscapedString(pw, String.valueOf(scalar));
68         if (quote) {
69             pw.print('\"');
70         }
71     }
72
73     /**
74      * Utility method that prints a string to the provided PrintWriter, escaping special
75      * characters.
76      */

77     protected static void encodeAsEscapedString(PrintWriter JavaDoc pw, String JavaDoc source) {
78         int len = source.length();
79         for (int i = 0; i < len; i++) {
80             char c = source.charAt(i);
81
82             switch (c) {
83                 case '\n':
84                     pw.print("\\n");
85                     continue;
86                 case '\r':
87                     pw.print("\\r");
88                     continue;
89                 case '\t':
90                     pw.print("\\t");
91                     continue;
92                 case '\b':
93                     pw.print("\\b");
94                     continue;
95                 case '\f':
96                     pw.print("\\f");
97                     continue;
98                 case '\\':
99                     pw.print("\\\\");
100                     continue;
101                 case '\'':
102                     pw.print("\\'");
103                     continue;
104                 case '\"':
105                     pw.print("\\\"");
106                     continue;
107                 default:
108                     pw.print(c);
109             }
110         }
111     }
112
113     protected SimpleNode(int i) {
114         id = i;
115     }
116
117     protected abstract String JavaDoc getExpressionOperator(int index);
118
119     protected boolean pruneNodeForPrunedChild(Object JavaDoc prunedChild) {
120         return true;
121     }
122
123     /**
124      * Implemented for backwards compatibility with exp package.
125      */

126     public String JavaDoc expName() {
127         return ExpressionParserTreeConstants.jjtNodeName[id];
128     }
129
130     /**
131      * Flattens the tree under this node by eliminating any children that are of the same
132      * class as this node and copying their children to this node.
133      */

134     protected void flattenTree() {
135         boolean shouldFlatten = false;
136         int newSize = 0;
137
138         for (int i = 0; i < children.length; i++) {
139             if (children[i].getClass() == getClass()) {
140                 shouldFlatten = true;
141                 newSize += children[i].jjtGetNumChildren();
142             }
143             else {
144                 newSize++;
145             }
146         }
147
148         if (shouldFlatten) {
149             Node[] newChildren = new Node[newSize];
150             int j = 0;
151
152             for (int i = 0; i < children.length; ++i) {
153                 Node c = children[i];
154                 if (c.getClass() == getClass()) {
155                     for (int k = 0; k < c.jjtGetNumChildren(); ++k)
156                         newChildren[j++] = c.jjtGetChild(k);
157                 }
158                 else {
159                     newChildren[j++] = c;
160                 }
161             }
162
163             if (j != newSize) {
164                 throw new ExpressionException("Assertion error: " + j + " != " + newSize);
165             }
166
167             this.children = newChildren;
168         }
169     }
170
171     public void encodeAsString(PrintWriter JavaDoc pw) {
172         if (parent != null) {
173             pw.print("(");
174         }
175
176         if ((children != null) && (children.length > 0)) {
177             for (int i = 0; i < children.length; ++i) {
178                 if (i > 0) {
179                     pw.print(' ');
180                     pw.print(getExpressionOperator(i));
181                     pw.print(' ');
182                 }
183
184                 ((SimpleNode) children[i]).encodeAsString(pw);
185             }
186         }
187
188         if (parent != null) {
189             pw.print(')');
190         }
191     }
192
193     public Object JavaDoc getOperand(int index) {
194         Node child = jjtGetChild(index);
195
196         // unwrap ASTScalar nodes - this is likely a temporary thing to keep it compatible
197
// with QualifierTranslator. In the future we might want to keep scalar nodes
198
// for the purpose of expression evaluation.
199
return unwrapChild(child);
200     }
201
202     protected Node wrapChild(Object JavaDoc child) {
203         return (child instanceof Node || child == null) ? (Node) child : new ASTScalar(
204                 child);
205     }
206
207     protected Object JavaDoc unwrapChild(Node child) {
208         return (child instanceof ASTScalar) ? ((ASTScalar) child).getValue() : child;
209     }
210
211     public int getOperandCount() {
212         return jjtGetNumChildren();
213     }
214
215     public void setOperand(int index, Object JavaDoc value) {
216         Node node = (value == null || value instanceof Node)
217                 ? (Node) value
218                 : new ASTScalar(value);
219         jjtAddChild(node, index);
220
221         // set the parent, as jjtAddChild doesn't do it...
222
if (node != null) {
223             node.jjtSetParent(this);
224         }
225     }
226
227     public void jjtOpen() {
228
229     }
230
231     public void jjtClose() {
232
233     }
234
235     public void jjtSetParent(Node n) {
236         parent = n;
237     }
238
239     public Node jjtGetParent() {
240         return parent;
241     }
242
243     public void jjtAddChild(Node n, int i) {
244         if (children == null) {
245             children = new Node[i + 1];
246         }
247         else if (i >= children.length) {
248             Node c[] = new Node[i + 1];
249             System.arraycopy(children, 0, c, 0, children.length);
250             children = c;
251         }
252         children[i] = n;
253     }
254
255     public Node jjtGetChild(int i) {
256         return children[i];
257     }
258
259     public final int jjtGetNumChildren() {
260         return (children == null) ? 0 : children.length;
261     }
262
263     /**
264      * Evaluates itself with object, pushing result on the stack.
265      */

266     protected abstract Object JavaDoc evaluateNode(Object JavaDoc o) throws Exception JavaDoc;
267
268     protected Object JavaDoc evaluateChild(int index, Object JavaDoc o) throws Exception JavaDoc {
269         return ((SimpleNode) jjtGetChild(index)).evaluate(o);
270     }
271
272     public Expression notExp() {
273         return new ASTNot(this);
274     }
275
276     public Object JavaDoc evaluate(Object JavaDoc o) {
277         // wrap in try/catch to provide unified exception processing
278
try {
279             return evaluateNode(o);
280         }
281         catch (Throwable JavaDoc th) {
282             String JavaDoc string = this.toString();
283             throw new ExpressionException(
284                     "Error evaluating expression '" + string + "'",
285                     string,
286                     Util.unwindException(th));
287         }
288     }
289 }
290
Popular Tags