| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.*; |
| 11 |
import net.sf.saxon.om.SequenceIterator; |
| 12 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.type.*; |
| 15 |
import net.sf.saxon.value.*; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class IntegerRangeTest extends Expression { |
| 27 |
|
| 28 |
private Operand valueOp; |
| 29 |
private Operand minOp; |
| 30 |
private Operand maxOp; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public IntegerRangeTest(Expression value, Expression min, Expression max) { |
| 41 |
valueOp = new Operand(this, value, OperandRole.ATOMIC_SEQUENCE); |
| 42 |
minOp = new Operand(this, min, OperandRole.SINGLE_ATOMIC); |
| 43 |
maxOp = new Operand(this, max, OperandRole.SINGLE_ATOMIC); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public Iterable<Operand> operands() { |
| 48 |
return operandList(valueOp, minOp, maxOp); |
| 49 |
} |
| 50 |
|
| 51 |
public Expression getValue() { |
| 52 |
return valueOp.getChildExpression(); |
| 53 |
} |
| 54 |
|
| 55 |
public void setValue(Expression value) { |
| 56 |
valueOp.setChildExpression(value); |
| 57 |
} |
| 58 |
|
| 59 |
public Expression getMin() { |
| 60 |
return minOp.getChildExpression(); |
| 61 |
} |
| 62 |
|
| 63 |
public void setMin(Expression min) { |
| 64 |
minOp.setChildExpression(min); |
| 65 |
} |
| 66 |
|
| 67 |
public Expression getMax() { |
| 68 |
return maxOp.getChildExpression(); |
| 69 |
} |
| 70 |
|
| 71 |
public void setMax(Expression max) { |
| 72 |
maxOp.setChildExpression(max); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 81 |
|
| 82 |
return this; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 102 |
if (Literal.isEmptySequence(getMin()) || Literal.isEmptySequence(getMax()) || Literal.isEmptySequence(getValue())) { |
| 103 |
return new Literal(BooleanValue.FALSE); |
| 104 |
} |
| 105 |
if (getMin() instanceof Literal && getMax() instanceof Literal && getValue() instanceof Literal) { |
| 106 |
BooleanValue result = evaluateItem(visitor.makeDynamicContext()); |
| 107 |
return new Literal(result); |
| 108 |
} |
| 109 |
return this; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public ItemType getItemType() { |
| 118 |
return BuiltInAtomicType.BOOLEAN; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public int computeCardinality() { |
| 126 |
return StaticProperty.EXACTLY_ONE; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public Expression copy(RebindingMap rebindings) { |
| 138 |
IntegerRangeTest exp = new IntegerRangeTest( |
| 139 |
getValue().copy(rebindings), getMin().copy(rebindings), getMax().copy(rebindings)); |
| 140 |
ExpressionTool.copyLocationInfo(this, exp); |
| 141 |
return exp; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
@Override |
| 153 |
public int getImplementationMethod() { |
| 154 |
return EVALUATE_METHOD; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
@Override |
| 164 |
public boolean equals(Object other) { |
| 165 |
return other instanceof IntegerRangeTest && ((IntegerRangeTest) other).getValue().isEqual(getValue()) |
| 166 |
&& ((IntegerRangeTest) other).getMin().isEqual(getMin()) |
| 167 |
&& ((IntegerRangeTest) other).getMax().isEqual(getMax()); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public int computeHashCode() { |
| 175 |
int h = getValue().hashCode() + 77; |
| 176 |
h ^= getMin().hashCode() ^ getMax().hashCode(); |
| 177 |
return h; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public BooleanValue evaluateItem(XPathContext c) throws XPathException { |
| 185 |
IntegerValue minVal = null; |
| 186 |
IntegerValue maxVal = null; |
| 187 |
StringConverter toDouble = null; |
| 188 |
SequenceIterator iter = getValue().iterate(c); |
| 189 |
AtomicValue atom; |
| 190 |
while ((atom = (AtomicValue) iter.next()) != null) { |
| 191 |
if (minVal == null) { |
| 192 |
minVal = (IntegerValue) getMin().evaluateItem(c); |
| 193 |
if (minVal == null) { |
| 194 |
return BooleanValue.FALSE; |
| 195 |
} |
| 196 |
maxVal = (IntegerValue) getMax().evaluateItem(c); |
| 197 |
if (maxVal == null || maxVal.compareTo(minVal) < 0) { |
| 198 |
return BooleanValue.FALSE; |
| 199 |
} |
| 200 |
} |
| 201 |
NumericValue v; |
| 202 |
if (atom instanceof UntypedAtomicValue) { |
| 203 |
if (toDouble == null) { |
| 204 |
toDouble = BuiltInAtomicType.DOUBLE.getStringConverter(c.getConfiguration().getConversionRules()); |
| 205 |
} |
| 206 |
ConversionResult result = toDouble.convertString(atom.getStringValueCS()); |
| 207 |
if (result instanceof ValidationFailure) { |
| 208 |
XPathException e = new XPathException("Failed to convert untypedAtomic value {" + |
| 209 |
atom.getStringValueCS() + "} to xs:integer", "FORG0001"); |
| 210 |
e.setLocation(getLocation()); |
| 211 |
throw e; |
| 212 |
} else { |
| 213 |
v = (DoubleValue) result.asAtomic(); |
| 214 |
} |
| 215 |
} else if (atom instanceof NumericValue) { |
| 216 |
v = (NumericValue) atom; |
| 217 |
} else { |
| 218 |
XPathException e = new XPathException("Cannot compare value of type " + |
| 219 |
atom.getUType() + " to xs:integer", "XPTY0004"); |
| 220 |
e.setIsTypeError(true); |
| 221 |
e.setLocation(getLocation()); |
| 222 |
throw e; |
| 223 |
} |
| 224 |
if (v.isWholeNumber() && v.compareTo(minVal) >= 0 && v.compareTo(maxVal) <= 0) { |
| 225 |
return BooleanValue.TRUE; |
| 226 |
} |
| 227 |
} |
| 228 |
return BooleanValue.FALSE; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
@Override |
| 239 |
public String getExpressionName() { |
| 240 |
return "intRangeTest"; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public void export(ExpressionPresenter destination) throws XPathException { |
| 249 |
destination.startElement("intRangeTest", this); |
| 250 |
getValue().export(destination); |
| 251 |
getMin().export(destination); |
| 252 |
getMax().export(destination); |
| 253 |
destination.endElement(); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
@Override |
| 265 |
public String toString() { |
| 266 |
return ExpressionTool.parenthesize(getValue()) + " = (" + |
| 267 |
ExpressionTool.parenthesize(getMin()) + " to " + |
| 268 |
ExpressionTool.parenthesize(getMax()) + ")"; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
} |
| 273 |
|