Debian Codegraph @

net/sf/saxon/tree/tiny/FollowingIterator.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 * Iterate over the following axis starting at a given node.
19 * The start node must not be a namespace or attribute node.
20 */
21
22 final class FollowingIterator implements AxisIterator {
23
24 private TinyTree tree;
25 private TinyNodeImpl startNode;
26 private NodeInfo current;
27 private NodeTest test;
28 private boolean includeDescendants;
29 int position = 0;
30 private final IntPredicate matcher;
31 private NodeInfo pending;
32
33 /**
34 * Create an iterator over the following axis
35 *
36 * @param doc the containing TinyTree
37 * @param node the start node. If the actual start was an attribute or namespace node, this will
38 * be the parent element of that attribute or namespace
39 * @param nodeTest condition that all the returned nodes must satisfy
40 * @param includeDescendants true if descendants of the start node are to be included. This will
41 * be false if the actual start was an element node, true if it was an attribute or namespace node
42 * (since the children of their parent follow the attribute or namespace in document order).
43 */
44
45 public FollowingIterator(TinyTree doc, TinyNodeImpl node,
46 NodeTest nodeTest, boolean includeDescendants) {
47 tree = doc;
48 test = nodeTest;
49 startNode = node;
50 this.includeDescendants = includeDescendants;
51 this.matcher = nodeTest.getMatcher(doc);
52 }
53
54 /*@Nullable*/
55 public NodeInfo next() {
56 if (pending != null) {
57 NodeInfo p = pending;
58 pending = null;
59 return p;
60 }
61 int nodeNr;
62 if (position <= 0) {
63 if (position < 0) {
64 // already at end
65 return null;
66 }
67 // first time call
68 nodeNr = startNode.nodeNr;
69
70 // skip the descendant nodes if any
71 if (includeDescendants) {
72 nodeNr++;
73 } else {
74 while (true) {
75 int nextSib = tree.next[nodeNr];
76 if (nextSib > nodeNr) {
77 nodeNr = nextSib;
78 break;
79 } else if (tree.depth[nextSib] == 0) {
80 current = null;
81 position = -1;
82 return null;
83 } else {
84 nodeNr = nextSib;
85 }
86 }
87 }
88 } else {
89 assert current != null;
90 TinyNodeImpl here;
91 if (current instanceof TinyTextualElement.TinyTextualElementText) {
92 here = (TinyNodeImpl)current.getParent();
93 } else {
94 here = (TinyNodeImpl)current;
95 }
96 nodeNr = here.nodeNr + 1;
97 }
98
99 while (true) {
100 if (tree.depth[nodeNr] == 0) {
101 current = null;
102 position = -1;
103 return null;
104 }
105 if (tree.nodeKind[nodeNr] == Type.TEXTUAL_ELEMENT) {
106 TinyTextualElement e = (TinyTextualElement)tree.getNode(nodeNr);
107 NodeInfo t = e.getTextNode();
108 if (matcher.test(nodeNr)) {
109 if (test.matchesNode(t)) {
110 pending = t;
111 }
112 position++;
113 return current = tree.getNode(nodeNr);
114 } else if (test.matchesNode(t)) {
115 position++;
116 return current = t;
117 }
118 } else if (matcher.test(nodeNr)) {
119 position++;
120 current = tree.getNode(nodeNr);
121 return current;
122 }
123 nodeNr++;
124 }
125 }
126
127 }
128