Package net.sf.saxon.om
Class NamespaceMap
- java.lang.Object
-
- net.sf.saxon.om.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 NamespaceUri[]
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
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description NamespaceMap
addAll(NamespaceBindingSet namespaces)
boolean
allowsNamespaceUndeclarations()
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 mapNamespaceMap
bind(java.lang.String prefix, NamespaceUri uri)
Add or remove a namespace bindingstatic NamespaceMap
emptyMap()
Get a namespace map containing no namespace bindingsboolean
equals(java.lang.Object obj)
static NamespaceMap
fromNamespaceResolver(NamespaceResolver resolver)
Create a NamespaceMap that captures all the information in a given NamespaceResolverNamespaceUri
getDefaultNamespace()
Get the default namespaceNamespaceBinding[]
getDifferences(NamespaceMap other, boolean addUndeclarations)
Get the differences between this NamespaceMap and another NamespaceMap, as an array of namespace declarations and undeclarationsNamespaceBinding[]
getNamespaceBindings()
Get all the namespace bindings defined in this namespace map as an arrayNamespaceUri
getNamespaceUri(java.lang.String prefix)
Get the URI associated with a given prefix.java.lang.String[]
getPrefixArray()
Get the prefixes present in the NamespaceMap, as an array, excluding the "xml" prefixNamespaceUri
getURIForPrefix(java.lang.String prefix, boolean useDefault)
Get the namespace URI corresponding to a given prefix.NamespaceUri[]
getURIsAsArray()
int
hashCode()
boolean
isEmpty()
Ask if the map contains only the bindingjava.util.Iterator<java.lang.String>
iteratePrefixes()
Get an iterator over the prefixes defined in this namespace map, including the "xml" prefix.java.util.Iterator<NamespaceBinding>
iterator()
Get an iterator over the namespace bindings defined in this namespace mapprotected NamespaceMap
makeNamespaceMap()
static NamespaceMap
of(java.lang.String prefix, NamespaceUri uri)
Get a namespace map containing a single namespace bindingNamespaceMap
put(java.lang.String prefix, NamespaceUri uri)
Add a new entry to the map, or replace an existing entry.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.NamespaceMap
remove(java.lang.String prefix)
Remove an entry from the mapint
size()
Get the number of entries in the mapjava.lang.String
toString()
-
-
-
Field Detail
-
prefixes
protected java.lang.String[] prefixes
-
uris
protected NamespaceUri[] 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, NamespaceUri uri)
Get a namespace map containing a single namespace binding- Parameters:
prefix
- the namespace prefixuri
- 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
-
getNamespaceUri
public NamespaceUri getNamespaceUri(java.lang.String prefix)
Get the URI associated with a given prefix. If the supplied prefix is "xml", the XML namespaceNamespaceConstant.XML
is returned, even if the map is empty.- Specified by:
getNamespaceUri
in interfaceNamespaceBindingSet
- 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 NamespaceUri getDefaultNamespace()
Get the default namespace- Returns:
- the namespace bound to the prefix "" if there is one, otherwise
NamespaceUri.NULL
.
-
put
public NamespaceMap put(java.lang.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 namespaceuri
- 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, 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; orNamespaceUri.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
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
-
addAll
public NamespaceMap addAll(NamespaceBindingSet namespaces)
-
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 interfacejava.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 theother
map but not inthis
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 NamespaceUri 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 interfaceNamespaceResolver
- 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 namespaceNamespaceConstant.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 interfaceNamespaceResolver
- 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 NamespaceUri[] getURIsAsArray()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
-