|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object sk.uniba.euromath.document.NamespaceManager
public final class NamespaceManager
Manages prefixes in given document. Upon constructing, namespace prefixes are unified by following algorithm:
xmlns
attribute). First prefix
definition is taken.xmlns="foo:bar"
), then this prefix definition is not
remembered. Instead, next prefix definition is remembered.xmlns:foo=""
definitions are processed as if the
xmlns=""
was encountered - empty namespaces are thus mapped to
empty prefix.xmlns
definitions are removed.getPreferredPrefix()
function.
As a result, document (except descendants of entity reference nodes) is
stripped of xmlns
attributes.
Thread unsafe.
Even when all elements/attributes from some namespaces are removed the
mapping is not deleted automatically. It is removed when the manager is
queried for all known namespaces, using getAllNamespaces()
,
getCounts()
or getElementNamespaces()
methods.
Nested Class Summary | |
---|---|
static class |
NamespaceManager.NodeCount
Class holding elements and attributes count. |
Constructor Summary | |
---|---|
NamespaceManager(NamespaceManager parent)
Constructs a namespace manager, that is a child of given namespace manager. |
Method Summary | |
---|---|
boolean |
acceptsNewPrefix(String namespaceUri,
String newPrefix)
When creating new element/attribute, check, whether chosen prefix can be used. |
void |
cleanMappings()
Removes all mappings of namespaces not present in the document. |
Attr |
createXmlnsAttribute(Element e,
String namespaceUri)
Creates an xmlns attribute, that maps known namespace to
registered prefix. |
void |
createXmlnsAttributes(Element e)
For each used namespace, except empty namespace, creates xmlns mapping. |
void |
createXmlnsAttributes(Element e,
Set<? extends String> namespaces)
Adds and/or replaces xmlns mapping for all given namespaces in given element. |
void |
declare(String namespaceUri,
String prefix)
Declares the namespace-prefix pair. |
Set<String> |
getAllNamespaces()
Returns all namespaces known to this manager. |
String |
getBestPrefix(String namespaceUri)
When creating new element/attribute from another namespace as their parent, we can specify new prefix. |
Map<String,NamespaceManager.NodeCount> |
getCounts()
Returns counts of nodes for each namespace. |
Set<String> |
getElementNamespaces()
Returns namespaces of all elements present in the document.Returned set cannot be modified in any way. |
String |
getNamespace(String prefix)
Returns namespace, that is currently mapped to given prefix. |
String |
getPrefix(String namespaceUri)
Returns prefix, that is currently mapped to given namespace. |
boolean |
isBestPrefixRequired(String namespaceUri)
Checks, if the prefix, returned by the getBestPrefix()
function, is only the best choice for new prefix, or if returned prefix
MUST be used. |
boolean |
isDefined(String namespaceUri,
Element e)
Checks, if given namespace is defined in given element. |
void |
observe(Node node)
|
void |
stopObservation(Node node)
|
String |
toString()
|
void |
undeclareNamespace(String namespaceUri)
Undeclares given namespace. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public NamespaceManager(NamespaceManager parent)
parent
- parent namespace manager.Method Detail |
---|
public void createXmlnsAttributes(Element e)
e
- element, where the xmlns
attributes will be
re-created.public void createXmlnsAttributes(Element e, Set<? extends String> namespaces)
e
- the element.namespaces
- Set of Strings. All these namespaces' mappings are
added to element.public Set<String> getAllNamespaces()
Returns all namespaces known to this manager. Returned set cannot be
modified in any way. null
namespace is represented as
empty string (""
), and always maps to empty (
""
) namespace. It never contains the EuroMath namespace,
xmlns namespace nor xml namespace.
All mappings of namespaces no longer present in the document are removed, but only for root manager.
public Set<String> getElementNamespaces()
null
namespace is
represented as empty string (""
), and always maps to
empty ( ""
) namespace. It never contains the EuroMath
namespace, xmlns namespace nor xml namespace.
All mappings of namespaces no longer present in the document are removed.
public Map<String,NamespaceManager.NodeCount> getCounts()
Returns counts of nodes for each namespace. Clones internal map, therefore the result may be cached when used multiple times.
All mappings of namespaces no longer present in the document are removed.
null
namespace is represented as an empty string.public void cleanMappings()
public String getBestPrefix(String namespaceUri)
When creating new element/attribute from another namespace as their parent, we can specify new prefix. This function returns the best suggestion of new prefix.
However, you can ask for every element/attribute's prefix, but there are cases, when this prefix is not changeable.
Warning: when creating attributes with same namespace as their owner element (local namespaces), their prefix AND namespace MUST be null.
namespaceUri
- namespace of new nametree being created.
null
is returned.public boolean isBestPrefixRequired(String namespaceUri)
getBestPrefix()
function, is only the best choice for new prefix, or if returned prefix
MUST be used.
namespaceUri
- namespace to check.
acceptsNewPrefix()
function. True, if only one
prefix can be used.public boolean acceptsNewPrefix(String namespaceUri, String newPrefix)
namespaceUri
- namespace.newPrefix
- desired prefix. When no prefix is desired,
null
must be specified. Only null
namespace
may have null
prefix.
public Attr createXmlnsAttribute(Element e, String namespaceUri)
xmlns
attribute, that maps known namespace to
registered prefix. EuroMath namespace is always mapped to
emp:
prefix. When an empty namespace is given, function
returns null
: empty namespace is always mapped to empty
prefix, and thus need not to be declared by the xmlns:
attribute. When an XML namespace is given then null
is
returned aswell.
e
- element, where to create element.namespaceUri
- creates xmlns mapping for this namespace.
null
if no prefix is
currently mapped to given namespace.public String getPrefix(String namespaceUri)
null
. GENE-ID
namespace is always mapped to gene:
prefix. Empty or
null
namespace ("") is always mapped to empty prefix ("").
XML namespace is always mapped to xml
prefix.
namespaceUri
- namespace.
null
.public String getNamespace(String prefix)
null
.
EuroMath namespace is always mapped to emp:
prefix. Empty
or null
prefix is always mapped to empty namespace ("").
prefix
- the prefix.
null
.public boolean isDefined(String namespaceUri, Element e)
namespaceUri
- namespacee
- element
true
if this namespace is defined in given element
with the xmlns:
attribute, or if a prefix is registered.public void declare(String namespaceUri, String prefix)
namespaceUri
- the namespace.prefix
- the prefix.
IllegalArgumentException
- if such prefix-namespace pair cannot be
registered.public void undeclareNamespace(String namespaceUri)
namespaceUri
- the namespace part of pair.public String toString()
toString
in class Object
public void observe(Node node)
observe
in interface INodeObserver
public void stopObservation(Node node)
stopObservation
in interface INodeObserver
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |