Class NamespaceContextImpl

  • All Implemented Interfaces:
    javax.xml.namespace.NamespaceContext

    public class NamespaceContextImpl
    extends java.lang.Object
    implements javax.xml.namespace.NamespaceContext
    Default implementation of NamespaceContext.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String cachedPrefix  
      private java.lang.String cachedURI  
      private java.util.List prefixList  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String checkContext​(int i)
      This method is used to restore the namespace state after an element is created.
      void endPrefixMapping​(java.lang.String pPrefix)
      Removes the declaration of the prefix, which has been defined last.
      java.lang.String getAttributePrefix​(java.lang.String pURI)
      Returns a non-empty prefix currently mapped to the given URL or null, if there is no such mapping.
      int getContext()
      Returns the current number of assigned prefixes.
      java.lang.String getNamespaceURI​(java.lang.String pPrefix)
      Given a prefix, returns the URI to which the prefix is currently mapped or null, if there is no such mapping.
      java.lang.String getPrefix​(java.lang.String pURI)
      Returns a prefix currently mapped to the given URI or null, if there is no such mapping.
      java.util.List getPrefixes()
      Returns a list of all prefixes, which are currently declared, in the order of declaration.
      java.util.Iterator getPrefixes​(java.lang.String pURI)
      Returns a collection to all prefixes bound to the given namespace URI.
      boolean isPrefixDeclared​(java.lang.String pPrefix)
      Returns whether a given prefix is currently declared.
      void reset()
      Resets the NamespaceSupport's state.
      void startPrefixMapping​(java.lang.String pPrefix, java.lang.String pURI)
      Declares a new prefix.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • prefixList

        private java.util.List prefixList
      • cachedPrefix

        private java.lang.String cachedPrefix
      • cachedURI

        private java.lang.String cachedURI
    • Constructor Detail

      • NamespaceContextImpl

        public NamespaceContextImpl()
    • Method Detail

      • reset

        public void reset()
        Resets the NamespaceSupport's state. Allows reusing the object.
      • startPrefixMapping

        public void startPrefixMapping​(java.lang.String pPrefix,
                                       java.lang.String pURI)
        Declares a new prefix. Typically called from within org.xml.sax.ContextHandler#startPrefixMapping(java.lang.String, java.lang.String).
        Throws:
        java.lang.IllegalArgumentException - Prefix or URI are null.
      • endPrefixMapping

        public void endPrefixMapping​(java.lang.String pPrefix)
        Removes the declaration of the prefix, which has been defined last. Typically called from within org.xml.sax.ContextHandler#endPrefixMapping(java.lang.String).
        Throws:
        java.lang.IllegalArgumentException - The prefix is null.
        java.lang.IllegalStateException - The prefix is not the prefix, which has been defined last. In other words, the calls to startPrefixMapping(String, String), and endPrefixMapping(String) aren't in LIFO order.
      • getNamespaceURI

        public java.lang.String getNamespaceURI​(java.lang.String pPrefix)
        Given a prefix, returns the URI to which the prefix is currently mapped or null, if there is no such mapping.

        Note: This methods behaviour is precisely defined by NamespaceContext.getNamespaceURI(java.lang.String).

        Specified by:
        getNamespaceURI in interface javax.xml.namespace.NamespaceContext
        Parameters:
        pPrefix - The prefix in question
      • getPrefix

        public java.lang.String getPrefix​(java.lang.String pURI)
        Returns a prefix currently mapped to the given URI or null, if there is no such mapping. This method may be used to find a possible prefix for an elements namespace URI. For attributes you should use getAttributePrefix(String). Note: This methods behaviour is precisely defined by NamespaceContext.getPrefix(java.lang.String).
        Specified by:
        getPrefix in interface javax.xml.namespace.NamespaceContext
        Parameters:
        pURI - The namespace URI in question
        Throws:
        java.lang.IllegalArgumentException - The namespace URI is null.
      • getAttributePrefix

        public java.lang.String getAttributePrefix​(java.lang.String pURI)
        Returns a non-empty prefix currently mapped to the given URL or null, if there is no such mapping. This method may be used to find a possible prefix for an attributes namespace URI. For elements you should use getPrefix(String).
        Parameters:
        pURI - Thhe namespace URI in question
        Throws:
        java.lang.IllegalArgumentException - The namespace URI is null.
      • getPrefixes

        public java.util.Iterator getPrefixes​(java.lang.String pURI)
        Returns a collection to all prefixes bound to the given namespace URI. Note: This methods behaviour is precisely defined by NamespaceContext.getPrefixes(java.lang.String).
        Specified by:
        getPrefixes in interface javax.xml.namespace.NamespaceContext
        Parameters:
        pURI - The namespace prefix in question
      • isPrefixDeclared

        public boolean isPrefixDeclared​(java.lang.String pPrefix)
        Returns whether a given prefix is currently declared.
      • getContext

        public int getContext()
        Returns the current number of assigned prefixes. Note, that a prefix may be assigned in several nested elements, in which case every assignment is counted.
        This method is typically called before invoking the method ContentHandler.startElement(String, String, String, org.xml.sax.Attributes). The return value is used as a saveable state. After invoking ContentHandler.endElement(String, String, String), the state is restored by calling checkContext(int).
      • checkContext

        public java.lang.String checkContext​(int i)
        This method is used to restore the namespace state after an element is created. It takes as input a state, as returned by getContext().
        For any prefix, which was since saving the state, the prefix is returned and deleted from the internal list. In other words, a typical use looks like this:
           NamespaceSupport nss;
           ContentHandler h;
           int context = nss.getContext();
           h.startElement("foo", "bar", "f:bar", new AttributesImpl());
           ...
           h.endElement("foo", "bar", "f:bar");
           for (;;) {
             String prefix = nss.checkContext(context);
             if (prefix == null) {
               break;
             }
             h.endPrefixMapping(prefix);
           }
         
      • getPrefixes

        public java.util.List getPrefixes()
        Returns a list of all prefixes, which are currently declared, in the order of declaration. Duplicates are possible, if a prefix has been assigned to more than one URI, or repeatedly to the same URI.