001: package net.sf.saxon.instruct;
002:
003: import net.sf.saxon.om.NamePool;
004: import net.sf.saxon.om.NamespaceConstant;
005: import net.sf.saxon.om.NamespaceResolver;
006:
007: import java.io.Serializable;
008: import java.util.ArrayList;
009: import java.util.Iterator;
010:
011: /**
012: * An object representing a list of Namespaces. Used when the namespace
013: * controller in the stylesheet needs to be kept for use at run-time. The list of namespaces
014: * is maintained in the form of numeric prefix/uri codes, which are only meaningful
015: * in the controller of a name pool; however, in order to save space, the NamespaceContext
016: * object does not keep a reference to the namepool, it requires this to be supplied
017: * by the caller.
018: */
019:
020: public final class SavedNamespaceContext implements Serializable,
021: NamespaceResolver {
022:
023: // TODO: static context can't vary within an XPath expression. Therefore, save the
024: // NamespaceContext at the outermost expression level if any subexpression needs it.
025: // (Or put a namespace context expression on the expression tree, which has no effect
026: // at run-time, but is available to descendant expressions).
027:
028: private int[] namespaceCodes;
029: private NamePool namePool;
030:
031: /**
032: * Create a NamespaceContext object
033: * @param nscodes an array of namespace codes. Each namespace code is an integer
034: * in which the first 16 bits represent the prefix (zero if it's the default namespace)
035: * and the next 16 bits represent the uri. These are codes held in the NamePool. The
036: * list will be searched from the "high" end.
037: */
038:
039: public SavedNamespaceContext(int[] nscodes, NamePool pool) {
040: namespaceCodes = nscodes;
041: namePool = pool;
042: }
043:
044: /**
045: * Get the list of in-scope namespaces held in this NamespaceContext
046: * @return the list of namespaces
047: */
048:
049: public int[] getNamespaceCodes() {
050: return namespaceCodes;
051: }
052:
053: /**
054: * Get the namespace URI corresponding to a given prefix. Return null
055: * if the prefix is not in scope.
056: * @param prefix the namespace prefix
057: * @param useDefault true if the default namespace is to be used when the
058: * prefix is ""
059: * @return the uri for the namespace, or null if the prefix is not in scope
060: */
061:
062: public String getURIForPrefix(String prefix, boolean useDefault) {
063:
064: if (prefix.equals("") && !useDefault) {
065: return "";
066: }
067:
068: if (prefix.equals("xml")) {
069: return NamespaceConstant.XML;
070: }
071:
072: for (int i = namespaceCodes.length - 1; i >= 0; i--) {
073: if (namePool.getPrefixFromNamespaceCode(namespaceCodes[i])
074: .equals(prefix)) {
075: return namePool
076: .getURIFromNamespaceCode(namespaceCodes[i]);
077: }
078: }
079:
080: if (prefix.equals("") && useDefault) {
081: // use the "default default namespace" - namely ""
082: return "";
083: } else {
084: return null;
085: }
086: }
087:
088: /**
089: * Get an iterator over all the prefixes declared in this namespace context. This will include
090: * the default namespace (prefix="") and the XML namespace where appropriate
091: */
092:
093: public Iterator iteratePrefixes() {
094: ArrayList prefixes = new ArrayList(namespaceCodes.length);
095: for (int i = 0; i < namespaceCodes.length; i++) {
096: prefixes.add(namePool
097: .getPrefixFromNamespaceCode(namespaceCodes[i]));
098: }
099: return prefixes.iterator();
100: }
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: //
|