| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 14 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 15 |
import net.sf.saxon.om.Item; |
| 16 |
import net.sf.saxon.om.NodeInfo; |
| 17 |
import net.sf.saxon.om.SequenceIterator; |
| 18 |
import net.sf.saxon.pattern.Pattern; |
| 19 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.tree.iter.ListIterator; |
| 22 |
import net.sf.saxon.tree.util.Navigator; |
| 23 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 24 |
import net.sf.saxon.type.ItemType; |
| 25 |
import net.sf.saxon.type.Type; |
| 26 |
import net.sf.saxon.value.Int64Value; |
| 27 |
import net.sf.saxon.value.IntegerValue; |
| 28 |
import net.sf.saxon.value.SequenceType; |
| 29 |
|
| 30 |
import java.util.ArrayList; |
| 31 |
import java.util.List; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class NumberInstruction extends Expression { |
| 41 |
|
| 42 |
public static final int SINGLE = 0; |
| 43 |
public static final int MULTI = 1; |
| 44 |
public static final int ANY = 2; |
| 45 |
public static final int SIMPLE = 3; |
| 46 |
public static final String[] LEVEL_NAMES = new String[]{"single", "multi", "any", "simple"}; |
| 47 |
|
| 48 |
private Operand selectOp; |
| 49 |
|
| 50 |
private int level; |
| 51 |
private Operand countOp; |
| 52 |
private Operand fromOp; |
| 53 |
private boolean hasVariablesInPatterns = false; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public NumberInstruction(Expression select, |
| 65 |
int level, |
| 66 |
Pattern count, |
| 67 |
Pattern from) { |
| 68 |
|
| 69 |
assert select != null; |
| 70 |
|
| 71 |
selectOp = new Operand(this, select, new OperandRole(0, OperandUsage.NAVIGATION, SequenceType.SINGLE_NODE)); |
| 72 |
|
| 73 |
this.level = level; |
| 74 |
if (count != null) { |
| 75 |
countOp = new Operand(this, count, OperandRole.INSPECT); |
| 76 |
} |
| 77 |
if (from != null) { |
| 78 |
fromOp = new Operand(this, from, OperandRole.INSPECT); |
| 79 |
} |
| 80 |
this.hasVariablesInPatterns = Pattern.patternContainsVariable(count) || Pattern.patternContainsVariable(from); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
@Override |
| 91 |
public boolean isInstruction() { |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public Iterable<Operand> operands() { |
| 97 |
return operandSparseList(selectOp, countOp, fromOp); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public int getLevel() { |
| 107 |
return level; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public Pattern getCount() { |
| 117 |
return countOp == null ? null : (Pattern) countOp.getChildExpression(); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public Pattern getFrom() { |
| 127 |
return fromOp == null ? null : (Pattern) fromOp.getChildExpression(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public Expression getSelect() { |
| 137 |
return selectOp.getChildExpression(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public Expression copy(RebindingMap rebindings) { |
| 151 |
NumberInstruction exp = new NumberInstruction(copy(selectOp, rebindings), level, |
| 152 |
copy(getCount(), rebindings), |
| 153 |
copy(getFrom(), rebindings)); |
| 154 |
ExpressionTool.copyLocationInfo(this, exp); |
| 155 |
return exp; |
| 156 |
} |
| 157 |
|
| 158 |
private Expression copy(Operand op, RebindingMap rebindings) { |
| 159 |
return op == null ? null : op.getChildExpression().copy(rebindings); |
| 160 |
} |
| 161 |
|
| 162 |
private Pattern copy(Pattern op, RebindingMap rebindings) { |
| 163 |
return op == null ? null : op.copy(rebindings); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public ItemType getItemType() { |
| 169 |
return BuiltInAtomicType.INTEGER; |
| 170 |
} |
| 171 |
|
| 172 |
public int computeCardinality() { |
| 173 |
switch (level) { |
| 174 |
case SIMPLE: |
| 175 |
case SINGLE: |
| 176 |
case ANY: |
| 177 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 178 |
case MULTI: |
| 179 |
default: |
| 180 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
@Override |
| 193 |
public int getImplementationMethod() { |
| 194 |
return ITERATE_METHOD; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
@Override |
| 213 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 214 |
Expression e = super.optimize(visitor, contextInfo); |
| 215 |
if (e != this) { |
| 216 |
return e; |
| 217 |
} |
| 218 |
if ("EE".equals(getPackageData().getTargetEdition())) { |
| 219 |
e = visitor.obtainOptimizer().optimizeNumberInstruction(this, contextInfo); |
| 220 |
if (e != null) { |
| 221 |
return e; |
| 222 |
} |
| 223 |
} |
| 224 |
return this; |
| 225 |
} |
| 226 |
|
| 227 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 228 |
List<IntegerValue> vec = new ArrayList<>(1); |
| 229 |
NodeInfo source = (NodeInfo) selectOp.getChildExpression().evaluateItem(context); |
| 230 |
|
| 231 |
switch (level) { |
| 232 |
case SIMPLE: { |
| 233 |
long value = Navigator.getNumberSimple(source, context); |
| 234 |
if (value != 0) { |
| 235 |
vec.add(Int64Value.makeIntegerValue(value)); |
| 236 |
} |
| 237 |
break; |
| 238 |
} |
| 239 |
case SINGLE: { |
| 240 |
long value = Navigator.getNumberSingle(source, getCount(), getFrom(), context); |
| 241 |
if (value != 0) { |
| 242 |
vec.add(Int64Value.makeIntegerValue(value)); |
| 243 |
} |
| 244 |
break; |
| 245 |
} |
| 246 |
case ANY: { |
| 247 |
long value = Navigator.getNumberAny(this, source, getCount(), getFrom(), context, hasVariablesInPatterns); |
| 248 |
if (value != 0) { |
| 249 |
vec.add(Int64Value.makeIntegerValue(value)); |
| 250 |
} |
| 251 |
break; |
| 252 |
} |
| 253 |
case MULTI: { |
| 254 |
for (long n : Navigator.getNumberMulti(source, getCount(), getFrom(), context)) { |
| 255 |
vec.add(Int64Value.makeIntegerValue(n)); |
| 256 |
} |
| 257 |
break; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
return new ListIterator<>(vec); |
| 262 |
} |
| 263 |
|
| 264 |
@Override |
| 265 |
public String getExpressionName() { |
| 266 |
return "xsl:number"; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public void export(ExpressionPresenter out) throws XPathException { |
| 275 |
out.startElement("nodeNum", this); |
| 276 |
out.emitAttribute("level", LEVEL_NAMES[level]); |
| 277 |
out.setChildRole("select"); |
| 278 |
selectOp.getChildExpression().export(out); |
| 279 |
if (countOp != null) { |
| 280 |
out.setChildRole("count"); |
| 281 |
getCount().export(out); |
| 282 |
} |
| 283 |
if (fromOp != null) { |
| 284 |
out.setChildRole("from"); |
| 285 |
getFrom().export(out); |
| 286 |
} |
| 287 |
out.endElement(); |
| 288 |
} |
| 289 |
} |
| 290 |
|