Debian Codegraph @

net/sf/saxon/expr/SimpleStepExpression.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.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 * A SimpleStepExpression is a special case of a SlashExpression in which the
26 * start expression selects a single item (or nothing), and the step expression is
27 * a simple AxisExpression. This is designed to avoid the costs of creating a new
28 * dynamic context for expressions (common in XQuery) such as
29 * for $b in EXPR return $b/title
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 /*@NotNull*/
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 /*@NotNull*/
79 @Override
80 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
81 return this;
82 }
83
84 /**
85 * Copy an expression. This makes a deep copy.
86 *
87 * @return the copy of the original expression
88 * @param rebindings
89 */
90 /*@NotNull*/
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 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
100 * This method indicates which of these methods is provided directly. The other methods will always be available
101 * indirectly, using an implementation that relies on one of the other methods.
102 *
103 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
104 * {@link #PROCESS_METHOD}
105 */
106 @Override
107 public int getImplementationMethod() {
108 return ITERATE_METHOD;
109 }
110
111 /**
112 * Evaluate the expression, returning an iterator over the result
113 *
114 * @param context the evaluation context
115 */
116 /*@NotNull*/
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