| 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.tiny; |
| 9 | |
| 10 | import net.sf.saxon.om.NodeInfo; |
| 11 | import net.sf.saxon.pattern.NodeTest; |
| 12 | import net.sf.saxon.tree.iter.AxisIterator; |
| 13 | |
| 14 | import java.util.function.IntPredicate; |
| 15 | |
| 16 | /** |
| 17 | * This class supports both the descendant:: and descendant-or-self:: axes, which are |
| 18 | * identical except for the route to the first candidate node. |
| 19 | * It enumerates descendants of the specified node. This version is for use when it is known |
| 20 | * that text nodes are not selected and when there are no external grafted subtrees. |
| 21 | * The calling code must ensure that the start node is not an attribute or namespace node. |
| 22 | */ |
| 23 | |
| 24 | final class DescendantIteratorSansText implements AxisIterator { |
| 25 | |
| 26 | private final TinyTree tree; |
| 27 | private int nextNodeNr; |
| 28 | private final int startDepth; |
| 29 | private final IntPredicate matcher; |
| 30 | |
| 31 | /** |
| 32 | * Create an iterator over the descendant axis |
| 33 | * |
| 34 | * @param doc the containing TinyTree |
| 35 | * @param node the node whose descendants are required |
| 36 | * @param nodeTest test to be satisfied by each returned node |
| 37 | */ |
| 38 | |
| 39 | DescendantIteratorSansText(/*@NotNull*/ TinyTree doc, /*@NotNull*/ TinyNodeImpl node, NodeTest nodeTest) { |
| 40 | tree = doc; |
| 41 | nextNodeNr = node.nodeNr; |
| 42 | startDepth = doc.depth[nextNodeNr]; |
| 43 | matcher = nodeTest.getMatcher(doc); |
| 44 | |
| 45 | } |
| 46 | |
| 47 | /*@Nullable*/ |
| 48 | public NodeInfo next() { |
| 49 | do { |
| 50 | nextNodeNr++; |
| 51 | try { |
| 52 | if (tree.depth[nextNodeNr] <= startDepth) { |
| 53 | nextNodeNr = -1; |
| 54 | return null; |
| 55 | } |
| 56 | } catch (ArrayIndexOutOfBoundsException e) { |
| 57 | // this shouldn't happen. If it does happen, it means the tree wasn't properly closed |
| 58 | // during construction (there is no stopper node at the end). In this case, we'll recover |
| 59 | // by returning end-of sequence |
| 60 | //System.err.println("********* no stopper node **********"); |
| 61 | nextNodeNr = -1; |
| 62 | return null; |
| 63 | } |
| 64 | } while (!matcher.test(nextNodeNr)); |
| 65 | |
| 66 | return tree.getNode(nextNodeNr); |
| 67 | } |
| 68 | |
| 69 | } |
| 70 |