| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
final class SiblingIterator implements AxisIterator, LookaheadIterator<NodeInfo>, AtomizedValueIterator<NodeInfo> { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private TinyTree tree; |
| 38 |
private int nextNodeNr; |
| 39 |
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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
SiblingIterator( TinyTree tree, TinyNodeImpl node, |
| 58 |
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) { |
| 65 |
parentNode = node; |
| 66 |
|
| 67 |
|
| 68 |
nextNodeNr = node.nodeNr + 1; |
| 69 |
|
| 70 |
} else { |
| 71 |
parentNode = (TinyNodeImpl) node.getParent(); |
| 72 |
if (parentNode == null) { |
| 73 |
nextNodeNr = -1; |
| 74 |
} else { |
| 75 |
|
| 76 |
nextNodeNr = tree.next[node.nodeNr]; |
| 77 |
while (tree.nodeKind[nextNodeNr] == Type.PARENT_POINTER) { |
| 78 |
|
| 79 |
nextNodeNr = tree.next[nextNodeNr]; |
| 80 |
} |
| 81 |
if (nextNodeNr < node.nodeNr) { |
| 82 |
|
| 83 |
nextNodeNr = -1; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
if (nextNodeNr >= 0 && nodeTest != null) { |
| 90 |
if (!matcher.test(nextNodeNr)) { |
| 91 |
needToAdvance = true; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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) { |
| 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 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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) { |
| 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 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 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) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return n != -1; |
| 216 |
} |
| 217 |
|
| 218 |
public int getProperties() { |
| 219 |
return LOOKAHEAD | ATOMIZING; |
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
|