javax.jcr.util
Class TraversingItemVisitor

java.lang.Object
  extended by javax.jcr.util.TraversingItemVisitor
All Implemented Interfaces:
ItemVisitor
Direct Known Subclasses:
TraversingItemVisitor.Default

public abstract class TraversingItemVisitor
extends java.lang.Object
implements ItemVisitor

An implementation of ItemVisitor.

TraversingItemVisitor is an abstract utility class which allows to easily traverse an Item hierarchy.

TraversingItemVisitor makes use of the Visitor Pattern as described in the book 'Design Patterns' by the Gang Of Four (Gamma et al.).

Tree traversal is done observing the left-to-right order of child Items if such an order is supported and exists.


Nested Class Summary
static class TraversingItemVisitor.Default
          Convenience class providing default implementations of the abstract methods of TraversingItemVisitor.
 
Field Summary
protected  boolean breadthFirst
          Indicates if traversal should be done in a breadth-first manner rather than depth-first (which is the default).
private  int currentLevel
          Used to track hierarchy level of item currently being processed.
private  java.util.LinkedList currentQueue
          Queues used to implement breadth-first traversal.
protected  int maxLevel
          The 0-based level up to which the hierarchy should be traversed (if it's -1, the hierarchy will be traversed until there are no more children of the current item).
private  java.util.LinkedList nextQueue
           
 
Constructor Summary
TraversingItemVisitor()
          Constructs a new instance of this class.
TraversingItemVisitor(boolean breadthFirst)
          Constructs a new instance of this class.
TraversingItemVisitor(boolean breadthFirst, int maxLevel)
          Constructs a new instance of this class.
 
Method Summary
protected abstract  void entering(Node node, int level)
          Implement this method to add behavior performed before a Node is visited.
protected abstract  void entering(Property property, int level)
          Implement this method to add behavior performed before a Property is visited.
protected abstract  void leaving(Node node, int level)
          Implement this method to add behavior performed after a Node is visited.
protected abstract  void leaving(Property property, int level)
          Implement this method to add behavior performed after a Property is visited.
 void visit(Node node)
          Called when the Visitor is passed to a Node.
 void visit(Property property)
          Called when the Visitor is passed to a Property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

breadthFirst

protected final boolean breadthFirst
Indicates if traversal should be done in a breadth-first manner rather than depth-first (which is the default).


maxLevel

protected final int maxLevel
The 0-based level up to which the hierarchy should be traversed (if it's -1, the hierarchy will be traversed until there are no more children of the current item).


currentQueue

private java.util.LinkedList currentQueue
Queues used to implement breadth-first traversal.


nextQueue

private java.util.LinkedList nextQueue

currentLevel

private int currentLevel
Used to track hierarchy level of item currently being processed.

Constructor Detail

TraversingItemVisitor

public TraversingItemVisitor()
Constructs a new instance of this class.

The tree of Items will be traversed in a depth-first manner (default behavior).


TraversingItemVisitor

public TraversingItemVisitor(boolean breadthFirst)
Constructs a new instance of this class.

Parameters:
breadthFirst - if breadthFirst is true then traversal is done in a breadth-first manner; otherwise it is done in a depth-first manner (which is the default behavior).

TraversingItemVisitor

public TraversingItemVisitor(boolean breadthFirst,
                             int maxLevel)
Constructs a new instance of this class.

Parameters:
breadthFirst - if breadthFirst is true then traversal is done in a breadth-first manner; otherwise it is done in a depth-first manner (which is the default behavior).
maxLevel - the 0-based level up to which the hierarchy should be traversed (if it's -1, the hierarchy will be traversed until there are no more children of the current item).
Method Detail

entering

protected abstract void entering(Property property,
                                 int level)
                          throws RepositoryException
Implement this method to add behavior performed before a Property is visited.

Parameters:
property - the Property that is accepting this visitor.
level - hierarchy level of this property (the root node starts at level 0).
Throws:
RepositoryException - if an error occurs.

entering

protected abstract void entering(Node node,
                                 int level)
                          throws RepositoryException
Implement this method to add behavior performed before a Node is visited.

Parameters:
node - the Node that is accepting this visitor.
level - hierarchy level of this node (the root node starts at level 0).
Throws:
RepositoryException - if an error occurs.

leaving

protected abstract void leaving(Property property,
                                int level)
                         throws RepositoryException
Implement this method to add behavior performed after a Property is visited.

Parameters:
property - the Property that is accepting this visitor.
level - hierarchy level of this property (the root node starts at level 0).
Throws:
RepositoryException - if an error occurs.

leaving

protected abstract void leaving(Node node,
                                int level)
                         throws RepositoryException
Implement this method to add behavior performed after a Node is visited.

Parameters:
node - the Node that is accepting this visitor.
level - hierarchy level of this node (the root node starts at level 0).
Throws:
RepositoryException - if an error occurs.

visit

public void visit(Property property)
           throws RepositoryException
Called when the Visitor is passed to a Property.

It calls TraversingItemVisitor.entering(Property, int) followed by TraversingItemVisitor.leaving(Property, int). Implement these abstract methods to specify behavior on 'arrival at' and 'after leaving' the Property.

If this method throws, the visiting process is aborted.

Specified by:
visit in interface ItemVisitor
Parameters:
property - the Property that is accepting this visitor.
Throws:
RepositoryException - if an error occurs.

visit

public void visit(Node node)
           throws RepositoryException
Called when the Visitor is passed to a Node.

It calls TraversingItemVisitor.entering(Node, int) followed by TraversingItemVisitor.leaving(Node, int). Implement these abstract methods to specify behavior on 'arrival at' and 'after leaving' the Node.

If this method throws, the visiting process is aborted.

Specified by:
visit in interface ItemVisitor
Parameters:
node - the Node that is accepting this visitor.
Throws:
RepositoryException - if an error occurs.