Class XmlNamespaceDictionary


  • @Beta
    public final class XmlNamespaceDictionary
    extends java.lang.Object
    Beta
    Thread-safe XML namespace dictionary that provides a one-to-one map of namespace alias to URI.

    Implementation is thread-safe. For maximum efficiency, applications should use a single globally-shared instance of the XML namespace dictionary.

    A namespace alias is uniquely mapped to a single namespace URI, and a namespace URI is uniquely mapped to a single namespace alias. In other words, it is not possible to have duplicates.

    Sample usage:

    
     static final XmlNamespaceDictionary DICTIONARY = new XmlNamespaceDictionary()
     .set("", "http://www.w3.org/2005/Atom")
     .set("activity", "http://activitystrea.ms/spec/1.0/")
     .set("georss", "http://www.georss.org/georss")
     .set("media", "http://search.yahoo.com/mrss/")
     .set("thr", "http://purl.org/syndication/thread/1.0");
     
    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.HashMap<java.lang.String,​java.lang.String> namespaceAliasToUriMap
      Map from XML namespace alias (or "" for the default namespace) to XML namespace URI.
      private java.util.HashMap<java.lang.String,​java.lang.String> namespaceUriToAliasMap
      Map from XML namespace URI to XML namespace alias (or "" for the default namespace).
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void computeAliases​(java.lang.Object element, java.util.SortedSet<java.lang.String> aliases)  
      java.lang.String getAliasForUri​(java.lang.String uri)
      Returns the namespace alias (or "" for the default namespace) for the given namespace URI.
      java.util.Map<java.lang.String,​java.lang.String> getAliasToUriMap()
      Returns an unmodified set of map entries for the map from namespace alias (or "" for the default namespace) to namespace URI.
      (package private) java.lang.String getNamespaceAliasForUriErrorOnUnknown​(java.lang.String namespaceUri)
      Returns the namespace alias to use for a given namespace URI, throwing an exception if the namespace URI can be found in this dictionary.
      (package private) java.lang.String getNamespaceUriForAliasHandlingUnknown​(boolean errorOnUnknown, java.lang.String alias)
      Returns the namespace URI to use for serialization for a given namespace alias, possibly using a predictable made-up namespace URI if the alias is not recognized.
      java.lang.String getUriForAlias​(java.lang.String alias)
      Returns the namespace URI for the given namespace alias (or "" for the default namespace).
      java.util.Map<java.lang.String,​java.lang.String> getUriToAliasMap()
      Returns an unmodified set of map entries for the map from namespace URI to namespace alias (or "" for the default namespace).
      void serialize​(org.xmlpull.v1.XmlSerializer serializer, java.lang.String elementName, java.lang.Object element)
      Shows a debug string representation of an element data object of key/value pairs.
      private void serialize​(org.xmlpull.v1.XmlSerializer serializer, java.lang.String elementName, java.lang.Object element, boolean errorOnUnknown)  
      void serialize​(org.xmlpull.v1.XmlSerializer serializer, java.lang.String elementNamespaceUri, java.lang.String elementLocalName, java.lang.Object element)
      Shows a debug string representation of an element data object of key/value pairs.
      private void serialize​(org.xmlpull.v1.XmlSerializer serializer, java.lang.String elementNamespaceUri, java.lang.String elementLocalName, java.lang.Object element, boolean errorOnUnknown)  
      XmlNamespaceDictionary set​(java.lang.String alias, java.lang.String uri)
      Adds a namespace of the given alias and URI.
      private XmlNamespaceDictionary.ElementSerializer startDoc​(org.xmlpull.v1.XmlSerializer serializer, java.lang.Object element, boolean errorOnUnknown, java.lang.String elementAlias)  
      (package private) static java.lang.String toSerializedValue​(java.lang.Object value)  
      java.lang.String toStringOf​(java.lang.String elementName, java.lang.Object element)
      Shows a debug string representation of an element data object of key/value pairs.
      • Methods inherited from class java.lang.Object

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

      • namespaceAliasToUriMap

        private final java.util.HashMap<java.lang.String,​java.lang.String> namespaceAliasToUriMap
        Map from XML namespace alias (or "" for the default namespace) to XML namespace URI.
      • namespaceUriToAliasMap

        private final java.util.HashMap<java.lang.String,​java.lang.String> namespaceUriToAliasMap
        Map from XML namespace URI to XML namespace alias (or "" for the default namespace).
    • Constructor Detail

      • XmlNamespaceDictionary

        public XmlNamespaceDictionary()
    • Method Detail

      • getAliasForUri

        public java.lang.String getAliasForUri​(java.lang.String uri)
        Returns the namespace alias (or "" for the default namespace) for the given namespace URI.
        Parameters:
        uri - namespace URI
        Since:
        1.3
      • getUriForAlias

        public java.lang.String getUriForAlias​(java.lang.String alias)
        Returns the namespace URI for the given namespace alias (or "" for the default namespace).
        Parameters:
        alias - namespace alias (or "" for the default namespace)
        Since:
        1.3
      • getAliasToUriMap

        public java.util.Map<java.lang.String,​java.lang.String> getAliasToUriMap()
        Returns an unmodified set of map entries for the map from namespace alias (or "" for the default namespace) to namespace URI.
        Since:
        1.3
      • getUriToAliasMap

        public java.util.Map<java.lang.String,​java.lang.String> getUriToAliasMap()
        Returns an unmodified set of map entries for the map from namespace URI to namespace alias (or "" for the default namespace).
        Since:
        1.3
      • set

        public XmlNamespaceDictionary set​(java.lang.String alias,
                                          java.lang.String uri)
        Adds a namespace of the given alias and URI.

        If the uri is null, the namespace alias will be removed. Similarly, if the alias is null, the namespace URI will be removed. Otherwise, if the alias is already mapped to a different URI, it will be remapped to the new URI. Similarly, if a URI is already mapped to a different alias, it will be remapped to the new alias.

        Parameters:
        alias - alias or null to remove the namespace URI
        uri - namespace URI or null to remove the namespace alias
        Returns:
        this namespace dictionary
        Since:
        1.3
      • toStringOf

        public java.lang.String toStringOf​(java.lang.String elementName,
                                           java.lang.Object element)
        Shows a debug string representation of an element data object of key/value pairs.
        Parameters:
        element - element data object (GenericXml, Map, or any object with public fields)
        elementName - optional XML element local name prefixed by its namespace alias -- for example "atom:entry" -- or null to make up something
      • serialize

        public void serialize​(org.xmlpull.v1.XmlSerializer serializer,
                              java.lang.String elementNamespaceUri,
                              java.lang.String elementLocalName,
                              java.lang.Object element)
                       throws java.io.IOException
        Shows a debug string representation of an element data object of key/value pairs.
        Parameters:
        element - element data object (GenericXml, Map, or any object with public fields)
        elementNamespaceUri - XML namespace URI or null for no namespace
        elementLocalName - XML local name
        Throws:
        java.io.IOException - I/O exception
      • serialize

        public void serialize​(org.xmlpull.v1.XmlSerializer serializer,
                              java.lang.String elementName,
                              java.lang.Object element)
                       throws java.io.IOException
        Shows a debug string representation of an element data object of key/value pairs.
        Parameters:
        element - element data object (GenericXml, Map, or any object with public fields)
        elementName - XML element local name prefixed by its namespace alias
        Throws:
        java.io.IOException - I/O exception
      • serialize

        private void serialize​(org.xmlpull.v1.XmlSerializer serializer,
                               java.lang.String elementNamespaceUri,
                               java.lang.String elementLocalName,
                               java.lang.Object element,
                               boolean errorOnUnknown)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • serialize

        private void serialize​(org.xmlpull.v1.XmlSerializer serializer,
                               java.lang.String elementName,
                               java.lang.Object element,
                               boolean errorOnUnknown)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • startDoc

        private XmlNamespaceDictionary.ElementSerializer startDoc​(org.xmlpull.v1.XmlSerializer serializer,
                                                                  java.lang.Object element,
                                                                  boolean errorOnUnknown,
                                                                  java.lang.String elementAlias)
                                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • computeAliases

        private void computeAliases​(java.lang.Object element,
                                    java.util.SortedSet<java.lang.String> aliases)
      • getNamespaceUriForAliasHandlingUnknown

        java.lang.String getNamespaceUriForAliasHandlingUnknown​(boolean errorOnUnknown,
                                                                java.lang.String alias)
        Returns the namespace URI to use for serialization for a given namespace alias, possibly using a predictable made-up namespace URI if the alias is not recognized.

        Specifically, if the namespace alias is not recognized, the namespace URI returned will be "http://unknown/" plus the alias, unless errorOnUnknown is true in which case it will throw an IllegalArgumentException.

        Parameters:
        errorOnUnknown - whether to thrown an exception if the namespace alias is not recognized
        alias - namespace alias
        Returns:
        namespace URI, using a predictable made-up namespace URI if the namespace alias is not recognized
        Throws:
        java.lang.IllegalArgumentException - if the namespace alias is not recognized and errorOnUnknown is true
      • getNamespaceAliasForUriErrorOnUnknown

        java.lang.String getNamespaceAliasForUriErrorOnUnknown​(java.lang.String namespaceUri)
        Returns the namespace alias to use for a given namespace URI, throwing an exception if the namespace URI can be found in this dictionary.
        Parameters:
        namespaceUri - namespace URI
        Throws:
        java.lang.IllegalArgumentException - if the namespace URI is not found in this dictionary
      • toSerializedValue

        static java.lang.String toSerializedValue​(java.lang.Object value)