public final class NamespaceStack extends java.lang.Object implements java.lang.Iterable<Namespace>
This stack implements the 'Namespace Rules' which XML uses, where a Namespace 'redefines' an existing Namespace if they share the same prefix. This class is intended to provide a high-performance mechanism for calculating the Namespace scope for an Element, and identifying what Namespaces an Element introduces in to the scope. This is not a validation tool.
This class implements Iterable which means it can be used in the context
of a for-each type loop:
The Iteration in the above example will return those Namespaces which are
in-scope for the current level of the stack. The Namespace order will follow
the JDOM 'standard'. The first namespace will be the Element's Namespace. The
subsequent Namespaces will be the other in-scope namespaces in alphabetical
order by the Namespace prefix.
NamespaceStack namespacestack = new NamespaceStack();
for (Namespace ns : namespacestack) {
...
}
NamespaceStack does not validate the push()/pop() cycles. It does not ensure that the pop() is for the same element that was previously pushed. Further, it does not check to make sure that the pushed() Element is the natural child of the previously pushed() Element.
Constructor and Description |
---|
NamespaceStack()
Create a NamespaceWalker ready to use as a stack.
|
NamespaceStack(Namespace[] seed)
Create a NamespaceWalker ready to use as a stack.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Iterable<Namespace> |
addedForward()
Return an Iterable containing all the Namespaces introduced to the
current-level's scope.
|
java.lang.Iterable<Namespace> |
addedReverse()
Return an Iterable containing all the Namespaces introduced to the
current-level's scope but in reverse order to
addedForward() . |
Namespace[] |
getAllNamespacesForURI(java.lang.String uri)
Get all prefixes in the current scope that are bound to the specified URI.
|
Namespace |
getFirstNamespaceForURI(java.lang.String uri)
Get the first Namespace in the current scope that is bound to the specified URI.
|
Namespace |
getNamespaceForPrefix(java.lang.String prefix)
Get the Namespace in the current scope with the specified prefix.
|
Namespace |
getRebound(java.lang.String prefix)
If the specified prefix was bound in the previous bind level, and has
been rebound to a different URI in the current level, then return the
Namespace the the prefix was bound to before.
|
Namespace[] |
getScope()
Return a new array instance representing the current scope.
|
boolean |
isInScope(Namespace ns)
Inspect the current scope and return true if the specified namespace is
in scope.
|
java.util.Iterator<Namespace> |
iterator()
Get all the Namespaces in-scope at the current level of the stack.
|
void |
pop()
Restore stack to the level prior to the current one.
|
void |
push(Attribute att)
Create a new in-scope level for the Stack based on an Attribute.
|
void |
push(Element element)
Create a new in-scope level for the Stack based on an Element.
|
void |
push(java.lang.Iterable<Namespace> namespaces)
Create a new in-scope level for the Stack based on an arbitrary set of Namespaces.
|
void |
push(Namespace... namespaces)
Create a new in-scope level for the Stack based on an arbitrary set of Namespaces.
|
public NamespaceStack()
for comprehensive notes.
public NamespaceStack(Namespace[] seed)
seed
- The namespaces to set as the top level of the stack.for comprehensive notes.
public void push(Element element)
addedForward()
Iterable. The order of
the added Namespaces follows the same rules as above: first the
Element Namespace (only if that Namespace is actually added) followed
by the other added namespaces in alphabetical-by-prefix order.
addedReverse()
Iterable.
element
- The element at the new level of the stack.public void push(Attribute att)
att
- The attribute to contribute to the namespace scope.public void push(java.lang.Iterable<Namespace> namespaces)
namespaces
- The Iterable format for the Namespaces.public void push(Namespace... namespaces)
namespaces
- The array of Namespaces.public void pop()
public java.lang.Iterable<Namespace> addedForward()
for the details on the data order.
public java.lang.Iterable<Namespace> addedReverse()
addedForward()
.for the details on the data order.
public java.util.Iterator<Namespace> iterator()
iterator
in interface java.lang.Iterable<Namespace>
for the details on the data order.
public Namespace[] getScope()
public boolean isInScope(Namespace ns)
ns
- The Namespace to checkpublic Namespace getNamespaceForPrefix(java.lang.String prefix)
prefix
- The prefix to get the namespace for (null is treated the same as "").public Namespace getFirstNamespaceForURI(java.lang.String uri)
uri
- The URI to get the first prefix for (null is treated the same as "").public Namespace[] getAllNamespacesForURI(java.lang.String uri)
uri
- The URI to get the first prefix for (null is treated the same as "").public Namespace getRebound(java.lang.String prefix)
prefix
- The prefix to check for re-bindingCopyright © 2021 Jason Hunter, Brett McLaughlin. All Rights Reserved.