Debian Codegraph @

net/sf/saxon/expr/flwor/OuterForExpression.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.flwor;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
12 import net.sf.saxon.expr.parser.ExpressionTool;
13 import net.sf.saxon.expr.parser.ExpressionVisitor;
14 import net.sf.saxon.expr.parser.RebindingMap;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.om.SequenceIterator;
17 import net.sf.saxon.trace.ExpressionPresenter;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.tree.iter.LookaheadIterator;
20 import net.sf.saxon.tree.iter.LookaheadIteratorImpl;
21 import net.sf.saxon.value.EmptySequence;
22
23 /**
24 * Expression class that implements the "outer for" clause of XQuery 3.0
25 */
26 public class OuterForExpression extends ForExpression {
27
28 /**
29 * Get the cardinality of the range variable
30 *
31 * @return the cardinality of the range variable (StaticProperty.EXACTLY_ONE).
32 * in a subclass
33 */
34
35 protected int getRangeVariableCardinality() {
36 return StaticProperty.ALLOWS_ZERO_OR_ONE;
37 }
38
39 /**
40 * Optimize the expression
41 */
42
43 /*@NotNull*/
44 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
45 // Very conservative optimization for the time being
46
47 Expression sequence0 = getSequence();
48 getSequenceOp().optimize(visitor, contextItemType);
49 Expression action0 = getAction();
50 getActionOp().optimize(visitor, contextItemType);
51
52 if (sequence0 != getSequence() || action0 != getAction()) {
53 // it's now worth re-attempting the "where" clause optimizations
54 return optimize(visitor, contextItemType);
55 }
56
57 return this;
58 }
59
60 /**
61 * Copy an expression. This makes a deep copy.
62 *
63 * @return the copy of the original expression
64 * @param rebindings
65 */
66
67 /*@NotNull*/
68 public Expression copy(RebindingMap rebindings) {
69 OuterForExpression forExp = new OuterForExpression();
70 ExpressionTool.copyLocationInfo(this, forExp);
71 forExp.setRequiredType(requiredType);
72 forExp.setVariableQName(variableName);
73 forExp.setSequence(getSequence().copy(rebindings));
74 Expression newAction = getAction().copy(rebindings);
75 forExp.setAction(newAction);
76 forExp.variableName = variableName;
77 ExpressionTool.rebindVariableReferences(newAction, this, forExp);
78 return forExp;
79 }
80
81 /**
82 * Iterate over the result of the expression
83 */
84
85 /*@NotNull*/
86 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
87
88 // First create an iteration of the base sequence.
89
90 // Then create a MappingIterator which applies a mapping function to each
91 // item in the base sequence. The mapping function is essentially the "return"
92 // expression, wrapped in a MappingAction object that is responsible also for
93 // setting the range variable at each step.
94
95 SequenceIterator base = getSequence().iterate(context);
96 LookaheadIterator ahead = LookaheadIteratorImpl.makeLookaheadIterator(base);
97 if (ahead.hasNext()) {
98 MappingFunction map = new MappingAction(context, getLocalSlotNumber(), getAction());
99 return new MappingIterator(ahead, map);
100 } else {
101 context.setLocalVariable(getLocalSlotNumber(), EmptySequence.getInstance());
102 return getAction().iterate(context);
103 }
104 }
105
106 /**
107 * Process this expression as an instruction, writing results to the current
108 * outputter
109 */
110
111 public void process(XPathContext context) throws XPathException {
112 SequenceIterator base = getSequence().iterate(context);
113 int position = 1;
114 int slot = getLocalSlotNumber();
115 LookaheadIterator ahead = LookaheadIteratorImpl.makeLookaheadIterator(base);
116 if (ahead.hasNext()) {
117 while (true) {
118 Item item = ahead.next();
119 if (item == null) break;
120 context.setLocalVariable(slot, item);
121 getAction().process(context);
122 }
123 } else {
124 context.setLocalVariable(getLocalSlotNumber(), EmptySequence.getInstance());
125 getAction().process(context);
126 }
127 }
128
129 /**
130 * Get a name identifying the kind of expression, in terms meaningful to a user.
131 *
132 * @return a name identifying the kind of expression, in terms meaningful to a user.
133 * The name will always be in the form of a lexical XML QName, and should match the name used
134 * in explain() output displaying the expression.
135 */
136 @Override
137 public String getExpressionName() {
138 return "outerFor";
139 }
140
141 /**
142 * Evaluate an updating expression, adding the results to a Pending Update List.
143 * The default implementation of this method, which is used for non-updating expressions,
144 * throws an UnsupportedOperationException
145 *
146 * @param context the XPath dynamic evaluation context
147 * @param pul the pending update list to which the results should be written
148 */
149
150 public void evaluatePendingUpdates(XPathContext context, PendingUpdateList pul) throws XPathException {
151 SequenceIterator base = getSequence().iterate(context);
152 int position = 1;
153 int slot = getLocalSlotNumber();
154 LookaheadIterator ahead = LookaheadIteratorImpl.makeLookaheadIterator(base);
155 if (ahead.hasNext()) {
156 while (true) {
157 Item item = ahead.next();
158 if (item == null) break;
159 context.setLocalVariable(slot, item);
160 getAction().evaluatePendingUpdates(context, pul);
161 }
162 } else {
163 context.setLocalVariable(getLocalSlotNumber(), EmptySequence.getInstance());
164 getAction().evaluatePendingUpdates(context, pul);
165 }
166 }
167
168
169 protected void explainSpecializedAttributes(ExpressionPresenter out) {
170 out.emitAttribute("outer", "true");
171 }
172
173
174 }
175
176 //Copyright (c) Saxonica 2008. All rights reserved.
177
178