1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 import com.icl.saxon.functions.BooleanFn; 4 5 6 9 10 class BooleanExpression extends BinaryExpression { 11 12 public BooleanExpression(){}; 13 14 public BooleanExpression(Expression p1, int operator, Expression p2) { 15 super(p1, operator, p2); 16 } 17 18 public Expression simplify() throws XPathException { 19 p1 = p1.simplify(); 20 p2 = p2.simplify(); 21 if ((p1 instanceof Value) && (p2 instanceof Value)) { 22 return evaluate(null); 23 } 24 25 28 30 if (p1 instanceof PositionRange && p2 instanceof PositionRange) { 31 PositionRange pr1 = (PositionRange)p1; 32 PositionRange pr2 = (PositionRange)p2; 33 if (pr1.getMaxPosition()==Integer.MAX_VALUE && pr2.getMinPosition()==1) { 34 return new PositionRange(pr1.getMinPosition(), pr2.getMaxPosition()); 35 } 36 if (pr2.getMaxPosition()==Integer.MAX_VALUE && pr1.getMinPosition()==1) { 37 return new PositionRange(pr2.getMinPosition(), pr1.getMaxPosition()); 38 } 39 } 40 return this; 41 } 42 43 public Value evaluate(Context c) throws XPathException { 44 return new BooleanValue(evaluateAsBoolean(c)); 45 } 46 47 public boolean evaluateAsBoolean(Context c) throws XPathException { 48 switch(operator) { 49 case Tokenizer.AND: 50 return p1.evaluateAsBoolean(c) && p2.evaluateAsBoolean(c); 51 case Tokenizer.OR: 52 return p1.evaluateAsBoolean(c) || p2.evaluateAsBoolean(c); 53 default: 54 throw new XPathException("Unknown operator in boolean expression"); 55 } 56 } 57 58 62 63 public int getDataType() { 64 return Value.BOOLEAN; 65 } 66 67 75 76 public Expression reduce(int dependencies, Context context) throws XPathException { 77 if ((getDependencies() & dependencies) != 0 ) { 78 Expression e = new BooleanExpression( 79 p1.reduce(dependencies, context), 80 operator, 81 p2.reduce(dependencies, context)); 82 e.setStaticContext(getStaticContext()); 83 return e.simplify(); 84 } else { 85 return this; 86 } 87 } 88 89 } 90 91 | Popular Tags |