1 package net.sf.saxon.expr; 2 import net.sf.saxon.om.Item; 3 import net.sf.saxon.om.NamePool; 4 import net.sf.saxon.om.SequenceIterator; 5 import net.sf.saxon.trans.XPathException; 6 import net.sf.saxon.type.ItemType; 7 import net.sf.saxon.type.Type; 8 import net.sf.saxon.value.BooleanValue; 9 import net.sf.saxon.value.NumericValue; 10 import net.sf.saxon.value.IntegerValue; 11 12 import java.io.PrintStream ; 13 import java.util.Iterator ; 14 15 20 21 public final class PositionRange extends ComputedExpression { 22 23 private Expression minPosition; 24 private Expression maxPosition; 26 29 30 public PositionRange(Expression min, Expression max) { 31 minPosition = min; 32 maxPosition = max; 33 adoptChildExpression(min); 34 adoptChildExpression(max); 35 } 36 37 40 41 public PositionRange(int min, int max) { 42 minPosition = new IntegerValue(min); 43 if (max == Integer.MAX_VALUE) { 44 maxPosition = null; 45 } else { 46 maxPosition = new IntegerValue(max); 47 } 48 } 49 50 54 55 public Expression simplify(StaticContext env) throws XPathException { 56 minPosition = minPosition.simplify(env); 57 if (maxPosition != null) { 58 maxPosition = maxPosition.simplify(env); 59 } 60 return this; 61 } 62 63 66 67 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 68 minPosition = minPosition.typeCheck(env, contextItemType); 69 if (maxPosition != null) { 70 maxPosition = maxPosition.typeCheck(env, contextItemType); 71 } 72 return this; 73 } 74 75 92 93 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 94 minPosition = minPosition.optimize(opt, env, contextItemType); 95 if (maxPosition != null) { 96 maxPosition = maxPosition.optimize(opt, env, contextItemType); 97 } 98 return this; 99 } 100 101 105 106 public int computeSpecialProperties() { 107 int p = super.computeSpecialProperties(); 108 return p | StaticProperty.NON_CREATIVE; 109 } 110 111 114 115 public Item evaluateItem(XPathContext c) throws XPathException { 116 int p = c.getContextPosition(); 117 if (maxPosition == null) { 118 NumericValue min = (NumericValue)minPosition.evaluateItem(c); 119 return BooleanValue.get(p >= min.longValue()); 120 } else { 121 NumericValue min = (NumericValue)minPosition.evaluateItem(c); 122 NumericValue max = (NumericValue)maxPosition.evaluateItem(c); 123 return BooleanValue.get(p >= min.longValue() && p <= max.longValue()); 124 } 125 } 126 127 130 131 public SequenceIterator makePositionIterator(SequenceIterator base, XPathContext c) throws XPathException { 132 int low, high; 133 NumericValue min = (NumericValue)minPosition.evaluateItem(c); 134 low = (int)min.longValue(); 135 if (maxPosition == null) { 136 high = Integer.MAX_VALUE; 137 } else { 138 NumericValue max = (NumericValue)maxPosition.evaluateItem(c); 139 high = (int)max.longValue(); 140 } 141 return PositionIterator.make(base, low, high); 142 } 143 144 148 149 public ItemType getItemType() { 150 return Type.BOOLEAN_TYPE; 151 } 152 153 156 157 public int computeCardinality() { 158 return StaticProperty.EXACTLY_ONE; 159 } 160 161 164 165 public int getIntrinsicDependencies() { 166 return StaticProperty.DEPENDS_ON_POSITION; 167 } 168 169 176 177 public Iterator iterateSubExpressions() { 178 if (maxPosition == null) { 179 return new MonoIterator(minPosition); 180 } else { 181 return new PairIterator(minPosition, maxPosition); 182 } 183 } 184 185 188 189 public boolean isFirstPositionOnly() { 190 try { 191 return (minPosition instanceof NumericValue && ((NumericValue)minPosition).longValue() == 1) && 192 (maxPosition instanceof NumericValue && ((NumericValue)maxPosition).longValue() == 1); 193 } catch (XPathException e) { 194 return false; 195 } 196 } 197 198 202 203 public boolean hasFocusDependentRange() { 204 return ((minPosition.getDependencies() & StaticProperty.DEPENDS_ON_FOCUS) != 0) || 205 (maxPosition != null && (maxPosition.getDependencies() & StaticProperty.DEPENDS_ON_FOCUS) != 0); 206 } 207 208 211 212 public boolean matchesAtMostOneItem() { 213 return maxPosition != null && minPosition.equals(maxPosition) && !hasFocusDependentRange(); 214 } 215 216 220 221 public TailExpression makeTailExpression(Expression start) { 222 if (maxPosition == null && minPosition instanceof IntegerValue) { 223 return new TailExpression(start, (int)((IntegerValue)minPosition).longValue()); 224 } else { 225 return null; 226 } 227 228 } 229 230 233 234 public void display(int level, NamePool pool, PrintStream out) { 235 out.println(ExpressionTool.indent(level) + "positionRange"); 236 minPosition.display(level+1, pool, out); 237 maxPosition.display(level+1, pool, out); 238 } 239 } 240 241 | Popular Tags |