Class NamespaceMap

java.lang.Object
net.sf.saxon.om.NamespaceMap
All Implemented Interfaces:
Iterable<NamespaceBinding>, NamespaceBindingSet, NamespaceResolver
Direct Known Subclasses:
NamespaceDeltaMap

public class NamespaceMap extends 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 Details Link icon

  • Constructor Details Link icon

    • NamespaceMap Link icon

      protected NamespaceMap()
    • NamespaceMap Link icon

      public NamespaceMap(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:
      IllegalArgumentException - if the "xml" prefix is bound to the wrong namespace, or if any other prefix is bound to the XML namespace
  • Method Details Link icon

    • emptyMap Link icon

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

      public static NamespaceMap of(String prefix, NamespaceUri 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:
      IllegalArgumentException - for an invalid mapping or if the namespace URI is empty
    • makeNamespaceMap Link icon

      protected NamespaceMap makeNamespaceMap()
    • fromNamespaceResolver Link icon

      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 Link icon

      public boolean allowsNamespaceUndeclarations()
    • size Link icon

      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 Link icon

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

      public NamespaceUri getNamespaceUri(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:
      getNamespaceUri 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 Link icon

      public NamespaceUri getDefaultNamespace()
      Get the default namespace
      Returns:
      the namespace bound to the prefix "" if there is one, otherwise NamespaceUri.NULL.
    • put Link icon

      public NamespaceMap put(String prefix, NamespaceUri 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:
      IllegalArgumentException - if an attempt is made to create an incorrect mapping for the "xml" prefix or URI.
    • bind Link icon

      public NamespaceMap bind(String prefix, NamespaceUri 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 NamespaceUri.NULL 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 Link icon

      public NamespaceMap remove(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 Link icon

      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
    • addAll Link icon

      public NamespaceMap addAll(NamespaceBindingSet namespaces)
    • applyDifferences Link icon

      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 Link icon

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

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

      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 Link icon

      public NamespaceUri getURIForPrefix(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 Link icon

      public Iterator<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 Link icon

      public 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 Link icon

      public NamespaceUri[] getURIsAsArray()
    • toString Link icon

      public String toString()
      Overrides:
      toString in class Object
    • hashCode Link icon

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals Link icon

      public boolean equals(Object obj)
      Overrides:
      equals in class Object