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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | NamespaceStack.BackwardWalker
Simple read-only iterator that walks an array of Namespace in reverse. |
| private static class | NamespaceStack.EmptyIterable
Convenience class that makes very fast work for an empty Namespace array. |
| private static class | NamespaceStack.ForwardWalker
Simple read-only iterator that walks an array of Namespace. |
| private static class | NamespaceStack.NamespaceIterable
Simple Iterable instance that produces either Forward or Backward read-only iterators of the Namespaces |
| Modifier and Type | Field and Description |
|---|---|
| private Namespace[][] | added
The namespaces added to the scope at each depth |
| private static final Namespace[] | |
| private int | depth
The current depth |
| private static final Namespace[] | EMPTY
A simple empty Namespace Array to avoid redundant empty instances |
| private static final Iterable | EMPTYITER
A simple Iterable instance that is always empty. |
| private static final List | |
| private static final Comparator | NSCOMP
A comparator that sorts Namespaces by their prefix. |
| private Namespace[][] | scope
The entire scope at each depth |
| Access | Constructor and Description |
|---|---|
| public | |
| public | NamespaceStack(Namespace[]
The namespaces to set as the top level of the stack. seed)Create a NamespaceWalker ready to use as a stack. |
| Modifier and Type | Method and Description |
|---|---|
| public Iterable | Returns: A read-only Iterable containing added Namespaces (may be empty);Return an Iterable containing all the Namespaces introduced to the current-level's scope. |
| public Iterable | Returns: A read-only Iterable containing added Namespaces (may be empty);Return an Iterable containing all the Namespaces introduced to the
current-level's scope but in reverse order to |
| private static final int | Returns: the 'insertion point' - This return value follows the same convention as the standard Java BinarySearch methods (see the JavaDoc for Arrays.binarySearch(). In summary, if the value exists then the return value is the index of the existing value. If the value was not found, then the return value will be negative, and the place where the missing value should be inserted, can be determined by adding 1, and converting back to positive (or converting to positive, and subtracting 1).The Namespaces to search. data, int The left side of the range to search INCLUSIVE left, int The right side of the range to search EXCLUSIVE right, final Namespace The Namespace to search for. key)Lots of reasons for having our own binarySearch. |
| private static final Namespace[] | Returns: The revised version of 'in-scope' if the scope has changed. If there is no modification then the same input scope will be returned.Where to add the namespace if it is 'new' store, Namespace The Namespace to check namespace, Namespace[] The array of Namespaces that are currently in-scope. scope)Inspect the scope array to see whether the namespace Namespace is 'new' or not. |
| public Namespace[] | Returns: All bound prefixes for the specified URI, or an empty array if the URI is not bound.The URI to get the first prefix for (null is treated the same as ""). uri)Get all prefixes in the current scope that are bound to the specified URI. |
| public Namespace | Returns: The first bound Namespace for the specified URI, or null if the URI is not bound.The URI to get the first prefix for (null is treated the same as ""). uri)Get the first Namespace in the current scope that is bound to the specified URI. |
| public Namespace | Returns: The Namespace with the specified prefix, or null if the prefix is not in scope.The prefix to get the namespace for (null is treated the same as ""). prefix)Get the Namespace in the current scope with the specified prefix. |
| public Namespace | Returns: the previous binding for the specified prefix, or null if the prefix was not previously bound, or was not changed in this level of the stack.The prefix to check for re-binding 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. |
| public Namespace[] | Returns: a copy of the current scope.Return a new array instance representing the current scope. |
| public boolean | |
| public Iterator | Returns: A read-only Iterator containing added Namespaces (may be empty);Implements java. |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| private final void |
| added | back to summary |
|---|---|
| private Namespace[][] added The namespaces added to the scope at each depth | |
| DEFAULTSEED | back to summary |
|---|---|
| private static final Namespace[] DEFAULTSEED | |
| depth | back to summary |
|---|---|
| private int depth The current depth | |
| EMPTY | back to summary |
|---|---|
| private static final Namespace[] EMPTY A simple empty Namespace Array to avoid redundant empty instances | |
| EMPTYITER | back to summary |
|---|---|
| private static final Iterable<Namespace> EMPTYITER A simple Iterable instance that is always empty. Saves some memory | |
| EMPTYLIST | back to summary |
|---|---|
| private static final List<Namespace> EMPTYLIST | |
| NSCOMP | back to summary |
|---|---|
| private static final Comparator<Namespace> NSCOMP A comparator that sorts Namespaces by their prefix. | |
| scope | back to summary |
|---|---|
| private Namespace[][] scope The entire scope at each depth | |
| NamespaceStack | back to summary |
|---|---|
| public NamespaceStack() Create a NamespaceWalker ready to use as a stack.
| |
| NamespaceStack | back to summary |
|---|---|
| public NamespaceStack(Namespace[] seed) Create a NamespaceWalker ready to use as a stack.
| |
| addedForward | back to summary |
|---|---|
| public Iterable Return an Iterable containing all the Namespaces introduced to the current-level's scope.
| |
| addedReverse | back to summary |
|---|---|
| public Iterable Return an Iterable containing all the Namespaces introduced to the
current-level's scope but in reverse order to
| |
| binarySearch | back to summary |
|---|---|
| private static final int binarySearch(final Namespace[] data, int left, int right, final Namespace key) Lots of reasons for having our own binarySearch.
| |
| checkNamespace | back to summary |
|---|---|
| private static final Namespace[] checkNamespace(List<Namespace> store, Namespace namespace, Namespace[] scope) Inspect the scope array to see whether the namespace Namespace is 'new' or not. If it is 'new' then it is added to the store List.
| |
| getAllNamespacesForURI | back to summary |
|---|---|
| public Namespace[] getAllNamespacesForURI(final String uri) Get all prefixes in the current scope that are bound to the specified URI. | |
| getFirstNamespaceForURI | back to summary |
|---|---|
| public Namespace getFirstNamespaceForURI(final String uri) Get the first Namespace in the current scope that is bound to the specified URI. | |
| getNamespaceForPrefix | back to summary |
|---|---|
| public Namespace getNamespaceForPrefix(final String prefix) Get the Namespace in the current scope with the specified prefix. | |
| getRebound | back to summary |
|---|---|
| public Namespace getRebound(final 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. | |
| getScope | back to summary |
|---|---|
| public Namespace[] getScope() Return a new array instance representing the current scope. Modifying the returned array will not affect this scope.
| |
| isInScope | back to summary |
|---|---|
| public boolean isInScope(Namespace ns) Inspect the current scope and return true if the specified namespace is in scope.
| |
| iterator | back to summary |
|---|---|
| public Iterator Implements java. Get all the Namespaces in-scope at the current level of the stack.
| |
| pop | back to summary |
|---|---|
| public void pop() Restore stack to the level prior to the current one. The various Iterator methods will thus return the data at the previous level. | |
| push | back to summary |
|---|---|
| public void push(Element element) Create a new in-scope level for the Stack based on an Element.
| |
| push | back to summary |
|---|---|
| public void push(Attribute att) Create a new in-scope level for the Stack based on an Attribute.
| |
| push | back to summary |
|---|---|
| public void push(Iterable<Namespace> namespaces) Create a new in-scope level for the Stack based on an arbitrary set of Namespaces. | |
| push | back to summary |
|---|---|
| public void push(Namespace... namespaces) Create a new in-scope level for the Stack based on an arbitrary set of Namespaces. The first Namespace in the list will be considered the 'primary' namespace for this scope and will be sorted to the front. If no namespaces are supplied then the 'current' scope will be duplicated (including sort order) as the new scope.
| |
| pushStack | back to summary |
|---|---|
| private final void pushStack(final Namespace mns, Namespace[] newscope, final List<Namespace> toadd) | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | |
| private final Namespace[] |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public Namespace | |
| public void | remove()
Overrides default java. |
| cursor | back to summary |
|---|---|
| pack-priv int cursor | |
| namespaces | back to summary |
|---|---|
| private final Namespace[] namespaces | |
| BackwardWalker | back to summary |
|---|---|
| public BackwardWalker(Namespace[] namespaces) | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Doc from java. Returns
| |
| next | back to summary |
|---|---|
| public Namespace next() Implements java. Doc from java. Returns the next element in the iteration. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public Iterator | |
| public Namespace | |
| public void | remove()
Overrides default java. |
| EmptyIterable | back to summary |
|---|---|
| private EmptyIterable() | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Doc from java. Returns
| |
| iterator | back to summary |
|---|---|
| public Iterator Implements java. Doc from java. Returns an iterator over elements of type | |
| next | back to summary |
|---|---|
| public Namespace next() Implements java. Doc from java. Returns the next element in the iteration. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | |
| private final Namespace[] |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public Namespace | |
| public void | remove()
Overrides default java. |
| cursor | back to summary |
|---|---|
| pack-priv int cursor | |
| namespaces | back to summary |
|---|---|
| private final Namespace[] namespaces | |
| ForwardWalker | back to summary |
|---|---|
| public ForwardWalker(Namespace[] namespaces) | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Doc from java. Returns
| |
| next | back to summary |
|---|---|
| public Namespace next() Implements java. Doc from java. Returns the next element in the iteration. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the
| |
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | |
| private final Namespace[] |
| Access | Constructor and Description |
|---|---|
| public |
| forward | back to summary |
|---|---|
| private final boolean forward | |
| namespaces | back to summary |
|---|---|
| private final Namespace[] namespaces | |
| NamespaceIterable | back to summary |
|---|---|
| public NamespaceIterable(Namespace[] data, boolean forward) | |
| iterator | back to summary |
|---|---|
| public Iterator Implements java. Doc from java. Returns an iterator over elements of type | |