| 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.GroundedValue; |
| 15 |
import net.sf.saxon.om.Item; |
| 16 |
import net.sf.saxon.om.MemoSequence; |
| 17 |
import net.sf.saxon.om.SequenceIterator; |
| 18 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.value.NumericValue; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class SubscriptExpression extends SingleItemFilter { |
| 27 |
|
| 28 |
private Operand subscriptOp; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public SubscriptExpression(Expression base, Expression subscript) { |
| 38 |
super(base); |
| 39 |
subscriptOp = new Operand(this, subscript, OperandRole.SINGLE_ATOMIC); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
public Expression getSubscript() { |
| 44 |
return subscriptOp.getChildExpression(); |
| 45 |
} |
| 46 |
|
| 47 |
public void setSubscript(Expression subscript) { |
| 48 |
subscriptOp.setChildExpression(subscript); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
@Override |
| 56 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 57 |
getOperand().typeCheck(visitor, contextInfo); |
| 58 |
return this; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 63 |
getOperand().optimize(visitor, contextInfo); |
| 64 |
if (Literal.isConstantOne(getSubscript())) { |
| 65 |
return FirstItemExpression.makeFirstItemExpression(getBaseExpression()); |
| 66 |
} |
| 67 |
return this; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public Expression copy(RebindingMap rebindings) { |
| 79 |
SubscriptExpression exp = new SubscriptExpression(getBaseExpression().copy(rebindings), getSubscript().copy(rebindings)); |
| 80 |
ExpressionTool.copyLocationInfo(this, exp); |
| 81 |
return exp; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public Iterable<Operand> operands() { |
| 86 |
return operandList(getOperand(), subscriptOp); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public Expression getSubscriptExpression() { |
| 96 |
return getSubscript(); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@Override |
| 108 |
public int getImplementationMethod() { |
| 109 |
return EVALUATE_METHOD; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public boolean equals(Object other) { |
| 120 |
return other instanceof SubscriptExpression && |
| 121 |
getBaseExpression().isEqual(((SubscriptExpression) other).getBaseExpression()) && |
| 122 |
getSubscript().isEqual(((SubscriptExpression) other).getSubscript()); |
| 123 |
} |
| 124 |
|
| 125 |
public int computeHashCode() { |
| 126 |
return getBaseExpression().hashCode() ^ getSubscript().hashCode(); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public int computeCardinality() { |
| 135 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
@Override |
| 146 |
public String getStreamerName() { |
| 147 |
return "SubscriptExpression"; |
| 148 |
} |
| 149 |
|
| 150 |
public Item evaluateItem(XPathContext context) throws XPathException { |
| 151 |
NumericValue index = (NumericValue) getSubscript().evaluateItem(context); |
| 152 |
if (index == null) { |
| 153 |
return null; |
| 154 |
} |
| 155 |
int intindex = index.asSubscript(); |
| 156 |
if (intindex != -1) { |
| 157 |
Item item; |
| 158 |
SequenceIterator<?> iter = getBaseExpression().iterate(context); |
| 159 |
if (intindex == 1) { |
| 160 |
item = iter.next(); |
| 161 |
} else if (iter instanceof MemoSequence.ProgressiveIterator) { |
| 162 |
MemoSequence mem = ((MemoSequence.ProgressiveIterator)iter).getMemoSequence(); |
| 163 |
item = mem.itemAt(intindex - 1); |
| 164 |
} else if ((iter.getProperties() & SequenceIterator.GROUNDED) != 0) { |
| 165 |
GroundedValue value = iter.materialize(); |
| 166 |
item = value.itemAt(intindex - 1); |
| 167 |
} else { |
| 168 |
SequenceIterator tail = TailIterator.make(iter, intindex); |
| 169 |
item = tail.next(); |
| 170 |
tail.close(); |
| 171 |
} |
| 172 |
return item; |
| 173 |
} else { |
| 174 |
|
| 175 |
return null; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
@Override |
| 187 |
public String getExpressionName() { |
| 188 |
return "subscript"; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public void export(ExpressionPresenter destination) throws XPathException { |
| 199 |
destination.startElement("subscript", this); |
| 200 |
getBaseExpression().export(destination); |
| 201 |
getSubscript().export(destination); |
| 202 |
destination.endElement(); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
@Override |
| 214 |
public String toString() { |
| 215 |
return ExpressionTool.parenthesize(getBaseExpression()) + "[" + getSubscript() + "]"; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
@Override |
| 227 |
public String toShortString() { |
| 228 |
return ExpressionTool.parenthesize(getBaseExpression()) + |
| 229 |
"[" + getSubscript().toShortString() + "]"; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
} |
| 234 |
|