Class NamespaceStack

java.lang.Object
org.jdom2.util.NamespaceStack
All Implemented Interfaces:
Iterable<Namespace>

public final class NamespaceStack extends Object implements Iterable<Namespace>
A high-performance stack for processing those Namespaces that are introduced or are in-scope at a point in an Element hierarchy.

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:

   NamespaceStack namespacestack = new NamespaceStack();
   for (Namespace ns : namespacestack) {
      ...
   }
 
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 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.

Author:
Rolf Lear
  • Constructor Details

    • NamespaceStack

      public NamespaceStack()
      Create a NamespaceWalker ready to use as a stack.
      See Also:
    • NamespaceStack

      public NamespaceStack(Namespace[] seed)
      Create a NamespaceWalker ready to use as a stack.
      Parameters:
      seed - The namespaces to set as the top level of the stack.
      See Also:
  • Method Details

    • push

      public void push(Element element)
      Create a new in-scope level for the Stack based on an Element.
      The Namespaces associated with the input Element are used to modify the 'in-scope' Namespaces in this NamespaceStack.
      The following 'rules' will be applied:
      • Namespaces used in the input Element that were not part of the previous scope will be added to the new scope level in the stack.
      • If a new Namespace is added to the scope, but the previous scope already had a namespace with the same prefix, then that previous namespace is removed from the new scope (the new Namespace replaces the previous namespace with the same prefix).
      • The order of the in-scope Namespaces will always be: first the Namespace of the input Element followed by all other in-scope Namespaces sorted alphabetically by prefix.
      • The new in-scope Namespace values will be available in this class's iterator() method (which is available as part of this class's Iterable implementation.
      • The namespaces added to the scope by the input Element will be available in the 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.
      • The same added namespaces are also available in reverse order in the addedReverse() Iterable.
      Parameters:
      element - The element at the new level of the stack.
    • push

      public void push(Attribute att)
      Create a new in-scope level for the Stack based on an Attribute.
      Parameters:
      att - The attribute to contribute to the namespace scope.
    • push

      public void push(Iterable<Namespace> namespaces)
      Create a new in-scope level for the Stack based on an arbitrary set of Namespaces.
      Parameters:
      namespaces - The Iterable format for the Namespaces.
    • push

      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.
      Parameters:
      namespaces - The array of Namespaces.
    • pop

      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.
    • addedForward

      public Iterable<Namespace> addedForward()
      Return an Iterable containing all the Namespaces introduced to the current-level's scope.
      Returns:
      A read-only Iterable containing added Namespaces (may be empty);
      See Also:
    • addedReverse

      public Iterable<Namespace> addedReverse()
      Return an Iterable containing all the Namespaces introduced to the current-level's scope but in reverse order to addedForward().
      Returns:
      A read-only Iterable containing added Namespaces (may be empty);
      See Also:
    • iterator

      public Iterator<Namespace> iterator()
      Get all the Namespaces in-scope at the current level of the stack.
      Specified by:
      iterator in interface Iterable<Namespace>
      Returns:
      A read-only Iterator containing added Namespaces (may be empty);
      See Also:
    • getScope

      public Namespace[] getScope()
      Return a new array instance representing the current scope. Modifying the returned array will not affect this scope.
      Returns:
      a copy of the current scope.
    • isInScope

      public boolean isInScope(Namespace ns)
      Inspect the current scope and return true if the specified namespace is in scope.
      Parameters:
      ns - The Namespace to check
      Returns:
      true if the current scope contains that Namespace.
    • getNamespaceForPrefix

      public Namespace getNamespaceForPrefix(String prefix)
      Get the Namespace in the current scope with the specified prefix.
      Parameters:
      prefix - The prefix to get the namespace for (null is treated the same as "").
      Returns:
      The Namespace with the specified prefix, or null if the prefix is not in scope.
      Since:
      JDOM 2.1.0
    • getFirstNamespaceForURI

      public Namespace getFirstNamespaceForURI(String uri)
      Get the first Namespace in the current scope that is bound to the specified URI.
      Parameters:
      uri - The URI to get the first prefix for (null is treated the same as "").
      Returns:
      The first bound Namespace for the specified URI, or null if the URI is not bound.
      Since:
      JDOM 2.1.0
    • getAllNamespacesForURI

      public Namespace[] getAllNamespacesForURI(String uri)
      Get all prefixes in the current scope that are bound to the specified URI.
      Parameters:
      uri - The URI to get the first prefix for (null is treated the same as "").
      Returns:
      All bound prefixes for the specified URI, or an empty array if the URI is not bound.
      Since:
      JDOM 2.1.0
    • getRebound

      public Namespace getRebound(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.
      Parameters:
      prefix - The prefix to check for re-binding
      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.
      Since:
      JDOM 2.1.0