public class NamespaceMap extends java.lang.Object implements NamespaceBindingSet, NamespaceResolver
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)
Modifier and Type | Field and Description |
---|---|
protected java.lang.String[] |
prefixes |
protected java.lang.String[] |
uris |
Modifier | Constructor and Description |
---|---|
protected |
NamespaceMap() |
|
NamespaceMap(java.util.List<NamespaceBinding> bindings)
Create a namespace map from a list of namespace bindings
|
Modifier and Type | Method and 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 map
|
NamespaceMap |
bind(java.lang.String prefix,
java.lang.String uri)
Add or remove a namespace binding
|
static NamespaceMap |
emptyMap()
Get a namespace map containing no namespace bindings
|
boolean |
equals(java.lang.Object obj) |
static NamespaceMap |
fromNamespaceResolver(NamespaceResolver resolver)
Create a NamespaceMap that captures all the information in a given NamespaceResolver
|
java.lang.String |
getDefaultNamespace()
Get the default namespace
|
NamespaceBinding[] |
getDifferences(NamespaceMap other,
boolean addUndeclarations)
Get the differences between this NamespaceMap and another NamespaceMap, as an array
of namespace declarations and undeclarations
|
NamespaceBinding[] |
getNamespaceBindings()
Get all the namespace bindings defined in this namespace map as an array
|
java.lang.String[] |
getPrefixArray()
Get the prefixes present in the NamespaceMap, as an array, excluding the "xml" prefix
|
java.lang.String |
getURI(java.lang.String prefix)
Get the URI associated with a given prefix.
|
java.lang.String |
getURIForPrefix(java.lang.String prefix,
boolean useDefault)
Get the namespace URI corresponding to a given prefix.
|
java.lang.String[] |
getURIsAsArray() |
int |
hashCode() |
boolean |
isEmpty()
Ask if the map contains only the binding
|
java.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 map
|
protected NamespaceMap |
makeNamespaceMap() |
static NamespaceMap |
of(java.lang.String prefix,
java.lang.String uri)
Get a namespace map containing a single namespace binding
|
NamespaceMap |
put(java.lang.String prefix,
java.lang.String 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 map
|
int |
size()
Get the number of entries in the map
|
java.lang.String |
toString() |
protected NamespaceMap()
public NamespaceMap(java.util.List<NamespaceBinding> 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 namespacepublic static NamespaceMap emptyMap()
public static NamespaceMap of(java.lang.String prefix, java.lang.String uri)
prefix
- the namespace prefixuri
- the namespace urijava.lang.IllegalArgumentException
- for an invalid mapping or if the namespace URI is emptyprotected NamespaceMap makeNamespaceMap()
public static NamespaceMap fromNamespaceResolver(NamespaceResolver resolver)
resolver
- the NamespaceResolverpublic boolean allowsNamespaceUndeclarations()
public int size()
public boolean isEmpty()
public java.lang.String getURI(java.lang.String prefix)
NamespaceConstant.XML
is returned, even if the map is empty.getURI
in interface NamespaceBindingSet
prefix
- the required prefix (may be an empty string to get the default namespace)public java.lang.String getDefaultNamespace()
public NamespaceMap put(java.lang.String prefix, java.lang.String uri)
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.java.lang.IllegalArgumentException
- if an attempt is made to create an incorrect
mapping for the "xml" prefix or URI.public NamespaceMap bind(java.lang.String prefix, java.lang.String uri)
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 removedpublic NamespaceMap remove(java.lang.String prefix)
prefix
- the entry to be removed from the mappublic NamespaceMap putAll(NamespaceMap delta)
delta
- prefix/uri pairs to be merged into this mappublic NamespaceMap addAll(NamespaceBindingSet namespaces)
public NamespaceMap applyDifferences(NamespaceDeltaMap delta)
delta
- a map of namespace declarations and undeclarations to be applieddelta
map.public java.util.Iterator<NamespaceBinding> iterator()
iterator
in interface java.lang.Iterable<NamespaceBinding>
public NamespaceBinding[] getNamespaceBindings()
public NamespaceBinding[] getDifferences(NamespaceMap other, boolean addUndeclarations)
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 = "").public java.lang.String getURIForPrefix(java.lang.String prefix, boolean useDefault)
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.public java.util.Iterator<java.lang.String> iteratePrefixes()
iteratePrefixes
in interface NamespaceResolver
public java.lang.String[] getPrefixArray()
public java.lang.String[] getURIsAsArray()
public java.lang.String toString()
toString
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
Copyright (c) 2004-2022 Saxonica Limited. All rights reserved.