Debian Codegraph @

net/sf/saxon/expr/flwor/ForClauseOuterPull.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.XPathContext;
11 import net.sf.saxon.om.FocusTrackingIterator;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.value.EmptySequence;
15 import net.sf.saxon.value.Int64Value;
16
17 /**
18 * This class represents the tuple stream returned by a "for" clause in a FLWOR expression
19 */
20 public class ForClauseOuterPull extends ForClausePull {
21
22 public ForClauseOuterPull(TuplePull base, ForClause forClause) {
23 super(base, forClause);
24 }
25
26 /**
27 * Deliver the next output tuple. Before returning, this method must set all the variables corresponding
28 * to the output tuple in the local stack frame associated with the context object
29 *
30 * @param context the dynamic evaluation context
31 * @return true if another tuple has been generated; false if the tuple stream is exhausted. If the
32 * method returns false, the values of the local variables corresponding to this tuple stream
33 * are undefined.
34 */
35 @Override
36 public boolean nextTuple(XPathContext context) throws XPathException {
37 while (true) {
38 Item next;
39 if (currentIteration == null) {
40 if (!base.nextTuple(context)) {
41 return false;
42 }
43 currentIteration = new FocusTrackingIterator(forClause.getSequence().iterate(context));
44 next = currentIteration.next();
45 if (next == null) {
46 context.setLocalVariable(forClause.getRangeVariable().getLocalSlotNumber(),
47 EmptySequence.getInstance());
48 if (forClause.getPositionVariable() != null) {
49 context.setLocalVariable(
50 forClause.getPositionVariable().getLocalSlotNumber(),
51 Int64Value.ZERO);
52 }
53 currentIteration = null;
54 return true;
55 }
56 } else {
57 next = currentIteration.next();
58 }
59 if (next != null) {
60 context.setLocalVariable(forClause.getRangeVariable().getLocalSlotNumber(), next);
61 if (forClause.getPositionVariable() != null) {
62 context.setLocalVariable(
63 forClause.getPositionVariable().getLocalSlotNumber(),
64 new Int64Value(currentIteration.position()));
65 }
66 return true;
67 } else {
68 currentIteration = null;
69 }
70 }
71 }
72
73 /**
74 * Close the tuple stream, indicating that although not all tuples have been read,
75 * no further tuples are required and resources can be released
76 */
77 @Override
78 public void close() {
79 base.close();
80 if (currentIteration != null) {
81 currentIteration.close();
82 }
83 }
84 }
85