Class DefaultComparisonFormatter
- java.lang.Object
-
- org.xmlunit.diff.DefaultComparisonFormatter
-
- All Implemented Interfaces:
ComparisonFormatter
public class DefaultComparisonFormatter extends java.lang.Object implements ComparisonFormatter
Formatter methods for aComparison
Object.
-
-
Constructor Summary
Constructors Constructor Description DefaultComparisonFormatter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
appendAttribute(java.lang.StringBuilder sb, org.w3c.dom.Attr aNode)
Formats a placeholder for an attribute forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected void
appendComment(java.lang.StringBuilder sb, org.w3c.dom.Comment aNode)
Formats a comment forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected void
appendDocumentElementIndication(java.lang.StringBuilder sb, org.w3c.dom.Document doc)
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(java.lang.StringBuilder sb, org.w3c.dom.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
appendDocumentXmlDeclaration(java.lang.StringBuilder sb, org.w3c.dom.Document doc)
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(java.lang.StringBuilder sb, org.w3c.dom.Element aNode)
Formats a placeholder for an element forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected void
appendFullDocumentHeader(java.lang.StringBuilder sb, org.w3c.dom.Document doc)
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)
.private void
appendOnlyElementStartTagWithAttributes(java.lang.StringBuilder sb, org.w3c.dom.Element element)
protected void
appendProcessingInstruction(java.lang.StringBuilder sb, org.w3c.dom.ProcessingInstruction instr)
Formats a processing instruction forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected void
appendText(java.lang.StringBuilder sb, org.w3c.dom.Text aNode)
Formats a text or CDATA node forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
.protected void
appendXPath(java.lang.StringBuilder sb, java.lang.String xpath)
Appends the XPath information forgetShortString(org.w3c.dom.Node, java.lang.String, org.xmlunit.diff.ComparisonType)
if present.protected javax.xml.transform.Transformer
createXmlTransformer(int numberOfBlanksToIndent)
Create a default Transformer to format a XML-Node to a String.java.lang.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.java.lang.String
getDetails(Comparison.Detail difference, ComparisonType type, boolean formatXml)
Return the xml node fromComparison.Detail.getTarget()
as formatted String.protected java.lang.String
getFormattedNodeXml(org.w3c.dom.Node nodeToConvert, boolean formatXml)
Formats a node with the help of an identity XML transformation.protected java.lang.String
getFullFormattedXml(org.w3c.dom.Node node, ComparisonType type, boolean formatXml)
Formats the node using a format suitable for the node type and comparison.protected java.lang.String
getShortString(org.w3c.dom.Node node, java.lang.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 java.lang.Object
getValue(java.lang.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 java.lang.String
nodeType(short type)
Provides a display text for the constant values of theNode
class that represent node types.
-
-
-
Method Detail
-
getDescription
public java.lang.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.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
-
getValue
protected java.lang.Object getValue(java.lang.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.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
protected java.lang.String getShortString(org.w3c.dom.Node node, java.lang.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.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
protected void appendXPath(java.lang.StringBuilder sb, java.lang.String xpath)
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
protected boolean appendDocumentXmlDeclaration(java.lang.StringBuilder sb, org.w3c.dom.Document doc)
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 to- Returns:
- true if the XML declaration has been appended
- Since:
- XMLUnit 2.4.0
-
appendDocumentElementIndication
protected void appendDocumentElementIndication(java.lang.StringBuilder sb, org.w3c.dom.Document doc)
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
protected boolean appendDocumentType(java.lang.StringBuilder sb, org.w3c.dom.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.- Parameters:
sb
- the builder to append totype
- the document type- Returns:
- true if the DOCTPYE has been appended
- Since:
- XMLUnit 2.4.0
-
appendProcessingInstruction
protected void appendProcessingInstruction(java.lang.StringBuilder sb, org.w3c.dom.ProcessingInstruction instr)
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
protected void appendComment(java.lang.StringBuilder sb, org.w3c.dom.Comment aNode)
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
protected void appendText(java.lang.StringBuilder sb, org.w3c.dom.Text aNode)
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
protected void appendElement(java.lang.StringBuilder sb, org.w3c.dom.Element aNode)
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
protected void appendAttribute(java.lang.StringBuilder sb, org.w3c.dom.Attr aNode)
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
public java.lang.String getDetails(Comparison.Detail difference, ComparisonType type, boolean formatXml)
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
protected java.lang.String getFullFormattedXml(org.w3c.dom.Node node, ComparisonType type, boolean formatXml)
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
protected void appendFullDocumentHeader(java.lang.StringBuilder sb, org.w3c.dom.Document doc)
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
-
appendOnlyElementStartTagWithAttributes
private void appendOnlyElementStartTagWithAttributes(java.lang.StringBuilder sb, org.w3c.dom.Element element)
-
getFormattedNodeXml
protected java.lang.String getFormattedNodeXml(org.w3c.dom.Node nodeToConvert, boolean formatXml)
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 javax.xml.transform.Transformer createXmlTransformer(int numberOfBlanksToIndent) throws javax.xml.transform.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:
javax.xml.transform.TransformerConfigurationException
- Since:
- XMLUnit 2.4.0
-
nodeType
protected java.lang.String nodeType(short type)
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
-
-