| 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 |
import net.sf.saxon.type.UType; |
| 15 |
|
| 16 |
import java.util.function.IntPredicate; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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( TinyTree doc, 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 |
|
| 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 |
|
| 68 |
while (nextAncestorDepth >= 0 && tree.depth[nextNodeNr] == nextAncestorDepth) { |
| 69 |
if (nextAncestorDepth-- <= 0) { |
| 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 |
|