Debian Codegraph @

net/sf/saxon/tree/tiny/GraftingDescendantIterator.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.AxisInfo;
11 import net.sf.saxon.om.NodeInfo;
12 import net.sf.saxon.pattern.NodeTest;
13 import net.sf.saxon.tree.iter.AxisIterator;
14 import net.sf.saxon.type.Type;
15 import net.sf.saxon.type.UType;
16
17 import java.util.function.IntPredicate;
18
19 /**
20 * This class supports both the descendant:: and descendant-or-self:: axes, which are
21 * identical except for the route to the first candidate node.
22 * It enumerates descendants of the specified node.
23 * The calling code must ensure that the start node is not an attribute or namespace node.
24 */
25
26 final class GraftingDescendantIterator implements AxisIterator {
27
28 private final TinyTree tree;
29 private final TinyNodeImpl startNode;
30 private int nextNodeNr;
31 private final int startDepth;
32 private final NodeTest test;
33 private final IntPredicate matcher;
34 private AxisIterator nestedIterator;
35 private NodeInfo pending = null;
36 private boolean includeTextNodes;
37
38 /**
39 * Create an iterator over the descendant axis
40 *
41 * @param doc the containing TinyTree
42 * @param node the node whose descendants are required
43 * @param nodeTest test to be satisfied by each returned node
44 */
45
46 GraftingDescendantIterator(/*@NotNull*/ TinyTree doc, /*@NotNull*/ TinyNodeImpl node, NodeTest nodeTest) {
47 tree = doc;
48 startNode = node;
49 test = nodeTest;
50 nextNodeNr = node.nodeNr;
51 startDepth = doc.depth[nextNodeNr];
52 matcher = nodeTest.getMatcher(doc);
53 includeTextNodes = nodeTest.getUType().overlaps(UType.TEXT);
54 }
55
56 /*@Nullable*/
57 public NodeInfo next() {
58 while (true) {
59 if (pending != null) {
60 NodeInfo p = pending;
61 pending = null;
62 return p;
63 }
64 if (nestedIterator != null) {
65 NodeInfo nested = nestedIterator.next();
66 if (nested != null) {
67 return nested;
68 } else {
69 nestedIterator = null;
70 }
71 }
72 nextNodeNr++;
73 try {
74 if (tree.depth[nextNodeNr] <= startDepth) {
75 nextNodeNr = -1;
76 return null;
77 }
78 if (tree.nodeKind[nextNodeNr] == Type.EXTERNAL_NODE_REFERENCE) {
79 byte axis = nextNodeNr == startNode.nodeNr ? AxisInfo.DESCENDANT : AxisInfo.DESCENDANT_OR_SELF;
80 nestedIterator = tree.externalNodes.get(tree.alpha[nextNodeNr])
81 .iterateAxis(axis, test);
82 continue;
83 }
84 if (includeTextNodes && tree.nodeKind[nextNodeNr] == Type.TEXTUAL_ELEMENT) {
85 pending = ((TinyTextualElement) tree.getNode(nextNodeNr)).getTextNode();
86 }
87 } catch (ArrayIndexOutOfBoundsException e) {
88 // this shouldn't happen. If it does happen, it means the tree wasn't properly closed
89 // during construction (there is no stopper node at the end). In this case, we'll recover
90 // by returning end-of sequence
91 //System.err.println("********* no stopper node **********");
92 nextNodeNr = -1;
93 return null;
94 }
95 if (matcher.test(nextNodeNr)) {
96 break;
97 }
98 }
99
100 return tree.getNode(nextNodeNr);
101 }
102
103 }
104