Class XpathNodeTracker
- java.lang.Object
-
- org.custommonkey.xmlunit.XpathNodeTracker
-
- All Implemented Interfaces:
XMLConstants
public class XpathNodeTracker extends java.lang.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
indentmethod creates a new level, making the tracker walk prepare to walk down the tree, theoutdentmethod 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
visitedNodeorvisitedhas been called - and maybe a current attribute at this node - the last one for whichvisitedAttributeorvisitedhas been called. Attributes are assumed to be at the same level as the nodes they belong to.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classXpathNodeTracker.TrackingEntryHolds node tracking details - one instance is used for each level of indentation in a DOM Provides reference between a String-ified Node value and the xpath index of that value
-
Field Summary
Fields Modifier and Type Field Description private XPathContextctxprivate java.util.LinkedList<XpathNodeTracker.TrackingEntry>levels-
Fields inherited from interface org.custommonkey.xmlunit.XMLConstants
CLOSE_NODE, END_CDATA, END_COMMENT, END_PROCESSING_INSTRUCTION, NULL_NS_URI, OPEN_END_NODE, OPEN_START_NODE, START_CDATA, START_COMMENT, START_DOCTYPE, START_PROCESSING_INSTRUCTION, W3C_XML_SCHEMA_INSTANCE_NO_NAMESPACE_SCHEMA_LOCATION_ATTR, W3C_XML_SCHEMA_INSTANCE_NS_URI, W3C_XML_SCHEMA_INSTANCE_SCHEMA_LOCATION_ATTR, W3C_XML_SCHEMA_NS_URI, XML_DECLARATION, XMLNS_PREFIX, XPATH_ATTRIBUTE_IDENTIFIER, XPATH_CHARACTER_NODE_IDENTIFIER, XPATH_COMMENT_IDENTIFIER, XPATH_NODE_INDEX_END, XPATH_NODE_INDEX_START, XPATH_PROCESSING_INSTRUCTION_IDENTIFIER, XPATH_SEPARATOR
-
-
Constructor Summary
Constructors Constructor Description XpathNodeTracker()Simple constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearTrackedAttribute()Call after processing attributes of an element and turning to compare the child nodes.private static java.lang.StringgetNodeName(org.w3c.dom.Node n)extracts the local name of a node.voidindent()Call before examining child nodes one level of indentation into DOMprivate voidnewLevel()voidoutdent()Call after examining child nodes, ie before returning back one level of indentation from DOM.voidpreloadChildList(java.util.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 comparisonprivate voidpreloadChildren(java.lang.Iterable<org.w3c.dom.Node> nodeList)Preload the nodes by visiting each in turn.voidpreloadNodeList(org.w3c.dom.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 comparisonvoidreset()Clear state data.java.lang.StringtoXpathString()voidvisited(org.w3c.dom.Node node)Call when visiting a node whose xpath location needs trackingprotected voidvisitedAttribute(java.lang.String visited)Invoked byvisitedwhen visiting an attribute node.protected voidvisitedNode(org.w3c.dom.Node visited, java.lang.String value)Invoked byvisitedwhen visited is an element, text, CDATA section, comment or processing instruction.
-
-
-
Field Detail
-
ctx
private XPathContext ctx
-
levels
private final java.util.LinkedList<XpathNodeTracker.TrackingEntry> levels
-
-
Method Detail
-
reset
public void reset()
Clear state data. Call if required to reuse an existing instance.
-
indent
public void indent()
-
newLevel
private void newLevel()
-
clearTrackedAttribute
public void clearTrackedAttribute()
Call after processing attributes of an element and turning to compare the child nodes.
-
outdent
public void outdent()
-
visited
public void visited(org.w3c.dom.Node node)
Call when visiting a node whose xpath location needs trackingDelegates to
visitedAttributefor attribute nodes,visitedNodefor 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(org.w3c.dom.Node visited, java.lang.String value)Invoked byvisitedwhen 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 thevisitedmethod delegates here.value- the local name of the element or an XPath identifier matching the type of node.
-
visitedAttribute
protected void visitedAttribute(java.lang.String visited)
Invoked byvisitedwhen visiting an attribute node.- Parameters:
visited- the local name of the attribute.
-
preloadNodeList
public void preloadNodeList(org.w3c.dom.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 comparisonMakes 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(java.util.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 comparisonMakes 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 java.lang.String toXpathString()
- Returns:
- the last visited node as an xpath-location String
-
getNodeName
private static java.lang.String getNodeName(org.w3c.dom.Node n)
extracts the local name of a node.
-
preloadChildren
private void preloadChildren(java.lang.Iterable<org.w3c.dom.Node> nodeList)
Preload the nodes by visiting each in turn. Required for pieces of test XML whose node children can be visited out of sequence by a DifferenceEngine comparisonMakes 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
-
-