KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigDecimal JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 import com.genimen.djeneric.language.Messages;
36 import com.genimen.djeneric.repository.DjExtent;
37 import com.genimen.djeneric.repository.DjList;
38 import com.genimen.djeneric.repository.exceptions.DjenericException;
39 import com.genimen.djeneric.tools.scriptengine.core.DjScriptParserEngine;
40 import com.genimen.djeneric.tools.scriptengine.core.ParseException;
41 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode;
42 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptCompileTimeScope;
43 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException;
44 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionTimeScope;
45
46 public class OperatorNode extends SimpleNode implements ValueExpression
47 {
48   private String JavaDoc operator;
49
50   public OperatorNode(int i)
51   {
52     super(i);
53   }
54
55   public OperatorNode(DjScriptParserEngine p, int i)
56   {
57     super(p, i);
58   }
59
60   public String JavaDoc getName()
61   {
62     return toString();
63   }
64
65   public String JavaDoc toString()
66   {
67     return getOperator() + " (op)";
68   }
69
70   public void setOperator(String JavaDoc operator)
71   {
72     this.operator = operator;
73   }
74
75   public String JavaDoc getOperator()
76   {
77     return operator;
78   }
79
80   public Object JavaDoc getValue(DjScriptExecutionTimeScope context) throws DjScriptExecutionException
81   {
82     ValueExpression left = (ValueExpression) getChild(0);
83     ValueExpression right = (ValueExpression) getChild(1);
84
85     // Concatenation of two functions?
86
if (getOperator().equals("."))
87     {
88       Object JavaDoc leftVal = left.getValue(context);
89       if (!(right instanceof PropertyOrFunctionNode))
90       {
91         throw new DjScriptExecutionException(Messages.getString("OperatorNode.NeedFunction"), this);
92       }
93       PropertyOrFunctionNode pof = (PropertyOrFunctionNode) right;
94       try
95       {
96         return pof.evalFunction(leftVal, context);
97       }
98       catch (Exception JavaDoc x)
99       {
100         throw new DjScriptExecutionException(x.getMessage(), this);
101       }
102     }
103
104     Object JavaDoc leftVal = left.getValue(context);
105     Object JavaDoc rightVal = right.getValue(context);
106
107     // First handle null stuff
108
if (leftVal == null || rightVal == null)
109     {
110       return null;
111     }
112
113     // String operation?
114
if ((leftVal instanceof String JavaDoc) || (rightVal instanceof String JavaDoc))
115     {
116       if (getOperator().equals("+")) return leftVal.toString() + rightVal.toString();
117       throw new DjScriptExecutionException(Messages.getString("OperatorNode.CanNotApplyString", getOperator()), this);
118     }
119
120     // Float op?
121
if ((leftVal instanceof BigDecimal JavaDoc) || (rightVal instanceof BigDecimal JavaDoc))
122     {
123       float l = toBigDecimal(leftVal).floatValue();
124       float r = toBigDecimal(rightVal).floatValue();
125
126       if (getOperator().equals("+")) return new Float JavaDoc(l + r);
127       if (getOperator().equals("-")) return new Float JavaDoc(l - r);
128       if (getOperator().equals("*")) return new Float JavaDoc(l * r);
129       if (getOperator().equals("/")) return new Float JavaDoc(l / r);
130       throw new DjScriptExecutionException(Messages.getString("OperatorNode.CanNotApplyFloats", getOperator()), this);
131     }
132     // Float op?
133
if ((leftVal instanceof Float JavaDoc) || (rightVal instanceof Float JavaDoc))
134     {
135       float l = toBigDecimal(leftVal).floatValue();
136       float r = toBigDecimal(rightVal).floatValue();
137
138       if (getOperator().equals("+")) return new Float JavaDoc(l + r);
139       if (getOperator().equals("-")) return new Float JavaDoc(l - r);
140       if (getOperator().equals("*")) return new Float JavaDoc(l * r);
141       if (getOperator().equals("/")) return new Float JavaDoc(l / r);
142       throw new DjScriptExecutionException(Messages.getString("OperatorNode.CanNotApplyFloats", getOperator()), this);
143     }
144     // Integer op?
145
if ((leftVal instanceof Integer JavaDoc) || (rightVal instanceof Integer JavaDoc))
146     {
147       int l = toLong(leftVal).intValue();
148       int r = toLong(rightVal).intValue();
149
150       if (getOperator().equals("+")) return new Integer JavaDoc(l + r);
151       if (getOperator().equals("-")) return new Integer JavaDoc(l - r);
152       if (getOperator().equals("*")) return new Integer JavaDoc(l * r);
153       if (getOperator().equals("/")) return new Integer JavaDoc(l / r);
154       if (getOperator().equals("%")) return new Integer JavaDoc(l % r);
155       throw new DjScriptExecutionException(Messages.getString("OperatorNode.CanNotApplyInts", getOperator()), this);
156     }
157     // List op?
158
if ((leftVal instanceof DjList) && (rightVal instanceof DjList))
159     {
160       DjList l = (DjList) leftVal;
161       DjList r = (DjList) rightVal;
162
163       if (getOperator().equals("+")) return addLists(l, r);
164
165       if (getOperator().equals("-")) return subtractLists(l, r);
166       throw new DjScriptExecutionException(Messages.getString("OperatorNode.CanNotApplySets", getOperator()), this);
167     }
168     throw new DjScriptExecutionException(Messages.getString("OperatorNode.CanNotApply", classOnly(leftVal),
169                                                             getOperator(), classOnly(rightVal)), this);
170   }
171
172   DjList addLists(DjList l, DjList r)
173   {
174     DjList lst = new DjList();
175     lst.setStoredTypeName(l.getStoredType());
176     lst.addAll(l);
177     for (int i = 0; i < r.size(); i++)
178     {
179       if (!lst.contains(r.get(i))) lst.add(r.get(i));
180     }
181     return lst;
182   }
183
184   DjList subtractLists(DjList l, DjList r)
185   {
186     DjList lst = new DjList();
187     lst.setStoredTypeName(l.getStoredType());
188     for (int i = 0; i < l.size(); i++)
189     {
190       if (!r.contains(l.get(i))) lst.add(l.get(i));
191     }
192     return lst;
193   }
194
195   public void translateOql(DjScriptExecutionTimeScope ctxt, StringBuffer JavaDoc result, HashMap JavaDoc parameters)
196       throws ParseException
197   {
198     result.append("(");
199     getChild(0).translateOql(ctxt, result, parameters);
200     result.append(operator);
201     getChild(1).translateOql(ctxt, result, parameters);
202     result.append(")");
203   }
204
205   public String JavaDoc getValidatedTypeName(DjScriptCompileTimeScope context) throws DjScriptExecutionException
206   {
207     ValueExpression left = (ValueExpression) getChild(0);
208     ValueExpression right = (ValueExpression) getChild(1);
209
210     if (left.getValidatedTypeName(context).equals(String JavaDoc.class.getName())
211         || right.getValidatedTypeName(context).equals(String JavaDoc.class.getName())) return String JavaDoc.class.getName();
212
213     if (left.getValidatedTypeName(context).equals(BigDecimal JavaDoc.class.getName())
214         || right.getValidatedTypeName(context).equals(BigDecimal JavaDoc.class.getName())) return Float JavaDoc.class.getName();
215
216     if (left.getValidatedTypeName(context).equals(Float JavaDoc.class.getName())
217         || right.getValidatedTypeName(context).equals(Float JavaDoc.class.getName())) return Float JavaDoc.class.getName();
218
219     if (left.getValidatedTypeName(context).equals(Long JavaDoc.class.getName())
220         || right.getValidatedTypeName(context).equals(Long JavaDoc.class.getName())) return Long JavaDoc.class.getName();
221
222     if (left.getValidatedTypeName(context).equals(Integer JavaDoc.class.getName())
223         || right.getValidatedTypeName(context).equals(Integer JavaDoc.class.getName())) return Integer JavaDoc.class.getName();
224
225     if (left.getValidatedTypeName(context).equals(DjList.class.getName())
226         && right.getValidatedTypeName(context).equals(DjList.class.getName())) return DjList.class.getName();
227
228     String JavaDoc leftType = left.getValidatedTypeName(context);
229     String JavaDoc rightType = right.getValidatedTypeName(context);
230     if (leftType.equals(rightType)) return leftType;
231
232     // check for it to be an existing type:
233
try
234     {
235       DjExtent le = context.getPersistenceManager().getExtentByObjectType(leftType);
236       DjExtent re = context.getPersistenceManager().getExtentByObjectType(rightType);
237       if (le.isInstanceof(re)) return re.getQualifiedObjectType();
238       if (re.isInstanceof(le)) return le.getQualifiedObjectType();
239
240       throw new DjScriptExecutionException("Kan operator " + getOperator() + " niet uitvoeren op "
241                                            + le.getQualifiedObjectType() + " en " + le.getQualifiedObjectType(), this);
242     }
243     catch (DjenericException onde)
244     {
245       throw new DjScriptExecutionException(onde, this);
246     }
247   }
248
249   public void validateScript(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException
250   {
251     getValidatedTypeName(ctxt);
252     super.validateScript(ctxt);
253   }
254 }
Popular Tags