1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 4 5 9 10 public class SingletonComparison extends Expression { 11 12 SingletonExpression node; 13 int operator; 14 Value value; 15 16 public SingletonComparison(SingletonExpression p1, int op, Value p2) { 17 node = p1; 18 operator = op; 19 value = p2; 20 } 21 22 26 27 public Expression simplify() throws XPathException { 28 return this; 29 } 30 31 36 37 public Value evaluate(Context c) throws XPathException { 38 return new BooleanValue(evaluateAsBoolean(c)); 39 } 40 41 46 47 public boolean evaluateAsBoolean(Context c) throws XPathException { 48 boolean exists = node.evaluateAsBoolean(c); 49 if (exists) { 50 if (value instanceof StringValue || 51 value instanceof FragmentValue || 52 value instanceof TextFragmentValue ) { 53 switch (operator) { 54 case Tokenizer.EQUALS: 55 return node.evaluateAsString(c).equals(value.asString()); 56 case Tokenizer.NE: 57 return !node.evaluateAsString(c).equals(value.asString()); 58 case Tokenizer.LT: 59 return node.evaluateAsNumber(c) < value.asNumber(); 60 case Tokenizer.LE: 61 return node.evaluateAsNumber(c) <= value.asNumber(); 62 case Tokenizer.GT: 63 return node.evaluateAsNumber(c) > value.asNumber(); 64 case Tokenizer.GE: 65 return node.evaluateAsNumber(c) >= value.asNumber(); 66 default: 67 throw new XPathException("Bad operator in singleton comparison"); 68 } 69 } else if (value instanceof NumericValue) { 70 switch(operator) { 71 case Tokenizer.EQUALS: 72 return node.evaluateAsNumber(c) == value.asNumber(); 73 case Tokenizer.NE: 74 return node.evaluateAsNumber(c) != value.asNumber(); 75 case Tokenizer.LT: 76 return node.evaluateAsNumber(c) < value.asNumber(); 77 case Tokenizer.LE: 78 return node.evaluateAsNumber(c) <= value.asNumber(); 79 case Tokenizer.GT: 80 return node.evaluateAsNumber(c) > value.asNumber(); 81 case Tokenizer.GE: 82 return node.evaluateAsNumber(c) >= value.asNumber(); 83 default: 84 throw new XPathException("Bad operator in singleton comparison"); 85 } 86 } else { 87 throw new XPathException("Unrecognized type in singleton comparison"); 88 } 89 90 } else { 91 return false; 92 } 93 } 94 95 99 100 public int getDataType() { 101 return Value.BOOLEAN; 102 } 103 104 109 110 public int getDependencies() { 111 return node.getDependencies(); 112 } 113 114 122 123 public Expression reduce(int dependencies, Context context) throws XPathException { 124 if ((node.getDependencies() & dependencies) != 0 ) { 125 Expression e = node.reduce(dependencies, context); 126 if (e instanceof SingletonExpression) { 127 e = new SingletonComparison( 128 (SingletonExpression)e, 129 operator, 130 value); 131 e.setStaticContext(getStaticContext()); 132 return e.simplify(); 133 } else if (e instanceof NodeSetValue) { 134 return new BooleanValue(((NodeSetValue)e).compare(operator, value)); 135 } else { 136 throw new XPathException("Failed to reduce SingletonComparison: returned " + e.getClass()); 137 } 138 } else { 139 return this; 140 } 141 } 142 145 146 public void display(int level) { 147 System.err.println(indent(level) + "SingletonComparison " + Tokenizer.tokens[operator]); 148 node.display(level+1); 149 value.display(level+1); 150 } 151 } 152 153 | Popular Tags |