Class DefaultComparisonFormatter
- All Implemented Interfaces:
ComparisonFormatter
Comparison
Object.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
appendAttribute
(StringBuilder sb, Attr aNode) Formats a placeholder for an attribute forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected void
appendComment
(StringBuilder sb, Comment aNode) Formats a comment forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected void
Appends a short indication of the document's root element like "<ElementName...>" forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected boolean
appendDocumentType
(StringBuilder sb, DocumentType type) Appends the XML DOCTYPE forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
orappendFullDocumentHeader(java.lang.StringBuilder, org.w3c.dom.Document)
if present.protected boolean
Appends the XML declaration forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
orappendFullDocumentHeader(java.lang.StringBuilder, org.w3c.dom.Document)
if it contains non-default values.protected void
appendElement
(StringBuilder sb, Element aNode) Formats a placeholder for an element forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected void
Appends the XML declaration and DOCTYPE if present as well as the document's root element forgetFullFormattedXml(org.w3c.dom.Node, org.xmlunit.diff.ComparisonType, boolean)
.protected void
Formats a processing instruction forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected void
appendText
(StringBuilder sb, Text aNode) Formats a text or CDATA node forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected void
appendXPath
(StringBuilder sb, String xpath) Appends the XPath information forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
if present.protected Transformer
createXmlTransformer
(int numberOfBlanksToIndent) Create a default Transformer to format a XML-Node to a String.getDescription
(Comparison difference) Return a short String of the Comparison including the XPath and the shorten value of the effected control and test Node.getDetails
(Comparison.Detail difference, ComparisonType type, boolean formatXml) Return the xml node fromComparison.Detail.getTarget()
as formatted String.protected String
getFormattedNodeXml
(Node nodeToConvert, boolean formatXml) Formats a node with the help of an identity XML transformation.protected String
getFullFormattedXml
(Node node, ComparisonType type, boolean formatXml) Formats the node using a format suitable for the node type and comparison.protected String
getShortString
(Node node, String xpath, ComparisonType type) Return a String representation forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
that describes the "thing" that has been compared so users know how to locate it.protected Object
getValue
(Object value, ComparisonType type) May alter the display of a comparison value forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
based on the comparison type.protected String
nodeType
(short type) Provides a display text for the constant values of theNode
class that represent node types.void
Set the TraX factory to use.
-
Constructor Details
-
DefaultComparisonFormatter
public DefaultComparisonFormatter()
-
-
Method Details
-
getDescription
Return a short String of the Comparison including the XPath and the shorten value of the effected control and test Node.In general the String will look like "Expected X 'Y' but was 'Z' - comparing A to B" where A and B are the result of invoking
getShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
on the target and XPath of the control and test details of the comparison. A is the description of the comparison and B and C are the control and test values (passed throughgetValue(java.lang.Object, org.xmlunit.diff.ComparisonType)
) respectively.For missing attributes the string has a slightly different format.
- Specified by:
getDescription
in interfaceComparisonFormatter
- Parameters:
difference
- the comparison to describe- Returns:
- a short description of the comparison
-
setTransformerFactory
Set the TraX factory to use.- Parameters:
f
- the factory to use - may be null in which case the default factory will be used.- Since:
- 2.8.0
-
getValue
May alter the display of a comparison value forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
based on the comparison type.This implementation returns
value
unless it is a comparison of node types in which case the numeric value (one of the constants defined in theNode
class) is mapped to a more useful String.- Parameters:
value
- the value to displaytype
- the comparison type- Returns:
- the display value
- Since:
- XMLUnit 2.4.0
-
getShortString
Return a String representation forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
that describes the "thing" that has been compared so users know how to locate it.Examples are "<bar ...> at /foo[1]/bar[1]" for a comparison of elements or "<!-- Comment Text --> at /foo[2]/comment()[1]" for a comment.
This implementation dispatches to several
appendX
methods based on the comparison type or the type of the node.- Parameters:
node
- the node to describexpath
- xpath of the node if applicabletype
- the comparison type- Returns:
- the formatted result
- Since:
- XMLUnit 2.4.0
-
appendXPath
Appends the XPath information forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
if present.- Parameters:
sb
- the builder to append toxpath
- the xpath to append, if any- Since:
- XMLUnit 2.4.0
-
appendDocumentXmlDeclaration
Appends the XML declaration forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
orappendFullDocumentHeader(java.lang.StringBuilder, org.w3c.dom.Document)
if it contains non-default values.- Parameters:
sb
- the builder to append todoc
- document to obtain the XML declaration from- Returns:
- true if the XML declaration has been appended
- Since:
- XMLUnit 2.4.0
-
appendDocumentElementIndication
Appends a short indication of the document's root element like "<ElementName...>" forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.- Parameters:
sb
- the builder to append todoc
- the XML document node- Since:
- XMLUnit 2.4.0
-
appendDocumentType
Appends the XML DOCTYPE forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
orappendFullDocumentHeader(java.lang.StringBuilder, org.w3c.dom.Document)
if present.- Parameters:
sb
- the builder to append totype
- the document type- Returns:
- true if the DOCTPYE has been appended
- Since:
- XMLUnit 2.4.0
-
appendProcessingInstruction
Formats a processing instruction forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.- Parameters:
sb
- the builder to append toinstr
- the processing instruction- Since:
- XMLUnit 2.4.0
-
appendComment
Formats a comment forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.- Parameters:
sb
- the builder to append toaNode
- the comment- Since:
- XMLUnit 2.4.0
-
appendText
Formats a text or CDATA node forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.- Parameters:
sb
- the builder to append toaNode
- the text or CDATA node- Since:
- XMLUnit 2.4.0
-
appendElement
Formats a placeholder for an element forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.- Parameters:
sb
- the builder to append toaNode
- the element- Since:
- XMLUnit 2.4.0
-
appendAttribute
Formats a placeholder for an attribute forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.- Parameters:
sb
- the builder to append toaNode
- the attribute- Since:
- XMLUnit 2.4.0
-
getDetails
Return the xml node fromComparison.Detail.getTarget()
as formatted String.Delegates to
getFullFormattedXml(org.w3c.dom.Node, org.xmlunit.diff.ComparisonType, boolean)
unless theComparison.Detail
'starget
is null.- Specified by:
getDetails
in interfaceComparisonFormatter
- Parameters:
difference
- TheComparison.getControlDetails()
orComparison.getTestDetails()
.type
- the implementation can return different details depending on the ComparisonType.formatXml
- set this to true if the Comparison was generated withDiffBuilder.ignoreWhitespace()
- this affects the indentation of the generated output.- Returns:
- the full xml node.
-
getFullFormattedXml
Formats the node using a format suitable for the node type and comparison.The implementation outputs the document prolog and start element for
Document
andDocumentType
nodes and may elect to format the node's parent element rather than just the node depending on the node and comparison type. It delegates toappendFullDocumentHeader(java.lang.StringBuilder, org.w3c.dom.Document)
orgetFormattedNodeXml(org.w3c.dom.Node, boolean)
.- Parameters:
node
- the node to formattype
- the comparison typeformatXml
- true if the Comparison was generated withDiffBuilder.ignoreWhitespace()
- this affects the indentation of the generated output- Returns:
- the fomatted XML
- Since:
- XMLUnit 2.4.0
-
appendFullDocumentHeader
Appends the XML declaration and DOCTYPE if present as well as the document's root element forgetFullFormattedXml(org.w3c.dom.Node, org.xmlunit.diff.ComparisonType, boolean)
.- Parameters:
sb
- the builder to append todoc
- the document to format- Since:
- XMLUnit 2.4.0
-
getFormattedNodeXml
Formats a node with the help of an identity XML transformation.- Parameters:
nodeToConvert
- the node to formatformatXml
- true if the Comparison was generated withDiffBuilder.ignoreWhitespace()
- this affects the indentation of the generated output- Returns:
- the fomatted XML
- Since:
- XMLUnit 2.4.0
-
createXmlTransformer
protected Transformer createXmlTransformer(int numberOfBlanksToIndent) throws TransformerConfigurationException Create a default Transformer to format a XML-Node to a String.- Parameters:
numberOfBlanksToIndent
- the number of spaces which is used for indent the XML-structure- Returns:
- the transformer
- Throws:
TransformerConfigurationException
- if JAXP wants to- Since:
- XMLUnit 2.4.0
-
nodeType
Provides a display text for the constant values of theNode
class that represent node types.- Parameters:
type
- the node type- Returns:
- the display text
- Since:
- XMLUnit 2.4.0
-