Debian Codegraph @

net/sf/saxon/tree/tiny/PrecedingIterator.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 import net.sf.saxon.type.UType;
15
16 import java.util.function.IntPredicate;
17
18 /**
19 * Enumerate all the nodes on the preceding axis from a given start node.
20 * The calling code ensures that the start node is not a root, attribute,
21 * or namespace node. As well as the standard XPath preceding axis, this
22 * class also implements a Saxon-specific "preceding-or-ancestor" axis
23 * which returns ancestor nodes as well as preceding nodes. This is used
24 * when performing xsl:number level="any".
25 */
26
27 final class PrecedingIterator implements AxisIterator {
28
29 private TinyTree tree;
30 private NodeInfo current;
31 private int nextAncestorDepth;
32 private boolean includeAncestors;
33 private final IntPredicate matcher;
34 private NodeInfo pending = null;
35 private NodeTest nodeTest;
36 private boolean matchesTextNodes;
37
38 public PrecedingIterator(/*@NotNull*/ TinyTree doc, /*@NotNull*/ TinyNodeImpl node,
39 NodeTest nodeTest, boolean includeAncestors) {
40
41 this.includeAncestors = includeAncestors;
42 tree = doc;
43 current = node;
44 nextAncestorDepth = doc.depth[node.nodeNr] - 1;
45 this.nodeTest = nodeTest;
46 this.matcher = nodeTest.getMatcher(doc);
47 matchesTextNodes = nodeTest.getUType().overlaps(UType.TEXT);
48 }
49
50 /*@Nullable*/
51 public NodeInfo next() {
52 if (pending != null) {
53 current = pending;
54 pending = null;
55 return current;
56 }
57 if (current == null) {
58 return null;
59 }
60 if (current instanceof TinyTextualElement.TinyTextualElementText) {
61 current = current.getParent();
62 }
63 int nextNodeNr = ((TinyNodeImpl) current).nodeNr;
64 while (true) {
65 if (!includeAncestors) {
66 nextNodeNr--;
67 // skip over ancestor elements
68 while (nextAncestorDepth >= 0 && tree.depth[nextNodeNr] == nextAncestorDepth) {
69 if (nextAncestorDepth-- <= 0) { // bug 1121528
70 current = null;
71 return null;
72 }
73 nextNodeNr--;
74 }
75 } else {
76 if (tree.depth[nextNodeNr] == 0) {
77 current = null;
78 return null;
79 } else {
80 nextNodeNr--;
81 }
82 }
83 if (matchesTextNodes && tree.nodeKind[nextNodeNr] == Type.TEXTUAL_ELEMENT) {
84 TinyTextualElement element = (TinyTextualElement)tree.getNode(nextNodeNr);
85 TinyTextualElement.TinyTextualElementText text = element.getTextNode();
86 if (nodeTest.matchesNode(text)) {
87 if (nodeTest.matchesNode(element)) {
88 pending = element;
89 }
90 return current = text;
91 } else if (nodeTest.matchesNode(element)) {
92 return current = element;
93 }
94 } else {
95 if (matcher.test(nextNodeNr)) {
96 current = tree.getNode(nextNodeNr);
97 return current;
98 }
99 if (tree.depth[nextNodeNr] == 0) {
100 current = null;
101 return null;
102 }
103 }
104 }
105 }
106
107 }
108