KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.om;
2
3 /**
4  * An implementation of the NamespaceDeclarations interface,
5  * based on encapsulating an array of namespace codes.
6  */

7
8 public class NamespaceDeclarationsImpl implements NamespaceDeclarations {
9
10     private NamePool namePool;
11     private int[] namespaceCodes;
12     private int used;
13
14     private static final int[] emptyArray = new int[0];
15
16     public NamespaceDeclarationsImpl() {};
17
18     /**
19      * Construct a set of namespace declarations
20      * @param pool the name pool
21      * @param codes an integer array holding the namespace codes. These
22      * codes are allocated by the name pool, and can be used to look up
23      * a prefix and uri in the name pool. If the array contains the integer
24      * -1, this acts as a terminator for the list. This is the format
25      * returned by the method {@link NodeInfo#getDeclaredNamespaces(int[])}.
26      * A value of null is equivalent to supplying an empty array.
27      */

28
29     public NamespaceDeclarationsImpl(NamePool pool, int[] codes) {
30         this.namePool = pool;
31         setNamespaceCodes(codes);
32     }
33
34     /**
35      * Set the name pool
36      */

37
38     public void setNamePool(NamePool pool) {
39         this.namePool = pool;
40     }
41
42     /**
43      * Set the namespace codes.
44      * @param codes an integer array holding the namespace codes. These
45      * codes are allocated by the name pool, and can be used to look up
46      * a prefix and uri in the name pool. If the array contains the integer
47      * -1, this acts as a terminator for the list. This is the format
48      * returned by the method {@link NodeInfo#getDeclaredNamespaces(int[])}.
49      * A value of null is equivalent to supplying an empty array.
50      */

51
52     public void setNamespaceCodes(int[] codes) {
53         if (codes == null) {
54             codes = emptyArray;
55         }
56         this.namespaceCodes = codes;
57         used = codes.length;
58         for (int i=0; i<codes.length; i++) {
59             if (codes[i] == -1) {
60                 used = i;
61                 break;
62             }
63         }
64     }
65
66     /**
67      * Get all the namespace codes, as an array.
68      *
69      * @param buffer a sacrificial array that the method is free to use to contain the result.
70      * May be null.
71      * @return an integer array containing namespace codes. The array may be filled completely
72      * with namespace codes, or it may be incompletely filled, in which case a -1 integer acts
73      * as a terminator.
74      */

75
76     public int[] getNamespaceCodes(int[] buffer) {
77         return namespaceCodes;
78     }
79
80     /**
81      * Get the number of declarations (and undeclarations) in this list.
82      */

83
84     public int getLength() {
85         return used;
86     }
87
88     /**
89      * Get the prefix of the n'th declaration (or undeclaration) in the list,
90      * counting from zero.
91      *
92      * @param index the index identifying which declaration is required.
93      * @return the namespace prefix. For a declaration or undeclaration of the
94      * default namespace, this is the zero-length string.
95      * @throws IndexOutOfBoundsException if the index is out of range.
96      */

97
98     public String JavaDoc getPrefix(int index) {
99         return namePool.getPrefixFromNamespaceCode(namespaceCodes[index]);
100     }
101
102     /**
103      * Get the namespace URI of the n'th declaration (or undeclaration) in the list,
104      * counting from zero.
105      *
106      * @param index the index identifying which declaration is required.
107      * @return the namespace URI. For a namespace undeclaration, this is the
108      * zero-length string.
109      * @throws IndexOutOfBoundsException if the index is out of range.
110      */

111
112     public String JavaDoc getURI(int index) {
113         return namePool.getURIFromNamespaceCode(namespaceCodes[index]);
114     }
115
116     /**
117      * Get the n'th declaration in the list in the form of a namespace code. Namespace
118      * codes can be translated into a prefix and URI by means of methods in the
119      * NamePool
120      *
121      * @param index the index identifying which declaration is required.
122      * @return the namespace code. This is an integer whose upper half indicates
123      * the prefix (0 represents the default namespace), and whose lower half indicates
124      * the URI (0 represents an undeclaration).
125      * @throws IndexOutOfBoundsException if the index is out of range.
126      * @see NamePool#getPrefixFromNamespaceCode(int)
127      * @see NamePool#getURIFromNamespaceCode(int)
128      */

129
130     public int getNamespaceCode(int index) {
131         return namespaceCodes[index];
132     }
133 }
134
135
136 //
137
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
138
// you may not use this file except in compliance with the License. You may obtain a copy of the
139
// License at http://www.mozilla.org/MPL/
140
//
141
// Software distributed under the License is distributed on an "AS IS" basis,
142
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
143
// See the License for the specific language governing rights and limitations under the License.
144
//
145
// The Original Code is: all this file.
146
//
147
// The Initial Developer of the Original Code is Michael H. Kay.
148
//
149
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
150
//
151
// Contributor(s): none.
152
//
153
Popular Tags