Home » xml-commons-external-1.4.01-src » org.xml » sax » helpers » [javadoc | source]

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

Home » xml-commons-external-1.4.01-src » org.xml » sax » helpers » [javadoc | source]