Debian Codegraph @

net/sf/saxon/tree/tiny/DescendantIterator.java

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