| 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.linked; |
| 9 | |
| 10 | import net.sf.saxon.om.NodeInfo; |
| 11 | import net.sf.saxon.pattern.NodeTest; |
| 12 | import net.sf.saxon.tree.iter.AxisIterator; |
| 13 | import net.sf.saxon.tree.iter.LookaheadIterator; |
| 14 | |
| 15 | abstract class TreeEnumeration implements AxisIterator, LookaheadIterator<NodeInfo> { |
| 16 | |
| 17 | protected NodeImpl start; |
| 18 | /*@Nullable*/ protected NodeImpl next; |
| 19 | protected NodeTest nodeTest; |
| 20 | /*@Nullable*/ protected NodeImpl current = null; |
| 21 | protected int position = 0; |
| 22 | |
| 23 | /** |
| 24 | * Create an axis enumeration for a given type and name of node, from a given |
| 25 | * origin node |
| 26 | * |
| 27 | * @param origin the node from which the axis originates |
| 28 | * @param nodeTest test to be satisfied by the returned nodes, or null if all nodes |
| 29 | * are to be returned. |
| 30 | */ |
| 31 | |
| 32 | public TreeEnumeration(NodeImpl origin, NodeTest nodeTest) { |
| 33 | next = origin; |
| 34 | start = origin; |
| 35 | this.nodeTest = nodeTest; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Test whether a node conforms to the node type and name constraints. |
| 40 | * Note that this returns true if the supplied node is null, this is a way of |
| 41 | * terminating a loop. |
| 42 | * |
| 43 | * @param node the node to be tested |
| 44 | * @return true if the node matches the requested node type and name |
| 45 | */ |
| 46 | |
| 47 | protected boolean conforms(/*@Nullable*/ NodeImpl node) { |
| 48 | return node == null || nodeTest == null || nodeTest.matchesNode(node); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Advance along the axis until a node is found that matches the required criteria |
| 53 | */ |
| 54 | |
| 55 | protected final void advance() { |
| 56 | do { |
| 57 | step(); |
| 58 | } while (!conforms(next)); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Advance one step along the axis: the resulting node might not meet the required |
| 63 | * criteria for inclusion |
| 64 | */ |
| 65 | |
| 66 | protected abstract void step(); |
| 67 | |
| 68 | /** |
| 69 | * Determine whether there are more items to come. Note that this operation |
| 70 | * is stateless and it is not necessary (or usual) to call it before calling |
| 71 | * next(). It is used only when there is an explicit need to tell if we |
| 72 | * are at the last element. |
| 73 | * |
| 74 | * @return true if there are more items in the sequence |
| 75 | */ |
| 76 | |
| 77 | public boolean hasNext() { |
| 78 | return next != null; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Return the next node in the sequence |
| 84 | */ |
| 85 | |
| 86 | /*@Nullable*/ |
| 87 | public final NodeInfo next() { |
| 88 | if (next == null) { |
| 89 | current = null; |
| 90 | position = -1; |
| 91 | return null; |
| 92 | } else { |
| 93 | current = next; |
| 94 | position++; |
| 95 | advance(); |
| 96 | return current; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Indicate that no more items are required from the iterator |
| 102 | */ |
| 103 | |
| 104 | public void close() { |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get properties of this iterator, as a bit-significant integer. |
| 109 | * |
| 110 | * @return the properties of this iterator. This will be some combination of |
| 111 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 112 | * and {@link #LOOKAHEAD}. It is always |
| 113 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 114 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 115 | */ |
| 116 | |
| 117 | public int getProperties() { |
| 118 | return LOOKAHEAD; |
| 119 | } |
| 120 | |
| 121 | } |
| 122 |