KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
25
26 import oracle.toplink.essentials.expressions.*;
27
28 /**
29  * INTERNAL
30  * <p><b>Purpose</b>: Represent an OR
31  * <p><b>Responsibilities</b>:<ul>
32  * <li> Generate the correct expression for an OR
33  * </ul>
34  * @author Jon Driscoll and Joel Lucuik
35  * @since TopLink 4.0
36  */

37 public class OrNode extends LogicalOperatorNode {
38
39     private Set leftOuterScopeVariables = null;
40     private Set rightOuterScopeVariables = null;
41
42     /**
43      * Return a new OrNode.
44      */

45     public OrNode() {
46         super();
47     }
48
49     /**
50      * INTERNAL
51      * Validate node and calculate its type.
52      */

53     public void validate(ParseTreeContext context) {
54         Set saved = context.getOuterScopeVariables();
55         if (left != null) {
56             context.resetOuterScopeVariables();
57             left.validate(context);
58             leftOuterScopeVariables = context.getOuterScopeVariables();
59         }
60         if (right != null) {
61             context.resetOuterScopeVariables();
62             right.validate(context);
63             rightOuterScopeVariables = context.getOuterScopeVariables();
64         }
65         context.resetOuterScopeVariables(saved);
66         if ((left != null) && (right != null)) {
67             left.validateParameter(context, right.getType());
68             right.validateParameter(context, left.getType());
69         }
70         
71         TypeHelper typeHelper = context.getTypeHelper();
72         setType(typeHelper.getBooleanType());
73     }
74
75     /**
76      * INTERNAL
77      * Return a TopLink expression by 'OR'ing the expressions from the left and right nodes
78      */

79     public Expression generateExpression(GenerationContext context) {
80         // Get the left expression
81
Expression leftExpr = getLeft().generateExpression(context);
82         leftExpr = appendOuterScopeVariableJoins(
83             leftExpr, leftOuterScopeVariables, context);
84
85         Expression rightExpr = getRight().generateExpression(context);
86         rightExpr = appendOuterScopeVariableJoins(
87             rightExpr, rightOuterScopeVariables, context);
88         
89         // Or it with whatever the right expression is
90
return leftExpr.or(rightExpr);
91     }
92
93     /**
94      * INTERNAL
95      */

96     private Expression appendOuterScopeVariableJoins(
97         Expression expr, Set outerScopeVariables, GenerationContext context) {
98         if ((outerScopeVariables == null) || outerScopeVariables.isEmpty()) {
99             // no outer scope variables => nothing to be done
100
return expr;
101         }
102         Expression joins = context.joinVariables(outerScopeVariables);
103         return appendExpression(expr, joins);
104     }
105     
106 }
107
Popular Tags