KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > parsing > Node


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.parsing;
23
24 import oracle.toplink.essentials.expressions.*;
25 import oracle.toplink.essentials.mappings.DatabaseMapping;
26 import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery;
27
28 /**
29  * INTERNAL
30  * <p><b>Purpose</b>: This is the superclass for all Nodes.
31  * <p><b>Responsibilities</b>:<ul>
32  * <li> Answer default answers for all method calls
33  * <li> Delegate most responsibilities to the sub-classes
34  * </ul>
35  * @author Jon Driscoll and Joel Lucuik
36  * @since TopLink 4.0
37  */

38 public class Node {
39     private int line;
40     private int column;
41     protected Node left = null;
42     protected Node right = null;
43     private Object JavaDoc type;
44     public boolean shouldGenerateExpression;
45
46     /**
47      * Return a new Node.
48      */

49     public Node() {
50         super();
51     }
52
53     /**
54      * INTERNAL
55      * Apply this node to the passed query
56      */

57     public void applyToQuery(ObjectLevelReadQuery theQuery, GenerationContext context) {
58     }
59
60     /**
61      * INTERNAL
62      * Add my expression semantics to the parentExpression. Each subclass will add a different expression and
63      * thus will need to override this method
64      */

65     public Expression addToExpression(Expression parentExpression, GenerationContext context) {
66         return parentExpression;
67     }
68
69     /**
70      * INTERNAL
71      * Get the string representation of this node.
72      * By default return toString()
73      */

74     public String JavaDoc getAsString() {
75         return toString();
76     }
77
78     /**
79      * INTERNAL
80      * Validate node and calculate its type.
81      */

82     public void validate(ParseTreeContext context) {
83         // Nothing to be validated here, but delegate to the child nodes.
84
if (left != null) {
85             left.validate(context);
86         }
87         if (right != null) {
88             right.validate(context);
89         }
90     }
91
92     /**
93      * INTERNAL
94      */

95     public void validateParameter(ParseTreeContext context, Object JavaDoc contextType) {
96         // nothing to be done
97
}
98
99     /**
100      * INTERNAL
101      * Generate an expression for the node. Each subclass will generate a different expression and
102      * thus will need to override this method
103      */

104     public Expression generateExpression(GenerationContext context) {
105         return null;
106     }
107
108     /**
109      * INTERNAL
110      * Return the left node
111      */

112     public Node getLeft() {
113         return left;
114     }
115
116     /**
117      * INTERNAL
118      * Return the right node
119      */

120     public Node getRight() {
121         return right;
122     }
123
124     /**
125      * INTERNAL
126      * Does this node have a left
127      */

128     public boolean hasLeft() {
129         return getLeft() != null;
130     }
131
132     /**
133      * INTERNAL
134      * Does this node have a right
135      */

136     public boolean hasRight() {
137         return getRight() != null;
138     }
139
140     /**
141      * INTERNAL
142      * Is this node an Aggregate node
143      */

144     public boolean isAggregateNode() {
145         return false;
146     }
147
148     /**
149      * INTERNAL
150      * Is this node a Dot node
151      */

152     public boolean isDotNode() {
153         return false;
154     }
155
156     /**
157      * INTERNAL
158      * Is this a literal node
159      */

160     public boolean isLiteralNode() {
161         return false;
162     }
163
164     /**
165      * INTERNAL
166      * Is this node a Multiply node
167      */

168     public boolean isMultiplyNode() {
169         return false;
170     }
171
172     /**
173      * INTERNAL
174      * Is this node a Not node
175      */

176     public boolean isNotNode() {
177         return false;
178     }
179
180     /**
181      * INTERNAL
182      * Is this a Parameter node
183      */

184     public boolean isParameterNode() {
185         return false;
186     }
187
188     /**
189      * INTERNAL
190      * Is this node a Divide node
191      */

192     public boolean isDivideNode() {
193         return false;
194     }
195
196     /**
197      * INTERNAL
198      * Is this node a Plus node
199      */

200     public boolean isPlusNode() {
201         return false;
202     }
203
204     /**
205      * INTERNAL
206      * Is this node a Minus node
207      */

208     public boolean isMinusNode() {
209         return false;
210     }
211
212     /**
213      * INTERNAL
214      * Is this node a VariableNode
215      */

216     public boolean isVariableNode() {
217         return false;
218     }
219
220     /**
221      * INTERNAL
222      * Is this node an AttributeNode
223      */

224     public boolean isAttributeNode() {
225         return false;
226     }
227
228     /**
229      * INTERNAL
230      * Is this node a CountNode
231      */

232     public boolean isCountNode() {
233         return false;
234     }
235
236     /**
237      * INTERNAL
238      * Is this node a ConstructorNode
239      */

240     public boolean isConstructorNode() {
241         return false;
242     }
243
244     /**
245      * INTERNAL
246      * Is this node a SubqueryNode
247      */

248     public boolean isSubqueryNode() {
249         return false;
250     }
251
252     /**
253      * INTERNAL
254      * Is this an escape node
255      */

256     public boolean isEscape() {
257         return false;// no it is not
258
}
259
260     /**
261      * resolveAttribute(): Answer the name of the attribute which is represented by the receiver.
262      * Subclasses should override this.
263      */

264     public String JavaDoc resolveAttribute() {
265         return "";
266     }
267
268     /**
269      * resolveClass: Answer the class associated with the content of this node. Default is to return null.
270      * Subclasses should override this.
271      */

272     public Class JavaDoc resolveClass(GenerationContext context) {
273         return null;
274     }
275     
276     /**
277      * resolveClass: Answer the class associated with the content of this node. Default is to return null.
278      * Subclasses should override this.
279      */

280     public Class JavaDoc resolveClass(GenerationContext context, Class JavaDoc ownerClass) {
281         return null;
282     }
283
284     /**
285      * resolveMapping: Answer the mapping associated with the contained nodes.
286      * Subclasses should override this.
287      */

288     public DatabaseMapping resolveMapping(GenerationContext context) {
289         return null;
290     }
291
292     /**
293      * resolveMapping: Answer the mapping associated with the contained nodes. Use the provided
294      * class as the context.
295      * Subclasses should override this.
296      */

297     public DatabaseMapping resolveMapping(GenerationContext context, Class JavaDoc ownerClass) {
298         return null;
299     }
300
301     /**
302      * INTERNAL
303      * Set the left node to the passed value
304      */

305     public void setLeft(Node newLeft) {
306         left = newLeft;
307     }
308
309     /**
310      * INTERNAL
311      * Set the right for this node
312      */

313     public void setRight(Node newRight) {
314         right = newRight;
315     }
316
317     public int getLine() {
318         return line;
319     }
320     
321     public void setLine(int line) {
322         this.line = line;
323     }
324
325     public int getColumn() {
326         return column;
327     }
328     
329     public void setColumn(int column) {
330         this.column = column;
331     }
332
333     /**
334      * INTERNAL
335      * Return the type of this node.
336      */

337     public Object JavaDoc getType() {
338         return type;
339     }
340
341     /**
342      * INTERNAL
343      * Set this node's type.
344      */

345     public void setType(Object JavaDoc type) {
346         this.type = type;
347     }
348
349     /**
350      * INTERNAL
351      * Returns left.and(right) if both are defined.
352      */

353     public Expression appendExpression(Expression left, Expression right) {
354         Expression expr = null;
355         if (left == null) {
356             expr = right;
357         } else if (right == null) {
358             expr = left;
359         } else {
360             expr = left.and(right);
361         }
362         return expr;
363     }
364     
365     public String JavaDoc toString() {
366         try {
367             return toString(1);
368         } catch (Throwable JavaDoc t) {
369             return t.toString();
370         }
371     }
372
373     public String JavaDoc toString(int indent) {
374         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
375         buffer.append(toStringDisplayName());
376         buffer.append("\r\n");
377         toStringIndent(indent, buffer);
378         if (hasLeft()) {
379             buffer.append("Left: " + getLeft().toString(indent + 1));
380         } else {
381             buffer.append("Left: null");
382         }
383
384         buffer.append("\r\n");
385         toStringIndent(indent, buffer);
386         if (hasRight()) {
387             buffer.append("Right: " + getRight().toString(indent + 1));
388         } else {
389             buffer.append("Right: null");
390         }
391         return buffer.toString();
392     }
393
394     public String JavaDoc toStringDisplayName() {
395         return getClass().toString().substring(getClass().toString().lastIndexOf('.') + 1, getClass().toString().length());
396     }
397
398     public void toStringIndent(int indent, StringBuffer JavaDoc buffer) {
399         for (int i = 0; i < indent; i++) {
400             buffer.append(" ");
401         }
402         ;
403     }
404 }
405
Popular Tags