Debian Codegraph @

net/sf/saxon/tree/tiny/NamedChildIterator.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.trans.XPathException;
14 import net.sf.saxon.tree.iter.AxisIterator;
15 import net.sf.saxon.tree.iter.LookaheadIterator;
16 import net.sf.saxon.type.Type;
17
18 /**
19 * This class is a fast path iterator for the child axis in the TinyTree, where the axis specifies
20 * an explicit name test for the required element nodes.
21 */
22
23 final class NamedChildIterator implements AxisIterator, LookaheadIterator<NodeInfo>, AtomizedValueIterator<NodeInfo> {
24
25 private TinyTree tree;
26 private int nextNodeNr;
27 private int fingerprint;
28 private TinyNodeImpl startNode;
29 private boolean needToAdvance = false;
30
31 /**
32 * Return an enumeration over children or siblings of the context node
33 *
34 * @param tree The TinyTree containing the context node
35 * @param node The context node, the start point for the iteration
36 * @param fingerprint The fingerprint of the required element children
37 */
38
39 NamedChildIterator(TinyTree tree, TinyNodeImpl node, int fingerprint) {
40 this.tree = tree;
41 this.fingerprint = fingerprint;
42 this.startNode = node;
43 startNode = node;
44
45 // move to first child
46 // ASSERT: we don't invoke this code unless the node has children
47 nextNodeNr = node.nodeNr + 1;
48
49 // check if this matches the conditions
50 //if (nextNodeNr >= 0) {
51 if (((tree.nodeKind[nextNodeNr] & 0xf) != Type.ELEMENT) || (tree.nameCode[nextNodeNr] & 0xfffff) != fingerprint) {
52 needToAdvance = true;
53 }
54 //}
55 }
56
57 /**
58 * Return the next node in the sequence
59 *
60 * @return the next node, or null if the end of the sequence is reached
61 */
62
63 /*@Nullable*/
64 public NodeInfo next() {
65 if (needToAdvance) {
66 final int thisNode = nextNodeNr;
67 do {
68 nextNodeNr = tree.next[nextNodeNr];
69 if (nextNodeNr < thisNode) { // indicates we've got to the last sibling
70 nextNodeNr = -1;
71 needToAdvance = false;
72 return null;
73 }
74 } while (((tree.nameCode[nextNodeNr] & 0xfffff) != fingerprint) || ((tree.nodeKind[nextNodeNr] & 0xf) != Type.ELEMENT));
75 } else if (nextNodeNr == -1) {
76 return null;
77 }
78 needToAdvance = true;
79 TinyNodeImpl nextNode = tree.getNode(nextNodeNr);
80 nextNode.setParentNode(startNode);
81 return nextNode;
82 }
83
84 /**
85 * Deliver the atomic value that is next in the atomized result
86 *
87 * @return the next atomic value
88 * @throws XPathException
89 * if a failure occurs reading or atomizing the next value
90 */
91 public AtomicSequence nextAtomizedValue() throws XPathException {
92 if (needToAdvance) {
93 final int thisNode = nextNodeNr;
94 do {
95 nextNodeNr = tree.next[nextNodeNr];
96 if (nextNodeNr < thisNode) { // indicates we've got to the last sibling
97 nextNodeNr = -1;
98 needToAdvance = false;
99 return null;
100 }
101 } while (((tree.nameCode[nextNodeNr] & 0xfffff) != fingerprint) || (tree.nodeKind[nextNodeNr] & 0xf) != Type.ELEMENT);
102 } else if (nextNodeNr == -1) {
103 return null;
104 }
105 needToAdvance = true;
106 return tree.getTypedValueOfElement(nextNodeNr);
107
108 }
109
110 /**
111 * Test whether there are any more nodes to come. This method is used only when testing whether the
112 * current item is the last in the sequence. It's not especially efficient, but is more efficient than
113 * the alternative strategy which involves counting how many nodes there are in the sequence.
114 *
115 * @return true if there are more items in the sequence
116 */
117
118 public boolean hasNext() {
119 int n = nextNodeNr;
120 if (needToAdvance) {
121 final int thisNode = n;
122 do {
123 n = tree.next[n];
124 if (n < thisNode) {
125 return false;
126 }
127 } while ((tree.nodeKind[n] & 0xf) != Type.ELEMENT || (tree.nameCode[n] & 0xfffff) != fingerprint);
128 return true;
129 } else {
130 return n != -1;
131 }
132 }
133
134 public int getProperties() {
135 return LOOKAHEAD | ATOMIZING;
136 }
137
138 }
139