Debian Codegraph @

net/sf/saxon/expr/LookupAllExpression.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.Configuration;
11 import net.sf.saxon.expr.instruct.Block;
12 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
13 import net.sf.saxon.expr.parser.ExpressionTool;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.expr.parser.RebindingMap;
16 import net.sf.saxon.ma.arrays.ArrayItem;
17 import net.sf.saxon.ma.arrays.ArrayItemType;
18 import net.sf.saxon.ma.arrays.SquareArrayConstructor;
19 import net.sf.saxon.ma.map.KeyValuePair;
20 import net.sf.saxon.ma.map.MapItem;
21 import net.sf.saxon.ma.map.MapType;
22 import net.sf.saxon.ma.map.TupleType;
23 import net.sf.saxon.om.GroundedValue;
24 import net.sf.saxon.om.Item;
25 import net.sf.saxon.om.SequenceIterator;
26 import net.sf.saxon.trace.ExpressionPresenter;
27 import net.sf.saxon.trans.XPathException;
28 import net.sf.saxon.type.AnyItemType;
29 import net.sf.saxon.type.ItemType;
30 import net.sf.saxon.type.TypeHierarchy;
31 import net.sf.saxon.type.UType;
32
33 import java.util.ArrayList;
34 import java.util.Iterator;
35 import java.util.List;
36
37
38 /**
39 * A lookup expression is an expression of the form A?*, where A must be a map or an array
40 */
41
42 public class LookupAllExpression extends UnaryExpression {
43
44 /**
45 * Constructor
46 *
47 * @param base The left hand operand (which must always select a sequence of maps or arrays).
48 */
49
50 public LookupAllExpression(Expression base) {
51 super(base);
52 }
53
54 @Override
55 protected OperandRole getOperandRole() {
56 return OperandRole.INSPECT;
57 }
58
59
60 /**
61 * Determine the data type of the items returned by this expression
62 *
63 * @return the type of the step
64 */
65
66 /*@NotNull*/
67 public final ItemType getItemType() {
68 ItemType base = getBaseExpression().getItemType();
69 if (base instanceof MapType) {
70 return ((MapType)base).getValueType().getPrimaryType();
71 } else if (base instanceof ArrayItemType) {
72 return ((ArrayItemType) base).getMemberType().getPrimaryType();
73 } else {
74 return AnyItemType.getInstance();
75 }
76 }
77
78
79 /**
80 * Get the static type of the expression as a UType, following precisely the type
81 * inference rules defined in the XSLT 3.0 specification.
82 *
83 * @return the static item type of the expression according to the XSLT 3.0 defined rules
84 * @param contextItemType The type of the context item (not used)
85 */
86 @Override
87 public UType getStaticUType(UType contextItemType) {
88 return getItemType().getUType();
89 }
90
91 /**
92 * Type-check the expression
93 */
94
95 /*@NotNull*/
96 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
97
98 Configuration config = visitor.getConfiguration();
99 TypeHierarchy th = config.getTypeHierarchy();
100
101 // Check the first operand
102 getOperand().typeCheck(visitor, contextInfo);
103
104 ItemType containerType = getBaseExpression().getItemType();
105 boolean isArrayLookup = containerType instanceof ArrayItemType;
106 boolean isMapLookup = containerType instanceof MapType || containerType instanceof TupleType;
107
108 if (!isArrayLookup && !isMapLookup) {
109 if (th.relationship(containerType, MapType.ANY_MAP_TYPE) == TypeHierarchy.DISJOINT &&
110 th.relationship(containerType, ArrayItemType.getInstance()) == TypeHierarchy.DISJOINT) {
111 XPathException err = new XPathException("The left-hand operand of '?' must be a map or an array; the supplied expression is of type " + containerType, "XPTY0004");
112 err.setLocation(getLocation());
113 err.setIsTypeError(true);
114 err.setFailingExpression(this);
115 throw err;
116 }
117 }
118
119 if (getBaseExpression() instanceof Literal) {
120 return new Literal(iterate(visitor.makeDynamicContext()).materialize());
121 }
122
123 return this;
124 }
125
126 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
127 getOperand().optimize(visitor, contextItemType);
128
129 if (getBaseExpression() instanceof Literal) {
130 return new Literal(iterate(visitor.makeDynamicContext()).materialize());
131 }
132
133 // See W3C bug 30228. In the interests of keeping certain tests streamable, we do a rewrite of [A,B,C]?*
134 // to (A, B, C).
135 if (getBaseExpression() instanceof SquareArrayConstructor) {
136 List<Expression> children = new ArrayList<>();
137 for (Operand o : getBaseExpression().operands()) {
138 children.add(o.getChildExpression().copy(new RebindingMap()));
139 }
140 Expression[] childExpressions = children.toArray(new Expression[0]);
141 Block block = new Block(childExpressions);
142 ExpressionTool.copyLocationInfo(this, block);
143 return block;
144 }
145 return this;
146 }
147
148
149 /**
150 * Return the estimated cost of evaluating an expression. This is a very crude measure based
151 * on the syntactic form of the expression (we have no knowledge of data values). We take
152 * the cost of evaluating a simple scalar comparison or arithmetic expression as 1 (one),
153 * and we assume that a sequence has length 5. The resulting estimates may be used, for
154 * example, to reorder the predicates in a filter expression so cheaper predicates are
155 * evaluated first.
156 */
157 @Override
158 public int getCost() {
159 return getBaseExpression().getCost() + 1;
160 }
161
162 /**
163 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
164 * This method indicates which of these methods is provided directly. The other methods will always be available
165 * indirectly, using an implementation that relies on one of the other methods.
166 *
167 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
168 * {@link #PROCESS_METHOD}
169 */
170 @Override
171 public int getImplementationMethod() {
172 return ITERATE_METHOD;
173 }
174
175 /**
176 * Copy an expression. This makes a deep copy.
177 *
178 * @return the copy of the original expression
179 * @param rebindings variables that need to be re-bound.
180 */
181
182 /*@NotNull*/
183 public LookupAllExpression copy(RebindingMap rebindings) {
184 return new LookupAllExpression(getBaseExpression().copy(rebindings));
185 }
186
187 /**
188 * Determine the static cardinality of the expression
189 */
190
191 public int computeCardinality() {
192 return StaticProperty.ALLOWS_ZERO_OR_MORE;
193 }
194
195 /**
196 * Is this expression the same as another expression?
197 */
198
199 public boolean equals(Object other) {
200 if (!(other instanceof LookupAllExpression)) {
201 return false;
202 }
203 LookupAllExpression p = (LookupAllExpression) other;
204 return getBaseExpression().isEqual(p.getBaseExpression());
205 }
206
207 /**
208 * get HashCode for comparing two expressions
209 */
210
211 public int computeHashCode() {
212 return "LookupAll".hashCode() ^ getBaseExpression().hashCode();
213 }
214
215 /**
216 * Iterate the lookup-expression in a given context
217 *
218 * @param context the evaluation context
219 */
220
221 /*@NotNull*/
222 public SequenceIterator<?> iterate(final XPathContext context) throws XPathException {
223
224 return new SequenceIterator() {
225
226 SequenceIterator level0 = getBaseExpression().iterate(context);
227 Iterator<?> level1 = null;
228 // delivers GroundedValue in the case of an array, or KeyValuePair in the case of a map
229 SequenceIterator level2 = null;
230
231 @Override
232 public Item next() throws XPathException {
233 if (level2 == null) {
234 if (level1 == null) {
235 Item base = level0.next();
236 if (base == null) {
237 return null;
238 } else if (base instanceof ArrayItem) {
239 level1 = ((ArrayItem)base).members().iterator();
240 return next();
241 } else if (base instanceof MapItem) {
242 level1 = ((MapItem)base).keyValuePairs().iterator();
243 return next();
244 } else {
245 LookupExpression.mustBeArrayOrMap(LookupAllExpression.this, base);
246 return null;
247 }
248 } else {
249 if (level1.hasNext()) {
250 Object nextEntry = level1.next();
251 if (nextEntry instanceof KeyValuePair) {
252 GroundedValue value = ((KeyValuePair)nextEntry).value;
253 level2 = value.iterate();
254 } else if (nextEntry instanceof GroundedValue) {
255 level2 = ((GroundedValue) nextEntry).iterate();
256 } else {
257 throw new IllegalStateException();
258 }
259 return next();
260 } else {
261 level1 = null;
262 return next();
263 }
264 }
265 } else {
266 Item next = level2.next();
267 if (next == null) {
268 level2 = null;
269 return next();
270 } else {
271 return next;
272 }
273 }
274 }
275
276 @Override
277 public void close() {
278 if (level0 != null) {
279 level0.close();
280 }
281 if (level2 != null) {
282 level2.close();
283 }
284 }
285
286 };
287
288 }
289
290 /**
291 * Diagnostic print of expression structure. The abstract expression tree
292 * is written to the supplied output destination.
293 */
294
295 public void export(ExpressionPresenter destination) throws XPathException {
296 destination.startElement("lookupAll", this);
297 getBaseExpression().export(destination);
298 destination.endElement();
299 }
300
301 /**
302 * The toString() method for an expression attempts to give a representation of the expression
303 * in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath.
304 * In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax
305 *
306 * @return a representation of the expression as a string
307 */
308
309 public String toString() {
310 return ExpressionTool.parenthesize(getBaseExpression()) + "?*";
311 }
312
313 public String toShortString() {
314 return getBaseExpression().toShortString() + "?*";
315 }
316
317 }
318