1 30 package com.genimen.djeneric.tools.scriptengine.core.nodes; 31 32 import java.util.HashMap ; 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 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 getName() 57 { 58 return toString(); 59 } 60 61 public String toString() 62 { 63 return getOperator(); 64 } 65 66 public void setOperator(String operator) 67 { 68 this.operator = operator; 69 } 70 71 public String getOperator() 72 { 73 return operator; 74 } 75 76 public Object getValue(DjScriptExecutionTimeScope context) throws DjScriptExecutionException 77 { 78 return new Boolean (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 result, HashMap 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 .class.getName())) return; 137 if (node instanceof ValueExpression) 138 { 139 ValueExpression ve = (ValueExpression) node; 140 if (ve.getValidatedTypeName(ctxt).equals(Boolean .class)) return; 141 } 142 143 if (node instanceof PropertyOrFunctionNode) 146 { 147 PropertyOrFunctionNode pfNode = (PropertyOrFunctionNode) node; 148 String functionName = pfNode.getFunctionName(); 149 return; 152 } 153 154 throw new DjScriptExecutionException("Expression is not boolean", node); 155 } 156 157 public String getValidatedTypeName(DjScriptCompileTimeScope context) throws DjScriptExecutionException 158 { 159 assertBoolean(context, getChild(0)); 160 assertBoolean(context, getChild(1)); 161 return Boolean .class.getName(); 162 } 163 164 public void validateScript(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException 165 { 166 getValidatedTypeName(ctxt); 167 } 168 169 } | Popular Tags |