Debian Codegraph @

net/sf/saxon/tree/util/SteppingNavigator.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2017 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.util;
9
10 import net.sf.saxon.pattern.*;
11 import net.sf.saxon.tree.iter.AxisIterator;
12 import net.sf.saxon.type.Type;
13
14
15 /**
16 * The SteppingNavigator is a utility class containing methods to assist with navigating a tree whose nodes
17 * implement the {@link SteppingNode} interface
18 */
19
20 public abstract class SteppingNavigator {
21
22 /**
23 * Get the next following node after a given node
24 *
25 * @param start the starting node
26 * @param anchor the node whose descendants are being scanned; the scan terminates when
27 * the anchor node is reached
28 * @return the next node in document order after the starting node, excluding attributes and namespaces;
29 * or null if no such node is found
30 */
31
32 static <N extends SteppingNode<N>> N getFollowingNode(N start, N anchor) {
33 N nodei = start.getFirstChild();
34 if (nodei != null) {
35 return nodei;
36 }
37 if (start.isSameNodeInfo(anchor)) {
38 return null;
39 }
40 nodei = start;
41 N parenti = start.getParent();
42 do {
43 nodei = nodei.getNextSibling();
44 if (nodei != null) {
45 return nodei;
46 } else if (parenti.isSameNodeInfo(anchor)) {
47 return null;
48 }
49 nodei = parenti;
50 parenti = parenti.getParent();
51 } while (parenti != null);
52
53 return null;
54 }
55
56 /**
57 * Interface representing a function to step from one node to another within a tree
58 */
59
60 private interface Stepper<N extends SteppingNode<N>> {
61 /**
62 * Step from one node to another
63 *
64 * @param node the start node
65 * @return the end node
66 */
67 N step(N node);
68 }
69
70 /**
71 * Stepper that steps from one node in a document to the next node in document order,
72 * excluding attribute and namespace nodes, returning null when the root of the subtree
73 * is reached.
74 */
75
76 private static class FollowingNodeStepper<N extends SteppingNode<N>> implements Stepper<N> {
77
78 N anchor;
79
80 /**
81 * Create a stepper to step successively through all nodes in a subtree
82 *
83 * @param anchor the root of the subtree, marking the end point of the iteration
84 */
85
86 FollowingNodeStepper(N anchor) {
87 this.anchor = anchor;
88 }
89
90 public N step(N node) {
91 return getFollowingNode(node, anchor);
92 }
93 }
94
95 /**
96 * Stepper that steps from one node in a document to the next node in document order,
97 * excluding attribute and namespace nodes, returning null when the root of the subtree
98 * is reached, and including only nodes that match a specified node test
99 */
100
101 private static class FollowingFilteredNodeStepper<N extends SteppingNode<N>> implements Stepper<N> {
102
103 N anchor;
104 NodeTest test;
105
106 /**
107 * Create a stepper to step successively through selected nodes in a subtree
108 *
109 * @param anchor the root of the subtree, marking the end point of the iteration
110 * @param test the test that returned nodes must satisfy
111 */
112
113 FollowingFilteredNodeStepper(N anchor, NodeTest test) {
114 this.anchor = anchor;
115 this.test = test;
116 }
117
118 public N step(N node) {
119 do {
120 node = getFollowingNode(node, anchor);
121 } while (node != null && !test.matchesNode(node));
122 return node;
123 }
124 }
125
126 /**
127 * Stepper that steps from one element in a document to the next element in document order,
128 * excluding attribute and namespace nodes, returning null when the root of the subtree
129 * is reached, and including only elements, with optional constraints on the namespace URI
130 * and/or local name.
131 */
132
133 private static class FollowingElementStepper<N extends SteppingNode<N>> implements Stepper<N> {
134
135 N anchor;
136 String uri;
137 String local;
138
139 /**
140 * Create a stepper to step successively through selected elements in a subtree
141 *
142 * @param anchor the root of the subtree, marking the end point of the iteration
143 * @param uri either null, or a namespace URI which the selected elements must match
144 * @param local either null, or a local name which the selected elements must match
145 */
146
147 FollowingElementStepper(N anchor, String uri, String local) {
148 this.anchor = anchor;
149 this.uri = uri;
150 this.local = local;
151 }
152
153 public N step(N node) {
154 return node.getSuccessorElement(anchor, uri, local);
155 }
156 }
157
158 /**
159 * Stepper that steps from one element in a document to the next element in document order,
160 * excluding attribute and namespace nodes, returning null when the root of the subtree
161 * is reached, and including only elements, with a constraint on the fingerprint of the element
162 */
163
164 private static class FollowingFingerprintedElementStepper<N extends SteppingNode<N>> implements Stepper<N> {
165
166 N anchor;
167 int fingerprint;
168
169 /**
170 * Create a stepper to step successively through selected elements in a subtree
171 *
172 * @param anchor the root of the subtree, marking the end point of the iteration
173 * @param fingerprint a fingerprint which selected elements must match
174 */
175
176 FollowingFingerprintedElementStepper(N anchor, int fingerprint) {
177 this.anchor = anchor;
178 this.fingerprint = fingerprint;
179 }
180
181 public N step(N node) {
182 do {
183 node = getFollowingNode(node, anchor);
184 } while (node != null && node.getFingerprint() != fingerprint);
185 return node;
186 }
187 }
188
189
190 /**
191 * An iterator over the descendant or descendant-or-self axis
192 */
193
194 public static class DescendantAxisIterator<N extends SteppingNode<N>> implements AxisIterator {
195
196 private N start;
197 private N current;
198
199 private Stepper<N> stepper;
200
201 /**
202 * Create an iterator over the descendant or descendant-or-self axis
203 *
204 * @param start the root of the subtree whose descendants are required
205 * @param includeSelf true if this is the descendant-or-self axis
206 * @param test the node-test that selected nodes must satisfy
207 */
208
209 public DescendantAxisIterator(N start, boolean includeSelf, NodeTest test) {
210 this.start = start;
211
212 if (!(includeSelf && test.matchesNode(start))) {
213 // initialize currNode to the start node if and only if this is NOT a descendant-or-self scan
214 current = start;
215 }
216
217 if (test == null || test == AnyNodeTest.getInstance()) {
218 stepper = new FollowingNodeStepper<>(start);
219 } else if (test instanceof NameTest) {
220 if (test.getPrimitiveType() == Type.ELEMENT) {
221 NameTest nt = (NameTest) test;
222 if (start.hasFingerprint()) {
223 stepper = new FollowingFingerprintedElementStepper<>(start, nt.getFingerprint());
224 } else {
225 stepper = new FollowingElementStepper<>(start, nt.getNamespaceURI(), nt.getLocalPart());
226 }
227 } else {
228 stepper = new FollowingFilteredNodeStepper<>(start, test);
229 }
230 } else if (test instanceof NodeKindTest) {
231 if (test.getPrimitiveType() == Type.ELEMENT) {
232 stepper = new FollowingElementStepper<>(start, null, null);
233 } else {
234 stepper = new FollowingFilteredNodeStepper<>(start, test);
235 }
236 } else if (test instanceof LocalNameTest) {
237 if (test.getPrimitiveType() == Type.ELEMENT) {
238 LocalNameTest nt = (LocalNameTest) test;
239 stepper = new FollowingElementStepper<>(start, null, nt.getLocalName());
240 } else {
241 stepper = new FollowingFilteredNodeStepper<>(start, test);
242 }
243 } else if (test instanceof NamespaceTest) {
244 if (test.getPrimitiveType() == Type.ELEMENT) {
245 NamespaceTest nt = (NamespaceTest) test;
246 stepper = new FollowingElementStepper<>(start, nt.getNamespaceURI(), null);
247 } else {
248 stepper = new FollowingFilteredNodeStepper<>(start, test);
249 }
250 } else {
251 stepper = new FollowingFilteredNodeStepper<>(start, test);
252 }
253 }
254
255
256 public N next() {
257 if (current == null) {
258 // implies includeSelf: first time round, return the start node
259 current = start;
260 return start;
261 }
262 N curr = stepper.step(current);
263
264 return current = curr;
265 }
266
267 public void close() {
268 }
269
270 public int getProperties() {
271 return 0;
272 }
273 }
274 }
275