| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 15 |
import java.util.function.IntPredicate; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
final class FollowingIterator implements AxisIterator { |
| 23 |
|
| 24 |
private TinyTree tree; |
| 25 |
private TinyNodeImpl startNode; |
| 26 |
private NodeInfo current; |
| 27 |
private NodeTest test; |
| 28 |
private boolean includeDescendants; |
| 29 |
int position = 0; |
| 30 |
private final IntPredicate matcher; |
| 31 |
private NodeInfo pending; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public FollowingIterator(TinyTree doc, TinyNodeImpl node, |
| 46 |
NodeTest nodeTest, boolean includeDescendants) { |
| 47 |
tree = doc; |
| 48 |
test = nodeTest; |
| 49 |
startNode = node; |
| 50 |
this.includeDescendants = includeDescendants; |
| 51 |
this.matcher = nodeTest.getMatcher(doc); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
public NodeInfo next() { |
| 56 |
if (pending != null) { |
| 57 |
NodeInfo p = pending; |
| 58 |
pending = null; |
| 59 |
return p; |
| 60 |
} |
| 61 |
int nodeNr; |
| 62 |
if (position <= 0) { |
| 63 |
if (position < 0) { |
| 64 |
|
| 65 |
return null; |
| 66 |
} |
| 67 |
|
| 68 |
nodeNr = startNode.nodeNr; |
| 69 |
|
| 70 |
|
| 71 |
if (includeDescendants) { |
| 72 |
nodeNr++; |
| 73 |
} else { |
| 74 |
while (true) { |
| 75 |
int nextSib = tree.next[nodeNr]; |
| 76 |
if (nextSib > nodeNr) { |
| 77 |
nodeNr = nextSib; |
| 78 |
break; |
| 79 |
} else if (tree.depth[nextSib] == 0) { |
| 80 |
current = null; |
| 81 |
position = -1; |
| 82 |
return null; |
| 83 |
} else { |
| 84 |
nodeNr = nextSib; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} else { |
| 89 |
assert current != null; |
| 90 |
TinyNodeImpl here; |
| 91 |
if (current instanceof TinyTextualElement.TinyTextualElementText) { |
| 92 |
here = (TinyNodeImpl)current.getParent(); |
| 93 |
} else { |
| 94 |
here = (TinyNodeImpl)current; |
| 95 |
} |
| 96 |
nodeNr = here.nodeNr + 1; |
| 97 |
} |
| 98 |
|
| 99 |
while (true) { |
| 100 |
if (tree.depth[nodeNr] == 0) { |
| 101 |
current = null; |
| 102 |
position = -1; |
| 103 |
return null; |
| 104 |
} |
| 105 |
if (tree.nodeKind[nodeNr] == Type.TEXTUAL_ELEMENT) { |
| 106 |
TinyTextualElement e = (TinyTextualElement)tree.getNode(nodeNr); |
| 107 |
NodeInfo t = e.getTextNode(); |
| 108 |
if (matcher.test(nodeNr)) { |
| 109 |
if (test.matchesNode(t)) { |
| 110 |
pending = t; |
| 111 |
} |
| 112 |
position++; |
| 113 |
return current = tree.getNode(nodeNr); |
| 114 |
} else if (test.matchesNode(t)) { |
| 115 |
position++; |
| 116 |
return current = t; |
| 117 |
} |
| 118 |
} else if (matcher.test(nodeNr)) { |
| 119 |
position++; |
| 120 |
current = tree.getNode(nodeNr); |
| 121 |
return current; |
| 122 |
} |
| 123 |
nodeNr++; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|