ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.util; ohair@286: ohair@286: import java.util.ArrayList; ohair@286: import java.util.EmptyStackException; ohair@286: import java.util.HashMap; ohair@286: import java.util.Iterator; ohair@286: import java.util.List; ohair@286: import java.util.Map; ohair@286: ohair@286: import com.sun.xml.internal.ws.encoding.soap.streaming.SOAPNamespaceConstants; ohair@286: ohair@286: /** ohair@286: * Encapsulate Namespace logic for use by SAX drivers. ohair@286: * ohair@286: *
ohair@286: * This module, both source code and documentation, is in the ohair@286: * Public Domain, and comes with NO WARRANTY. ohair@286: *
ohair@286: * ohair@286: *

This class encapsulates the logic of Namespace processing: ohair@286: * it tracks the declarations currently in force for each context ohair@286: * and automatically processes qualified XML 1.0 names into their ohair@286: * Namespace parts; it can also be used in reverse for generating ohair@286: * XML 1.0 from Namespaces.

ohair@286: * ohair@286: *

Namespace support objects are reusable, but the reset method ohair@286: * must be invoked between each session.

ohair@286: * ohair@286: *

Here is a simple session:

ohair@286: * ohair@286: *
ohair@286:  * String parts[] = new String[3];
ohair@286:  * NamespaceSupport support = new NamespaceSupport();
ohair@286:  *
ohair@286:  * support.pushContext();
ohair@286:  * support.declarePrefix("", "http://www.w3.org/1999/xhtml");
ohair@286:  * support.declarePrefix("dc", "http://www.purl.org/dc#");
ohair@286:  *
ohair@286:  * String parts[] = support.processName("p", parts, false);
ohair@286:  * System.out.println("Namespace URI: " + parts[0]);
ohair@286:  * System.out.println("Local name: " + parts[1]);
ohair@286:  * System.out.println("Raw name: " + parts[2]);
ohair@286: 
ohair@286:  * String parts[] = support.processName("dc:title", parts, false);
ohair@286:  * System.out.println("Namespace URI: " + parts[0]);
ohair@286:  * System.out.println("Local name: " + parts[1]);
ohair@286:  * System.out.println("Raw name: " + parts[2]);
ohair@286: 
ohair@286:  * support.popContext();
ohair@286:  * 
ohair@286: * ohair@286: *

Note that this class is optimized for the use case where most ohair@286: * elements do not contain Namespace declarations: if the same ohair@286: * prefix/URI mapping is repeated for each context (for example), this ohair@286: * class will be somewhat less efficient.

ohair@286: * ohair@286: * @author David Megginson ohair@286: * @author WS Development Team ohair@286: */ alanb@368: public final class NamespaceSupport { ohair@286: ohair@286: /* added two new methods, slideContextUp() and slideContextDown() ohair@286: * needed to implement the revised streaming parser class (Parser2) ohair@286: */ ohair@286: ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: // Constants. ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: ohair@286: /** ohair@286: * The XML Namespace as a constant. ohair@286: * ohair@286: *

This is the Namespace URI that is automatically mapped ohair@286: * to the "xml" prefix.

ohair@286: */ ohair@286: public final static String XMLNS = "http://www.w3.org/XML/1998/namespace"; ohair@286: ohair@286: /** ohair@286: * An empty enumeration. ohair@286: */ ohair@286: private final static Iterable EMPTY_ENUMERATION = ohair@286: new ArrayList(); ohair@286: ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: // Constructor. ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: ohair@286: /** ohair@286: * Create a new Namespace support object. ohair@286: */ ohair@286: public NamespaceSupport() { ohair@286: reset(); ohair@286: } ohair@286: ohair@286: // PBG May 6 2002 added a copy constructor to support recording ohair@286: public NamespaceSupport(NamespaceSupport that) { ohair@286: contexts = new Context[that.contexts.length]; ohair@286: currentContext = null; ohair@286: contextPos = that.contextPos; ohair@286: ohair@286: Context currentParent = null; ohair@286: ohair@286: for (int i = 0; i < that.contexts.length; i++) { ohair@286: Context thatContext = that.contexts[i]; ohair@286: ohair@286: if (thatContext == null) { ohair@286: contexts[i] = null; ohair@286: continue; ohair@286: } ohair@286: ohair@286: Context thisContext = new Context(thatContext, currentParent); ohair@286: contexts[i] = thisContext; ohair@286: if (that.currentContext == thatContext) { ohair@286: currentContext = thisContext; ohair@286: } ohair@286: ohair@286: currentParent = thisContext; ohair@286: } ohair@286: } ohair@286: ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: // Context management. ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: ohair@286: /** ohair@286: * Reset this Namespace support object for reuse. ohair@286: * ohair@286: *

It is necessary to invoke this method before reusing the ohair@286: * Namespace support object for a new session.

ohair@286: */ ohair@286: public void reset() { ohair@286: contexts = new Context[32]; ohair@286: contextPos = 0; ohair@286: contexts[contextPos] = currentContext = new Context(); ohair@286: currentContext.declarePrefix("xml", XMLNS); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Start a new Namespace context. ohair@286: * ohair@286: *

Normally, you should push a new context at the beginning ohair@286: * of each XML element: the new context will automatically inherit ohair@286: * the declarations of its parent context, but it will also keep ohair@286: * track of which declarations were made within this context.

ohair@286: * ohair@286: *

The Namespace support object always starts with a base context ohair@286: * already in force: in this context, only the "xml" prefix is ohair@286: * declared.

ohair@286: * ohair@286: * @see #popContext ohair@286: */ ohair@286: public void pushContext() { ohair@286: int max = contexts.length; ohair@286: contextPos++; ohair@286: ohair@286: // Extend the array if necessary ohair@286: if (contextPos >= max) { ohair@286: Context newContexts[] = new Context[max * 2]; ohair@286: System.arraycopy(contexts, 0, newContexts, 0, max); ohair@286: contexts = newContexts; ohair@286: } ohair@286: ohair@286: // Allocate the context if necessary. ohair@286: currentContext = contexts[contextPos]; ohair@286: if (currentContext == null) { ohair@286: contexts[contextPos] = currentContext = new Context(); ohair@286: } ohair@286: ohair@286: // Set the parent, if any. ohair@286: if (contextPos > 0) { ohair@286: currentContext.setParent(contexts[contextPos - 1]); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Revert to the previous Namespace context. ohair@286: * ohair@286: *

Normally, you should pop the context at the end of each ohair@286: * XML element. After popping the context, all Namespace prefix ohair@286: * mappings that were previously in force are restored.

ohair@286: * ohair@286: *

You must not attempt to declare additional Namespace ohair@286: * prefixes after popping a context, unless you push another ohair@286: * context first.

ohair@286: * ohair@286: * @see #pushContext ohair@286: */ ohair@286: public void popContext() { ohair@286: contextPos--; ohair@286: if (contextPos < 0) { ohair@286: throw new EmptyStackException(); ohair@286: } ohair@286: currentContext = contexts[contextPos]; ohair@286: } ohair@286: ohair@286: /* ohair@286: * added for the revised streaming parser class (Parser2) ohair@286: * Move the context artificially up one level (i.e. contracting it). ohair@286: */ ohair@286: public void slideContextUp() { ohair@286: contextPos--; ohair@286: currentContext = contexts[contextPos]; ohair@286: } ohair@286: ohair@286: /* ohair@286: * added for the revised streaming parser class (Parser2) ohair@286: * Move the context artificially down one level (i.e. expanding it). ohair@286: */ ohair@286: public void slideContextDown() { ohair@286: contextPos++; ohair@286: ohair@286: if (contexts[contextPos] == null) { ohair@286: // trying to slide to a context that was never created ohair@286: contexts[contextPos] = contexts[contextPos - 1]; ohair@286: } ohair@286: ohair@286: currentContext = contexts[contextPos]; ohair@286: } ohair@286: ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: // Operations within a context. ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: ohair@286: /** ohair@286: * Declare a Namespace prefix. ohair@286: * ohair@286: *

This method declares a prefix in the current Namespace ohair@286: * context; the prefix will remain in force until this context ohair@286: * is popped, unless it is shadowed in a descendant context.

ohair@286: * ohair@286: *

To declare a default Namespace, use the empty string. The ohair@286: * prefix must not be "xml" or "xmlns".

ohair@286: * ohair@286: *

Note that you must not declare a prefix after ohair@286: * you've pushed and popped another Namespace.

ohair@286: * ohair@286: *

Note that there is an asymmetry in this library: while {@link ohair@286: * #getPrefix getPrefix} will not return the default "" prefix, ohair@286: * even if you have declared one; to check for a default prefix, ohair@286: * you have to look it up explicitly using {@link #getURI getURI}. ohair@286: * This asymmetry exists to make it easier to look up prefixes ohair@286: * for attribute names, where the default prefix is not allowed.

ohair@286: * ohair@286: * @param prefix The prefix to declare, or null for the empty ohair@286: * string. ohair@286: * @param uri The Namespace URI to associate with the prefix. ohair@286: * @return true if the prefix was legal, false otherwise ohair@286: * @see #processName ohair@286: * @see #getURI ohair@286: * @see #getPrefix ohair@286: */ ohair@286: public boolean declarePrefix(String prefix, String uri) { ohair@286: // bugfix#: 4989753 ohair@286: if ((prefix.equals("xml") && !uri.equals(SOAPNamespaceConstants.XMLNS)) ohair@286: || prefix.equals("xmlns")) { ohair@286: return false; ohair@286: } else { ohair@286: currentContext.declarePrefix(prefix, uri); ohair@286: return true; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Process a raw XML 1.0 name. ohair@286: * ohair@286: *

This method processes a raw XML 1.0 name in the current ohair@286: * context by removing the prefix and looking it up among the ohair@286: * prefixes currently declared. The return value will be the ohair@286: * array supplied by the caller, filled in as follows:

ohair@286: * ohair@286: *
ohair@286: *
parts[0]
ohair@286: *
The Namespace URI, or an empty string if none is ohair@286: * in use.
ohair@286: *
parts[1]
ohair@286: *
The local name (without prefix).
ohair@286: *
parts[2]
ohair@286: *
The original raw name.
ohair@286: *
ohair@286: * ohair@286: *

All of the strings in the array will be internalized. If ohair@286: * the raw name has a prefix that has not been declared, then ohair@286: * the return value will be null.

ohair@286: * ohair@286: *

Note that attribute names are processed differently than ohair@286: * element names: an unprefixed element name will received the ohair@286: * default Namespace (if any), while an unprefixed element name ohair@286: * will not.

ohair@286: * ohair@286: * @param qName The raw XML 1.0 name to be processed. ohair@286: * @param parts An array supplied by the caller, capable of ohair@286: * holding at least three members. ohair@286: * @param isAttribute A flag indicating whether this is an ohair@286: * attribute name (true) or an element name (false). ohair@286: * @return The supplied array holding three internalized strings ohair@286: * representing the Namespace URI (or empty string), the ohair@286: * local name, and the raw XML 1.0 name; or null if there ohair@286: * is an undeclared prefix. ohair@286: * @see #declarePrefix ohair@286: * @see java.lang.String#intern */ ohair@286: public String[] processName( ohair@286: String qName, ohair@286: String parts[], ohair@286: boolean isAttribute) { ohair@286: String myParts[] = currentContext.processName(qName, isAttribute); ohair@286: if (myParts == null) { ohair@286: return null; ohair@286: } else { ohair@286: parts[0] = myParts[0]; ohair@286: parts[1] = myParts[1]; ohair@286: parts[2] = myParts[2]; ohair@286: return parts; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Look up a prefix and get the currently-mapped Namespace URI. ohair@286: * ohair@286: *

This method looks up the prefix in the current context. ohair@286: * Use the empty string ("") for the default Namespace.

ohair@286: * ohair@286: * @param prefix The prefix to look up. ohair@286: * @return The associated Namespace URI, or null if the prefix ohair@286: * is undeclared in this context. ohair@286: * @see #getPrefix ohair@286: * @see #getPrefixes ohair@286: */ ohair@286: public String getURI(String prefix) { ohair@286: return currentContext.getURI(prefix); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Return an enumeration of all prefixes currently declared. ohair@286: * ohair@286: *

Note: if there is a default prefix, it will not be ohair@286: * returned in this enumeration; check for the default prefix ohair@286: * using the {@link #getURI getURI} with an argument of "".

ohair@286: * ohair@286: * @return An enumeration of all prefixes declared in the ohair@286: * current context except for the empty (default) ohair@286: * prefix. ohair@286: * @see #getDeclaredPrefixes ohair@286: * @see #getURI ohair@286: */ ohair@286: public Iterable getPrefixes() { ohair@286: return currentContext.getPrefixes(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Return one of the prefixes mapped to a Namespace URI. ohair@286: * ohair@286: *

If more than one prefix is currently mapped to the same ohair@286: * URI, this method will make an arbitrary selection; if you ohair@286: * want all of the prefixes, use the {@link #getPrefixes} ohair@286: * method instead.

ohair@286: * ohair@286: *

Note: this will never return the empty (default) prefix; ohair@286: * to check for a default prefix, use the {@link #getURI getURI} ohair@286: * method with an argument of "".

ohair@286: * ohair@286: * @param uri The Namespace URI. ohair@286: * @return One of the prefixes currently mapped to the URI supplied, ohair@286: * or null if none is mapped or if the URI is assigned to ohair@286: * the default Namespace. ohair@286: * @see #getPrefixes(java.lang.String) ohair@286: * @see #getURI ohair@286: */ ohair@286: public String getPrefix(String uri) { ohair@286: return currentContext.getPrefix(uri); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Return an enumeration of all prefixes currently declared for a URI. ohair@286: * ohair@286: *

This method returns prefixes mapped to a specific Namespace ohair@286: * URI. The xml: prefix will be included. If you want only one ohair@286: * prefix that's mapped to the Namespace URI, and you don't care ohair@286: * which one you get, use the {@link #getPrefix getPrefix} ohair@286: * method instead.

ohair@286: * ohair@286: *

Note: the empty (default) prefix is never included ohair@286: * in this enumeration; to check for the presence of a default ohair@286: * Namespace, use the {@link #getURI getURI} method with an ohair@286: * argument of "".

ohair@286: * ohair@286: * @param uri The Namespace URI. ohair@286: * @return An enumeration of all prefixes declared in the ohair@286: * current context. ohair@286: * @see #getPrefix ohair@286: * @see #getDeclaredPrefixes ohair@286: * @see #getURI ohair@286: */ ohair@286: public Iterator getPrefixes(String uri) { ohair@286: List prefixes = new ArrayList(); ohair@286: for (String prefix: getPrefixes()) { ohair@286: if (uri.equals(getURI(prefix))) { ohair@286: prefixes.add(prefix); ohair@286: } ohair@286: } ohair@286: return prefixes.iterator(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Return an enumeration of all prefixes declared in this context. ohair@286: * ohair@286: *

The empty (default) prefix will be included in this ohair@286: * enumeration; note that this behaviour differs from that of ohair@286: * {@link #getPrefix} and {@link #getPrefixes}.

ohair@286: * ohair@286: * @return An enumeration of all prefixes declared in this ohair@286: * context. ohair@286: * @see #getPrefixes ohair@286: * @see #getURI ohair@286: */ ohair@286: public Iterable getDeclaredPrefixes() { ohair@286: return currentContext.getDeclaredPrefixes(); ohair@286: } ohair@286: ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: // Internal state. ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: ohair@286: private Context contexts[]; ohair@286: private Context currentContext; ohair@286: private int contextPos; ohair@286: ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: // Internal classes. ohair@286: //////////////////////////////////////////////////////////////////// ohair@286: ohair@286: /** ohair@286: * Internal class for a single Namespace context. ohair@286: * ohair@286: *

This module caches and reuses Namespace contexts, so the number ohair@286: * allocated will be equal to the element depth of the document, not to the ohair@286: * total number of elements (i.e. 5-10 rather than tens of thousands).

ohair@286: */ ohair@286: final static class Context { ohair@286: ohair@286: /** ohair@286: * Create the root-level Namespace context. ohair@286: */ ohair@286: Context() { ohair@286: copyTables(); ohair@286: } ohair@286: // PGB May 6 2002 added copy constructor ohair@286: Context(Context that, Context newParent) { ohair@286: if (that == null) { ohair@286: copyTables(); ohair@286: return; ohair@286: } ohair@286: ohair@286: if (newParent != null && !that.tablesDirty) { ohair@286: prefixTable = ohair@286: that.prefixTable == that.parent.prefixTable ohair@286: ? newParent.prefixTable ohair@286: : (HashMap) that.prefixTable.clone(); ohair@286: ohair@286: uriTable = ohair@286: that.uriTable == that.parent.uriTable ohair@286: ? newParent.uriTable ohair@286: : (HashMap) that.uriTable.clone(); ohair@286: elementNameTable = ohair@286: that.elementNameTable == that.parent.elementNameTable ohair@286: ? newParent.elementNameTable ohair@286: : (HashMap) that.elementNameTable.clone(); ohair@286: attributeNameTable = ohair@286: that.attributeNameTable == that.parent.attributeNameTable ohair@286: ? newParent.attributeNameTable ohair@286: : (HashMap) that.attributeNameTable.clone(); ohair@286: defaultNS = ohair@286: that.defaultNS == that.parent.defaultNS ohair@286: ? newParent.defaultNS ohair@286: : that.defaultNS; ohair@286: } else { ohair@286: prefixTable = (HashMap) that.prefixTable.clone(); ohair@286: uriTable = (HashMap) that.uriTable.clone(); ohair@286: elementNameTable = (HashMap) that.elementNameTable.clone(); ohair@286: attributeNameTable = (HashMap) that.attributeNameTable.clone(); ohair@286: defaultNS = that.defaultNS; ohair@286: } ohair@286: ohair@286: tablesDirty = that.tablesDirty; ohair@286: parent = newParent; ohair@286: declarations = ohair@286: that.declarations == null ohair@286: ? null ohair@286: : (ArrayList) that.declarations.clone(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * (Re)set the parent of this Namespace context. ohair@286: * ohair@286: * @param parent The parent Namespace context object. ohair@286: */ ohair@286: void setParent(Context parent) { ohair@286: this.parent = parent; ohair@286: declarations = null; ohair@286: prefixTable = parent.prefixTable; ohair@286: uriTable = parent.uriTable; ohair@286: elementNameTable = parent.elementNameTable; ohair@286: attributeNameTable = parent.attributeNameTable; ohair@286: defaultNS = parent.defaultNS; ohair@286: tablesDirty = false; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Declare a Namespace prefix for this context. ohair@286: * ohair@286: * @param prefix The prefix to declare. ohair@286: * @param uri The associated Namespace URI. ohair@286: * @see org.xml.sax.helpers.NamespaceSupport#declarePrefix ohair@286: */ ohair@286: void declarePrefix(String prefix, String uri) { ohair@286: // Lazy processing... ohair@286: if (!tablesDirty) { ohair@286: copyTables(); ohair@286: } ohair@286: if (declarations == null) { ohair@286: declarations = new ArrayList(); ohair@286: } ohair@286: ohair@286: prefix = prefix.intern(); ohair@286: uri = uri.intern(); ohair@286: if ("".equals(prefix)) { ohair@286: if ("".equals(uri)) { ohair@286: defaultNS = null; ohair@286: } else { ohair@286: defaultNS = uri; ohair@286: } ohair@286: } else { ohair@286: prefixTable.put(prefix, uri); ohair@286: uriTable.put(uri, prefix); // may wipe out another prefix ohair@286: } ohair@286: declarations.add(prefix); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Process a raw XML 1.0 name in this context. ohair@286: * ohair@286: * @param qName The raw XML 1.0 name. ohair@286: * @param isAttribute true if this is an attribute name. ohair@286: * @return An array of three strings containing the ohair@286: * URI part (or empty string), the local part, ohair@286: * and the raw name, all internalized, or null ohair@286: * if there is an undeclared prefix. ohair@286: * @see org.xml.sax.helpers.NamespaceSupport#processName ohair@286: */ ohair@286: String[] processName(String qName, boolean isAttribute) { ohair@286: String name[]; ohair@286: Map table; ohair@286: ohair@286: // Select the appropriate table. ohair@286: if (isAttribute) { ohair@286: table = elementNameTable; ohair@286: } else { ohair@286: table = attributeNameTable; ohair@286: } ohair@286: ohair@286: // Start by looking in the cache, and ohair@286: // return immediately if the name ohair@286: // is already known in this content ohair@286: name = (String[]) table.get(qName); ohair@286: if (name != null) { ohair@286: return name; ohair@286: } ohair@286: ohair@286: // We haven't seen this name in this ohair@286: // context before. ohair@286: name = new String[3]; ohair@286: int index = qName.indexOf(':'); ohair@286: ohair@286: // No prefix. ohair@286: if (index == -1) { ohair@286: if (isAttribute || defaultNS == null) { ohair@286: name[0] = ""; ohair@286: } else { ohair@286: name[0] = defaultNS; ohair@286: } ohair@286: name[1] = qName.intern(); ohair@286: name[2] = name[1]; ohair@286: } ohair@286: ohair@286: // Prefix ohair@286: else { ohair@286: String prefix = qName.substring(0, index); ohair@286: String local = qName.substring(index + 1); ohair@286: String uri; ohair@286: if ("".equals(prefix)) { ohair@286: uri = defaultNS; ohair@286: } else { ohair@286: uri = (String) prefixTable.get(prefix); ohair@286: } ohair@286: if (uri == null) { ohair@286: return null; ohair@286: } ohair@286: name[0] = uri; ohair@286: name[1] = local.intern(); ohair@286: name[2] = qName.intern(); ohair@286: } ohair@286: ohair@286: // Save in the cache for future use. ohair@286: table.put(name[2], name); ohair@286: tablesDirty = true; ohair@286: return name; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Look up the URI associated with a prefix in this context. ohair@286: * ohair@286: * @param prefix The prefix to look up. ohair@286: * @return The associated Namespace URI, or null if none is ohair@286: * declared. ohair@286: * @see org.xml.sax.helpers.NamespaceSupport#getURI ohair@286: */ ohair@286: String getURI(String prefix) { ohair@286: if ("".equals(prefix)) { ohair@286: return defaultNS; ohair@286: } else if (prefixTable == null) { ohair@286: return null; ohair@286: } else { ohair@286: return (String) prefixTable.get(prefix); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Look up one of the prefixes associated with a URI in this context. ohair@286: * ohair@286: *

Since many prefixes may be mapped to the same URI, ohair@286: * the return value may be unreliable.

ohair@286: * ohair@286: * @param uri The URI to look up. ohair@286: * @return The associated prefix, or null if none is declared. ohair@286: * @see org.xml.sax.helpers.NamespaceSupport#getPrefix ohair@286: */ ohair@286: String getPrefix(String uri) { ohair@286: if (uriTable == null) { ohair@286: return null; ohair@286: } else { ohair@286: return (String) uriTable.get(uri); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Return an enumeration of prefixes declared in this context. ohair@286: * ohair@286: * @return An enumeration of prefixes (possibly empty). ohair@286: * @see org.xml.sax.helpers.NamespaceSupport#getDeclaredPrefixes ohair@286: */ ohair@286: Iterable getDeclaredPrefixes() { ohair@286: if (declarations == null) { ohair@286: return EMPTY_ENUMERATION; ohair@286: } else { ohair@286: return declarations; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Return an enumeration of all prefixes currently in force. ohair@286: * ohair@286: *

The default prefix, if in force, is not ohair@286: * returned, and will have to be checked for separately.

ohair@286: * ohair@286: * @return An enumeration of prefixes (never empty). ohair@286: * @see org.xml.sax.helpers.NamespaceSupport#getPrefixes ohair@286: */ ohair@286: Iterable getPrefixes() { ohair@286: if (prefixTable == null) { ohair@286: return EMPTY_ENUMERATION; ohair@286: } else { ohair@286: return prefixTable.keySet(); ohair@286: } ohair@286: } ohair@286: ohair@286: //////////////////////////////////////////////////////////////// ohair@286: // Internal methods. ohair@286: //////////////////////////////////////////////////////////////// ohair@286: ohair@286: /** ohair@286: * Copy on write for the internal tables in this context. ohair@286: * ohair@286: *

This class is optimized for the normal case where most ohair@286: * elements do not contain Namespace declarations.

ohair@286: */ ohair@286: private void copyTables() { ohair@286: if (prefixTable != null) { ohair@286: prefixTable = (HashMap) prefixTable.clone(); ohair@286: } else { ohair@286: prefixTable = new HashMap(); ohair@286: } ohair@286: if (uriTable != null) { ohair@286: uriTable = (HashMap) uriTable.clone(); ohair@286: } else { ohair@286: uriTable = new HashMap(); ohair@286: } ohair@286: elementNameTable = new HashMap(); ohair@286: attributeNameTable = new HashMap(); ohair@286: tablesDirty = true; ohair@286: } ohair@286: ohair@286: //////////////////////////////////////////////////////////////// ohair@286: // Protected state. ohair@286: //////////////////////////////////////////////////////////////// ohair@286: ohair@286: HashMap prefixTable; ohair@286: HashMap uriTable; ohair@286: // PBG May 6 2002 changed these two from Map to HashMap ohair@286: HashMap elementNameTable; ohair@286: HashMap attributeNameTable; ohair@286: String defaultNS = null; ohair@286: ohair@286: //////////////////////////////////////////////////////////////// ohair@286: // Internal state. ohair@286: //////////////////////////////////////////////////////////////// ohair@286: ohair@286: // PBG May 6 2002 changed this from List to ArrayList ohair@286: private ArrayList declarations = null; ohair@286: private boolean tablesDirty = false; ohair@286: private Context parent = null; ohair@286: } ohair@286: }