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