Class XpathNodeTracker

java.lang.Object
org.custommonkey.xmlunit.XpathNodeTracker
All Implemented Interfaces:
XMLConstants

public class XpathNodeTracker extends Object implements XMLConstants
Tracks Nodes visited by the DifferenceEngine and converts that information into an Xpath-String to supply to the NodeDetail of a Difference instance.

The tracker has the concept of a level which corresponds to the depth of the tree it has visited. The indent method creates a new level, making the tracker walk prepare to walk down the tree, the outdent method moves one level up and any information about the previous level is discarded.

At each level there may be a current Node - the last one for which visitedNode or visited has been called - and maybe a current attribute at this node - the last one for which visitedAttribute or visited has been called. Attributes are assumed to be at the same level as the nodes they belong to.

See Also:
  • Constructor Details

    • XpathNodeTracker

      public XpathNodeTracker()
      Simple constructor
  • Method Details

    • reset

      public void reset()
      Clear state data. Call if required to reuse an existing instance.
    • indent

      public void indent()
      Call before examining child nodes one level of indentation into DOM

      Any subsequent call to visited is assumed to belong to nodes one level deeper into the tree than the nodes visited before calling indent.

      As a side effect, the current attribute - if any - is reset.

    • clearTrackedAttribute

      public void clearTrackedAttribute()
      Call after processing attributes of an element and turning to compare the child nodes.
    • outdent

      public void outdent()
      Call after examining child nodes, ie before returning back one level of indentation from DOM.

      Any subsequent call to visited is assumed to belong to nodes one level closer to the root of the tree than the nodes visited before calling outdent.

    • visited

      public void visited(Node node)
      Call when visiting a node whose xpath location needs tracking

      Delegates to visitedAttribute for attribute nodes, visitedNode for elements, texts, CDATA sections, comments or processing instructions and ignores any other type of node.

      Parameters:
      node - the Node being visited - must not be null.
    • visitedNode

      protected void visitedNode(Node visited, String value)
      Invoked by visited when visited is an element, text, CDATA section, comment or processing instruction.
      Parameters:
      visited - the visited node - Unit tests call this with null values, so it is not safe to assume it will never be null. It will never be null when the visited method delegates here.
      value - the local name of the element or an XPath identifier matching the type of node.
    • visitedAttribute

      protected void visitedAttribute(String visited)
      Invoked by visited when visiting an attribute node.
      Parameters:
      visited - the local name of the attribute.
    • preloadNodeList

      public void preloadNodeList(NodeList nodeList)
      Preload the items in a NodeList by visiting each in turn Required for pieces of test XML whose node children can be visited out of sequence by a DifferenceEngine comparison

      Makes the nodes of this list known as nodes that are visitable at the current level and makes the last child node the current node as a side effect.

      Parameters:
      nodeList - the items to preload
    • preloadChildList

      public void preloadChildList(List nodeList)
      Preload the items in a List by visiting each in turn Required for pieces of test XML whose node children can be visited out of sequence by a DifferenceEngine comparison

      Makes the nodes of this list known as nodes that are visitable at the current level and makes the last child node the current node as a side effect.

      Parameters:
      nodeList - the items to preload
    • toXpathString

      public String toXpathString()
      Returns:
      the last visited node as an xpath-location String