Debian Codegraph @

net/sf/saxon/tree/tiny/SiblingIterator.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.AtomicSequence;
11 import net.sf.saxon.om.AtomizedValueIterator;
12 import net.sf.saxon.om.NodeInfo;
13 import net.sf.saxon.pattern.NodeTest;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.iter.AxisIterator;
16 import net.sf.saxon.tree.iter.LookaheadIterator;
17 import net.sf.saxon.type.Type;
18 import net.sf.saxon.value.UntypedAtomicValue;
19 import net.sf.saxon.z.IntSetPredicate;
20
21 import java.util.function.IntPredicate;
22
23 /**
24 * This class supports both the child:: and following-sibling:: axes, which are
25 * identical except for the route to the first candidate node.
26 * It enumerates either the children or the following siblings of the specified node.
27 * In the case of children, the specified node must always
28 * be a node that has children: to ensure this, construct the iterator
29 * using NodeInfo#iterateAxis()
30 */
31
32 final class SiblingIterator implements AxisIterator, LookaheadIterator<NodeInfo>, AtomizedValueIterator<NodeInfo> {
33
34 // NOTE: have experimented with a dedicated iterator for the child axis matching against
35 // elements only, by fingerprint - no measurable improvement obtained.
36
37 private TinyTree tree;
38 private int nextNodeNr;
39 /*@Nullable*/ private NodeTest test;
40 private TinyNodeImpl startNode;
41 private TinyNodeImpl parentNode;
42 private boolean getChildren;
43 private boolean needToAdvance = false;
44 private final IntPredicate matcher;
45
46 /**
47 * Return an enumeration over children or siblings of the context node
48 *
49 * @param tree The TinyTree containing the context node
50 * @param node The context node, the start point for the iteration
51 * @param nodeTest Test that the selected nodes must satisfy, or null indicating
52 * that all nodes are selected
53 * @param getChildren True if children of the context node are to be returned, false
54 * if following siblings are required
55 */
56
57 SiblingIterator(/*@NotNull*/ TinyTree tree, /*@NotNull*/ TinyNodeImpl node,
58 /*@Nullable*/ NodeTest nodeTest, boolean getChildren) {
59 this.tree = tree;
60 test = nodeTest;
61 matcher = nodeTest == null ? IntSetPredicate.ALWAYS_TRUE : nodeTest.getMatcher(tree);
62 startNode = node;
63 this.getChildren = getChildren;
64 if (getChildren) { // child:: axis
65 parentNode = node;
66 // move to first child
67 // ASSERT: we don't invoke this code unless the node has children
68 nextNodeNr = node.nodeNr + 1;
69
70 } else { // following-sibling:: axis
71 parentNode = (TinyNodeImpl) node.getParent();
72 if (parentNode == null) {
73 nextNodeNr = -1;
74 } else {
75 // move to next sibling
76 nextNodeNr = tree.next[node.nodeNr];
77 while (tree.nodeKind[nextNodeNr] == Type.PARENT_POINTER) {
78 // skip dummy nodes
79 nextNodeNr = tree.next[nextNodeNr];
80 }
81 if (nextNodeNr < node.nodeNr) {
82 // if "next" pointer goes backwards, it's really an owner pointer from the last sibling
83 nextNodeNr = -1;
84 }
85 }
86 }
87
88 // check if this matches the conditions
89 if (nextNodeNr >= 0 && nodeTest != null) {
90 if (!matcher.test(nextNodeNr)) {
91 needToAdvance = true;
92 }
93 }
94 }
95
96 /**
97 * Return the next node in the sequence
98 *
99 * @return the next node, or null if the end of the sequence is reached
100 */
101
102 /*@Nullable*/
103 public NodeInfo next() {
104 if (needToAdvance) {
105 final int thisNode = nextNodeNr;
106 final int[] tNext = tree.next;
107 final NodeTest nTest = test;
108 if (nTest == null) {
109 do {
110 nextNodeNr = tNext[nextNodeNr];
111 } while (tree.nodeKind[nextNodeNr] == Type.PARENT_POINTER);
112 } else {
113 do {
114 nextNodeNr = tNext[nextNodeNr];
115 } while (nextNodeNr >= thisNode && !matcher.test(nextNodeNr));
116 }
117
118 if (nextNodeNr < thisNode) { // indicates we've got to the last sibling
119 nextNodeNr = -1;
120 needToAdvance = false;
121 return null;
122 }
123 }
124
125 if (nextNodeNr == -1) {
126 return null;
127 }
128 needToAdvance = true;
129 TinyNodeImpl nextNode = tree.getNode(nextNodeNr);
130 nextNode.setParentNode(parentNode);
131 return nextNode;
132 }
133
134 /**
135 * Deliver the atomic value that is next in the atomized result
136 *
137 * @return the next atomic value
138 * @throws net.sf.saxon.trans.XPathException
139 * if a failure occurs reading or atomizing the next value
140 */
141 public AtomicSequence nextAtomizedValue() throws XPathException {
142 if (needToAdvance) {
143 final int thisNode = nextNodeNr;
144 final NodeTest nTest = test;
145 final int[] tNext = tree.next;
146 if (nTest == null) {
147 do {
148 nextNodeNr = tNext[nextNodeNr];
149 } while (tree.nodeKind[nextNodeNr] == Type.PARENT_POINTER);
150 } else {
151 do {
152 nextNodeNr = tNext[nextNodeNr];
153 } while (nextNodeNr >= thisNode && !matcher.test(nextNodeNr));
154 }
155
156 if (nextNodeNr < thisNode) { // indicates we've got to the last sibling
157 nextNodeNr = -1;
158 needToAdvance = false;
159 return null;
160 }
161 }
162
163 if (nextNodeNr == -1) {
164 return null;
165 }
166 needToAdvance = true;
167 int kind = tree.nodeKind[nextNodeNr];
168 switch (kind) {
169 case Type.TEXT: {
170 return new UntypedAtomicValue(TinyTextImpl.getStringValue(tree, nextNodeNr));
171 }
172 case Type.WHITESPACE_TEXT: {
173 return new UntypedAtomicValue(WhitespaceTextImpl.getStringValueCS(tree, nextNodeNr));
174 }
175 case Type.ELEMENT:
176 case Type.TEXTUAL_ELEMENT: {
177 return tree.getTypedValueOfElement(nextNodeNr);
178 }
179 case Type.COMMENT:
180 case Type.PROCESSING_INSTRUCTION:
181 return tree.getAtomizedValueOfUntypedNode(nextNodeNr);
182 default:
183 throw new AssertionError("Unknown node kind on child axis");
184 }
185 }
186
187 /**
188 * Test whether there are any more nodes to come. This method is used only when testing whether the
189 * current item is the last in the sequence. It's not especially efficient, but is more efficient than
190 * the alternative strategy which involves counting how many nodes there are in the sequence.
191 *
192 * @return true if there are more items in the sequence
193 */
194
195 public boolean hasNext() {
196 int n = nextNodeNr;
197 if (needToAdvance) {
198 final NodeTest nTest = test;
199 final int[] tNext = tree.next;
200 if (nTest == null) {
201 do {
202 n = tNext[n];
203 } while (tree.nodeKind[n] == Type.PARENT_POINTER);
204 } else {
205 do {
206 n = tNext[n];
207 } while (n >= nextNodeNr && !matcher.test(n));
208 }
209
210 if (n < nextNodeNr) { // indicates we've got to the last sibling
211 return false;
212 }
213 }
214
215 return n != -1;
216 }
217
218 public int getProperties() {
219 return LOOKAHEAD | ATOMIZING;
220 }
221
222 }
223