1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 4 5 9 10 class ArithmeticExpression extends BinaryExpression { 11 12 public ArithmeticExpression(){}; 13 14 public ArithmeticExpression(Expression p1, int operator, Expression p2) { 15 super(p1, operator, p2); 16 } 17 18 23 24 public Value evaluate(Context c) throws XPathException { 25 return new NumericValue(evaluateAsNumber(c)); 26 } 27 28 33 34 public double evaluateAsNumber(Context c) throws XPathException { 35 switch(operator) { 36 case Tokenizer.PLUS: 37 return p1.evaluateAsNumber(c) + p2.evaluateAsNumber(c); 38 case Tokenizer.MINUS: 39 return p1.evaluateAsNumber(c) - p2.evaluateAsNumber(c); 40 case Tokenizer.MULT: 41 return p1.evaluateAsNumber(c) * p2.evaluateAsNumber(c); 42 case Tokenizer.DIV: 43 return p1.evaluateAsNumber(c) / p2.evaluateAsNumber(c); 44 case Tokenizer.MOD: 45 return p1.evaluateAsNumber(c) % p2.evaluateAsNumber(c); 46 case Tokenizer.NEGATE: 47 return -p2.evaluateAsNumber(c); 48 49 default: 50 throw new XPathException("Unknown operator in arithmetic expression"); 51 } 52 } 53 54 58 59 public int getDataType() { 60 return Value.NUMBER; 61 } 62 63 71 72 public Expression reduce(int dependencies, Context context) throws XPathException { 73 if ((getDependencies() & dependencies) != 0 ) { 74 Expression e = new ArithmeticExpression( 75 p1.reduce(dependencies, context), 76 operator, 77 p2.reduce(dependencies, context)); 78 e.setStaticContext(getStaticContext()); 79 return e.simplify(); 80 } else { 81 return this; 82 } 83 } 84 85 } 86 87 | Popular Tags |