1 package com.icl.saxon.tinytree; 2 import com.icl.saxon.om.NodeInfo; 3 import com.icl.saxon.om.NamePool; 4 import com.icl.saxon.pattern.NodeTest; 5 import com.icl.saxon.pattern.NodeTypeTest; 6 import com.icl.saxon.om.Namespace; 7 import com.icl.saxon.om.AxisEnumeration; 8 import java.util.Vector; 9 10 13 14 final class NamespaceEnumeration implements AxisEnumeration { 15 16 private TinyDocumentImpl document; 17 private TinyElementImpl element; 18 private NamePool pool; 19 private int owner; 20 private int currentElement; 21 private int index; 22 private Vector list = new Vector(); 23 private NodeTest nodeTest; 24 private int last = -1; 25 private int xmlNamespace; 26 27 31 32 protected NamespaceEnumeration(TinyElementImpl node, NodeTest nodeTest) { 33 element = node; 35 owner = node.nodeNr; 36 document = (TinyDocumentImpl)node.getDocumentRoot(); 37 pool = document.getNamePool(); 38 currentElement = owner; 39 index = document.length[currentElement]; this.nodeTest = nodeTest; 41 xmlNamespace = pool.allocate("", "", "xml"); 42 advance(); 43 } 44 45 private void advance() { 46 if (index == 0) { 48 index = -1; 49 return; 50 } else if (index > 0) { 51 while (index < document.numberOfNamespaces && 52 document.namespaceParent[index] == currentElement) { 53 54 int nsCode = document.namespaceCode[index]; 55 56 59 if (nsCode == Namespace.NULL_CODE) { 60 list.addElement(new Short((short)0)); 61 } else { 62 if (matches(nsCode)) { 63 short prefixCode = (short)(nsCode>>16); 64 65 int max = list.size(); 66 boolean duplicate = false; 67 68 for (int j=0; j<max; ) { 70 short nsj = ((Short)(list.elementAt(j++))).shortValue(); 71 if (nsj==prefixCode) { 72 duplicate = true; 73 break; 74 } 75 } 76 if (!duplicate) { 77 list.addElement(new Short(prefixCode)); 78 return; 79 } 80 } 81 } 82 83 index++; 84 } 85 } 86 87 NodeInfo parent = document.getNode(currentElement).getParent(); 88 if (parent.getNodeType()==NodeInfo.ROOT) { 89 if (nodeTest.matches(NodeInfo.NAMESPACE, xmlNamespace)) { 90 index = 0; 91 } else { 92 index = -1; 93 } 94 } else { 95 currentElement = ((TinyElementImpl)parent).nodeNr; 96 index = document.length[currentElement]; advance(); 98 } 99 100 } 101 102 private boolean matches(int nsCode) { 103 if (nodeTest instanceof NodeTypeTest && nodeTest.getNodeType()==NodeInfo.NAMESPACE) { 104 return true; 106 } else { 107 int nameCode = pool.allocate("", "", pool.getPrefixFromNamespaceCode(nsCode)); 108 return nodeTest.matches(NodeInfo.NAMESPACE, nameCode); 109 } 110 } 111 112 public boolean hasMoreElements() { 113 return index>=0; 114 } 115 116 public NodeInfo nextElement() { 117 TinyNamespaceImpl nsi = document.getNamespaceNode(index); 119 nsi.setParentNode(owner); 120 advance(); 121 return nsi; 122 } 123 124 public boolean isSorted() { 125 return false; 126 } 127 128 public boolean isReverseSorted() { 129 return false; 130 } 131 132 public boolean isPeer() { 133 return true; 134 } 135 136 139 140 public int getLastPosition() { 141 if (last >= 0) return last; 142 NamespaceEnumeration enum = 143 new NamespaceEnumeration(element, nodeTest); 144 last = 0; 145 while (enum.hasMoreElements()) { 146 enum.nextElement(); 147 last++; 148 } 149 return last; 150 } 151 } 152 153 154 155 | Popular Tags |