Class NamespaceStack

  • All Implemented Interfaces:
    java.lang.Iterable<Namespace>

    public final class NamespaceStack
    extends java.lang.Object
    implements java.lang.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 Summary

      Constructors 
      Constructor Description
      NamespaceStack()
      Create a NamespaceWalker ready to use as a stack.
      NamespaceStack​(Namespace[] seed)
      Create a NamespaceWalker ready to use as a stack.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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[] 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • NamespaceStack

        public NamespaceStack()
        Create a NamespaceWalker ready to use as a stack.
        See Also:
        for comprehensive notes.
      • 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:
        for comprehensive notes.
    • Method Detail

      • 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.
      • 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 java.lang.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:
        for the details on the data order.
      • addedReverse

        public 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().
        Returns:
        A read-only Iterable containing added Namespaces (may be empty);
        See Also:
        for the details on the data order.
      • iterator

        public java.util.Iterator<Namespace> iterator()
        Get all the Namespaces in-scope at the current level of the stack.
        Specified by:
        iterator in interface java.lang.Iterable<Namespace>
        Returns:
        A read-only Iterator containing added Namespaces (may be empty);
        See Also:
        for the details on the data order.
      • 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.