| 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.instruct; |
| 9 | |
| 10 | import net.sf.saxon.expr.Operand; |
| 11 | import net.sf.saxon.expr.XPathContext; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.om.SequenceIterator; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | |
| 16 | /** |
| 17 | * Iterate over the instructions in the Block, concatenating the result of each instruction |
| 18 | * into a single combined sequence. |
| 19 | */ |
| 20 | |
| 21 | public class BlockIterator implements SequenceIterator<Item<?>> { |
| 22 | |
| 23 | private Operand[] operanda; |
| 24 | private int currentChildExpr = 0; |
| 25 | private SequenceIterator currentIter; |
| 26 | private XPathContext context; |
| 27 | private int position = 0; |
| 28 | |
| 29 | public BlockIterator(Operand[] operanda, XPathContext context) { |
| 30 | this.operanda = operanda; |
| 31 | this.currentChildExpr = 0; |
| 32 | this.context = context; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the next item in the sequence. <BR> |
| 37 | * |
| 38 | * @return the next item, or null if there are no more items. |
| 39 | * @throws net.sf.saxon.trans.XPathException |
| 40 | * if an error occurs retrieving the next item |
| 41 | */ |
| 42 | |
| 43 | public Item next() throws XPathException { |
| 44 | if (position < 0) { |
| 45 | return null; |
| 46 | } |
| 47 | while (true) { |
| 48 | if (currentIter == null) { |
| 49 | currentIter = operanda[currentChildExpr++].getChildExpression().iterate(context); |
| 50 | } |
| 51 | Item current = currentIter.next(); |
| 52 | if (current != null) { |
| 53 | position++; |
| 54 | return current; |
| 55 | } |
| 56 | currentIter = null; |
| 57 | if (currentChildExpr >= operanda.length) { |
| 58 | position = -1; |
| 59 | return null; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public void close() { |
| 65 | if (currentIter != null) { |
| 66 | currentIter.close(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get properties of this iterator, as a bit-significant integer. |
| 72 | * |
| 73 | * @return the properties of this iterator. This will be some combination of |
| 74 | * properties such as {@link SequenceIterator#GROUNDED}, {@link SequenceIterator#LAST_POSITION_FINDER}, |
| 75 | * and {@link SequenceIterator#LOOKAHEAD}. It is always |
| 76 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 77 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 78 | */ |
| 79 | |
| 80 | public int getProperties() { |
| 81 | return 0; |
| 82 | } |
| 83 | } |
| 84 |