Class DefaultComparisonFormatter

java.lang.Object
org.xmlunit.diff.DefaultComparisonFormatter
All Implemented Interfaces:
ComparisonFormatter

public class DefaultComparisonFormatter extends Object implements ComparisonFormatter
Formatter methods for a Comparison Object.
  • Constructor Details

    • DefaultComparisonFormatter

      public DefaultComparisonFormatter()
  • Method Details

    • 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(Node, String, 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 through getValue(Object, ComparisonType)) respectively.

      For missing attributes the string has a slightly different format.

      Specified by:
      getDescription in interface ComparisonFormatter
      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 for getShortString(Node, String, 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 the Node class) is mapped to a more useful String.

      Parameters:
      value - the value to display
      type - 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 for getShortString(Node, String, 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 describe
      xpath - xpath of the node if applicable
      type - the comparison type
      Returns:
      the formatted result
      Since:
      XMLUnit 2.4.0
    • appendXPath

      protected void appendXPath(StringBuilder sb, String xpath)
      Appends the XPath information for getShortString(Node, String, ComparisonType) if present.
      Parameters:
      sb - the builder to append to
      xpath - the xpath to append, if any
      Since:
      XMLUnit 2.4.0
    • appendDocumentXmlDeclaration

      protected boolean appendDocumentXmlDeclaration(StringBuilder sb, Document doc)
      Appends the XML declaration for getShortString(Node, String, ComparisonType) or appendFullDocumentHeader(StringBuilder, Document) if it contains non-default values.
      Parameters:
      sb - the builder to append to
      doc - 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...>" for getShortString(Node, String, ComparisonType).
      Parameters:
      sb - the builder to append to
      doc - the XML document node
      Since:
      XMLUnit 2.4.0
    • appendDocumentType

      protected boolean appendDocumentType(StringBuilder sb, DocumentType type)
      Parameters:
      sb - the builder to append to
      type - 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 for getShortString(Node, String, ComparisonType).
      Parameters:
      sb - the builder to append to
      instr - the processing instruction
      Since:
      XMLUnit 2.4.0
    • appendComment

      protected void appendComment(StringBuilder sb, Comment aNode)
      Parameters:
      sb - the builder to append to
      aNode - the comment
      Since:
      XMLUnit 2.4.0
    • appendText

      protected void appendText(StringBuilder sb, Text aNode)
      Formats a text or CDATA node for getShortString(Node, String, ComparisonType).
      Parameters:
      sb - the builder to append to
      aNode - the text or CDATA node
      Since:
      XMLUnit 2.4.0
    • appendElement

      protected void appendElement(StringBuilder sb, Element aNode)
      Formats a placeholder for an element for getShortString(Node, String, ComparisonType).
      Parameters:
      sb - the builder to append to
      aNode - the element
      Since:
      XMLUnit 2.4.0
    • appendAttribute

      protected void appendAttribute(StringBuilder sb, Attr aNode)
      Formats a placeholder for an attribute for getShortString(Node, String, ComparisonType).
      Parameters:
      sb - the builder to append to
      aNode - the attribute
      Since:
      XMLUnit 2.4.0
    • getDetails

      public String getDetails(Comparison.Detail difference, ComparisonType type, boolean formatXml)
      Return the xml node from Comparison.Detail.getTarget() as formatted String.

      Delegates to getFullFormattedXml(Node, ComparisonType, boolean) unless the Comparison.Detail's target is null.

      Specified by:
      getDetails in interface ComparisonFormatter
      Parameters:
      difference - The Comparison.getControlDetails() or Comparison.getTestDetails().
      type - the implementation can return different details depending on the ComparisonType.
      formatXml - set this to true if the Comparison was generated with DiffBuilder.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 Document and DocumentType 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 to appendFullDocumentHeader(StringBuilder, Document) or getFormattedNodeXml(Node, boolean).

      Parameters:
      node - the node to format
      type - the comparison type
      formatXml - true if the Comparison was generated with DiffBuilder.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 for getFullFormattedXml(Node, ComparisonType, boolean).
      Parameters:
      sb - the builder to append to
      doc - 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 format
      formatXml - true if the Comparison was generated with DiffBuilder.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

      protected String nodeType(short type)
      Provides a display text for the constant values of the Node class that represent node types.
      Parameters:
      type - the node type
      Returns:
      the display text
      Since:
      XMLUnit 2.4.0