public final class NamespaceContexHelper
extends java.lang.Object
implements org.jvnet.staxex.NamespaceContextEx
A namespace is declared on a namespace context. Namespace contexts are pushed on and popped off the namespace context stack.
A declared namespace will be in scope iff the context that it was declared on has not been popped off the stack.
When instantiated the namespace stack consists of the root namespace context, which contains, by default, the "xml" and "xmlns" declarations. Namespaces may be declarations may be declared on the root context. The root context cannot be popped but can be reset to contain just the "xml" and "xmlns" declarations.
Implementation note: determining the prefix from a namespace URI (or vice versa) is efficient when there are few namespace declarations i.e. what is considered to be the case for namespace declarations in 'average' XML documents. The look up of a namespace URI given a prefix is performed in O(n) time. The look up of a prefix given a namespace URI is performed in O(2n) time.
The implementation does not scale when there are many namespace declarations. TODO: Use a hash map when there are many namespace declarations.
Modifier and Type | Class and Description |
---|---|
private class |
NamespaceContexHelper.NamespaceBindingImpl |
Modifier and Type | Field and Description |
---|---|
private int |
contextPosition |
private int[] |
contexts |
private static int |
DEFAULT_SIZE |
private int |
namespacePosition |
private java.lang.String[] |
namespaceURIs |
private java.lang.String[] |
prefixes |
Constructor and Description |
---|
NamespaceContexHelper()
Create a new NamespaceContexHelper.
|
Modifier and Type | Method and Description |
---|---|
void |
declareDefaultNamespace(java.lang.String namespaceURI)
Declare a default namespace.
|
void |
declareNamespace(java.lang.String prefix,
java.lang.String namespaceURI)
Declare a namespace.
|
java.lang.String |
getNamespaceURI(java.lang.String prefix) |
java.lang.String |
getPrefix(java.lang.String namespaceURI) |
java.util.Iterator |
getPrefixes(java.lang.String namespaceURI) |
java.util.Iterator<org.jvnet.staxex.NamespaceContextEx.Binding> |
iterator() |
void |
popContext()
Pop the namespace context off the stack.
|
void |
pushContext()
Push a namespace context on the stack.
|
void |
resetContexts()
Reset namespace contexts.
|
private void |
resizeContexts() |
private void |
resizeNamespaces() |
private static int DEFAULT_SIZE
private java.lang.String[] prefixes
private java.lang.String[] namespaceURIs
private int namespacePosition
private int[] contexts
private int contextPosition
public NamespaceContexHelper()
public java.lang.String getNamespaceURI(java.lang.String prefix)
getNamespaceURI
in interface javax.xml.namespace.NamespaceContext
public java.lang.String getPrefix(java.lang.String namespaceURI)
getPrefix
in interface javax.xml.namespace.NamespaceContext
public java.util.Iterator getPrefixes(java.lang.String namespaceURI)
getPrefixes
in interface javax.xml.namespace.NamespaceContext
public java.util.Iterator<org.jvnet.staxex.NamespaceContextEx.Binding> iterator()
iterator
in interface java.lang.Iterable<org.jvnet.staxex.NamespaceContextEx.Binding>
iterator
in interface org.jvnet.staxex.NamespaceContextEx
public void declareDefaultNamespace(java.lang.String namespaceURI)
namespaceURI
- the namespace URI to declare, may be null.public void declareNamespace(java.lang.String prefix, java.lang.String namespaceURI)
The namespace will be declared on the current namespace context.
The namespace can be removed by popping the current namespace context, or, if the declaration occured in the root context, by reseting the namespace context.
A default namespace can be declared by passing ""
as
the value of the prefix parameter.
A namespace may be undeclared by passing null
as the
value of the namespaceURI parameter.
prefix
- the namespace prefix to declare, may not be null.namespaceURI
- the namespace URI to declare, may be null.IllegalArgumentException,
- if the prefix is null.private void resizeNamespaces()
public void pushContext()
private void resizeContexts()
public void popContext()
Namespaces declared within the context (to be popped) will be removed and no longer be in scope.
public void resetContexts()
Pop all namespace contexts and reset the root context.