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

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

aoqi@0: * aoqi@0: *

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

aoqi@0: * aoqi@0: *

Here is a simple session:

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

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

aoqi@0: * aoqi@0: *

Although SAX drivers (parsers) may choose to use this class to aoqi@0: * implement namespace handling, they are not required to do so. aoqi@0: * Applications must track namespace information themselves if they aoqi@0: * want to use namespace information. aoqi@0: * aoqi@0: * @since SAX 2.0 aoqi@0: * @author David Megginson aoqi@0: * @version 2.0.1 (sax2r2) aoqi@0: */ aoqi@0: final class NamespaceSupport aoqi@0: { aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Constants. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * The XML Namespace URI as a constant. aoqi@0: * The value is http://www.w3.org/XML/1998/namespace aoqi@0: * as defined in the "Namespaces in XML" * recommendation. aoqi@0: * aoqi@0: *

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

aoqi@0: */ aoqi@0: public final static String XMLNS = aoqi@0: "http://www.w3.org/XML/1998/namespace"; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * The namespace declaration URI as a constant. aoqi@0: * The value is http://www.w3.org/xmlns/2000/, as defined aoqi@0: * in a backwards-incompatible erratum to the "Namespaces in XML" aoqi@0: * recommendation. Because that erratum postdated SAX2, SAX2 defaults aoqi@0: * to the original recommendation, and does not normally use this URI. aoqi@0: * aoqi@0: * aoqi@0: *

This is the Namespace URI that is optionally applied to aoqi@0: * xmlns and xmlns:* attributes, which are used to aoqi@0: * declare namespaces.

aoqi@0: * aoqi@0: * @since SAX 2.1alpha aoqi@0: * @see #setNamespaceDeclUris aoqi@0: * @see #isNamespaceDeclUris aoqi@0: */ aoqi@0: public final static String NSDECL = aoqi@0: "http://www.w3.org/xmlns/2000/"; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * An empty enumeration. aoqi@0: */ aoqi@0: private final static Enumeration EMPTY_ENUMERATION = aoqi@0: new Vector().elements(); aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Constructor. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Create a new Namespace support object. aoqi@0: */ aoqi@0: public NamespaceSupport () aoqi@0: { aoqi@0: reset(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Context management. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Reset this Namespace support object for reuse. aoqi@0: * aoqi@0: *

It is necessary to invoke this method before reusing the aoqi@0: * Namespace support object for a new session. If namespace aoqi@0: * declaration URIs are to be supported, that flag must also aoqi@0: * be set to a non-default value. aoqi@0: *

aoqi@0: * aoqi@0: * @see #setNamespaceDeclUris aoqi@0: */ aoqi@0: public void reset () aoqi@0: { aoqi@0: contexts = new Context[32]; aoqi@0: namespaceDeclUris = false; aoqi@0: contextPos = 0; aoqi@0: contexts[contextPos] = currentContext = new Context(); aoqi@0: currentContext.declarePrefix("xml", XMLNS); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Start a new Namespace context. aoqi@0: * The new context will automatically inherit aoqi@0: * the declarations of its parent context, but it will also keep aoqi@0: * track of which declarations were made within this context. aoqi@0: * aoqi@0: *

Event callback code should start a new context once per element. aoqi@0: * This means being ready to call this in either of two places. aoqi@0: * For elements that don't include namespace declarations, the aoqi@0: * ContentHandler.startElement() callback is the right place. aoqi@0: * For elements with such a declaration, it'd done in the first aoqi@0: * ContentHandler.startPrefixMapping() callback. aoqi@0: * A boolean flag can be used to aoqi@0: * track whether a context has been started yet. When either of aoqi@0: * those methods is called, it checks the flag to see if a new context aoqi@0: * needs to be started. If so, it starts the context and sets the aoqi@0: * flag. After ContentHandler.startElement() aoqi@0: * does that, it always clears the flag. aoqi@0: * aoqi@0: *

Normally, SAX drivers would push a new context at the beginning aoqi@0: * of each XML element. Then they perform a first pass over the aoqi@0: * attributes to process all namespace declarations, making aoqi@0: * ContentHandler.startPrefixMapping() callbacks. aoqi@0: * Then a second pass is made, to determine the namespace-qualified aoqi@0: * names for all attributes and for the element name. aoqi@0: * Finally all the information for the aoqi@0: * ContentHandler.startElement() callback is available, aoqi@0: * so it can then be made. aoqi@0: * aoqi@0: *

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

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

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

aoqi@0: * aoqi@0: *

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

aoqi@0: * aoqi@0: * @see #pushContext aoqi@0: */ aoqi@0: public void popContext () aoqi@0: { aoqi@0: contexts[contextPos].clear(); aoqi@0: contextPos--; aoqi@0: if (contextPos < 0) { aoqi@0: throw new EmptyStackException(); aoqi@0: } aoqi@0: currentContext = contexts[contextPos]; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Operations within a context. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Declare a Namespace prefix. All prefixes must be declared aoqi@0: * before they are referenced. For example, a SAX driver (parser) aoqi@0: * would scan an element's attributes aoqi@0: * in two passes: first for namespace declarations, aoqi@0: * then a second pass using {@link #processName processName()} to aoqi@0: * interpret prefixes against (potentially redefined) prefixes. aoqi@0: * aoqi@0: *

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

aoqi@0: * aoqi@0: *

To declare the default element Namespace, use the empty string as aoqi@0: * the prefix.

aoqi@0: * aoqi@0: *

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

aoqi@0: * aoqi@0: * @param prefix The prefix to declare, or the empty string to aoqi@0: * indicate the default element namespace. This may never have aoqi@0: * the value "xml" or "xmlns". aoqi@0: * @param uri The Namespace URI to associate with the prefix. aoqi@0: * @return true if the prefix was legal, false otherwise aoqi@0: * aoqi@0: * @see #processName aoqi@0: * @see #getURI aoqi@0: * @see #getPrefix aoqi@0: */ aoqi@0: public boolean declarePrefix (String prefix, String uri) aoqi@0: { aoqi@0: if (prefix.equals("xml") || prefix.equals("xmlns")) { aoqi@0: return false; aoqi@0: } else { aoqi@0: currentContext.declarePrefix(prefix, uri); aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Process a raw XML qualified name, after all declarations in the aoqi@0: * current context have been handled by {@link #declarePrefix aoqi@0: * declarePrefix()}. aoqi@0: * aoqi@0: *

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

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

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

aoqi@0: * aoqi@0: *

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

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

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

aoqi@0: * aoqi@0: * @param prefix The prefix to look up. aoqi@0: * @return The associated Namespace URI, or null if the prefix aoqi@0: * is undeclared in this context. aoqi@0: * @see #getPrefix aoqi@0: * @see #getPrefixes aoqi@0: */ aoqi@0: public String getURI (String prefix) aoqi@0: { aoqi@0: return currentContext.getURI(prefix); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return an enumeration of all prefixes whose declarations are aoqi@0: * active in the current context. aoqi@0: * This includes declarations from parent contexts that have aoqi@0: * not been overridden. aoqi@0: * aoqi@0: *

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

aoqi@0: * aoqi@0: * @return An enumeration of prefixes (never empty). aoqi@0: * @see #getDeclaredPrefixes aoqi@0: * @see #getURI aoqi@0: */ aoqi@0: public Enumeration getPrefixes () aoqi@0: { aoqi@0: return currentContext.getPrefixes(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return one of the prefixes mapped to a Namespace URI. aoqi@0: * aoqi@0: *

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

aoqi@0: * aoqi@0: *

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

aoqi@0: * aoqi@0: * @param uri the namespace URI aoqi@0: * @return one of the prefixes currently mapped to the URI supplied, aoqi@0: * or null if none is mapped or if the URI is assigned to aoqi@0: * the default namespace aoqi@0: * @see #getPrefixes(java.lang.String) aoqi@0: * @see #getURI aoqi@0: */ aoqi@0: public String getPrefix (String uri) aoqi@0: { aoqi@0: return currentContext.getPrefix(uri); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return an enumeration of all prefixes for a given URI whose aoqi@0: * declarations are active in the current context. aoqi@0: * This includes declarations from parent contexts that have aoqi@0: * not been overridden. aoqi@0: * aoqi@0: *

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

aoqi@0: * aoqi@0: *

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

aoqi@0: * aoqi@0: * @param uri The Namespace URI. aoqi@0: * @return An enumeration of prefixes (never empty). aoqi@0: * @see #getPrefix aoqi@0: * @see #getDeclaredPrefixes aoqi@0: * @see #getURI aoqi@0: */ aoqi@0: public Enumeration getPrefixes (String uri) aoqi@0: { aoqi@0: Vector prefixes = new Vector(); aoqi@0: Enumeration allPrefixes = getPrefixes(); aoqi@0: while (allPrefixes.hasMoreElements()) { aoqi@0: String prefix = (String)allPrefixes.nextElement(); aoqi@0: if (uri.equals(getURI(prefix))) { aoqi@0: prefixes.addElement(prefix); aoqi@0: } aoqi@0: } aoqi@0: return prefixes.elements(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return an enumeration of all prefixes declared in this context. aoqi@0: * aoqi@0: *

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

aoqi@0: * aoqi@0: * @return An enumeration of all prefixes declared in this aoqi@0: * context. aoqi@0: * @see #getPrefixes aoqi@0: * @see #getURI aoqi@0: */ aoqi@0: public Enumeration getDeclaredPrefixes () aoqi@0: { aoqi@0: return currentContext.getDeclaredPrefixes(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Controls whether namespace declaration attributes are placed aoqi@0: * into the {@link #NSDECL NSDECL} namespace aoqi@0: * by {@link #processName processName()}. This may only be aoqi@0: * changed before any contexts have been pushed. aoqi@0: * aoqi@0: * @since SAX 2.1alpha aoqi@0: * aoqi@0: * @exception IllegalStateException when attempting to set this aoqi@0: * after any context has been pushed. aoqi@0: */ aoqi@0: public void setNamespaceDeclUris (boolean value) aoqi@0: { aoqi@0: if (contextPos != 0) aoqi@0: throw new IllegalStateException (); aoqi@0: if (value == namespaceDeclUris) aoqi@0: return; aoqi@0: namespaceDeclUris = value; aoqi@0: if (value) aoqi@0: currentContext.declarePrefix ("xmlns", NSDECL); aoqi@0: else { aoqi@0: contexts[contextPos] = currentContext = new Context(); aoqi@0: currentContext.declarePrefix("xml", XMLNS); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns true if namespace declaration attributes are placed into aoqi@0: * a namespace. This behavior is not the default. aoqi@0: * aoqi@0: * @since SAX 2.1alpha aoqi@0: */ aoqi@0: public boolean isNamespaceDeclUris () aoqi@0: { return namespaceDeclUris; } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Internal state. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: private Context contexts[]; aoqi@0: private Context currentContext; aoqi@0: private int contextPos; aoqi@0: private boolean namespaceDeclUris; aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Internal classes. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: /** aoqi@0: * Internal class for a single Namespace context. aoqi@0: * aoqi@0: *

This module caches and reuses Namespace contexts, aoqi@0: * so the number allocated aoqi@0: * will be equal to the element depth of the document, not to the total aoqi@0: * number of elements (i.e. 5-10 rather than tens of thousands). aoqi@0: * Also, data structures used to represent contexts are shared when aoqi@0: * possible (child contexts without declarations) to further reduce aoqi@0: * the amount of memory that's consumed. aoqi@0: *

aoqi@0: */ aoqi@0: final class Context { aoqi@0: aoqi@0: /** aoqi@0: * Create the root-level Namespace context. aoqi@0: */ aoqi@0: Context () aoqi@0: { aoqi@0: copyTables(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * (Re)set the parent of this Namespace context. aoqi@0: * The context must either have been freshly constructed, aoqi@0: * or must have been cleared. aoqi@0: * aoqi@0: * @param context The parent Namespace context object. aoqi@0: */ aoqi@0: void setParent (Context parent) aoqi@0: { aoqi@0: this.parent = parent; aoqi@0: declarations = null; aoqi@0: prefixTable = parent.prefixTable; aoqi@0: uriTable = parent.uriTable; aoqi@0: elementNameTable = parent.elementNameTable; aoqi@0: attributeNameTable = parent.attributeNameTable; aoqi@0: defaultNS = parent.defaultNS; aoqi@0: declSeen = false; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Makes associated state become collectible, aoqi@0: * invalidating this context. aoqi@0: * {@link #setParent} must be called before aoqi@0: * this context may be used again. aoqi@0: */ aoqi@0: void clear () aoqi@0: { aoqi@0: parent = null; aoqi@0: prefixTable = null; aoqi@0: uriTable = null; aoqi@0: elementNameTable = null; aoqi@0: attributeNameTable = null; aoqi@0: defaultNS = ""; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Declare a Namespace prefix for this context. aoqi@0: * aoqi@0: * @param prefix The prefix to declare. aoqi@0: * @param uri The associated Namespace URI. aoqi@0: * @see org.xml.sax.helpers.NamespaceSupport#declarePrefix aoqi@0: */ aoqi@0: void declarePrefix (String prefix, String uri) aoqi@0: { aoqi@0: // Lazy processing... aoqi@0: // if (!declsOK) aoqi@0: // throw new IllegalStateException ( aoqi@0: // "can't declare any more prefixes in this context"); aoqi@0: if (!declSeen) { aoqi@0: copyTables(); aoqi@0: } aoqi@0: if (declarations == null) { aoqi@0: declarations = new Vector(); aoqi@0: } aoqi@0: aoqi@0: prefix = prefix.intern(); aoqi@0: uri = uri.intern(); aoqi@0: if ("".equals(prefix)) { aoqi@0: defaultNS = uri; aoqi@0: } else { aoqi@0: prefixTable.put(prefix, uri); aoqi@0: uriTable.put(uri, prefix); // may wipe out another prefix aoqi@0: } aoqi@0: declarations.addElement(prefix); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Process an XML qualified name in this context. aoqi@0: * aoqi@0: * @param qName The XML qualified name. aoqi@0: * @param isAttribute true if this is an attribute name. aoqi@0: * @return An array of three strings containing the aoqi@0: * URI part (or empty string), the local part, aoqi@0: * and the raw name, all internalized, or null aoqi@0: * if there is an undeclared prefix. aoqi@0: * @see org.xml.sax.helpers.NamespaceSupport#processName aoqi@0: */ aoqi@0: String [] processName (String qName, boolean isAttribute) aoqi@0: { aoqi@0: String name[]; aoqi@0: Hashtable table; aoqi@0: aoqi@0: // Select the appropriate table. aoqi@0: if (isAttribute) { aoqi@0: table = attributeNameTable; aoqi@0: } else { aoqi@0: table = elementNameTable; aoqi@0: } aoqi@0: aoqi@0: // Start by looking in the cache, and aoqi@0: // return immediately if the name aoqi@0: // is already known in this content aoqi@0: name = (String[])table.get(qName); aoqi@0: if (name != null) { aoqi@0: return name; aoqi@0: } aoqi@0: aoqi@0: // We haven't seen this name in this aoqi@0: // context before. Maybe in the parent aoqi@0: // context, but we can't assume prefix aoqi@0: // bindings are the same. aoqi@0: name = new String[3]; aoqi@0: name[2] = qName.intern(); aoqi@0: int index = qName.indexOf(':'); aoqi@0: aoqi@0: aoqi@0: // No prefix. aoqi@0: if (index == -1) { aoqi@0: if (isAttribute) { aoqi@0: if (qName == "xmlns" && namespaceDeclUris) aoqi@0: name[0] = NSDECL; aoqi@0: else aoqi@0: name[0] = ""; aoqi@0: } else { aoqi@0: name[0] = defaultNS; aoqi@0: } aoqi@0: name[1] = name[2]; aoqi@0: } aoqi@0: aoqi@0: // Prefix aoqi@0: else { aoqi@0: String prefix = qName.substring(0, index); aoqi@0: String local = qName.substring(index+1); aoqi@0: String uri; aoqi@0: if ("".equals(prefix)) { aoqi@0: uri = defaultNS; aoqi@0: } else { aoqi@0: uri = (String)prefixTable.get(prefix); aoqi@0: } aoqi@0: if (uri == null aoqi@0: || (!isAttribute && "xmlns".equals (prefix))) { aoqi@0: return null; aoqi@0: } aoqi@0: name[0] = uri; aoqi@0: name[1] = local.intern(); aoqi@0: } aoqi@0: aoqi@0: // Save in the cache for future use. aoqi@0: // (Could be shared with parent context...) aoqi@0: table.put(name[2], name); aoqi@0: return name; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Look up the URI associated with a prefix in this context. aoqi@0: * aoqi@0: * @param prefix The prefix to look up. aoqi@0: * @return The associated Namespace URI, or null if none is aoqi@0: * declared. aoqi@0: * @see org.xml.sax.helpers.NamespaceSupport#getURI aoqi@0: */ aoqi@0: String getURI (String prefix) aoqi@0: { aoqi@0: if ("".equals(prefix)) { aoqi@0: return defaultNS; aoqi@0: } else if (prefixTable == null) { aoqi@0: return null; aoqi@0: } else { aoqi@0: return (String)prefixTable.get(prefix); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Look up one of the prefixes associated with a URI in this context. aoqi@0: * aoqi@0: *

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

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

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

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

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

aoqi@0: */ aoqi@0: private void copyTables () aoqi@0: { aoqi@0: if (prefixTable != null) { aoqi@0: prefixTable = (Hashtable)prefixTable.clone(); aoqi@0: } else { aoqi@0: prefixTable = new Hashtable(); aoqi@0: } aoqi@0: if (uriTable != null) { aoqi@0: uriTable = (Hashtable)uriTable.clone(); aoqi@0: } else { aoqi@0: uriTable = new Hashtable(); aoqi@0: } aoqi@0: elementNameTable = new Hashtable(); aoqi@0: attributeNameTable = new Hashtable(); aoqi@0: declSeen = true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////// aoqi@0: // Protected state. aoqi@0: //////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: Hashtable prefixTable; aoqi@0: Hashtable uriTable; aoqi@0: Hashtable elementNameTable; aoqi@0: Hashtable attributeNameTable; aoqi@0: String defaultNS = ""; aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////// aoqi@0: // Internal state. aoqi@0: //////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: private Vector declarations = null; aoqi@0: private boolean declSeen = false; aoqi@0: private Context parent = null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // end of NamespaceSupport.java