Class NamespaceMap

  • All Implemented Interfaces:
    java.lang.Iterable<NamespaceBinding>, NamespaceBindingSet, NamespaceResolver
    Direct Known Subclasses:
    NamespaceDeltaMap

    public class NamespaceMap
    extends java.lang.Object
    implements NamespaceBindingSet, NamespaceResolver
    Holds a set of namespace bindings as a simple immutable map from prefixes to URIs.

    A NamespaceMap never physically contains a binding for the XML namespace, but some interfaces behave as if it did.

    The map may or may not contain a binding for the default namespace, represented by the prefix "" (zero-length string)

    The map must not contain any namespace undeclarations: that is, the namespace will never be "" (zero-length string)

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.lang.String[] prefixes  
      protected java.lang.String[] uris  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected NamespaceMap()  
        NamespaceMap​(java.util.List<NamespaceBinding> bindings)
      Create a namespace map from a list of namespace bindings
    • Field Detail

      • prefixes

        protected java.lang.String[] prefixes
      • uris

        protected java.lang.String[] uris
    • Constructor Detail

      • NamespaceMap

        protected NamespaceMap()
      • NamespaceMap

        public NamespaceMap​(java.util.List<NamespaceBinding> bindings)
        Create a namespace map from a list of namespace bindings
        Parameters:
        bindings - the list of namespace bindings. If there is more that one binding for the same prefix, the last one wins. Any binding of the prefix "xml" to the XML namespace is ignored, but an incorrect binding of the XML namespace causes an exception.
        Throws:
        java.lang.IllegalArgumentException - if the "xml" prefix is bound to the wrong namespace, or if any other prefix is bound to the XML namespace
    • Method Detail

      • emptyMap

        public static NamespaceMap emptyMap()
        Get a namespace map containing no namespace bindings
        Returns:
        an empty namespace map
      • of

        public static NamespaceMap of​(java.lang.String prefix,
                                      java.lang.String uri)
        Get a namespace map containing a single namespace binding
        Parameters:
        prefix - the namespace prefix
        uri - the namespace uri
        Returns:
        a map containing the single binding; or an empty map if the binding is the standard binding of the XML namespace
        Throws:
        java.lang.IllegalArgumentException - for an invalid mapping or if the namespace URI is empty
      • makeNamespaceMap

        protected NamespaceMap makeNamespaceMap()
      • fromNamespaceResolver

        public static NamespaceMap fromNamespaceResolver​(NamespaceResolver resolver)
        Create a NamespaceMap that captures all the information in a given NamespaceResolver
        Parameters:
        resolver - the NamespaceResolver
        Returns:
        the new NamespaceMap
      • allowsNamespaceUndeclarations

        public boolean allowsNamespaceUndeclarations()
      • size

        public int size()
        Get the number of entries in the map
        Returns:
        the number of prefix-uri bindings (excluding any binding for the XML namespace)
      • isEmpty

        public boolean isEmpty()
        Ask if the map contains only the binding
        Returns:
        true if the map contains no bindings
      • getURI

        public java.lang.String getURI​(java.lang.String prefix)
        Get the URI associated with a given prefix. If the supplied prefix is "xml", the XML namespace NamespaceConstant.XML is returned, even if the map is empty.
        Specified by:
        getURI in interface NamespaceBindingSet
        Parameters:
        prefix - the required prefix (may be an empty string to get the default namespace)
        Returns:
        the associated URI, or null if no mapping is present. Note that we return null when no mapping is present, even for the case where the prefix is the empty string.
      • getDefaultNamespace

        public java.lang.String getDefaultNamespace()
        Get the default namespace
        Returns:
        the namespace bound to the prefix "" if there is one, otherwise "".
      • put

        public NamespaceMap put​(java.lang.String prefix,
                                java.lang.String uri)
        Add a new entry to the map, or replace an existing entry. An attempt to add a binding of the "xml" prefix to the XML namespace is silently ignored.
        Parameters:
        prefix - the prefix whose entry is to be added or replaced. May be zero-length to represent the default namespace
        uri - the URI to be associated with this prefix; if zero-length or null, any existing mapping for the prefix is removed.
        Returns:
        a new map containing the added or replaced entry (or this map, unchanged, if the prefix-uri mapping was already present in the old map).
        Throws:
        java.lang.IllegalArgumentException - if an attempt is made to create an incorrect mapping for the "xml" prefix or URI.
      • bind

        public NamespaceMap bind​(java.lang.String prefix,
                                 java.lang.String uri)
        Add or remove a namespace binding
        Parameters:
        prefix - the namespace prefix ("" for the default namespace)
        uri - the namespace URI to which the prefix is bound; or "" to indicate that an existing binding for the prefix is to be removed
        Returns:
        a new map with the entry added or removed as appropriate (or this map, unchanged, as appropriate).
      • remove

        public NamespaceMap remove​(java.lang.String prefix)
        Remove an entry from the map
        Parameters:
        prefix - the entry to be removed from the map
        Returns:
        a new map in which the relevant entry has been removed, or this map (unchanged) if the requested entry was not present
      • putAll

        public NamespaceMap putAll​(NamespaceMap delta)
        Merge the prefix/uri pairs in the supplied delta with the prefix/uri pairs in this namespace map, to create a new namespace map. If a prefix is present in both maps, then the one in delta takes precedence
        Parameters:
        delta - prefix/uri pairs to be merged into this map
        Returns:
        a new map, the result of the merge
      • applyDifferences

        public NamespaceMap applyDifferences​(NamespaceDeltaMap delta)
        Create a map containing all namespace declarations in this map, plus any namespace declarations and minus any namespace undeclarations in the delta map
        Parameters:
        delta - a map of namespace declarations and undeclarations to be applied
        Returns:
        a map combining the namespace declarations in this map with the declarations and undeclarations in the delta map.
      • iterator

        public java.util.Iterator<NamespaceBinding> iterator()
        Get an iterator over the namespace bindings defined in this namespace map
        Specified by:
        iterator in interface java.lang.Iterable<NamespaceBinding>
        Returns:
        an iterator over the namespace bindings. (In the current implementation they will be in alphabetical order of namespace prefix.)
      • getNamespaceBindings

        public NamespaceBinding[] getNamespaceBindings()
        Get all the namespace bindings defined in this namespace map as an array
        Returns:
        the array of namespace bindings
      • getDifferences

        public NamespaceBinding[] getDifferences​(NamespaceMap other,
                                                 boolean addUndeclarations)
        Get the differences between this NamespaceMap and another NamespaceMap, as an array of namespace declarations and undeclarations
        Parameters:
        other - typically the namespaces on the parent element, in which case the method returns the namespace declarations and undeclarations corresponding to the difference between this child element and its parent.
        addUndeclarations - if true, then when a namespace is declared in the other map but not in this map, a namespace undeclaration (binding the prefix to the dummy URI "") will be included in the result. If false, namespace undeclarations are included in the result only for the default namespace (prefix = "").
        Returns:
        the array of namespace declarations and undeclarations
      • getURIForPrefix

        public java.lang.String getURIForPrefix​(java.lang.String prefix,
                                                boolean useDefault)
        Get the namespace URI corresponding to a given prefix. Return null if the prefix is not in scope.
        Specified by:
        getURIForPrefix in interface NamespaceResolver
        Parameters:
        prefix - the namespace prefix. May be the zero-length string, indicating that there is no prefix. This indicates either the default namespace or the null namespace, depending on the value of useDefault. The prefix "xml" is always recognized as corresponding to the XML namespace NamespaceConstant.XML
        useDefault - true if the default namespace is to be used when the prefix is "". If false, the method returns "" when the prefix is "". The default namespace is a property of the NamespaceResolver; in general it corresponds to the "default namespace for elements and types", but that cannot be assumed.
        Returns:
        the uri for the namespace, or null if the prefix is not in scope. The "null namespace" is represented by the pseudo-URI "".
      • iteratePrefixes

        public java.util.Iterator<java.lang.String> iteratePrefixes()
        Get an iterator over the prefixes defined in this namespace map, including the "xml" prefix.
        Specified by:
        iteratePrefixes in interface NamespaceResolver
        Returns:
        an iterator over the prefixes. (In the current implementation they will be in alphabetical order, except that the "xml" prefix will always come last.)
      • getPrefixArray

        public java.lang.String[] getPrefixArray()
        Get the prefixes present in the NamespaceMap, as an array, excluding the "xml" prefix
        Returns:
        the prefixes present in the map, not including the "xml" prefix
      • getURIsAsArray

        public java.lang.String[] getURIsAsArray()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object