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 java.util.*;
040:
041: import oracle.toplink.essentials.expressions.*;
042:
043: /**
044: * INTERNAL
045: * <p><b>Purpose</b>: Represent an OR
046: * <p><b>Responsibilities</b>:<ul>
047: * <li> Generate the correct expression for an OR
048: * </ul>
049: * @author Jon Driscoll and Joel Lucuik
050: * @since TopLink 4.0
051: */
052: public class OrNode extends LogicalOperatorNode {
053:
054: private Set leftOuterScopeVariables = null;
055: private Set rightOuterScopeVariables = null;
056:
057: /**
058: * Return a new OrNode.
059: */
060: public OrNode() {
061: super ();
062: }
063:
064: /**
065: * INTERNAL
066: * Validate node and calculate its type.
067: */
068: public void validate(ParseTreeContext context) {
069: Set saved = context.getOuterScopeVariables();
070: if (left != null) {
071: context.resetOuterScopeVariables();
072: left.validate(context);
073: leftOuterScopeVariables = context.getOuterScopeVariables();
074: }
075: if (right != null) {
076: context.resetOuterScopeVariables();
077: right.validate(context);
078: rightOuterScopeVariables = context.getOuterScopeVariables();
079: }
080: context.resetOuterScopeVariables(saved);
081: if ((left != null) && (right != null)) {
082: left.validateParameter(context, right.getType());
083: right.validateParameter(context, left.getType());
084: }
085:
086: TypeHelper typeHelper = context.getTypeHelper();
087: setType(typeHelper.getBooleanType());
088: }
089:
090: /**
091: * INTERNAL
092: * Return a TopLink expression by 'OR'ing the expressions from the left and right nodes
093: */
094: public Expression generateExpression(GenerationContext context) {
095: // Get the left expression
096: Expression leftExpr = getLeft().generateExpression(context);
097: leftExpr = appendOuterScopeVariableJoins(leftExpr,
098: leftOuterScopeVariables, context);
099:
100: Expression rightExpr = getRight().generateExpression(context);
101: rightExpr = appendOuterScopeVariableJoins(rightExpr,
102: rightOuterScopeVariables, context);
103:
104: // Or it with whatever the right expression is
105: return leftExpr.or(rightExpr);
106: }
107:
108: /**
109: * INTERNAL
110: */
111: private Expression appendOuterScopeVariableJoins(Expression expr,
112: Set outerScopeVariables, GenerationContext context) {
113: if ((outerScopeVariables == null)
114: || outerScopeVariables.isEmpty()) {
115: // no outer scope variables => nothing to be done
116: return expr;
117: }
118: Expression joins = context.joinVariables(outerScopeVariables);
119: return appendExpression(expr, joins);
120: }
121:
122: }
|