| 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; |
| 9 | |
| 10 | import net.sf.saxon.om.AtomicSequence; |
| 11 | import net.sf.saxon.om.AtomizedValueIterator; |
| 12 | import net.sf.saxon.om.SequenceIterator; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.value.AtomicValue; |
| 15 | |
| 16 | /** |
| 17 | * This iterator returns a sequence of atomic values, the result of atomizing the sequence |
| 18 | * of nodes returned by an underlying SequenceIterator. |
| 19 | */ |
| 20 | |
| 21 | public final class AxisAtomizingIterator implements SequenceIterator<AtomicValue> { |
| 22 | |
| 23 | private AtomizedValueIterator base; |
| 24 | private AtomicSequence results = null; |
| 25 | private int atomicPosition = 0; |
| 26 | |
| 27 | /** |
| 28 | * Construct an atomizing iterator |
| 29 | * |
| 30 | * @param base the base iterator (whose nodes are to be atomized) |
| 31 | */ |
| 32 | |
| 33 | public AxisAtomizingIterator(AtomizedValueIterator base) { |
| 34 | this.base = base; |
| 35 | } |
| 36 | |
| 37 | public AtomicValue next() throws XPathException { |
| 38 | while (true) { |
| 39 | if (results != null) { |
| 40 | if (atomicPosition < results.getLength()) { |
| 41 | return results.itemAt(atomicPosition++); |
| 42 | } else { |
| 43 | results = null; |
| 44 | continue; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | AtomicSequence atomized = base.nextAtomizedValue(); |
| 49 | if (atomized == null) { |
| 50 | results = null; |
| 51 | return null; |
| 52 | } |
| 53 | if (atomized instanceof AtomicValue) { |
| 54 | // common case (the atomized value of the node is a single atomic value) |
| 55 | results = null; |
| 56 | return (AtomicValue) atomized; |
| 57 | } else { |
| 58 | results = atomized; |
| 59 | atomicPosition = 0; |
| 60 | // continue |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public void close() { |
| 66 | base.close(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get properties of this iterator, as a bit-significant integer. |
| 71 | * |
| 72 | * @return the properties of this iterator. This will be some combination of |
| 73 | * properties such as {@link net.sf.saxon.om.SequenceIterator#GROUNDED}, {@link net.sf.saxon.om.SequenceIterator#LAST_POSITION_FINDER}, |
| 74 | * and {@link net.sf.saxon.om.SequenceIterator#LOOKAHEAD}. It is always |
| 75 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 76 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 77 | */ |
| 78 | |
| 79 | public int getProperties() { |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | } |
| 84 |