| 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.om.NodeInfo; |
| 11 | |
| 12 | import java.util.Iterator; |
| 13 | |
| 14 | /** |
| 15 | * An AxisIterator that wraps a Java Iterator. |
| 16 | * @param <B> the class of the external nodes being wrapped. |
| 17 | */ |
| 18 | |
| 19 | public class NodeWrappingAxisIterator<B> |
| 20 | implements AxisIterator, LookaheadIterator<NodeInfo> { |
| 21 | |
| 22 | |
| 23 | Iterator<? extends B> base; |
| 24 | private NodeWrappingFunction<? super B, NodeInfo> wrappingFunction; |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Create a SequenceIterator over a given iterator |
| 29 | * |
| 30 | * @param base the base Iterator |
| 31 | * @param wrappingFunction a function that wraps objects of type B in a Saxon NodeInfo |
| 32 | */ |
| 33 | |
| 34 | public NodeWrappingAxisIterator( |
| 35 | Iterator<? extends B> base, |
| 36 | NodeWrappingFunction<? super B, NodeInfo> wrappingFunction) { |
| 37 | this.base = base; |
| 38 | this.wrappingFunction = wrappingFunction; |
| 39 | } |
| 40 | |
| 41 | public Iterator<? extends B> getBaseIterator() { |
| 42 | return base; |
| 43 | } |
| 44 | |
| 45 | public NodeWrappingFunction<? super B, NodeInfo> getNodeWrappingFunction() { |
| 46 | return wrappingFunction; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | public boolean hasNext() { |
| 51 | return base.hasNext(); |
| 52 | } |
| 53 | |
| 54 | /*@Nullable*/ |
| 55 | public NodeInfo next() { |
| 56 | while (base.hasNext()) { |
| 57 | B next = base.next(); |
| 58 | if (!isIgnorable(next)) { |
| 59 | return wrappingFunction.wrap(next); |
| 60 | } |
| 61 | } |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | public void close() { |
| 66 | } |
| 67 | |
| 68 | public boolean isIgnorable(B node) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get properties of this iterator, as a bit-significant integer. |
| 74 | * |
| 75 | * @return the properties of this iterator. This will be some combination of |
| 76 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 77 | * and {@link #LOOKAHEAD}. It is always |
| 78 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 79 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 80 | */ |
| 81 | |
| 82 | public int getProperties() { |
| 83 | return LOOKAHEAD; |
| 84 | } |
| 85 | |
| 86 | } |
| 87 |