1 23 24 package org.objectweb.medor.expression.lib; 25 26 import org.objectweb.medor.expression.api.Expression; 27 import org.objectweb.medor.expression.api.ExpressionException; 28 import org.objectweb.medor.expression.api.MalformedExpressionException; 29 import org.objectweb.medor.expression.api.Operand; 30 import org.objectweb.medor.expression.api.Operator; 31 import org.objectweb.medor.expression.api.ParameterOperand; 32 import org.objectweb.medor.expression.api.TypingException; 33 import org.objectweb.jorm.type.api.PType; 34 import org.objectweb.jorm.type.api.PTypeSpace; 35 36 40 public class IndexedLocate extends BasicOperator { 41 42 58 public IndexedLocate(Expression inputString, Expression substring, 59 Expression fromIndex) { 60 super(PTypeSpace.INT, new Expression[]{inputString, substring, fromIndex}); 61 } 62 63 public IndexedLocate() { 64 super(PTypeSpace.INT); 65 } 66 67 public Operand evaluate(ParameterOperand[] pos, Object o) 68 throws ExpressionException { 69 try { 70 result.setValue( 71 evaluate( 72 getExpression(0).evaluate(pos, o).getString(), 73 getExpression(1).evaluate(pos, o).getString(), 74 getExpression(2).evaluate(pos, o).getInt())); 75 } 76 catch (NullPointerException e) { 77 throw new ExpressionException("Unevaluable Expression: May not be compiled"); 79 } 80 return result; 81 } 82 83 public Operand compileExpression() 84 throws ExpressionException, MalformedExpressionException { 85 if ((getExpression(0) != null) 86 && (getExpression(1) != null) 87 && (getExpression(2) != null)) { 88 getExpression(0).compileExpression(); 89 getExpression(1).compileExpression(); 90 getExpression(2).compileExpression(); 91 if ((getExpression(0).getType().getTypeCode() == 92 PType.TYPECODE_STRING) && 93 (getExpression(1).getType().getTypeCode() == 94 PType.TYPECODE_STRING) && 95 (getExpression(2).getType().isa(PTypeSpace.INT))) { 96 result = new BasicVariableOperand(type); 97 verified = true; 98 } 99 else 100 throw new TypingException("Attempted argument types: String, String, int"); 102 } 103 else 104 throw new MalformedExpressionException("Null children value"); 106 return result; 107 } 108 109 public int evaluate(String inputString, String subString, int fromIndex) { 110 return inputString.indexOf(subString, fromIndex - 1); 111 } 112 113 public String getOperatorString() { 114 return Operator.INDEXEDLOCATE; 115 } 116 } | Popular Tags |