Class XmlNamespaceDictionary
Beta
Thread-safe XML namespace dictionary that provides a one-to-one map of namespace alias to URI.
Implementation is thread-safe. For maximum efficiency, applications should use a single globally-shared instance of the XML namespace dictionary.
A namespace alias is uniquely mapped to a single namespace URI, and a namespace URI is uniquely mapped to a single namespace alias. In other words, it is not possible to have duplicates.
Sample usage:
static final XmlNamespaceDictionary DICTIONARY = new XmlNamespaceDictionary()
.set("", "http://www.w3.org/2005/Atom")
.set("activity", "http://activitystrea.ms/spec/1.0/")
.set("georss", "http://www.georss.org/georss")
.set("media", "http://search.yahoo.com/mrss/")
.set("thr", "http://purl.org/syndication/thread/1.0");
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) class
-
Field Summary
FieldsModifier and TypeFieldDescriptionMap from XML namespace alias (or""
for the default namespace) to XML namespace URI.Map from XML namespace URI to XML namespace alias (or""
for the default namespace). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
computeAliases
(Object element, SortedSet<String> aliases) getAliasForUri
(String uri) Returns the namespace alias (or""
for the default namespace) for the given namespace URI.Returns an unmodified set of map entries for the map from namespace alias (or""
for the default namespace) to namespace URI.(package private) String
getNamespaceAliasForUriErrorOnUnknown
(String namespaceUri) Returns the namespace alias to use for a given namespace URI, throwing an exception if the namespace URI can be found in this dictionary.(package private) String
getNamespaceUriForAliasHandlingUnknown
(boolean errorOnUnknown, String alias) Returns the namespace URI to use for serialization for a given namespace alias, possibly using a predictable made-up namespace URI if the alias is not recognized.getUriForAlias
(String alias) Returns the namespace URI for the given namespace alias (or""
for the default namespace).Returns an unmodified set of map entries for the map from namespace URI to namespace alias (or""
for the default namespace).void
Shows a debug string representation of an element data object of key/value pairs.private void
serialize
(org.xmlpull.v1.XmlSerializer serializer, String elementName, Object element, boolean errorOnUnknown) void
serialize
(org.xmlpull.v1.XmlSerializer serializer, String elementNamespaceUri, String elementLocalName, Object element) Shows a debug string representation of an element data object of key/value pairs.private void
serialize
(org.xmlpull.v1.XmlSerializer serializer, String elementNamespaceUri, String elementLocalName, Object element, boolean errorOnUnknown) Adds a namespace of the given alias and URI.startDoc
(org.xmlpull.v1.XmlSerializer serializer, Object element, boolean errorOnUnknown, String elementAlias) (package private) static String
toSerializedValue
(Object value) toStringOf
(String elementName, Object element) Shows a debug string representation of an element data object of key/value pairs.
-
Field Details
-
namespaceAliasToUriMap
Map from XML namespace alias (or""
for the default namespace) to XML namespace URI. -
namespaceUriToAliasMap
Map from XML namespace URI to XML namespace alias (or""
for the default namespace).
-
-
Constructor Details
-
XmlNamespaceDictionary
public XmlNamespaceDictionary()
-
-
Method Details
-
getAliasForUri
Returns the namespace alias (or""
for the default namespace) for the given namespace URI.- Parameters:
uri
- namespace URI- Since:
- 1.3
-
getUriForAlias
Returns the namespace URI for the given namespace alias (or""
for the default namespace).- Parameters:
alias
- namespace alias (or""
for the default namespace)- Since:
- 1.3
-
getAliasToUriMap
Returns an unmodified set of map entries for the map from namespace alias (or""
for the default namespace) to namespace URI.- Since:
- 1.3
-
getUriToAliasMap
Returns an unmodified set of map entries for the map from namespace URI to namespace alias (or""
for the default namespace).- Since:
- 1.3
-
set
Adds a namespace of the given alias and URI.If the uri is
null
, the namespace alias will be removed. Similarly, if the alias isnull
, the namespace URI will be removed. Otherwise, if the alias is already mapped to a different URI, it will be remapped to the new URI. Similarly, if a URI is already mapped to a different alias, it will be remapped to the new alias.- Parameters:
alias
- alias ornull
to remove the namespace URIuri
- namespace URI ornull
to remove the namespace alias- Returns:
- this namespace dictionary
- Since:
- 1.3
-
toStringOf
Shows a debug string representation of an element data object of key/value pairs.- Parameters:
elementName
- optional XML element local name prefixed by its namespace alias -- for example"atom:entry"
-- ornull
to make up somethingelement
- element data object (GenericXml
,Map
, or any object with public fields)
-
serialize
public void serialize(org.xmlpull.v1.XmlSerializer serializer, String elementNamespaceUri, String elementLocalName, Object element) throws IOException Shows a debug string representation of an element data object of key/value pairs.- Parameters:
elementNamespaceUri
- XML namespace URI ornull
for no namespaceelementLocalName
- XML local nameelement
- element data object (GenericXml
,Map
, or any object with public fields)- Throws:
IOException
- I/O exception
-
serialize
public void serialize(org.xmlpull.v1.XmlSerializer serializer, String elementName, Object element) throws IOException Shows a debug string representation of an element data object of key/value pairs.- Parameters:
elementName
- XML element local name prefixed by its namespace aliaselement
- element data object (GenericXml
,Map
, or any object with public fields)- Throws:
IOException
- I/O exception
-
serialize
private void serialize(org.xmlpull.v1.XmlSerializer serializer, String elementNamespaceUri, String elementLocalName, Object element, boolean errorOnUnknown) throws IOException - Throws:
IOException
-
serialize
private void serialize(org.xmlpull.v1.XmlSerializer serializer, String elementName, Object element, boolean errorOnUnknown) throws IOException - Throws:
IOException
-
startDoc
private XmlNamespaceDictionary.ElementSerializer startDoc(org.xmlpull.v1.XmlSerializer serializer, Object element, boolean errorOnUnknown, String elementAlias) throws IOException - Throws:
IOException
-
computeAliases
-
getNamespaceUriForAliasHandlingUnknown
Returns the namespace URI to use for serialization for a given namespace alias, possibly using a predictable made-up namespace URI if the alias is not recognized.Specifically, if the namespace alias is not recognized, the namespace URI returned will be
"http://unknown/"
plus the alias, unlesserrorOnUnknown
istrue
in which case it will throw anIllegalArgumentException
.- Parameters:
errorOnUnknown
- whether to thrown an exception if the namespace alias is not recognizedalias
- namespace alias- Returns:
- namespace URI, using a predictable made-up namespace URI if the namespace alias is not recognized
- Throws:
IllegalArgumentException
- if the namespace alias is not recognized anderrorOnUnknown
istrue
-
getNamespaceAliasForUriErrorOnUnknown
Returns the namespace alias to use for a given namespace URI, throwing an exception if the namespace URI can be found in this dictionary.- Parameters:
namespaceUri
- namespace URI- Throws:
IllegalArgumentException
- if the namespace URI is not found in this dictionary
-
toSerializedValue
-