| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.tiny; |
| 9 |
|
| 10 |
import net.sf.saxon.om.AxisInfo; |
| 11 |
import net.sf.saxon.om.NodeInfo; |
| 12 |
import net.sf.saxon.pattern.NodeTest; |
| 13 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 14 |
import net.sf.saxon.type.Type; |
| 15 |
import net.sf.saxon.type.UType; |
| 16 |
|
| 17 |
import java.util.function.IntPredicate; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
final class GraftingDescendantIterator implements AxisIterator { |
| 27 |
|
| 28 |
private final TinyTree tree; |
| 29 |
private final TinyNodeImpl startNode; |
| 30 |
private int nextNodeNr; |
| 31 |
private final int startDepth; |
| 32 |
private final NodeTest test; |
| 33 |
private final IntPredicate matcher; |
| 34 |
private AxisIterator nestedIterator; |
| 35 |
private NodeInfo pending = null; |
| 36 |
private boolean includeTextNodes; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
GraftingDescendantIterator( TinyTree doc, TinyNodeImpl node, NodeTest nodeTest) { |
| 47 |
tree = doc; |
| 48 |
startNode = node; |
| 49 |
test = nodeTest; |
| 50 |
nextNodeNr = node.nodeNr; |
| 51 |
startDepth = doc.depth[nextNodeNr]; |
| 52 |
matcher = nodeTest.getMatcher(doc); |
| 53 |
includeTextNodes = nodeTest.getUType().overlaps(UType.TEXT); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
public NodeInfo next() { |
| 58 |
while (true) { |
| 59 |
if (pending != null) { |
| 60 |
NodeInfo p = pending; |
| 61 |
pending = null; |
| 62 |
return p; |
| 63 |
} |
| 64 |
if (nestedIterator != null) { |
| 65 |
NodeInfo nested = nestedIterator.next(); |
| 66 |
if (nested != null) { |
| 67 |
return nested; |
| 68 |
} else { |
| 69 |
nestedIterator = null; |
| 70 |
} |
| 71 |
} |
| 72 |
nextNodeNr++; |
| 73 |
try { |
| 74 |
if (tree.depth[nextNodeNr] <= startDepth) { |
| 75 |
nextNodeNr = -1; |
| 76 |
return null; |
| 77 |
} |
| 78 |
if (tree.nodeKind[nextNodeNr] == Type.EXTERNAL_NODE_REFERENCE) { |
| 79 |
byte axis = nextNodeNr == startNode.nodeNr ? AxisInfo.DESCENDANT : AxisInfo.DESCENDANT_OR_SELF; |
| 80 |
nestedIterator = tree.externalNodes.get(tree.alpha[nextNodeNr]) |
| 81 |
.iterateAxis(axis, test); |
| 82 |
continue; |
| 83 |
} |
| 84 |
if (includeTextNodes && tree.nodeKind[nextNodeNr] == Type.TEXTUAL_ELEMENT) { |
| 85 |
pending = ((TinyTextualElement) tree.getNode(nextNodeNr)).getTextNode(); |
| 86 |
} |
| 87 |
} catch (ArrayIndexOutOfBoundsException e) { |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
nextNodeNr = -1; |
| 93 |
return null; |
| 94 |
} |
| 95 |
if (matcher.test(nextNodeNr)) { |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return tree.getNode(nextNodeNr); |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|