1 30 package com.genimen.djeneric.tools.scriptengine.core.nodes; 31 32 import java.math.BigDecimal ; 33 import java.util.HashMap ; 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 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 getName() 61 { 62 return toString(); 63 } 64 65 public String toString() 66 { 67 return getOperator() + " (op)"; 68 } 69 70 public void setOperator(String operator) 71 { 72 this.operator = operator; 73 } 74 75 public String getOperator() 76 { 77 return operator; 78 } 79 80 public Object getValue(DjScriptExecutionTimeScope context) throws DjScriptExecutionException 81 { 82 ValueExpression left = (ValueExpression) getChild(0); 83 ValueExpression right = (ValueExpression) getChild(1); 84 85 if (getOperator().equals(".")) 87 { 88 Object 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 x) 99 { 100 throw new DjScriptExecutionException(x.getMessage(), this); 101 } 102 } 103 104 Object leftVal = left.getValue(context); 105 Object rightVal = right.getValue(context); 106 107 if (leftVal == null || rightVal == null) 109 { 110 return null; 111 } 112 113 if ((leftVal instanceof String ) || (rightVal instanceof String )) 115 { 116 if (getOperator().equals("+")) return leftVal.toString() + rightVal.toString(); 117 throw new DjScriptExecutionException(Messages.getString("OperatorNode.CanNotApplyString", getOperator()), this); 118 } 119 120 if ((leftVal instanceof BigDecimal ) || (rightVal instanceof BigDecimal )) 122 { 123 float l = toBigDecimal(leftVal).floatValue(); 124 float r = toBigDecimal(rightVal).floatValue(); 125 126 if (getOperator().equals("+")) return new Float (l + r); 127 if (getOperator().equals("-")) return new Float (l - r); 128 if (getOperator().equals("*")) return new Float (l * r); 129 if (getOperator().equals("/")) return new Float (l / r); 130 throw new DjScriptExecutionException(Messages.getString("OperatorNode.CanNotApplyFloats", getOperator()), this); 131 } 132 if ((leftVal instanceof Float ) || (rightVal instanceof Float )) 134 { 135 float l = toBigDecimal(leftVal).floatValue(); 136 float r = toBigDecimal(rightVal).floatValue(); 137 138 if (getOperator().equals("+")) return new Float (l + r); 139 if (getOperator().equals("-")) return new Float (l - r); 140 if (getOperator().equals("*")) return new Float (l * r); 141 if (getOperator().equals("/")) return new Float (l / r); 142 throw new DjScriptExecutionException(Messages.getString("OperatorNode.CanNotApplyFloats", getOperator()), this); 143 } 144 if ((leftVal instanceof Integer ) || (rightVal instanceof Integer )) 146 { 147 int l = toLong(leftVal).intValue(); 148 int r = toLong(rightVal).intValue(); 149 150 if (getOperator().equals("+")) return new Integer (l + r); 151 if (getOperator().equals("-")) return new Integer (l - r); 152 if (getOperator().equals("*")) return new Integer (l * r); 153 if (getOperator().equals("/")) return new Integer (l / r); 154 if (getOperator().equals("%")) return new Integer (l % r); 155 throw new DjScriptExecutionException(Messages.getString("OperatorNode.CanNotApplyInts", getOperator()), this); 156 } 157 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 result, HashMap 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 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 .class.getName()) 211 || right.getValidatedTypeName(context).equals(String .class.getName())) return String .class.getName(); 212 213 if (left.getValidatedTypeName(context).equals(BigDecimal .class.getName()) 214 || right.getValidatedTypeName(context).equals(BigDecimal .class.getName())) return Float .class.getName(); 215 216 if (left.getValidatedTypeName(context).equals(Float .class.getName()) 217 || right.getValidatedTypeName(context).equals(Float .class.getName())) return Float .class.getName(); 218 219 if (left.getValidatedTypeName(context).equals(Long .class.getName()) 220 || right.getValidatedTypeName(context).equals(Long .class.getName())) return Long .class.getName(); 221 222 if (left.getValidatedTypeName(context).equals(Integer .class.getName()) 223 || right.getValidatedTypeName(context).equals(Integer .class.getName())) return Integer .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 leftType = left.getValidatedTypeName(context); 229 String rightType = right.getValidatedTypeName(context); 230 if (leftType.equals(rightType)) return leftType; 231 232 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 |