src/share/jaxws_classes/com/sun/xml/internal/txw2/NamespaceSupport.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/txw2/NamespaceSupport.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,845 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +// NamespaceSupport.java - generic Namespace support for SAX.
    1.30 +// http://www.saxproject.org
    1.31 +// Written by David Megginson
    1.32 +// This class is in the Public Domain.  NO WARRANTY!
    1.33 +
    1.34 +package com.sun.xml.internal.txw2;
    1.35 +
    1.36 +import java.util.EmptyStackException;
    1.37 +import java.util.Enumeration;
    1.38 +import java.util.Hashtable;
    1.39 +import java.util.Vector;
    1.40 +
    1.41 +
    1.42 +/**
    1.43 + * Encapsulate Namespace logic for use by applications using SAX,
    1.44 + * or internally by SAX drivers.
    1.45 + *
    1.46 + * <blockquote>
    1.47 + * <em>This module, both source code and documentation, is in the
    1.48 + * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
    1.49 + * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
    1.50 + * for further information.
    1.51 + * </blockquote>
    1.52 + *
    1.53 + * <p>This class encapsulates the logic of Namespace processing: it
    1.54 + * tracks the declarations currently in force for each context and
    1.55 + * automatically processes qualified XML names into their Namespace
    1.56 + * parts; it can also be used in reverse for generating XML qnames
    1.57 + * from Namespaces.</p>
    1.58 + *
    1.59 + * <p>Namespace support objects are reusable, but the reset method
    1.60 + * must be invoked between each session.</p>
    1.61 + *
    1.62 + * <p>Here is a simple session:</p>
    1.63 + *
    1.64 + * <pre>
    1.65 + * String parts[] = new String[3];
    1.66 + * NamespaceSupport support = new NamespaceSupport();
    1.67 + *
    1.68 + * support.pushContext();
    1.69 + * support.declarePrefix("", "http://www.w3.org/1999/xhtml");
    1.70 + * support.declarePrefix("dc", "http://www.purl.org/dc#");
    1.71 + *
    1.72 + * parts = support.processName("p", parts, false);
    1.73 + * System.out.println("Namespace URI: " + parts[0]);
    1.74 + * System.out.println("Local name: " + parts[1]);
    1.75 + * System.out.println("Raw name: " + parts[2]);
    1.76 + *
    1.77 + * parts = support.processName("dc:title", parts, false);
    1.78 + * System.out.println("Namespace URI: " + parts[0]);
    1.79 + * System.out.println("Local name: " + parts[1]);
    1.80 + * System.out.println("Raw name: " + parts[2]);
    1.81 + *
    1.82 + * support.popContext();
    1.83 + * </pre>
    1.84 + *
    1.85 + * <p>Note that this class is optimized for the use case where most
    1.86 + * elements do not contain Namespace declarations: if the same
    1.87 + * prefix/URI mapping is repeated for each context (for example), this
    1.88 + * class will be somewhat less efficient.</p>
    1.89 + *
    1.90 + * <p>Although SAX drivers (parsers) may choose to use this class to
    1.91 + * implement namespace handling, they are not required to do so.
    1.92 + * Applications must track namespace information themselves if they
    1.93 + * want to use namespace information.
    1.94 + *
    1.95 + * @since SAX 2.0
    1.96 + * @author David Megginson
    1.97 + * @version 2.0.1 (sax2r2)
    1.98 + */
    1.99 +final class NamespaceSupport
   1.100 +{
   1.101 +
   1.102 +
   1.103 +    ////////////////////////////////////////////////////////////////////
   1.104 +    // Constants.
   1.105 +    ////////////////////////////////////////////////////////////////////
   1.106 +
   1.107 +
   1.108 +    /**
   1.109 +     * The XML Namespace URI as a constant.
   1.110 +     * The value is <code>http://www.w3.org/XML/1998/namespace</code>
   1.111 +     * as defined in the "Namespaces in XML" * recommendation.
   1.112 +     *
   1.113 +     * <p>This is the Namespace URI that is automatically mapped
   1.114 +     * to the "xml" prefix.</p>
   1.115 +     */
   1.116 +    public final static String XMLNS =
   1.117 +        "http://www.w3.org/XML/1998/namespace";
   1.118 +
   1.119 +
   1.120 +    /**
   1.121 +     * The namespace declaration URI as a constant.
   1.122 +     * The value is <code>http://www.w3.org/xmlns/2000/</code>, as defined
   1.123 +     * in a backwards-incompatible erratum to the "Namespaces in XML"
   1.124 +     * recommendation.  Because that erratum postdated SAX2, SAX2 defaults
   1.125 +     * to the original recommendation, and does not normally use this URI.
   1.126 +     *
   1.127 +     *
   1.128 +     * <p>This is the Namespace URI that is optionally applied to
   1.129 +     * <em>xmlns</em> and <em>xmlns:*</em> attributes, which are used to
   1.130 +     * declare namespaces.  </p>
   1.131 +     *
   1.132 +     * @since SAX 2.1alpha
   1.133 +     * @see #setNamespaceDeclUris
   1.134 +     * @see #isNamespaceDeclUris
   1.135 +     */
   1.136 +    public final static String NSDECL =
   1.137 +        "http://www.w3.org/xmlns/2000/";
   1.138 +
   1.139 +
   1.140 +    /**
   1.141 +     * An empty enumeration.
   1.142 +     */
   1.143 +    private final static Enumeration EMPTY_ENUMERATION =
   1.144 +        new Vector().elements();
   1.145 +
   1.146 +
   1.147 +    ////////////////////////////////////////////////////////////////////
   1.148 +    // Constructor.
   1.149 +    ////////////////////////////////////////////////////////////////////
   1.150 +
   1.151 +
   1.152 +    /**
   1.153 +     * Create a new Namespace support object.
   1.154 +     */
   1.155 +    public NamespaceSupport ()
   1.156 +    {
   1.157 +        reset();
   1.158 +    }
   1.159 +
   1.160 +
   1.161 +
   1.162 +    ////////////////////////////////////////////////////////////////////
   1.163 +    // Context management.
   1.164 +    ////////////////////////////////////////////////////////////////////
   1.165 +
   1.166 +
   1.167 +    /**
   1.168 +     * Reset this Namespace support object for reuse.
   1.169 +     *
   1.170 +     * <p>It is necessary to invoke this method before reusing the
   1.171 +     * Namespace support object for a new session.  If namespace
   1.172 +     * declaration URIs are to be supported, that flag must also
   1.173 +     * be set to a non-default value.
   1.174 +     * </p>
   1.175 +     *
   1.176 +     * @see #setNamespaceDeclUris
   1.177 +     */
   1.178 +    public void reset ()
   1.179 +    {
   1.180 +        contexts = new Context[32];
   1.181 +        namespaceDeclUris = false;
   1.182 +        contextPos = 0;
   1.183 +        contexts[contextPos] = currentContext = new Context();
   1.184 +        currentContext.declarePrefix("xml", XMLNS);
   1.185 +    }
   1.186 +
   1.187 +
   1.188 +    /**
   1.189 +     * Start a new Namespace context.
   1.190 +     * The new context will automatically inherit
   1.191 +     * the declarations of its parent context, but it will also keep
   1.192 +     * track of which declarations were made within this context.
   1.193 +     *
   1.194 +     * <p>Event callback code should start a new context once per element.
   1.195 +     * This means being ready to call this in either of two places.
   1.196 +     * For elements that don't include namespace declarations, the
   1.197 +     * <em>ContentHandler.startElement()</em> callback is the right place.
   1.198 +     * For elements with such a declaration, it'd done in the first
   1.199 +     * <em>ContentHandler.startPrefixMapping()</em> callback.
   1.200 +     * A boolean flag can be used to
   1.201 +     * track whether a context has been started yet.  When either of
   1.202 +     * those methods is called, it checks the flag to see if a new context
   1.203 +     * needs to be started.  If so, it starts the context and sets the
   1.204 +     * flag.  After <em>ContentHandler.startElement()</em>
   1.205 +     * does that, it always clears the flag.
   1.206 +     *
   1.207 +     * <p>Normally, SAX drivers would push a new context at the beginning
   1.208 +     * of each XML element.  Then they perform a first pass over the
   1.209 +     * attributes to process all namespace declarations, making
   1.210 +     * <em>ContentHandler.startPrefixMapping()</em> callbacks.
   1.211 +     * Then a second pass is made, to determine the namespace-qualified
   1.212 +     * names for all attributes and for the element name.
   1.213 +     * Finally all the information for the
   1.214 +     * <em>ContentHandler.startElement()</em> callback is available,
   1.215 +     * so it can then be made.
   1.216 +     *
   1.217 +     * <p>The Namespace support object always starts with a base context
   1.218 +     * already in force: in this context, only the "xml" prefix is
   1.219 +     * declared.</p>
   1.220 +     *
   1.221 +     * @see org.xml.sax.ContentHandler
   1.222 +     * @see #popContext
   1.223 +     */
   1.224 +    public void pushContext ()
   1.225 +    {
   1.226 +        int max = contexts.length;
   1.227 +
   1.228 +        contextPos++;
   1.229 +
   1.230 +                                // Extend the array if necessary
   1.231 +        if (contextPos >= max) {
   1.232 +            Context newContexts[] = new Context[max*2];
   1.233 +            System.arraycopy(contexts, 0, newContexts, 0, max);
   1.234 +            max *= 2;
   1.235 +            contexts = newContexts;
   1.236 +        }
   1.237 +
   1.238 +                                // Allocate the context if necessary.
   1.239 +        currentContext = contexts[contextPos];
   1.240 +        if (currentContext == null) {
   1.241 +            contexts[contextPos] = currentContext = new Context();
   1.242 +        }
   1.243 +
   1.244 +                                // Set the parent, if any.
   1.245 +        if (contextPos > 0) {
   1.246 +            currentContext.setParent(contexts[contextPos - 1]);
   1.247 +        }
   1.248 +    }
   1.249 +
   1.250 +
   1.251 +    /**
   1.252 +     * Revert to the previous Namespace context.
   1.253 +     *
   1.254 +     * <p>Normally, you should pop the context at the end of each
   1.255 +     * XML element.  After popping the context, all Namespace prefix
   1.256 +     * mappings that were previously in force are restored.</p>
   1.257 +     *
   1.258 +     * <p>You must not attempt to declare additional Namespace
   1.259 +     * prefixes after popping a context, unless you push another
   1.260 +     * context first.</p>
   1.261 +     *
   1.262 +     * @see #pushContext
   1.263 +     */
   1.264 +    public void popContext ()
   1.265 +    {
   1.266 +        contexts[contextPos].clear();
   1.267 +        contextPos--;
   1.268 +        if (contextPos < 0) {
   1.269 +            throw new EmptyStackException();
   1.270 +        }
   1.271 +        currentContext = contexts[contextPos];
   1.272 +    }
   1.273 +
   1.274 +
   1.275 +
   1.276 +    ////////////////////////////////////////////////////////////////////
   1.277 +    // Operations within a context.
   1.278 +    ////////////////////////////////////////////////////////////////////
   1.279 +
   1.280 +
   1.281 +    /**
   1.282 +     * Declare a Namespace prefix.  All prefixes must be declared
   1.283 +     * before they are referenced.  For example, a SAX driver (parser)
   1.284 +     * would scan an element's attributes
   1.285 +     * in two passes:  first for namespace declarations,
   1.286 +     * then a second pass using {@link #processName processName()} to
   1.287 +     * interpret prefixes against (potentially redefined) prefixes.
   1.288 +     *
   1.289 +     * <p>This method declares a prefix in the current Namespace
   1.290 +     * context; the prefix will remain in force until this context
   1.291 +     * is popped, unless it is shadowed in a descendant context.</p>
   1.292 +     *
   1.293 +     * <p>To declare the default element Namespace, use the empty string as
   1.294 +     * the prefix.</p>
   1.295 +     *
   1.296 +     * <p>Note that there is an asymmetry in this library: {@link
   1.297 +     * #getPrefix getPrefix} will not return the "" prefix,
   1.298 +     * even if you have declared a default element namespace.
   1.299 +     * To check for a default namespace,
   1.300 +     * you have to look it up explicitly using {@link #getURI getURI}.
   1.301 +     * This asymmetry exists to make it easier to look up prefixes
   1.302 +     * for attribute names, where the default prefix is not allowed.</p>
   1.303 +     *
   1.304 +     * @param prefix The prefix to declare, or the empty string to
   1.305 +     *  indicate the default element namespace.  This may never have
   1.306 +     *  the value "xml" or "xmlns".
   1.307 +     * @param uri The Namespace URI to associate with the prefix.
   1.308 +     * @return true if the prefix was legal, false otherwise
   1.309 +     *
   1.310 +     * @see #processName
   1.311 +     * @see #getURI
   1.312 +     * @see #getPrefix
   1.313 +     */
   1.314 +    public boolean declarePrefix (String prefix, String uri)
   1.315 +    {
   1.316 +        if (prefix.equals("xml") || prefix.equals("xmlns")) {
   1.317 +            return false;
   1.318 +        } else {
   1.319 +            currentContext.declarePrefix(prefix, uri);
   1.320 +            return true;
   1.321 +        }
   1.322 +    }
   1.323 +
   1.324 +
   1.325 +    /**
   1.326 +     * Process a raw XML qualified name, after all declarations in the
   1.327 +     * current context have been handled by {@link #declarePrefix
   1.328 +     * declarePrefix()}.
   1.329 +     *
   1.330 +     * <p>This method processes a raw XML qualified name in the
   1.331 +     * current context by removing the prefix and looking it up among
   1.332 +     * the prefixes currently declared.  The return value will be the
   1.333 +     * array supplied by the caller, filled in as follows:</p>
   1.334 +     *
   1.335 +     * <dl>
   1.336 +     * <dt>parts[0]</dt>
   1.337 +     * <dd>The Namespace URI, or an empty string if none is
   1.338 +     *  in use.</dd>
   1.339 +     * <dt>parts[1]</dt>
   1.340 +     * <dd>The local name (without prefix).</dd>
   1.341 +     * <dt>parts[2]</dt>
   1.342 +     * <dd>The original raw name.</dd>
   1.343 +     * </dl>
   1.344 +     *
   1.345 +     * <p>All of the strings in the array will be internalized.  If
   1.346 +     * the raw name has a prefix that has not been declared, then
   1.347 +     * the return value will be null.</p>
   1.348 +     *
   1.349 +     * <p>Note that attribute names are processed differently than
   1.350 +     * element names: an unprefixed element name will receive the
   1.351 +     * default Namespace (if any), while an unprefixed attribute name
   1.352 +     * will not.</p>
   1.353 +     *
   1.354 +     * @param qName The XML qualified name to be processed.
   1.355 +     * @param parts An array supplied by the caller, capable of
   1.356 +     *        holding at least three members.
   1.357 +     * @param isAttribute A flag indicating whether this is an
   1.358 +     *        attribute name (true) or an element name (false).
   1.359 +     * @return The supplied array holding three internalized strings
   1.360 +     *        representing the Namespace URI (or empty string), the
   1.361 +     *        local name, and the XML qualified name; or null if there
   1.362 +     *        is an undeclared prefix.
   1.363 +     * @see #declarePrefix
   1.364 +     * @see java.lang.String#intern */
   1.365 +    public String [] processName (String qName, String parts[],
   1.366 +                                  boolean isAttribute)
   1.367 +    {
   1.368 +        String myParts[] = currentContext.processName(qName, isAttribute);
   1.369 +        if (myParts == null) {
   1.370 +            return null;
   1.371 +        } else {
   1.372 +            parts[0] = myParts[0];
   1.373 +            parts[1] = myParts[1];
   1.374 +            parts[2] = myParts[2];
   1.375 +            return parts;
   1.376 +        }
   1.377 +    }
   1.378 +
   1.379 +
   1.380 +    /**
   1.381 +     * Look up a prefix and get the currently-mapped Namespace URI.
   1.382 +     *
   1.383 +     * <p>This method looks up the prefix in the current context.
   1.384 +     * Use the empty string ("") for the default Namespace.</p>
   1.385 +     *
   1.386 +     * @param prefix The prefix to look up.
   1.387 +     * @return The associated Namespace URI, or null if the prefix
   1.388 +     *         is undeclared in this context.
   1.389 +     * @see #getPrefix
   1.390 +     * @see #getPrefixes
   1.391 +     */
   1.392 +    public String getURI (String prefix)
   1.393 +    {
   1.394 +        return currentContext.getURI(prefix);
   1.395 +    }
   1.396 +
   1.397 +
   1.398 +    /**
   1.399 +     * Return an enumeration of all prefixes whose declarations are
   1.400 +     * active in the current context.
   1.401 +     * This includes declarations from parent contexts that have
   1.402 +     * not been overridden.
   1.403 +     *
   1.404 +     * <p><strong>Note:</strong> if there is a default prefix, it will not be
   1.405 +     * returned in this enumeration; check for the default prefix
   1.406 +     * using the {@link #getURI getURI} with an argument of "".</p>
   1.407 +     *
   1.408 +     * @return An enumeration of prefixes (never empty).
   1.409 +     * @see #getDeclaredPrefixes
   1.410 +     * @see #getURI
   1.411 +     */
   1.412 +    public Enumeration getPrefixes ()
   1.413 +    {
   1.414 +        return currentContext.getPrefixes();
   1.415 +    }
   1.416 +
   1.417 +
   1.418 +    /**
   1.419 +     * Return one of the prefixes mapped to a Namespace URI.
   1.420 +     *
   1.421 +     * <p>If more than one prefix is currently mapped to the same
   1.422 +     * URI, this method will make an arbitrary selection; if you
   1.423 +     * want all of the prefixes, use the {@link #getPrefixes}
   1.424 +     * method instead.</p>
   1.425 +     *
   1.426 +     * <p><strong>Note:</strong> this will never return the empty (default) prefix;
   1.427 +     * to check for a default prefix, use the {@link #getURI getURI}
   1.428 +     * method with an argument of "".</p>
   1.429 +     *
   1.430 +     * @param uri the namespace URI
   1.431 +     * @return one of the prefixes currently mapped to the URI supplied,
   1.432 +     *         or null if none is mapped or if the URI is assigned to
   1.433 +     *         the default namespace
   1.434 +     * @see #getPrefixes(java.lang.String)
   1.435 +     * @see #getURI
   1.436 +     */
   1.437 +    public String getPrefix (String uri)
   1.438 +    {
   1.439 +        return currentContext.getPrefix(uri);
   1.440 +    }
   1.441 +
   1.442 +
   1.443 +    /**
   1.444 +     * Return an enumeration of all prefixes for a given URI whose
   1.445 +     * declarations are active in the current context.
   1.446 +     * This includes declarations from parent contexts that have
   1.447 +     * not been overridden.
   1.448 +     *
   1.449 +     * <p>This method returns prefixes mapped to a specific Namespace
   1.450 +     * URI.  The xml: prefix will be included.  If you want only one
   1.451 +     * prefix that's mapped to the Namespace URI, and you don't care
   1.452 +     * which one you get, use the {@link #getPrefix getPrefix}
   1.453 +     *  method instead.</p>
   1.454 +     *
   1.455 +     * <p><strong>Note:</strong> the empty (default) prefix is <em>never</em> included
   1.456 +     * in this enumeration; to check for the presence of a default
   1.457 +     * Namespace, use the {@link #getURI getURI} method with an
   1.458 +     * argument of "".</p>
   1.459 +     *
   1.460 +     * @param uri The Namespace URI.
   1.461 +     * @return An enumeration of prefixes (never empty).
   1.462 +     * @see #getPrefix
   1.463 +     * @see #getDeclaredPrefixes
   1.464 +     * @see #getURI
   1.465 +     */
   1.466 +    public Enumeration getPrefixes (String uri)
   1.467 +    {
   1.468 +        Vector prefixes = new Vector();
   1.469 +        Enumeration allPrefixes = getPrefixes();
   1.470 +        while (allPrefixes.hasMoreElements()) {
   1.471 +            String prefix = (String)allPrefixes.nextElement();
   1.472 +            if (uri.equals(getURI(prefix))) {
   1.473 +                prefixes.addElement(prefix);
   1.474 +            }
   1.475 +        }
   1.476 +        return prefixes.elements();
   1.477 +    }
   1.478 +
   1.479 +
   1.480 +    /**
   1.481 +     * Return an enumeration of all prefixes declared in this context.
   1.482 +     *
   1.483 +     * <p>The empty (default) prefix will be included in this
   1.484 +     * enumeration; note that this behaviour differs from that of
   1.485 +     * {@link #getPrefix} and {@link #getPrefixes}.</p>
   1.486 +     *
   1.487 +     * @return An enumeration of all prefixes declared in this
   1.488 +     *         context.
   1.489 +     * @see #getPrefixes
   1.490 +     * @see #getURI
   1.491 +     */
   1.492 +    public Enumeration getDeclaredPrefixes ()
   1.493 +    {
   1.494 +        return currentContext.getDeclaredPrefixes();
   1.495 +    }
   1.496 +
   1.497 +    /**
   1.498 +     * Controls whether namespace declaration attributes are placed
   1.499 +     * into the {@link #NSDECL NSDECL} namespace
   1.500 +     * by {@link #processName processName()}.  This may only be
   1.501 +     * changed before any contexts have been pushed.
   1.502 +     *
   1.503 +     * @since SAX 2.1alpha
   1.504 +     *
   1.505 +     * @exception IllegalStateException when attempting to set this
   1.506 +     *  after any context has been pushed.
   1.507 +     */
   1.508 +    public void setNamespaceDeclUris (boolean value)
   1.509 +    {
   1.510 +        if (contextPos != 0)
   1.511 +            throw new IllegalStateException ();
   1.512 +        if (value == namespaceDeclUris)
   1.513 +            return;
   1.514 +        namespaceDeclUris = value;
   1.515 +        if (value)
   1.516 +            currentContext.declarePrefix ("xmlns", NSDECL);
   1.517 +        else {
   1.518 +            contexts[contextPos] = currentContext = new Context();
   1.519 +            currentContext.declarePrefix("xml", XMLNS);
   1.520 +        }
   1.521 +    }
   1.522 +
   1.523 +    /**
   1.524 +     * Returns true if namespace declaration attributes are placed into
   1.525 +     * a namespace.  This behavior is not the default.
   1.526 +     *
   1.527 +     * @since SAX 2.1alpha
   1.528 +     */
   1.529 +    public boolean isNamespaceDeclUris ()
   1.530 +        { return namespaceDeclUris; }
   1.531 +
   1.532 +
   1.533 +
   1.534 +    ////////////////////////////////////////////////////////////////////
   1.535 +    // Internal state.
   1.536 +    ////////////////////////////////////////////////////////////////////
   1.537 +
   1.538 +    private Context contexts[];
   1.539 +    private Context currentContext;
   1.540 +    private int contextPos;
   1.541 +    private boolean namespaceDeclUris;
   1.542 +
   1.543 +
   1.544 +    ////////////////////////////////////////////////////////////////////
   1.545 +    // Internal classes.
   1.546 +    ////////////////////////////////////////////////////////////////////
   1.547 +
   1.548 +    /**
   1.549 +     * Internal class for a single Namespace context.
   1.550 +     *
   1.551 +     * <p>This module caches and reuses Namespace contexts,
   1.552 +     * so the number allocated
   1.553 +     * will be equal to the element depth of the document, not to the total
   1.554 +     * number of elements (i.e. 5-10 rather than tens of thousands).
   1.555 +     * Also, data structures used to represent contexts are shared when
   1.556 +     * possible (child contexts without declarations) to further reduce
   1.557 +     * the amount of memory that's consumed.
   1.558 +     * </p>
   1.559 +     */
   1.560 +    final class Context {
   1.561 +
   1.562 +        /**
   1.563 +         * Create the root-level Namespace context.
   1.564 +         */
   1.565 +        Context ()
   1.566 +        {
   1.567 +            copyTables();
   1.568 +        }
   1.569 +
   1.570 +
   1.571 +        /**
   1.572 +         * (Re)set the parent of this Namespace context.
   1.573 +         * The context must either have been freshly constructed,
   1.574 +         * or must have been cleared.
   1.575 +         *
   1.576 +         * @param context The parent Namespace context object.
   1.577 +         */
   1.578 +        void setParent (Context parent)
   1.579 +        {
   1.580 +            this.parent = parent;
   1.581 +            declarations = null;
   1.582 +            prefixTable = parent.prefixTable;
   1.583 +            uriTable = parent.uriTable;
   1.584 +            elementNameTable = parent.elementNameTable;
   1.585 +            attributeNameTable = parent.attributeNameTable;
   1.586 +            defaultNS = parent.defaultNS;
   1.587 +            declSeen = false;
   1.588 +        }
   1.589 +
   1.590 +        /**
   1.591 +         * Makes associated state become collectible,
   1.592 +         * invalidating this context.
   1.593 +         * {@link #setParent} must be called before
   1.594 +         * this context may be used again.
   1.595 +         */
   1.596 +        void clear ()
   1.597 +        {
   1.598 +            parent = null;
   1.599 +            prefixTable = null;
   1.600 +            uriTable = null;
   1.601 +            elementNameTable = null;
   1.602 +            attributeNameTable = null;
   1.603 +            defaultNS = "";
   1.604 +        }
   1.605 +
   1.606 +
   1.607 +        /**
   1.608 +         * Declare a Namespace prefix for this context.
   1.609 +         *
   1.610 +         * @param prefix The prefix to declare.
   1.611 +         * @param uri The associated Namespace URI.
   1.612 +         * @see org.xml.sax.helpers.NamespaceSupport#declarePrefix
   1.613 +         */
   1.614 +        void declarePrefix (String prefix, String uri)
   1.615 +        {
   1.616 +                                // Lazy processing...
   1.617 +//          if (!declsOK)
   1.618 +//              throw new IllegalStateException (
   1.619 +//                  "can't declare any more prefixes in this context");
   1.620 +            if (!declSeen) {
   1.621 +                copyTables();
   1.622 +            }
   1.623 +            if (declarations == null) {
   1.624 +                declarations = new Vector();
   1.625 +            }
   1.626 +
   1.627 +            prefix = prefix.intern();
   1.628 +            uri = uri.intern();
   1.629 +            if ("".equals(prefix)) {
   1.630 +                    defaultNS = uri;
   1.631 +            } else {
   1.632 +                prefixTable.put(prefix, uri);
   1.633 +                uriTable.put(uri, prefix); // may wipe out another prefix
   1.634 +            }
   1.635 +            declarations.addElement(prefix);
   1.636 +        }
   1.637 +
   1.638 +
   1.639 +        /**
   1.640 +         * Process an XML qualified name in this context.
   1.641 +         *
   1.642 +         * @param qName The XML qualified name.
   1.643 +         * @param isAttribute true if this is an attribute name.
   1.644 +         * @return An array of three strings containing the
   1.645 +         *         URI part (or empty string), the local part,
   1.646 +         *         and the raw name, all internalized, or null
   1.647 +         *         if there is an undeclared prefix.
   1.648 +         * @see org.xml.sax.helpers.NamespaceSupport#processName
   1.649 +         */
   1.650 +        String [] processName (String qName, boolean isAttribute)
   1.651 +        {
   1.652 +            String name[];
   1.653 +            Hashtable table;
   1.654 +
   1.655 +                                // Select the appropriate table.
   1.656 +            if (isAttribute) {
   1.657 +                table = attributeNameTable;
   1.658 +            } else {
   1.659 +                table = elementNameTable;
   1.660 +            }
   1.661 +
   1.662 +                                // Start by looking in the cache, and
   1.663 +                                // return immediately if the name
   1.664 +                                // is already known in this content
   1.665 +            name = (String[])table.get(qName);
   1.666 +            if (name != null) {
   1.667 +                return name;
   1.668 +            }
   1.669 +
   1.670 +                                // We haven't seen this name in this
   1.671 +                                // context before.  Maybe in the parent
   1.672 +                                // context, but we can't assume prefix
   1.673 +                                // bindings are the same.
   1.674 +            name = new String[3];
   1.675 +            name[2] = qName.intern();
   1.676 +            int index = qName.indexOf(':');
   1.677 +
   1.678 +
   1.679 +                                // No prefix.
   1.680 +            if (index == -1) {
   1.681 +                if (isAttribute) {
   1.682 +                    if (qName == "xmlns" && namespaceDeclUris)
   1.683 +                        name[0] = NSDECL;
   1.684 +                    else
   1.685 +                        name[0] = "";
   1.686 +                } else {
   1.687 +                    name[0] = defaultNS;
   1.688 +                }
   1.689 +                name[1] = name[2];
   1.690 +            }
   1.691 +
   1.692 +                                // Prefix
   1.693 +            else {
   1.694 +                String prefix = qName.substring(0, index);
   1.695 +                String local = qName.substring(index+1);
   1.696 +                String uri;
   1.697 +                if ("".equals(prefix)) {
   1.698 +                    uri = defaultNS;
   1.699 +                } else {
   1.700 +                    uri = (String)prefixTable.get(prefix);
   1.701 +                }
   1.702 +                if (uri == null
   1.703 +                        || (!isAttribute && "xmlns".equals (prefix))) {
   1.704 +                    return null;
   1.705 +                }
   1.706 +                name[0] = uri;
   1.707 +                name[1] = local.intern();
   1.708 +            }
   1.709 +
   1.710 +                                // Save in the cache for future use.
   1.711 +                                // (Could be shared with parent context...)
   1.712 +            table.put(name[2], name);
   1.713 +            return name;
   1.714 +        }
   1.715 +
   1.716 +
   1.717 +        /**
   1.718 +         * Look up the URI associated with a prefix in this context.
   1.719 +         *
   1.720 +         * @param prefix The prefix to look up.
   1.721 +         * @return The associated Namespace URI, or null if none is
   1.722 +         *         declared.
   1.723 +         * @see org.xml.sax.helpers.NamespaceSupport#getURI
   1.724 +         */
   1.725 +        String getURI (String prefix)
   1.726 +        {
   1.727 +            if ("".equals(prefix)) {
   1.728 +                return defaultNS;
   1.729 +            } else if (prefixTable == null) {
   1.730 +                return null;
   1.731 +            } else {
   1.732 +                return (String)prefixTable.get(prefix);
   1.733 +            }
   1.734 +        }
   1.735 +
   1.736 +
   1.737 +        /**
   1.738 +         * Look up one of the prefixes associated with a URI in this context.
   1.739 +         *
   1.740 +         * <p>Since many prefixes may be mapped to the same URI,
   1.741 +         * the return value may be unreliable.</p>
   1.742 +         *
   1.743 +         * @param uri The URI to look up.
   1.744 +         * @return The associated prefix, or null if none is declared.
   1.745 +         * @see org.xml.sax.helpers.NamespaceSupport#getPrefix
   1.746 +         */
   1.747 +        String getPrefix (String uri) {
   1.748 +            if (uriTable != null) {
   1.749 +                String uriPrefix = (String)uriTable.get(uri);
   1.750 +                if (uriPrefix == null) return null;
   1.751 +                String verifyNamespace = (String) prefixTable.get(uriPrefix);
   1.752 +                if (uri.equals(verifyNamespace)) {
   1.753 +                    return uriPrefix;
   1.754 +                }
   1.755 +            }
   1.756 +            return null;
   1.757 +        }
   1.758 +
   1.759 +
   1.760 +        /**
   1.761 +         * Return an enumeration of prefixes declared in this context.
   1.762 +         *
   1.763 +         * @return An enumeration of prefixes (possibly empty).
   1.764 +         * @see org.xml.sax.helpers.NamespaceSupport#getDeclaredPrefixes
   1.765 +         */
   1.766 +        Enumeration getDeclaredPrefixes ()
   1.767 +        {
   1.768 +            if (declarations == null) {
   1.769 +                return EMPTY_ENUMERATION;
   1.770 +            } else {
   1.771 +                return declarations.elements();
   1.772 +            }
   1.773 +        }
   1.774 +
   1.775 +
   1.776 +        /**
   1.777 +         * Return an enumeration of all prefixes currently in force.
   1.778 +         *
   1.779 +         * <p>The default prefix, if in force, is <em>not</em>
   1.780 +         * returned, and will have to be checked for separately.</p>
   1.781 +         *
   1.782 +         * @return An enumeration of prefixes (never empty).
   1.783 +         * @see org.xml.sax.helpers.NamespaceSupport#getPrefixes
   1.784 +         */
   1.785 +        Enumeration getPrefixes ()
   1.786 +        {
   1.787 +            if (prefixTable == null) {
   1.788 +                return EMPTY_ENUMERATION;
   1.789 +            } else {
   1.790 +                return prefixTable.keys();
   1.791 +            }
   1.792 +        }
   1.793 +
   1.794 +
   1.795 +
   1.796 +        ////////////////////////////////////////////////////////////////
   1.797 +        // Internal methods.
   1.798 +        ////////////////////////////////////////////////////////////////
   1.799 +
   1.800 +
   1.801 +        /**
   1.802 +         * Copy on write for the internal tables in this context.
   1.803 +         *
   1.804 +         * <p>This class is optimized for the normal case where most
   1.805 +         * elements do not contain Namespace declarations.</p>
   1.806 +         */
   1.807 +        private void copyTables ()
   1.808 +        {
   1.809 +            if (prefixTable != null) {
   1.810 +                prefixTable = (Hashtable)prefixTable.clone();
   1.811 +            } else {
   1.812 +                prefixTable = new Hashtable();
   1.813 +            }
   1.814 +            if (uriTable != null) {
   1.815 +                uriTable = (Hashtable)uriTable.clone();
   1.816 +            } else {
   1.817 +                uriTable = new Hashtable();
   1.818 +            }
   1.819 +            elementNameTable = new Hashtable();
   1.820 +            attributeNameTable = new Hashtable();
   1.821 +            declSeen = true;
   1.822 +        }
   1.823 +
   1.824 +
   1.825 +
   1.826 +        ////////////////////////////////////////////////////////////////
   1.827 +        // Protected state.
   1.828 +        ////////////////////////////////////////////////////////////////
   1.829 +
   1.830 +        Hashtable prefixTable;
   1.831 +        Hashtable uriTable;
   1.832 +        Hashtable elementNameTable;
   1.833 +        Hashtable attributeNameTable;
   1.834 +        String defaultNS = "";
   1.835 +
   1.836 +
   1.837 +
   1.838 +        ////////////////////////////////////////////////////////////////
   1.839 +        // Internal state.
   1.840 +        ////////////////////////////////////////////////////////////////
   1.841 +
   1.842 +        private Vector declarations = null;
   1.843 +        private boolean declSeen = false;
   1.844 +        private Context parent = null;
   1.845 +    }
   1.846 +}
   1.847 +
   1.848 +// end of NamespaceSupport.java

mercurial