1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 4 import java.util.*; 5 6 9 10 public abstract class Value extends Expression { 11 12 18 19 public static double stringToNumber(String s) { 20 if (s.indexOf('+')>=0 || 22 s.indexOf('e')>=0 || 23 s.indexOf('E')>=0 ) { 24 return Double.NaN; 25 } 26 try { 27 return new Double (s.trim()).doubleValue(); 28 } catch (NumberFormatException err) { 29 return Double.NaN; 30 } 31 } 32 33 36 37 public static final int BOOLEAN = 1; 38 public static final int NUMBER = 2; 39 public static final int STRING = 3; 40 public static final int NODESET = 4; 41 public static final int OBJECT = 6; 43 public static final int ANY = -1; 44 45 50 51 public Value evaluate(Context context) throws XPathException { 52 return this; 53 } 54 55 59 60 public Expression simplify() { 61 return this; 62 } 63 64 69 70 public int getDependencies() { 71 return 0; 72 } 73 74 78 79 public abstract String asString() throws XPathException; 80 81 85 86 public abstract double asNumber() throws XPathException; 87 88 92 93 public abstract boolean asBoolean() throws XPathException; 94 95 102 103 public boolean equals(Value other) throws XPathException { 104 105 107 if (other instanceof NodeSetValue) 108 return other.equals(this); 109 110 if (this instanceof BooleanValue || other instanceof BooleanValue) 111 return this.asBoolean() == other.asBoolean(); 112 113 if (this instanceof NumericValue || other instanceof NumericValue) 114 return this.asNumber() == other.asNumber(); 115 116 return this.asString().equals(other.asString()); 117 118 } 119 120 125 126 public boolean notEquals(Value other) throws XPathException { 127 128 130 if (other instanceof NodeSetValue) 131 return other.notEquals(this); 132 133 return !equals(other); 134 } 135 136 142 143 public boolean compare(int operator, Value other) throws XPathException { 144 145 if (operator==Tokenizer.EQUALS) return equals(other); 146 if (operator==Tokenizer.NE) return notEquals(other); 147 148 if (other instanceof NodeSetValue) { 149 return other.compare(inverse(operator), this); 150 } 151 152 return numericCompare(operator, this.asNumber(), other.asNumber()); 153 } 154 155 159 160 protected final static int inverse(int operator) { 161 switch(operator) { 162 case Tokenizer.LT: 163 return Tokenizer.GT; 164 case Tokenizer.LE: 165 return Tokenizer.GE; 166 case Tokenizer.GT: 167 return Tokenizer.LT; 168 case Tokenizer.GE: 169 return Tokenizer.LE; 170 default: 171 return operator; 172 } 173 } 174 175 176 protected final boolean numericCompare(int operator, double x, double y) { 177 switch(operator) { 178 case Tokenizer.LT: 179 return x < y; 180 case Tokenizer.LE: 181 return x <= y; 182 case Tokenizer.GT: 183 return x > y; 184 case Tokenizer.GE: 185 return x >= y; 186 default: 187 return false; 188 } 189 } 190 191 199 200 public Expression reduce(int dependencies, Context context) { 201 return this; 202 } 203 204 209 210 public abstract Object convertToJava(Class target) throws XPathException; 211 212 216 217 public abstract int conversionPreference(Class required); 218 219 } 220 221 | Popular Tags |