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

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 // NamespaceSupport.java - generic Namespace support for SAX.
    27 // http://www.saxproject.org
    28 // Written by David Megginson
    29 // This class is in the Public Domain.  NO WARRANTY!
    31 package com.sun.xml.internal.txw2;
    33 import java.util.EmptyStackException;
    34 import java.util.Enumeration;
    35 import java.util.Hashtable;
    36 import java.util.Vector;
    39 /**
    40  * Encapsulate Namespace logic for use by applications using SAX,
    41  * or internally by SAX drivers.
    42  *
    43  * <blockquote>
    44  * <em>This module, both source code and documentation, is in the
    45  * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
    46  * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
    47  * for further information.
    48  * </blockquote>
    49  *
    50  * <p>This class encapsulates the logic of Namespace processing: it
    51  * tracks the declarations currently in force for each context and
    52  * automatically processes qualified XML names into their Namespace
    53  * parts; it can also be used in reverse for generating XML qnames
    54  * from Namespaces.</p>
    55  *
    56  * <p>Namespace support objects are reusable, but the reset method
    57  * must be invoked between each session.</p>
    58  *
    59  * <p>Here is a simple session:</p>
    60  *
    61  * <pre>
    62  * String parts[] = new String[3];
    63  * NamespaceSupport support = new NamespaceSupport();
    64  *
    65  * support.pushContext();
    66  * support.declarePrefix("", "http://www.w3.org/1999/xhtml");
    67  * support.declarePrefix("dc", "http://www.purl.org/dc#");
    68  *
    69  * parts = support.processName("p", parts, false);
    70  * System.out.println("Namespace URI: " + parts[0]);
    71  * System.out.println("Local name: " + parts[1]);
    72  * System.out.println("Raw name: " + parts[2]);
    73  *
    74  * parts = support.processName("dc:title", parts, false);
    75  * System.out.println("Namespace URI: " + parts[0]);
    76  * System.out.println("Local name: " + parts[1]);
    77  * System.out.println("Raw name: " + parts[2]);
    78  *
    79  * support.popContext();
    80  * </pre>
    81  *
    82  * <p>Note that this class is optimized for the use case where most
    83  * elements do not contain Namespace declarations: if the same
    84  * prefix/URI mapping is repeated for each context (for example), this
    85  * class will be somewhat less efficient.</p>
    86  *
    87  * <p>Although SAX drivers (parsers) may choose to use this class to
    88  * implement namespace handling, they are not required to do so.
    89  * Applications must track namespace information themselves if they
    90  * want to use namespace information.
    91  *
    92  * @since SAX 2.0
    93  * @author David Megginson
    94  * @version 2.0.1 (sax2r2)
    95  */
    96 final class NamespaceSupport
    97 {
   100     ////////////////////////////////////////////////////////////////////
   101     // Constants.
   102     ////////////////////////////////////////////////////////////////////
   105     /**
   106      * The XML Namespace URI as a constant.
   107      * The value is <code>http://www.w3.org/XML/1998/namespace</code>
   108      * as defined in the "Namespaces in XML" * recommendation.
   109      *
   110      * <p>This is the Namespace URI that is automatically mapped
   111      * to the "xml" prefix.</p>
   112      */
   113     public final static String XMLNS =
   114         "http://www.w3.org/XML/1998/namespace";
   117     /**
   118      * The namespace declaration URI as a constant.
   119      * The value is <code>http://www.w3.org/xmlns/2000/</code>, as defined
   120      * in a backwards-incompatible erratum to the "Namespaces in XML"
   121      * recommendation.  Because that erratum postdated SAX2, SAX2 defaults
   122      * to the original recommendation, and does not normally use this URI.
   123      *
   124      *
   125      * <p>This is the Namespace URI that is optionally applied to
   126      * <em>xmlns</em> and <em>xmlns:*</em> attributes, which are used to
   127      * declare namespaces.  </p>
   128      *
   129      * @since SAX 2.1alpha
   130      * @see #setNamespaceDeclUris
   131      * @see #isNamespaceDeclUris
   132      */
   133     public final static String NSDECL =
   134         "http://www.w3.org/xmlns/2000/";
   137     /**
   138      * An empty enumeration.
   139      */
   140     private final static Enumeration EMPTY_ENUMERATION =
   141         new Vector().elements();
   144     ////////////////////////////////////////////////////////////////////
   145     // Constructor.
   146     ////////////////////////////////////////////////////////////////////
   149     /**
   150      * Create a new Namespace support object.
   151      */
   152     public NamespaceSupport ()
   153     {
   154         reset();
   155     }
   159     ////////////////////////////////////////////////////////////////////
   160     // Context management.
   161     ////////////////////////////////////////////////////////////////////
   164     /**
   165      * Reset this Namespace support object for reuse.
   166      *
   167      * <p>It is necessary to invoke this method before reusing the
   168      * Namespace support object for a new session.  If namespace
   169      * declaration URIs are to be supported, that flag must also
   170      * be set to a non-default value.
   171      * </p>
   172      *
   173      * @see #setNamespaceDeclUris
   174      */
   175     public void reset ()
   176     {
   177         contexts = new Context[32];
   178         namespaceDeclUris = false;
   179         contextPos = 0;
   180         contexts[contextPos] = currentContext = new Context();
   181         currentContext.declarePrefix("xml", XMLNS);
   182     }
   185     /**
   186      * Start a new Namespace context.
   187      * The new context will automatically inherit
   188      * the declarations of its parent context, but it will also keep
   189      * track of which declarations were made within this context.
   190      *
   191      * <p>Event callback code should start a new context once per element.
   192      * This means being ready to call this in either of two places.
   193      * For elements that don't include namespace declarations, the
   194      * <em>ContentHandler.startElement()</em> callback is the right place.
   195      * For elements with such a declaration, it'd done in the first
   196      * <em>ContentHandler.startPrefixMapping()</em> callback.
   197      * A boolean flag can be used to
   198      * track whether a context has been started yet.  When either of
   199      * those methods is called, it checks the flag to see if a new context
   200      * needs to be started.  If so, it starts the context and sets the
   201      * flag.  After <em>ContentHandler.startElement()</em>
   202      * does that, it always clears the flag.
   203      *
   204      * <p>Normally, SAX drivers would push a new context at the beginning
   205      * of each XML element.  Then they perform a first pass over the
   206      * attributes to process all namespace declarations, making
   207      * <em>ContentHandler.startPrefixMapping()</em> callbacks.
   208      * Then a second pass is made, to determine the namespace-qualified
   209      * names for all attributes and for the element name.
   210      * Finally all the information for the
   211      * <em>ContentHandler.startElement()</em> callback is available,
   212      * so it can then be made.
   213      *
   214      * <p>The Namespace support object always starts with a base context
   215      * already in force: in this context, only the "xml" prefix is
   216      * declared.</p>
   217      *
   218      * @see org.xml.sax.ContentHandler
   219      * @see #popContext
   220      */
   221     public void pushContext ()
   222     {
   223         int max = contexts.length;
   225         contextPos++;
   227                                 // Extend the array if necessary
   228         if (contextPos >= max) {
   229             Context newContexts[] = new Context[max*2];
   230             System.arraycopy(contexts, 0, newContexts, 0, max);
   231             max *= 2;
   232             contexts = newContexts;
   233         }
   235                                 // Allocate the context if necessary.
   236         currentContext = contexts[contextPos];
   237         if (currentContext == null) {
   238             contexts[contextPos] = currentContext = new Context();
   239         }
   241                                 // Set the parent, if any.
   242         if (contextPos > 0) {
   243             currentContext.setParent(contexts[contextPos - 1]);
   244         }
   245     }
   248     /**
   249      * Revert to the previous Namespace context.
   250      *
   251      * <p>Normally, you should pop the context at the end of each
   252      * XML element.  After popping the context, all Namespace prefix
   253      * mappings that were previously in force are restored.</p>
   254      *
   255      * <p>You must not attempt to declare additional Namespace
   256      * prefixes after popping a context, unless you push another
   257      * context first.</p>
   258      *
   259      * @see #pushContext
   260      */
   261     public void popContext ()
   262     {
   263         contexts[contextPos].clear();
   264         contextPos--;
   265         if (contextPos < 0) {
   266             throw new EmptyStackException();
   267         }
   268         currentContext = contexts[contextPos];
   269     }
   273     ////////////////////////////////////////////////////////////////////
   274     // Operations within a context.
   275     ////////////////////////////////////////////////////////////////////
   278     /**
   279      * Declare a Namespace prefix.  All prefixes must be declared
   280      * before they are referenced.  For example, a SAX driver (parser)
   281      * would scan an element's attributes
   282      * in two passes:  first for namespace declarations,
   283      * then a second pass using {@link #processName processName()} to
   284      * interpret prefixes against (potentially redefined) prefixes.
   285      *
   286      * <p>This method declares a prefix in the current Namespace
   287      * context; the prefix will remain in force until this context
   288      * is popped, unless it is shadowed in a descendant context.</p>
   289      *
   290      * <p>To declare the default element Namespace, use the empty string as
   291      * the prefix.</p>
   292      *
   293      * <p>Note that there is an asymmetry in this library: {@link
   294      * #getPrefix getPrefix} will not return the "" prefix,
   295      * even if you have declared a default element namespace.
   296      * To check for a default namespace,
   297      * you have to look it up explicitly using {@link #getURI getURI}.
   298      * This asymmetry exists to make it easier to look up prefixes
   299      * for attribute names, where the default prefix is not allowed.</p>
   300      *
   301      * @param prefix The prefix to declare, or the empty string to
   302      *  indicate the default element namespace.  This may never have
   303      *  the value "xml" or "xmlns".
   304      * @param uri The Namespace URI to associate with the prefix.
   305      * @return true if the prefix was legal, false otherwise
   306      *
   307      * @see #processName
   308      * @see #getURI
   309      * @see #getPrefix
   310      */
   311     public boolean declarePrefix (String prefix, String uri)
   312     {
   313         if (prefix.equals("xml") || prefix.equals("xmlns")) {
   314             return false;
   315         } else {
   316             currentContext.declarePrefix(prefix, uri);
   317             return true;
   318         }
   319     }
   322     /**
   323      * Process a raw XML qualified name, after all declarations in the
   324      * current context have been handled by {@link #declarePrefix
   325      * declarePrefix()}.
   326      *
   327      * <p>This method processes a raw XML qualified name in the
   328      * current context by removing the prefix and looking it up among
   329      * the prefixes currently declared.  The return value will be the
   330      * array supplied by the caller, filled in as follows:</p>
   331      *
   332      * <dl>
   333      * <dt>parts[0]</dt>
   334      * <dd>The Namespace URI, or an empty string if none is
   335      *  in use.</dd>
   336      * <dt>parts[1]</dt>
   337      * <dd>The local name (without prefix).</dd>
   338      * <dt>parts[2]</dt>
   339      * <dd>The original raw name.</dd>
   340      * </dl>
   341      *
   342      * <p>All of the strings in the array will be internalized.  If
   343      * the raw name has a prefix that has not been declared, then
   344      * the return value will be null.</p>
   345      *
   346      * <p>Note that attribute names are processed differently than
   347      * element names: an unprefixed element name will receive the
   348      * default Namespace (if any), while an unprefixed attribute name
   349      * will not.</p>
   350      *
   351      * @param qName The XML qualified name to be processed.
   352      * @param parts An array supplied by the caller, capable of
   353      *        holding at least three members.
   354      * @param isAttribute A flag indicating whether this is an
   355      *        attribute name (true) or an element name (false).
   356      * @return The supplied array holding three internalized strings
   357      *        representing the Namespace URI (or empty string), the
   358      *        local name, and the XML qualified name; or null if there
   359      *        is an undeclared prefix.
   360      * @see #declarePrefix
   361      * @see java.lang.String#intern */
   362     public String [] processName (String qName, String parts[],
   363                                   boolean isAttribute)
   364     {
   365         String myParts[] = currentContext.processName(qName, isAttribute);
   366         if (myParts == null) {
   367             return null;
   368         } else {
   369             parts[0] = myParts[0];
   370             parts[1] = myParts[1];
   371             parts[2] = myParts[2];
   372             return parts;
   373         }
   374     }
   377     /**
   378      * Look up a prefix and get the currently-mapped Namespace URI.
   379      *
   380      * <p>This method looks up the prefix in the current context.
   381      * Use the empty string ("") for the default Namespace.</p>
   382      *
   383      * @param prefix The prefix to look up.
   384      * @return The associated Namespace URI, or null if the prefix
   385      *         is undeclared in this context.
   386      * @see #getPrefix
   387      * @see #getPrefixes
   388      */
   389     public String getURI (String prefix)
   390     {
   391         return currentContext.getURI(prefix);
   392     }
   395     /**
   396      * Return an enumeration of all prefixes whose declarations are
   397      * active in the current context.
   398      * This includes declarations from parent contexts that have
   399      * not been overridden.
   400      *
   401      * <p><strong>Note:</strong> if there is a default prefix, it will not be
   402      * returned in this enumeration; check for the default prefix
   403      * using the {@link #getURI getURI} with an argument of "".</p>
   404      *
   405      * @return An enumeration of prefixes (never empty).
   406      * @see #getDeclaredPrefixes
   407      * @see #getURI
   408      */
   409     public Enumeration getPrefixes ()
   410     {
   411         return currentContext.getPrefixes();
   412     }
   415     /**
   416      * Return one of the prefixes mapped to a Namespace URI.
   417      *
   418      * <p>If more than one prefix is currently mapped to the same
   419      * URI, this method will make an arbitrary selection; if you
   420      * want all of the prefixes, use the {@link #getPrefixes}
   421      * method instead.</p>
   422      *
   423      * <p><strong>Note:</strong> this will never return the empty (default) prefix;
   424      * to check for a default prefix, use the {@link #getURI getURI}
   425      * method with an argument of "".</p>
   426      *
   427      * @param uri the namespace URI
   428      * @return one of the prefixes currently mapped to the URI supplied,
   429      *         or null if none is mapped or if the URI is assigned to
   430      *         the default namespace
   431      * @see #getPrefixes(java.lang.String)
   432      * @see #getURI
   433      */
   434     public String getPrefix (String uri)
   435     {
   436         return currentContext.getPrefix(uri);
   437     }
   440     /**
   441      * Return an enumeration of all prefixes for a given URI whose
   442      * declarations are active in the current context.
   443      * This includes declarations from parent contexts that have
   444      * not been overridden.
   445      *
   446      * <p>This method returns prefixes mapped to a specific Namespace
   447      * URI.  The xml: prefix will be included.  If you want only one
   448      * prefix that's mapped to the Namespace URI, and you don't care
   449      * which one you get, use the {@link #getPrefix getPrefix}
   450      *  method instead.</p>
   451      *
   452      * <p><strong>Note:</strong> the empty (default) prefix is <em>never</em> included
   453      * in this enumeration; to check for the presence of a default
   454      * Namespace, use the {@link #getURI getURI} method with an
   455      * argument of "".</p>
   456      *
   457      * @param uri The Namespace URI.
   458      * @return An enumeration of prefixes (never empty).
   459      * @see #getPrefix
   460      * @see #getDeclaredPrefixes
   461      * @see #getURI
   462      */
   463     public Enumeration getPrefixes (String uri)
   464     {
   465         Vector prefixes = new Vector();
   466         Enumeration allPrefixes = getPrefixes();
   467         while (allPrefixes.hasMoreElements()) {
   468             String prefix = (String)allPrefixes.nextElement();
   469             if (uri.equals(getURI(prefix))) {
   470                 prefixes.addElement(prefix);
   471             }
   472         }
   473         return prefixes.elements();
   474     }
   477     /**
   478      * Return an enumeration of all prefixes declared in this context.
   479      *
   480      * <p>The empty (default) prefix will be included in this
   481      * enumeration; note that this behaviour differs from that of
   482      * {@link #getPrefix} and {@link #getPrefixes}.</p>
   483      *
   484      * @return An enumeration of all prefixes declared in this
   485      *         context.
   486      * @see #getPrefixes
   487      * @see #getURI
   488      */
   489     public Enumeration getDeclaredPrefixes ()
   490     {
   491         return currentContext.getDeclaredPrefixes();
   492     }
   494     /**
   495      * Controls whether namespace declaration attributes are placed
   496      * into the {@link #NSDECL NSDECL} namespace
   497      * by {@link #processName processName()}.  This may only be
   498      * changed before any contexts have been pushed.
   499      *
   500      * @since SAX 2.1alpha
   501      *
   502      * @exception IllegalStateException when attempting to set this
   503      *  after any context has been pushed.
   504      */
   505     public void setNamespaceDeclUris (boolean value)
   506     {
   507         if (contextPos != 0)
   508             throw new IllegalStateException ();
   509         if (value == namespaceDeclUris)
   510             return;
   511         namespaceDeclUris = value;
   512         if (value)
   513             currentContext.declarePrefix ("xmlns", NSDECL);
   514         else {
   515             contexts[contextPos] = currentContext = new Context();
   516             currentContext.declarePrefix("xml", XMLNS);
   517         }
   518     }
   520     /**
   521      * Returns true if namespace declaration attributes are placed into
   522      * a namespace.  This behavior is not the default.
   523      *
   524      * @since SAX 2.1alpha
   525      */
   526     public boolean isNamespaceDeclUris ()
   527         { return namespaceDeclUris; }
   531     ////////////////////////////////////////////////////////////////////
   532     // Internal state.
   533     ////////////////////////////////////////////////////////////////////
   535     private Context contexts[];
   536     private Context currentContext;
   537     private int contextPos;
   538     private boolean namespaceDeclUris;
   541     ////////////////////////////////////////////////////////////////////
   542     // Internal classes.
   543     ////////////////////////////////////////////////////////////////////
   545     /**
   546      * Internal class for a single Namespace context.
   547      *
   548      * <p>This module caches and reuses Namespace contexts,
   549      * so the number allocated
   550      * will be equal to the element depth of the document, not to the total
   551      * number of elements (i.e. 5-10 rather than tens of thousands).
   552      * Also, data structures used to represent contexts are shared when
   553      * possible (child contexts without declarations) to further reduce
   554      * the amount of memory that's consumed.
   555      * </p>
   556      */
   557     final class Context {
   559         /**
   560          * Create the root-level Namespace context.
   561          */
   562         Context ()
   563         {
   564             copyTables();
   565         }
   568         /**
   569          * (Re)set the parent of this Namespace context.
   570          * The context must either have been freshly constructed,
   571          * or must have been cleared.
   572          *
   573          * @param context The parent Namespace context object.
   574          */
   575         void setParent (Context parent)
   576         {
   577             this.parent = parent;
   578             declarations = null;
   579             prefixTable = parent.prefixTable;
   580             uriTable = parent.uriTable;
   581             elementNameTable = parent.elementNameTable;
   582             attributeNameTable = parent.attributeNameTable;
   583             defaultNS = parent.defaultNS;
   584             declSeen = false;
   585         }
   587         /**
   588          * Makes associated state become collectible,
   589          * invalidating this context.
   590          * {@link #setParent} must be called before
   591          * this context may be used again.
   592          */
   593         void clear ()
   594         {
   595             parent = null;
   596             prefixTable = null;
   597             uriTable = null;
   598             elementNameTable = null;
   599             attributeNameTable = null;
   600             defaultNS = "";
   601         }
   604         /**
   605          * Declare a Namespace prefix for this context.
   606          *
   607          * @param prefix The prefix to declare.
   608          * @param uri The associated Namespace URI.
   609          * @see org.xml.sax.helpers.NamespaceSupport#declarePrefix
   610          */
   611         void declarePrefix (String prefix, String uri)
   612         {
   613                                 // Lazy processing...
   614 //          if (!declsOK)
   615 //              throw new IllegalStateException (
   616 //                  "can't declare any more prefixes in this context");
   617             if (!declSeen) {
   618                 copyTables();
   619             }
   620             if (declarations == null) {
   621                 declarations = new Vector();
   622             }
   624             prefix = prefix.intern();
   625             uri = uri.intern();
   626             if ("".equals(prefix)) {
   627                     defaultNS = uri;
   628             } else {
   629                 prefixTable.put(prefix, uri);
   630                 uriTable.put(uri, prefix); // may wipe out another prefix
   631             }
   632             declarations.addElement(prefix);
   633         }
   636         /**
   637          * Process an XML qualified name in this context.
   638          *
   639          * @param qName The XML qualified name.
   640          * @param isAttribute true if this is an attribute name.
   641          * @return An array of three strings containing the
   642          *         URI part (or empty string), the local part,
   643          *         and the raw name, all internalized, or null
   644          *         if there is an undeclared prefix.
   645          * @see org.xml.sax.helpers.NamespaceSupport#processName
   646          */
   647         String [] processName (String qName, boolean isAttribute)
   648         {
   649             String name[];
   650             Hashtable table;
   652                                 // Select the appropriate table.
   653             if (isAttribute) {
   654                 table = attributeNameTable;
   655             } else {
   656                 table = elementNameTable;
   657             }
   659                                 // Start by looking in the cache, and
   660                                 // return immediately if the name
   661                                 // is already known in this content
   662             name = (String[])table.get(qName);
   663             if (name != null) {
   664                 return name;
   665             }
   667                                 // We haven't seen this name in this
   668                                 // context before.  Maybe in the parent
   669                                 // context, but we can't assume prefix
   670                                 // bindings are the same.
   671             name = new String[3];
   672             name[2] = qName.intern();
   673             int index = qName.indexOf(':');
   676                                 // No prefix.
   677             if (index == -1) {
   678                 if (isAttribute) {
   679                     if (qName == "xmlns" && namespaceDeclUris)
   680                         name[0] = NSDECL;
   681                     else
   682                         name[0] = "";
   683                 } else {
   684                     name[0] = defaultNS;
   685                 }
   686                 name[1] = name[2];
   687             }
   689                                 // Prefix
   690             else {
   691                 String prefix = qName.substring(0, index);
   692                 String local = qName.substring(index+1);
   693                 String uri;
   694                 if ("".equals(prefix)) {
   695                     uri = defaultNS;
   696                 } else {
   697                     uri = (String)prefixTable.get(prefix);
   698                 }
   699                 if (uri == null
   700                         || (!isAttribute && "xmlns".equals (prefix))) {
   701                     return null;
   702                 }
   703                 name[0] = uri;
   704                 name[1] = local.intern();
   705             }
   707                                 // Save in the cache for future use.
   708                                 // (Could be shared with parent context...)
   709             table.put(name[2], name);
   710             return name;
   711         }
   714         /**
   715          * Look up the URI associated with a prefix in this context.
   716          *
   717          * @param prefix The prefix to look up.
   718          * @return The associated Namespace URI, or null if none is
   719          *         declared.
   720          * @see org.xml.sax.helpers.NamespaceSupport#getURI
   721          */
   722         String getURI (String prefix)
   723         {
   724             if ("".equals(prefix)) {
   725                 return defaultNS;
   726             } else if (prefixTable == null) {
   727                 return null;
   728             } else {
   729                 return (String)prefixTable.get(prefix);
   730             }
   731         }
   734         /**
   735          * Look up one of the prefixes associated with a URI in this context.
   736          *
   737          * <p>Since many prefixes may be mapped to the same URI,
   738          * the return value may be unreliable.</p>
   739          *
   740          * @param uri The URI to look up.
   741          * @return The associated prefix, or null if none is declared.
   742          * @see org.xml.sax.helpers.NamespaceSupport#getPrefix
   743          */
   744         String getPrefix (String uri) {
   745             if (uriTable != null) {
   746                 String uriPrefix = (String)uriTable.get(uri);
   747                 if (uriPrefix == null) return null;
   748                 String verifyNamespace = (String) prefixTable.get(uriPrefix);
   749                 if (uri.equals(verifyNamespace)) {
   750                     return uriPrefix;
   751                 }
   752             }
   753             return null;
   754         }
   757         /**
   758          * Return an enumeration of prefixes declared in this context.
   759          *
   760          * @return An enumeration of prefixes (possibly empty).
   761          * @see org.xml.sax.helpers.NamespaceSupport#getDeclaredPrefixes
   762          */
   763         Enumeration getDeclaredPrefixes ()
   764         {
   765             if (declarations == null) {
   766                 return EMPTY_ENUMERATION;
   767             } else {
   768                 return declarations.elements();
   769             }
   770         }
   773         /**
   774          * Return an enumeration of all prefixes currently in force.
   775          *
   776          * <p>The default prefix, if in force, is <em>not</em>
   777          * returned, and will have to be checked for separately.</p>
   778          *
   779          * @return An enumeration of prefixes (never empty).
   780          * @see org.xml.sax.helpers.NamespaceSupport#getPrefixes
   781          */
   782         Enumeration getPrefixes ()
   783         {
   784             if (prefixTable == null) {
   785                 return EMPTY_ENUMERATION;
   786             } else {
   787                 return prefixTable.keys();
   788             }
   789         }
   793         ////////////////////////////////////////////////////////////////
   794         // Internal methods.
   795         ////////////////////////////////////////////////////////////////
   798         /**
   799          * Copy on write for the internal tables in this context.
   800          *
   801          * <p>This class is optimized for the normal case where most
   802          * elements do not contain Namespace declarations.</p>
   803          */
   804         private void copyTables ()
   805         {
   806             if (prefixTable != null) {
   807                 prefixTable = (Hashtable)prefixTable.clone();
   808             } else {
   809                 prefixTable = new Hashtable();
   810             }
   811             if (uriTable != null) {
   812                 uriTable = (Hashtable)uriTable.clone();
   813             } else {
   814                 uriTable = new Hashtable();
   815             }
   816             elementNameTable = new Hashtable();
   817             attributeNameTable = new Hashtable();
   818             declSeen = true;
   819         }
   823         ////////////////////////////////////////////////////////////////
   824         // Protected state.
   825         ////////////////////////////////////////////////////////////////
   827         Hashtable prefixTable;
   828         Hashtable uriTable;
   829         Hashtable elementNameTable;
   830         Hashtable attributeNameTable;
   831         String defaultNS = "";
   835         ////////////////////////////////////////////////////////////////
   836         // Internal state.
   837         ////////////////////////////////////////////////////////////////
   839         private Vector declarations = null;
   840         private boolean declSeen = false;
   841         private Context parent = null;
   842     }
   843 }
   845 // end of NamespaceSupport.java

mercurial