1 package com.icl.saxon.tinytree; 2 import com.icl.saxon.pattern.NodeTest; 3 import com.icl.saxon.om.AxisEnumeration; 4 import com.icl.saxon.om.NodeInfo; 5 6 10 11 final class AncestorEnumeration implements AxisEnumeration { 12 13 private int nextNodeNr; 14 private TinyDocumentImpl document; 15 private TinyNodeImpl node; 16 private NodeTest test; 17 private TinyNodeImpl first = null; 18 private boolean includeSelf; 19 private int last = -1; 20 21 public AncestorEnumeration(TinyDocumentImpl doc, TinyNodeImpl node, 22 NodeTest nodeTest, boolean includeSelf) { 23 document = doc; 24 test = nodeTest; 25 this.node = node; 26 this.includeSelf = includeSelf; 27 if (includeSelf && nodeTest.matches(node)) { 28 first = node; 29 } 30 31 34 TinyNodeImpl next = (TinyNodeImpl)node.getParent(); 35 nextNodeNr = next.nodeNr; 36 if (!nodeTest.matches(next)) { 37 advance(); 38 } 39 } 40 41 public boolean hasMoreElements() { 42 return first != null || nextNodeNr >= 0; 43 } 44 45 public NodeInfo nextElement() { 46 if (first!=null) { 47 NodeInfo n = first; 48 first = null; 49 return n; 50 } else { 51 TinyNodeImpl node = document.getNode(nextNodeNr); 52 advance(); 53 return node; 54 } 55 } 56 57 private void advance() { 58 int parentDepth = document.depth[nextNodeNr] - 1; 59 do { 60 do { 61 nextNodeNr--; 62 if (nextNodeNr<0) return; 63 } while (document.depth[nextNodeNr] > parentDepth); 64 if (test.matches(document.nodeType[nextNodeNr], 65 document.nameCode[nextNodeNr])) { 66 return; 67 } 68 parentDepth--; 69 } while ( nextNodeNr >= 0); 70 } 71 72 public boolean isSorted() { 73 return false; 74 } 75 76 public boolean isReverseSorted() { 77 return true; 78 } 79 80 public boolean isPeer() { 81 return false; 82 } 83 84 87 88 public int getLastPosition() { 89 if (last>=0) return last; 90 AncestorEnumeration enum = 91 new AncestorEnumeration(document, node, test, includeSelf); 92 last = 0; 93 while (enum.hasMoreElements()) { 94 enum.nextElement(); 95 last++; 96 } 97 return last; 98 } 99 100 101 } 102 103 104 105 | Popular Tags |