| 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.tree.iter; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 11 | import net.sf.saxon.om.AtomicSequence; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.om.SequenceIterator; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | import net.sf.saxon.value.AtomicValue; |
| 16 | |
| 17 | /** |
| 18 | * AtomizingIterator returns the atomization of an underlying sequence supplied |
| 19 | * as an iterator. We use a specialist class rather than a general-purpose |
| 20 | * MappingIterator for performance, especially as the relationship of items |
| 21 | * in the result sequence to those in the base sequence is often one-to-one. |
| 22 | * <p>This AtomizingIterator is capable of handling list-typed nodes whose atomized value |
| 23 | * is a sequence of more than one item. When it is known that all input will be untyped, |
| 24 | * an {@link UntypedAtomizingIterator} is used in preference.</p> |
| 25 | */ |
| 26 | |
| 27 | public class AtomizingIterator implements SequenceIterator<AtomicValue> { |
| 28 | |
| 29 | private SequenceIterator base; |
| 30 | /*@Nullable*/ private AtomicSequence currentValue = null; |
| 31 | private int currentValuePosition = 1; |
| 32 | private int currentValueSize = 1; |
| 33 | private RoleDiagnostic roleDiagnostic; |
| 34 | |
| 35 | /** |
| 36 | * Construct an AtomizingIterator that will atomize the values returned by the base iterator. |
| 37 | * |
| 38 | * @param base the base iterator |
| 39 | */ |
| 40 | |
| 41 | public AtomizingIterator(SequenceIterator base) { |
| 42 | this.base = base; |
| 43 | } |
| 44 | |
| 45 | public void setRoleDiagnostic(RoleDiagnostic role) { |
| 46 | this.roleDiagnostic = role; |
| 47 | } |
| 48 | |
| 49 | /*@Nullable*/ |
| 50 | public AtomicValue next() throws XPathException { |
| 51 | while (true) { |
| 52 | if (currentValue != null) { |
| 53 | if (currentValuePosition < currentValueSize) { |
| 54 | return currentValue.itemAt(currentValuePosition++); |
| 55 | } else { |
| 56 | currentValue = null; |
| 57 | } |
| 58 | } |
| 59 | Item nextSource = base.next(); |
| 60 | if (nextSource != null) { |
| 61 | try { |
| 62 | AtomicSequence v = nextSource.atomize(); |
| 63 | if (v instanceof AtomicValue) { |
| 64 | return (AtomicValue) v; |
| 65 | } else { |
| 66 | currentValue = v; |
| 67 | currentValuePosition = 0; |
| 68 | currentValueSize = currentValue.getLength(); |
| 69 | // now go round the loop to get the first item from the atomized value |
| 70 | } |
| 71 | } catch (XPathException e) { |
| 72 | if (roleDiagnostic == null) { |
| 73 | throw e; |
| 74 | } else { |
| 75 | String message = e.getMessage() + ". Failed while atomizing the " + roleDiagnostic.getMessage(); |
| 76 | throw new XPathException(message, e.getErrorCodeLocalPart(), e.getLocator()); |
| 77 | } |
| 78 | } |
| 79 | } else { |
| 80 | currentValue = null; |
| 81 | return null; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public void close() { |
| 87 | base.close(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get properties of this iterator, as a bit-significant integer. |
| 92 | * |
| 93 | * @return the properties of this iterator. This will be some combination of |
| 94 | * properties such as {@link net.sf.saxon.om.SequenceIterator#GROUNDED}, {@link net.sf.saxon.om.SequenceIterator#LAST_POSITION_FINDER}, |
| 95 | * and {@link net.sf.saxon.om.SequenceIterator#LOOKAHEAD}. It is always |
| 96 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 97 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 98 | */ |
| 99 | |
| 100 | public int getProperties() { |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | } |
| 105 |