KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > tinytree > NamespaceEnumeration


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 /**
11 * Enumeration of the namespace nodes of an element
12 */

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     /**
28     * Constructor. Note: this constructor will only be called if the owning
29     * node is an element. Otherwise, an EmptyEnumeration will be returned
30     */

31
32     protected NamespaceEnumeration(TinyElementImpl node, NodeTest nodeTest) {
33         // System.err.println("new NS enum");
34
element = node;
35         owner = node.nodeNr;
36         document = (TinyDocumentImpl)node.getDocumentRoot();
37         pool = document.getNamePool();
38         currentElement = owner;
39         index = document.length[currentElement]; // by convention
40
this.nodeTest = nodeTest;
41         xmlNamespace = pool.allocate("", "", "xml");
42         advance();
43     }
44
45     private void advance() {
46         // System.err.println("NSEnum advance index=" + index + " max= " + document.numberOfNamespaces);
47
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                 // don't return a namespace undeclaration (xmlns=""), but add it to the list
57
// of prefixes encountered, to suppress outer xmlns="xyz" declarations
58

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                         // Don't add a node if the prefix has been previously encountered
69
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]; // by convention
97
advance();
98         }
99         
100     }
101
102     private boolean matches(int nsCode) {
103         if (nodeTest instanceof NodeTypeTest && nodeTest.getNodeType()==NodeInfo.NAMESPACE) {
104             // fast path when selecting namespace::*
105
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         // System.err.println("next NS, index = " + index);
118
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     /**
137     * Get the last position, that is the number of nodes in the enumeration
138     */

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 //
156
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
157
// you may not use this file except in compliance with the License. You may obtain a copy of the
158
// License at http://www.mozilla.org/MPL/
159
//
160
// Software distributed under the License is distributed on an "AS IS" basis,
161
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
162
// See the License for the specific language governing rights and limitations under the License.
163
//
164
// The Original Code is: all this file.
165
//
166
// The Initial Developer of the Original Code is
167
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
168
//
169
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
170
//
171
// Contributor(s): none.
172
//
173
Popular Tags