Debian Codegraph @

net/sf/saxon/expr/flwor/ForClauseOuterPush.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.Item;
12 import net.sf.saxon.om.SequenceIterator;
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 implements the changes to the tuple stream effected by a "for" clause in a FLWOR expression
19 * where "allowing empty" is specified
20 */
21 public class ForClauseOuterPush extends TuplePush {
22
23 protected TuplePush destination;
24 protected ForClause forClause;
25
26 public ForClauseOuterPush(TuplePush destination, ForClause forClause) {
27 this.destination = destination;
28 this.forClause = forClause;
29 }
30
31 /*
32 * Process the next tuple.
33 */
34 @Override
35 public void processTuple(XPathContext context) throws XPathException {
36 SequenceIterator iter = forClause.getSequence().iterate(context);
37 int pos = 0;
38 Item next = iter.next();
39 if (next == null) {
40 context.setLocalVariable(forClause.getRangeVariable().getLocalSlotNumber(), EmptySequence.getInstance());
41 if (forClause.getPositionVariable() != null) {
42 context.setLocalVariable(forClause.getPositionVariable().getLocalSlotNumber(), Int64Value.ZERO);
43 }
44 destination.processTuple(context);
45 } else {
46 while (true) {
47 context.setLocalVariable(forClause.getRangeVariable().getLocalSlotNumber(), next);
48 if (forClause.getPositionVariable() != null) {
49 context.setLocalVariable(
50 forClause.getPositionVariable().getLocalSlotNumber(),
51 new Int64Value(++pos));
52 }
53 destination.processTuple(context);
54 next = iter.next();
55 if (next == null) {
56 break;
57 }
58 }
59 }
60 }
61
62 /*
63 * Close the tuple stream
64 */
65 @Override
66 public void close() throws XPathException {
67 destination.close();
68 }
69 }
70