| 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.GroundedValue; |
| 12 |
import net.sf.saxon.om.Item; |
| 13 |
import net.sf.saxon.om.SequenceIterator; |
| 14 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 17 |
import net.sf.saxon.type.ItemType; |
| 18 |
import net.sf.saxon.type.UType; |
| 19 |
import net.sf.saxon.value.*; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class RangeExpression extends BinaryExpression { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public RangeExpression(Expression start, Expression end) { |
| 40 |
super(start, Token.TO, end); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 49 |
getLhs().typeCheck(visitor, contextInfo); |
| 50 |
getRhs().typeCheck(visitor, contextInfo); |
| 51 |
|
| 52 |
boolean backCompat = visitor.getStaticContext().isInBackwardsCompatibleMode(); |
| 53 |
TypeChecker tc = visitor.getConfiguration().getTypeChecker(backCompat); |
| 54 |
RoleDiagnostic role0 = new RoleDiagnostic(RoleDiagnostic.BINARY_EXPR, "to", 0); |
| 55 |
setLhsExpression(tc.staticTypeCheck( |
| 56 |
getLhsExpression(), SequenceType.OPTIONAL_INTEGER, role0, visitor)); |
| 57 |
|
| 58 |
RoleDiagnostic role1 = new RoleDiagnostic(RoleDiagnostic.BINARY_EXPR, "to", 1); |
| 59 |
setRhsExpression(tc.staticTypeCheck( |
| 60 |
getRhsExpression(), SequenceType.OPTIONAL_INTEGER, role1, visitor)); |
| 61 |
|
| 62 |
return makeConstantRange(); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 82 |
getLhs().optimize(visitor, contextInfo); |
| 83 |
getRhs().optimize(visitor, contextInfo); |
| 84 |
return makeConstantRange(); |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
private Expression makeConstantRange() throws XPathException { |
| 89 |
if (getLhsExpression() instanceof Literal && getRhsExpression() instanceof Literal) { |
| 90 |
GroundedValue v0 = ((Literal) getLhsExpression()).getValue(); |
| 91 |
GroundedValue v1 = ((Literal) getRhsExpression()).getValue(); |
| 92 |
if (v0 instanceof Int64Value && v1 instanceof Int64Value) { |
| 93 |
long i0 = ((Int64Value) v0).longValue(); |
| 94 |
long i1 = ((Int64Value) v1).longValue(); |
| 95 |
Literal result; |
| 96 |
if (i0 > i1) { |
| 97 |
result = Literal.makeEmptySequence(); |
| 98 |
} else if (i0 == i1) { |
| 99 |
result = Literal.makeLiteral(Int64Value.makeIntegerValue(i0), this); |
| 100 |
} else { |
| 101 |
if (i1 - i0 > Integer.MAX_VALUE) { |
| 102 |
throw new XPathException("Maximum length of sequence in Saxon is " + Integer.MAX_VALUE, "XPDY0130"); |
| 103 |
} |
| 104 |
result = Literal.makeLiteral(new IntegerRange(i0, i1), this); |
| 105 |
} |
| 106 |
ExpressionTool.copyLocationInfo(this, result); |
| 107 |
return result; |
| 108 |
} |
| 109 |
} |
| 110 |
return this; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public ItemType getItemType() { |
| 120 |
return BuiltInAtomicType.INTEGER; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
@Override |
| 131 |
public UType getStaticUType(UType contextItemType) { |
| 132 |
return UType.DECIMAL; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public int computeCardinality() { |
| 140 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
@Override |
| 158 |
public IntegerValue[] getIntegerBounds() { |
| 159 |
IntegerValue[] start = getLhsExpression().getIntegerBounds(); |
| 160 |
IntegerValue[] end = getLhsExpression().getIntegerBounds(); |
| 161 |
if (start == null || end == null) { |
| 162 |
return null; |
| 163 |
} else { |
| 164 |
|
| 165 |
return new IntegerValue[]{start[0], end[1]}; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public Expression copy(RebindingMap rebindings) { |
| 178 |
RangeExpression exp = new RangeExpression(getLhsExpression().copy(rebindings), getRhsExpression().copy(rebindings)); |
| 179 |
ExpressionTool.copyLocationInfo(this, exp); |
| 180 |
return exp; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
@Override |
| 192 |
public int getImplementationMethod() { |
| 193 |
return ITERATE_METHOD; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
@Override |
| 204 |
public String getExpressionName() { |
| 205 |
return "range"; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
@Override |
| 215 |
public void export(ExpressionPresenter out) throws XPathException { |
| 216 |
out.startElement("to", this); |
| 217 |
getLhsExpression().export(out); |
| 218 |
getRhsExpression().export(out); |
| 219 |
out.endElement(); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 228 |
IntegerValue av1 = (IntegerValue) getLhsExpression().evaluateItem(context); |
| 229 |
IntegerValue av2 = (IntegerValue) getRhsExpression().evaluateItem(context); |
| 230 |
return RangeIterator.makeRangeIterator(av1, av2); |
| 231 |
} |
| 232 |
|
| 233 |
} |
| 234 |
|