1 23 24 package org.objectweb.medor.expression.lib; 25 26 import org.objectweb.medor.expression.api.BinaryOperator; 27 import org.objectweb.medor.expression.api.Expression; 28 import org.objectweb.medor.expression.api.ExpressionException; 29 import org.objectweb.medor.expression.api.MalformedExpressionException; 30 import org.objectweb.medor.expression.api.Operand; 31 import org.objectweb.medor.expression.api.Operator; 32 import org.objectweb.medor.expression.api.ParameterOperand; 33 import org.objectweb.medor.expression.api.TypingException; 34 import org.objectweb.jorm.type.api.PType; 35 import org.objectweb.jorm.type.api.PTypeSpace; 36 37 public class FirstLocate extends BasicBinaryOperator implements BinaryOperator { 38 39 46 public FirstLocate(Expression substring, Expression instring) { 47 super(substring, instring); 48 } 49 50 public FirstLocate() { 51 } 52 53 public org.objectweb.medor.expression.api.Operand evaluate(ParameterOperand[] pos, Object o) 54 throws ExpressionException { 55 try { 56 result.setValue( 57 evaluate( 58 expressions[0].evaluate(pos, o).getString(), 59 expressions[1].evaluate(pos, o).getString())); 60 } 61 catch (NullPointerException e) { 62 throw new IllegalStateException ("Unevaluable Expression: Not compiled"); 64 } 65 return result; 66 } 67 68 public int evaluate(String op1, String op2) { 69 return op2.indexOf(op1) + 1; 70 } 71 72 public Operand compileExpression() 73 throws ExpressionException, MalformedExpressionException { 74 if ((expressions[0] != null) && (expressions[1] != null)) { 75 expressions[0].compileExpression(); 76 expressions[1].compileExpression(); 77 if (!(expressions[0].getType() == null || expressions[1].getType() == null)) { 78 if ((expressions[0].getType().getTypeCode() == PType.TYPECODE_STRING) && 79 (expressions[1].getType().getTypeCode() == PType.TYPECODE_STRING)) { 80 type = PTypeSpace.INT; 81 result = new BasicVariableOperand(type); 82 verified = true; 83 } 84 else 85 throw new TypingException("Attempt arg type : String, String"); 87 } 88 else 89 throw new MalformedExpressionException("null children value"); 90 } 91 else 92 throw new MalformedExpressionException("null children value"); 94 return result; 95 } 96 97 public String getOperatorString() { 98 return Operator.FIRSTLOCATE; 99 } 100 } 101 | Popular Tags |