| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 11 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 13 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 14 |
import net.sf.saxon.om.AxisInfo; |
| 15 |
import net.sf.saxon.om.NodeInfo; |
| 16 |
import net.sf.saxon.om.SequenceIterator; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 19 |
import net.sf.saxon.type.ErrorType; |
| 20 |
import net.sf.saxon.type.ItemType; |
| 21 |
import net.sf.saxon.value.SequenceType; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public final class SimpleStepExpression extends SlashExpression { |
| 33 |
|
| 34 |
public SimpleStepExpression(Expression start, Expression step) { |
| 35 |
super(start, step); |
| 36 |
if (!(step instanceof AxisExpression)) { |
| 37 |
throw new IllegalArgumentException(); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
private static OperandRole STEP_ROLE = new OperandRole(OperandRole.USES_NEW_FOCUS | OperandRole.HIGHER_ORDER, OperandUsage.TRANSMISSION, SequenceType.ANY_SEQUENCE); |
| 42 |
|
| 43 |
@Override |
| 44 |
protected OperandRole getOperandRole(int arg) { |
| 45 |
return arg == 0 ? OperandRole.FOCUS_CONTROLLING_SELECT : STEP_ROLE; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
@Override |
| 50 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 51 |
|
| 52 |
getLhs().typeCheck(visitor, contextInfo); |
| 53 |
|
| 54 |
ItemType selectType = getStart().getItemType(); |
| 55 |
if (selectType == ErrorType.getInstance()) { |
| 56 |
return Literal.makeEmptySequence(); |
| 57 |
} |
| 58 |
|
| 59 |
ContextItemStaticInfo cit = visitor.getConfiguration().makeContextItemStaticInfo(selectType, false); |
| 60 |
cit.setContextSettingExpression(getStart()); |
| 61 |
|
| 62 |
getRhs().typeCheck(visitor, cit); |
| 63 |
|
| 64 |
if (!(getStep() instanceof AxisExpression)) { |
| 65 |
if (Literal.isEmptySequence(getStep())) { |
| 66 |
return getStep(); |
| 67 |
} |
| 68 |
SlashExpression se = new SlashExpression(getStart(), getStep()); |
| 69 |
ExpressionTool.copyLocationInfo(this, se); |
| 70 |
return se; |
| 71 |
} |
| 72 |
if (getStart() instanceof ContextItemExpression && AxisInfo.isForwards[((AxisExpression) getStep()).getAxis()]) { |
| 73 |
return getStep(); |
| 74 |
} |
| 75 |
return this; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
@Override |
| 80 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 81 |
return this; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
@Override |
| 92 |
public SimpleStepExpression copy(RebindingMap rebindings) { |
| 93 |
SimpleStepExpression exp = new SimpleStepExpression(getStart().copy(rebindings), getStep().copy(rebindings)); |
| 94 |
ExpressionTool.copyLocationInfo(this, exp); |
| 95 |
return exp; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@Override |
| 107 |
public int getImplementationMethod() { |
| 108 |
return ITERATE_METHOD; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@Override |
| 118 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 119 |
NodeInfo origin = null; |
| 120 |
try { |
| 121 |
origin = (NodeInfo) getStart().evaluateItem(context); |
| 122 |
} catch (XPathException e) { |
| 123 |
if ("XPDY0002".equals(e.getErrorCodeLocalPart()) && !e.hasBeenReported()) { |
| 124 |
throw new XPathException("The context item for axis step " |
| 125 |
+ toShortString() + " is absent", "XPDY0002", getLocation()); |
| 126 |
} else { |
| 127 |
throw e; |
| 128 |
} |
| 129 |
} |
| 130 |
if (origin == null) { |
| 131 |
return EmptyIterator.getInstance(); |
| 132 |
} |
| 133 |
return ((AxisExpression) getStep()).iterate(origin); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public String getExpressionName() { |
| 138 |
return "simpleStep"; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
} |
| 143 |
|