001: package net.sf.saxon.om;
002:
003: /**
004: * An implementation of the NamespaceDeclarations interface,
005: * based on encapsulating an array of namespace codes.
006: */
007:
008: public class NamespaceDeclarationsImpl implements NamespaceDeclarations {
009:
010: private NamePool namePool;
011: private int[] namespaceCodes;
012: private int used;
013:
014: private static final int[] emptyArray = new int[0];
015:
016: public NamespaceDeclarationsImpl() {
017: };
018:
019: /**
020: * Construct a set of namespace declarations
021: * @param pool the name pool
022: * @param codes an integer array holding the namespace codes. These
023: * codes are allocated by the name pool, and can be used to look up
024: * a prefix and uri in the name pool. If the array contains the integer
025: * -1, this acts as a terminator for the list. This is the format
026: * returned by the method {@link NodeInfo#getDeclaredNamespaces(int[])}.
027: * A value of null is equivalent to supplying an empty array.
028: */
029:
030: public NamespaceDeclarationsImpl(NamePool pool, int[] codes) {
031: this .namePool = pool;
032: setNamespaceCodes(codes);
033: }
034:
035: /**
036: * Set the name pool
037: */
038:
039: public void setNamePool(NamePool pool) {
040: this .namePool = pool;
041: }
042:
043: /**
044: * Set the namespace codes.
045: * @param codes an integer array holding the namespace codes. These
046: * codes are allocated by the name pool, and can be used to look up
047: * a prefix and uri in the name pool. If the array contains the integer
048: * -1, this acts as a terminator for the list. This is the format
049: * returned by the method {@link NodeInfo#getDeclaredNamespaces(int[])}.
050: * A value of null is equivalent to supplying an empty array.
051: */
052:
053: public void setNamespaceCodes(int[] codes) {
054: if (codes == null) {
055: codes = emptyArray;
056: }
057: this .namespaceCodes = codes;
058: used = codes.length;
059: for (int i = 0; i < codes.length; i++) {
060: if (codes[i] == -1) {
061: used = i;
062: break;
063: }
064: }
065: }
066:
067: /**
068: * Get all the namespace codes, as an array.
069: *
070: * @param buffer a sacrificial array that the method is free to use to contain the result.
071: * May be null.
072: * @return an integer array containing namespace codes. The array may be filled completely
073: * with namespace codes, or it may be incompletely filled, in which case a -1 integer acts
074: * as a terminator.
075: */
076:
077: public int[] getNamespaceCodes(int[] buffer) {
078: return namespaceCodes;
079: }
080:
081: /**
082: * Get the number of declarations (and undeclarations) in this list.
083: */
084:
085: public int getLength() {
086: return used;
087: }
088:
089: /**
090: * Get the prefix of the n'th declaration (or undeclaration) in the list,
091: * counting from zero.
092: *
093: * @param index the index identifying which declaration is required.
094: * @return the namespace prefix. For a declaration or undeclaration of the
095: * default namespace, this is the zero-length string.
096: * @throws IndexOutOfBoundsException if the index is out of range.
097: */
098:
099: public String getPrefix(int index) {
100: return namePool
101: .getPrefixFromNamespaceCode(namespaceCodes[index]);
102: }
103:
104: /**
105: * Get the namespace URI of the n'th declaration (or undeclaration) in the list,
106: * counting from zero.
107: *
108: * @param index the index identifying which declaration is required.
109: * @return the namespace URI. For a namespace undeclaration, this is the
110: * zero-length string.
111: * @throws IndexOutOfBoundsException if the index is out of range.
112: */
113:
114: public String getURI(int index) {
115: return namePool.getURIFromNamespaceCode(namespaceCodes[index]);
116: }
117:
118: /**
119: * Get the n'th declaration in the list in the form of a namespace code. Namespace
120: * codes can be translated into a prefix and URI by means of methods in the
121: * NamePool
122: *
123: * @param index the index identifying which declaration is required.
124: * @return the namespace code. This is an integer whose upper half indicates
125: * the prefix (0 represents the default namespace), and whose lower half indicates
126: * the URI (0 represents an undeclaration).
127: * @throws IndexOutOfBoundsException if the index is out of range.
128: * @see NamePool#getPrefixFromNamespaceCode(int)
129: * @see NamePool#getURIFromNamespaceCode(int)
130: */
131:
132: public int getNamespaceCode(int index) {
133: return namespaceCodes[index];
134: }
135: }
136:
137: //
138: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
139: // you may not use this file except in compliance with the License. You may obtain a copy of the
140: // License at http://www.mozilla.org/MPL/
141: //
142: // Software distributed under the License is distributed on an "AS IS" basis,
143: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
144: // See the License for the specific language governing rights and limitations under the License.
145: //
146: // The Original Code is: all this file.
147: //
148: // The Initial Developer of the Original Code is Michael H. Kay.
149: //
150: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
151: //
152: // Contributor(s): none.
153: //
|