KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > om > NamespaceResolverAsDeclarations


1 package net.sf.saxon.om;
2
3 import java.util.List JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.ArrayList JavaDoc;
6
7 /**
8  * An implentation of NamespaceDeclarations that contains all the inscope namespaces
9  * made available by a NamespaceResolver.
10  */

11 public class NamespaceResolverAsDeclarations implements NamespaceDeclarations {
12
13     private NamePool pool;
14     private NamespaceResolver resolver;
15     private List JavaDoc prefixes;
16
17     public NamespaceResolverAsDeclarations(NamePool pool, NamespaceResolver resolver) {
18         this.pool = pool;
19         this.resolver = resolver;
20         prefixes = new ArrayList JavaDoc(10);
21         Iterator JavaDoc iter = resolver.iteratePrefixes();
22         while (iter.hasNext()) {
23             prefixes.add(iter.next());
24         }
25     }
26
27     /**
28      * Get the number of declarations (and undeclarations) in this list.
29      */

30
31     public int getLength() {
32         return prefixes.size();
33     }
34
35     /**
36      * Get the prefix of the n'th declaration (or undeclaration) in the list,
37      * counting from zero.
38      *
39      * @param index the index identifying which declaration is required.
40      * @return the namespace prefix. For a declaration or undeclaration of the
41      * default namespace, this is the zero-length string.
42      * @throws IndexOutOfBoundsException if the index is out of range.
43      */

44
45     public String JavaDoc getPrefix(int index) {
46         return (String JavaDoc)prefixes.get(index);
47     }
48
49     /**
50      * Get the namespace URI of the n'th declaration (or undeclaration) in the list,
51      * counting from zero.
52      *
53      * @param index the index identifying which declaration is required.
54      * @return the namespace URI. For a namespace undeclaration, this is the
55      * zero-length string.
56      * @throws IndexOutOfBoundsException if the index is out of range.
57      */

58
59     public String JavaDoc getURI(int index) {
60         return resolver.getURIForPrefix((String JavaDoc)prefixes.get(index), true);
61     }
62
63     /**
64      * Get the n'th declaration in the list in the form of a namespace code. Namespace
65      * codes can be translated into a prefix and URI by means of methods in the
66      * NamePool
67      *
68      * @param index the index identifying which declaration is required.
69      * @return the namespace code. This is an integer whose upper half indicates
70      * the prefix (0 represents the default namespace), and whose lower half indicates
71      * the URI (0 represents an undeclaration).
72      * @throws IndexOutOfBoundsException if the index is out of range.
73      * @see NamePool#getPrefixFromNamespaceCode(int)
74      * @see NamePool#getURIFromNamespaceCode(int)
75      */

76
77     public int getNamespaceCode(int index) {
78         String JavaDoc prefix = getPrefix(index);
79         String JavaDoc uri = getURI(index);
80         return pool.allocateNamespaceCode(prefix, uri);
81     }
82
83     /**
84      * Get all the namespace codes, as an array.
85      *
86      * @param buffer a sacrificial array that the method is free to use to contain the result.
87      * May be null.
88      * @return an integer array containing namespace codes. The array may be filled completely
89      * with namespace codes, or it may be incompletely filled, in which case a -1 integer acts
90      * as a terminator.
91      */

92
93     public int[] getNamespaceCodes(int[] buffer) {
94         if (buffer.length < getLength()) {
95             buffer = new int[getLength()];
96         }
97         for (int i=0; i<getLength(); i++) {
98             buffer[i] = getNamespaceCode(i);
99         }
100         return buffer;
101     }
102 }
103
104 //
105
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
106
// you may not use this file except in compliance with the License. You may obtain a copy of the
107
// License at http://www.mozilla.org/MPL/
108
//
109
// Software distributed under the License is distributed on an "AS IS" basis,
110
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
111
// See the License for the specific language governing rights and limitations under the License.
112
//
113
// The Original Code is: all this file.
114
//
115
// The Initial Developer of the Original Code is Michael H. Kay.
116
//
117
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
118
//
119
// Contributor(s): none.
120
//
Popular Tags