Class DefaultComparisonFormatter
- java.lang.Object
-
- org.xmlunit.diff.DefaultComparisonFormatter
-
- All Implemented Interfaces:
ComparisonFormatter
public class DefaultComparisonFormatter extends Object implements ComparisonFormatter
Formatter methods for aComparisonObject.
-
-
Constructor Summary
Constructors Constructor Description DefaultComparisonFormatter()
-
Method Summary
-
-
-
Method Detail
-
getDescription
public 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:
getDescriptionin interfaceComparisonFormatter- Parameters:
difference- the comparison to describe- Returns:
- a short description of the comparison
-
setTransformerFactory
public void setTransformerFactory(TransformerFactory f)
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
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.This implementation returns
valueunless it is a comparison of node types in which case the numeric value (one of the constants defined in theNodeclass) 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 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.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
appendXmethods 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(StringBuilder sb, 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(StringBuilder sb, 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 todoc- document to obtain the XML declaration from- Returns:
- true if the XML declaration has been appended
- Since:
- XMLUnit 2.4.0
-
appendDocumentElementIndication
protected void appendDocumentElementIndication(StringBuilder sb, 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(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.- 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(StringBuilder sb, 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(StringBuilder sb, 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(StringBuilder sb, 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(StringBuilder sb, 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(StringBuilder sb, 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 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'stargetis null.- Specified by:
getDetailsin 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 String getFullFormattedXml(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
DocumentandDocumentTypenodes 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(StringBuilder sb, 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
-
getFormattedNodeXml
protected String getFormattedNodeXml(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 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
-
-