Debian Codegraph @

net/sf/saxon/expr/SubscriptExpression.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * A SubscriptExpression represents a FilterExpression of the form EXPR[n]
24 * where n is known to be singleton numeric and to be independent of the focus; it does not need to be constant
25 */
26 public class SubscriptExpression extends SingleItemFilter {
27
28 private Operand subscriptOp;
29
30 /**
31 * Construct a SubscriptExpression
32 *
33 * @param base the expression to be filtered
34 * @param subscript the positional subscript filter
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 * Type-check the expression.
54 */
55 @Override
56 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
57 getOperand().typeCheck(visitor, contextInfo);
58 return this;
59 }
60
61 /*@NotNull*/
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 * Copy an expression. This makes a deep copy.
72 *
73 * @return the copy of the original expression
74 * @param rebindings variables that need to be re-bound
75 */
76
77 /*@NotNull*/
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 * Get the subscript expression
91 *
92 * @return the expression used to compute the one-based start offset
93 */
94
95 public Expression getSubscriptExpression() {
96 return getSubscript();
97 }
98
99 /**
100 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
101 * This method indicates which of these methods is provided directly. The other methods will always be available
102 * indirectly, using an implementation that relies on one of the other methods.
103 *
104 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
105 * {@link #PROCESS_METHOD}
106 */
107 @Override
108 public int getImplementationMethod() {
109 return EVALUATE_METHOD;
110 }
111
112 /**
113 * Compare two expressions to see if they are equal
114 *
115 * @param other the other expression
116 * @return true if the expressions are equivalent
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 * Get the static cardinality: this implementation is appropriate for [1] and [last()] which will always
131 * return something if the input is non-empty
132 */
133
134 public int computeCardinality() {
135 return StaticProperty.ALLOWS_ZERO_OR_ONE;
136 }
137
138
139 /**
140 * Get the (partial) name of a class that supports streaming of this kind of expression
141 *
142 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
143 * or null if there is no such class
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 // there is no item at the required position
175 return null;
176 }
177 }
178
179 /**
180 * Get a name identifying the kind of expression, in terms meaningful to a user.
181 *
182 * @return a name identifying the kind of expression, in terms meaningful to a user.
183 * The name will always be in the form of a lexical XML QName, and should match the name used
184 * in export() output displaying the expression.
185 */
186 @Override
187 public String getExpressionName() {
188 return "subscript";
189 }
190
191
192
193 /**
194 * Diagnostic print of expression structure. The abstract expression tree
195 * is written to the supplied output destination.
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 * <p>The toString() method for an expression attempts to give a representation of the expression
207 * in an XPath-like form.</p>
208 * <p>For subclasses of Expression that represent XPath expressions, the result should always be a string that
209 * parses as an XPath 3.0 expression.</p>
210 *
211 * @return a representation of the expression as a string
212 */
213 @Override
214 public String toString() {
215 return ExpressionTool.parenthesize(getBaseExpression()) + "[" + getSubscript() + "]";
216 }
217
218 /**
219 * <p>The toString() method for an expression attempts to give a representation of the expression
220 * in an XPath-like form.</p>
221 * <p>For subclasses of Expression that represent XPath expressions, the result should always be a string that
222 * parses as an XPath 3.0 expression.</p>
223 *
224 * @return a representation of the expression as a string
225 */
226 @Override
227 public String toShortString() {
228 return ExpressionTool.parenthesize(getBaseExpression()) +
229 "[" + getSubscript().toShortString() + "]";
230 }
231
232
233 }
234