Class NamespaceMap

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

    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

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

      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
        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.
        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
        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
        prefix - the namespace prefix
        uri - the namespace uri
        a map containing the single binding; or an empty map if the binding is the standard binding of the XML namespace
        java.lang.IllegalArgumentException - for an invalid mapping or if the namespace URI is empty
      • fromNamespaceResolver

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

        public boolean allowsNamespaceUndeclarations()
      • size

        public int size()
        Get the number of entries in the map
        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
        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
        prefix - the required prefix (may be an empty string to get the default namespace)
        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
        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.
        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, any existing mapping for the prefix is removed.
        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).
        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
        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
      • remove

        public NamespaceMap remove​(java.lang.String prefix)
        Remove an entry from the map
        prefix - the entry to be removed from the map
        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
        delta - prefix/uri pairs to be merged into this map
        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
        delta - a map of namespace declarations and undeclarations to be applied
        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>
        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
        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
        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 = "").
      • 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
        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.
        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
        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
        the prefixes present in the map, not including the "xml" prefix
      • getURIsAsArray

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

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

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

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