001: package net.sf.saxon.om;
002:
003: import java.util.List;
004: import java.util.Iterator;
005: import java.util.ArrayList;
006:
007: /**
008: * An implentation of NamespaceDeclarations that contains all the inscope namespaces
009: * made available by a NamespaceResolver.
010: */
011: public class NamespaceResolverAsDeclarations implements
012: NamespaceDeclarations {
013:
014: private NamePool pool;
015: private NamespaceResolver resolver;
016: private List prefixes;
017:
018: public NamespaceResolverAsDeclarations(NamePool pool,
019: NamespaceResolver resolver) {
020: this .pool = pool;
021: this .resolver = resolver;
022: prefixes = new ArrayList(10);
023: Iterator iter = resolver.iteratePrefixes();
024: while (iter.hasNext()) {
025: prefixes.add(iter.next());
026: }
027: }
028:
029: /**
030: * Get the number of declarations (and undeclarations) in this list.
031: */
032:
033: public int getLength() {
034: return prefixes.size();
035: }
036:
037: /**
038: * Get the prefix of the n'th declaration (or undeclaration) in the list,
039: * counting from zero.
040: *
041: * @param index the index identifying which declaration is required.
042: * @return the namespace prefix. For a declaration or undeclaration of the
043: * default namespace, this is the zero-length string.
044: * @throws IndexOutOfBoundsException if the index is out of range.
045: */
046:
047: public String getPrefix(int index) {
048: return (String) prefixes.get(index);
049: }
050:
051: /**
052: * Get the namespace URI of the n'th declaration (or undeclaration) in the list,
053: * counting from zero.
054: *
055: * @param index the index identifying which declaration is required.
056: * @return the namespace URI. For a namespace undeclaration, this is the
057: * zero-length string.
058: * @throws IndexOutOfBoundsException if the index is out of range.
059: */
060:
061: public String getURI(int index) {
062: return resolver.getURIForPrefix((String) prefixes.get(index),
063: true);
064: }
065:
066: /**
067: * Get the n'th declaration in the list in the form of a namespace code. Namespace
068: * codes can be translated into a prefix and URI by means of methods in the
069: * NamePool
070: *
071: * @param index the index identifying which declaration is required.
072: * @return the namespace code. This is an integer whose upper half indicates
073: * the prefix (0 represents the default namespace), and whose lower half indicates
074: * the URI (0 represents an undeclaration).
075: * @throws IndexOutOfBoundsException if the index is out of range.
076: * @see NamePool#getPrefixFromNamespaceCode(int)
077: * @see NamePool#getURIFromNamespaceCode(int)
078: */
079:
080: public int getNamespaceCode(int index) {
081: String prefix = getPrefix(index);
082: String uri = getURI(index);
083: return pool.allocateNamespaceCode(prefix, uri);
084: }
085:
086: /**
087: * Get all the namespace codes, as an array.
088: *
089: * @param buffer a sacrificial array that the method is free to use to contain the result.
090: * May be null.
091: * @return an integer array containing namespace codes. The array may be filled completely
092: * with namespace codes, or it may be incompletely filled, in which case a -1 integer acts
093: * as a terminator.
094: */
095:
096: public int[] getNamespaceCodes(int[] buffer) {
097: if (buffer.length < getLength()) {
098: buffer = new int[getLength()];
099: }
100: for (int i = 0; i < getLength(); i++) {
101: buffer[i] = getNamespaceCode(i);
102: }
103: return buffer;
104: }
105: }
106:
107: //
108: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
109: // you may not use this file except in compliance with the License. You may obtain a copy of the
110: // License at http://www.mozilla.org/MPL/
111: //
112: // Software distributed under the License is distributed on an "AS IS" basis,
113: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
114: // See the License for the specific language governing rights and limitations under the License.
115: //
116: // The Original Code is: all this file.
117: //
118: // The Initial Developer of the Original Code is Michael H. Kay.
119: //
120: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
121: //
122: // Contributor(s): none.
123: //
|