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