Debian Codegraph @

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