001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.internal.parsing;
038:
039: import oracle.toplink.essentials.expressions.*;
040: import oracle.toplink.essentials.mappings.DatabaseMapping;
041: import oracle.toplink.essentials.queryframework.ObjectLevelReadQuery;
042:
043: /**
044: * INTERNAL
045: * <p><b>Purpose</b>: This is the superclass for all Nodes.
046: * <p><b>Responsibilities</b>:<ul>
047: * <li> Answer default answers for all method calls
048: * <li> Delegate most responsibilities to the sub-classes
049: * </ul>
050: * @author Jon Driscoll and Joel Lucuik
051: * @since TopLink 4.0
052: */
053: public class Node {
054: private int line;
055: private int column;
056: protected Node left = null;
057: protected Node right = null;
058: private Object type;
059: public boolean shouldGenerateExpression;
060:
061: /**
062: * Return a new Node.
063: */
064: public Node() {
065: super ();
066: }
067:
068: /**
069: * INTERNAL
070: * Apply this node to the passed query
071: */
072: public void applyToQuery(ObjectLevelReadQuery theQuery,
073: GenerationContext context) {
074: }
075:
076: /**
077: * INTERNAL
078: * Add my expression semantics to the parentExpression. Each subclass will add a different expression and
079: * thus will need to override this method
080: */
081: public Expression addToExpression(Expression parentExpression,
082: GenerationContext context) {
083: return parentExpression;
084: }
085:
086: /**
087: * INTERNAL
088: * Get the string representation of this node.
089: * By default return toString()
090: */
091: public String getAsString() {
092: return toString();
093: }
094:
095: /**
096: * INTERNAL
097: * Check the child node for an unqualified field access and if so,
098: * replace it by a qualified field access.
099: */
100: public Node qualifyAttributeAccess(ParseTreeContext context) {
101: if (left != null) {
102: left = left.qualifyAttributeAccess(context);
103: }
104: if (right != null) {
105: right = right.qualifyAttributeAccess(context);
106: }
107: return this ;
108: }
109:
110: /**
111: * INTERNAL
112: * Validate node and calculate its type.
113: */
114: public void validate(ParseTreeContext context) {
115: // Nothing to be validated here, but delegate to the child nodes.
116: if (left != null) {
117: left.validate(context);
118: }
119: if (right != null) {
120: right.validate(context);
121: }
122: }
123:
124: /**
125: * INTERNAL
126: */
127: public void validateParameter(ParseTreeContext context,
128: Object contextType) {
129: // nothing to be done
130: }
131:
132: /**
133: * INTERNAL
134: * Generate an expression for the node. Each subclass will generate a different expression and
135: * thus will need to override this method
136: */
137: public Expression generateExpression(GenerationContext context) {
138: return null;
139: }
140:
141: /**
142: * INTERNAL
143: * Return the left node
144: */
145: public Node getLeft() {
146: return left;
147: }
148:
149: /**
150: * INTERNAL
151: * Return the right node
152: */
153: public Node getRight() {
154: return right;
155: }
156:
157: /**
158: * INTERNAL
159: * Does this node have a left
160: */
161: public boolean hasLeft() {
162: return getLeft() != null;
163: }
164:
165: /**
166: * INTERNAL
167: * Does this node have a right
168: */
169: public boolean hasRight() {
170: return getRight() != null;
171: }
172:
173: /**
174: * INTERNAL
175: * Is this node an Aggregate node
176: */
177: public boolean isAggregateNode() {
178: return false;
179: }
180:
181: /**
182: * INTERNAL
183: * Is this node a Dot node
184: */
185: public boolean isDotNode() {
186: return false;
187: }
188:
189: /**
190: * INTERNAL
191: * Is this a literal node
192: */
193: public boolean isLiteralNode() {
194: return false;
195: }
196:
197: /**
198: * INTERNAL
199: * Is this node a Multiply node
200: */
201: public boolean isMultiplyNode() {
202: return false;
203: }
204:
205: /**
206: * INTERNAL
207: * Is this node a Not node
208: */
209: public boolean isNotNode() {
210: return false;
211: }
212:
213: /**
214: * INTERNAL
215: * Is this a Parameter node
216: */
217: public boolean isParameterNode() {
218: return false;
219: }
220:
221: /**
222: * INTERNAL
223: * Is this node a Divide node
224: */
225: public boolean isDivideNode() {
226: return false;
227: }
228:
229: /**
230: * INTERNAL
231: * Is this node a Plus node
232: */
233: public boolean isPlusNode() {
234: return false;
235: }
236:
237: /**
238: * INTERNAL
239: * Is this node a Minus node
240: */
241: public boolean isMinusNode() {
242: return false;
243: }
244:
245: /**
246: * INTERNAL
247: * Is this node a VariableNode
248: */
249: public boolean isVariableNode() {
250: return false;
251: }
252:
253: /**
254: * INTERNAL
255: * Is this node an AttributeNode
256: */
257: public boolean isAttributeNode() {
258: return false;
259: }
260:
261: /**
262: * INTERNAL
263: * Is this node a CountNode
264: */
265: public boolean isCountNode() {
266: return false;
267: }
268:
269: /**
270: * INTERNAL
271: * Is this node a ConstructorNode
272: */
273: public boolean isConstructorNode() {
274: return false;
275: }
276:
277: /**
278: * INTERNAL
279: * Is this node a SubqueryNode
280: */
281: public boolean isSubqueryNode() {
282: return false;
283: }
284:
285: /**
286: * INTERNAL
287: * Is this an escape node
288: */
289: public boolean isEscape() {
290: return false;// no it is not
291: }
292:
293: /**
294: * resolveAttribute(): Answer the name of the attribute which is represented by the receiver.
295: * Subclasses should override this.
296: */
297: public String resolveAttribute() {
298: return "";
299: }
300:
301: /**
302: * resolveClass: Answer the class associated with the content of this node. Default is to return null.
303: * Subclasses should override this.
304: */
305: public Class resolveClass(GenerationContext context) {
306: return null;
307: }
308:
309: /**
310: * resolveClass: Answer the class associated with the content of this node. Default is to return null.
311: * Subclasses should override this.
312: */
313: public Class resolveClass(GenerationContext context,
314: Class ownerClass) {
315: return null;
316: }
317:
318: /**
319: * resolveMapping: Answer the mapping associated with the contained nodes.
320: * Subclasses should override this.
321: */
322: public DatabaseMapping resolveMapping(GenerationContext context) {
323: return null;
324: }
325:
326: /**
327: * resolveMapping: Answer the mapping associated with the contained nodes. Use the provided
328: * class as the context.
329: * Subclasses should override this.
330: */
331: public DatabaseMapping resolveMapping(GenerationContext context,
332: Class ownerClass) {
333: return null;
334: }
335:
336: /**
337: * INTERNAL
338: * Set the left node to the passed value
339: */
340: public void setLeft(Node newLeft) {
341: left = newLeft;
342: }
343:
344: /**
345: * INTERNAL
346: * Set the right for this node
347: */
348: public void setRight(Node newRight) {
349: right = newRight;
350: }
351:
352: public int getLine() {
353: return line;
354: }
355:
356: public void setLine(int line) {
357: this .line = line;
358: }
359:
360: public int getColumn() {
361: return column;
362: }
363:
364: public void setColumn(int column) {
365: this .column = column;
366: }
367:
368: /**
369: * INTERNAL
370: * Return the type of this node.
371: */
372: public Object getType() {
373: return type;
374: }
375:
376: /**
377: * INTERNAL
378: * Set this node's type.
379: */
380: public void setType(Object type) {
381: this .type = type;
382: }
383:
384: /**
385: * INTERNAL
386: * Returns left.and(right) if both are defined.
387: */
388: public Expression appendExpression(Expression left, Expression right) {
389: Expression expr = null;
390: if (left == null) {
391: expr = right;
392: } else if (right == null) {
393: expr = left;
394: } else {
395: expr = left.and(right);
396: }
397: return expr;
398: }
399:
400: public String toString() {
401: try {
402: return toString(1);
403: } catch (Throwable t) {
404: return t.toString();
405: }
406: }
407:
408: public String toString(int indent) {
409: StringBuffer buffer = new StringBuffer();
410: buffer.append(toStringDisplayName());
411: buffer.append("\r\n");
412: toStringIndent(indent, buffer);
413: if (hasLeft()) {
414: buffer.append("Left: " + getLeft().toString(indent + 1));
415: } else {
416: buffer.append("Left: null");
417: }
418:
419: buffer.append("\r\n");
420: toStringIndent(indent, buffer);
421: if (hasRight()) {
422: buffer.append("Right: " + getRight().toString(indent + 1));
423: } else {
424: buffer.append("Right: null");
425: }
426: return buffer.toString();
427: }
428:
429: public String toStringDisplayName() {
430: return getClass().toString().substring(
431: getClass().toString().lastIndexOf('.') + 1,
432: getClass().toString().length());
433: }
434:
435: public void toStringIndent(int indent, StringBuffer buffer) {
436: for (int i = 0; i < indent; i++) {
437: buffer.append(" ");
438: }
439: ;
440: }
441: }
|