KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > scriptengine > core > nodes > AndOrNode


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.scriptengine.core.nodes;
31
32 import java.util.HashMap JavaDoc;
33
34 import com.genimen.djeneric.language.Messages;
35 import com.genimen.djeneric.tools.scriptengine.core.DjScriptParserEngine;
36 import com.genimen.djeneric.tools.scriptengine.core.ParseException;
37 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode;
38 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptCompileTimeScope;
39 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException;
40 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionTimeScope;
41
42 public class AndOrNode extends SimpleNode implements BooleanExpression
43 {
44   private String JavaDoc operator;
45
46   public AndOrNode(int i)
47   {
48     super(i);
49   }
50
51   public AndOrNode(DjScriptParserEngine p, int i)
52   {
53     super(p, i);
54   }
55
56   public String JavaDoc getName()
57   {
58     return toString();
59   }
60
61   public String JavaDoc toString()
62   {
63     return getOperator();
64   }
65
66   public void setOperator(String JavaDoc operator)
67   {
68     this.operator = operator;
69   }
70
71   public String JavaDoc getOperator()
72   {
73     return operator;
74   }
75
76   public Object JavaDoc getValue(DjScriptExecutionTimeScope context) throws DjScriptExecutionException
77   {
78     return new Boolean JavaDoc(isTrue(context));
79   }
80
81   public boolean isTrue(DjScriptExecutionTimeScope context) throws DjScriptExecutionException
82   {
83     if (!(getChild(0) instanceof BooleanExpression))
84     {
85       throw new DjScriptExecutionException(Messages.getString("AndOrNode.ExpressionNotBoolean"), getChild(0));
86     }
87     if (!(getChild(1) instanceof BooleanExpression))
88     {
89       throw new DjScriptExecutionException(Messages.getString("AndOrNode.ExpressionNotBoolean"), getChild(1));
90     }
91
92     BooleanExpression left = (BooleanExpression) getChild(0);
93     BooleanExpression right = (BooleanExpression) getChild(1);
94
95     if (getOperator().equals("&&"))
96     {
97       return left.isTrue(context) && right.isTrue(context);
98     }
99     else if (getOperator().equals("||"))
100     {
101       return left.isTrue(context) || right.isTrue(context);
102     }
103     throw new DjScriptExecutionException("Unsupported operator: " + getOperator(), this);
104   }
105
106   private boolean isAnd()
107   {
108     return getOperator().equals("&&") || getOperator().equals("and");
109   }
110
111   private boolean isOr()
112   {
113     return getOperator().equals("||") || getOperator().equals("or");
114   }
115
116   public void translateOql(DjScriptExecutionTimeScope ctxt, StringBuffer JavaDoc result, HashMap JavaDoc parameters)
117       throws ParseException
118   {
119     result.append("(");
120
121     getChild(0).translateOql(ctxt, result, parameters);
122
123     if (isAnd()) result.append("\nand ");
124     else if (isOr()) result.append("\nor ");
125     else throw new ParseException(Messages.getString("AndOrNode.UnsupportedOperator") + getOperator());
126
127     getChild(1).translateOql(ctxt, result, parameters);
128
129     result.append(")");
130   }
131
132   private void assertBoolean(DjScriptCompileTimeScope ctxt, SimpleNode node) throws DjScriptExecutionException
133   {
134     if (node instanceof BooleanExpression) return;
135     if (node instanceof ValueExpression
136         && ((ValueExpression) node).getValidatedTypeName(ctxt).equals(Boolean JavaDoc.class.getName())) return;
137     if (node instanceof ValueExpression)
138     {
139       ValueExpression ve = (ValueExpression) node;
140       if (ve.getValidatedTypeName(ctxt).equals(Boolean JavaDoc.class)) return;
141     }
142
143     // It might be a function/condition; check this function to be a defined condition.
144
// (Assume that functions in a boolean context are in fact conditions
145
if (node instanceof PropertyOrFunctionNode)
146     {
147       PropertyOrFunctionNode pfNode = (PropertyOrFunctionNode) node;
148       String JavaDoc functionName = pfNode.getFunctionName();
149       // TODO check the return type of the function
150
// for now just return as if it is a boolean
151
return;
152     }
153
154     throw new DjScriptExecutionException("Expression is not boolean", node);
155   }
156
157   public String JavaDoc getValidatedTypeName(DjScriptCompileTimeScope context) throws DjScriptExecutionException
158   {
159     assertBoolean(context, getChild(0));
160     assertBoolean(context, getChild(1));
161     return Boolean JavaDoc.class.getName();
162   }
163
164   public void validateScript(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException
165   {
166     getValidatedTypeName(ctxt);
167   }
168
169 }
Popular Tags