com.gridsystems
Class GridXMLComparator

java.lang.Object
  extended by com.gridsystems.GridXMLComparator

public class GridXMLComparator
extends java.lang.Object

"Logical" comparator for XML trees.

This class implements a "relaxed" comparison between two XML documents. Starting from the document root elements, a recursive comparison is performed for each pair of nodes, using the following criteria to determine their equivalency:

  1. XML comment clauses are ignored.
  2. Text nodes among nested elements are ignored.
  3. Both elements must have the same name, and same attribute set (the attribute order is not important).
  4. If both elements contain a single child of type TEXT, the contents must be the same for both nodes.
  5. Both elements must have the same number of child elements
  6. For each child element in the "left" element, an equivalent element must be present in the "right" one. This equivalence is determined through a recursive call to the same comparison algorithm. The order of children elements is not taken into account.
NOTE: Ignoring text nodes among elements allows the following two XML fragments to be considered as equivalent:
   <field>
     <value>123</value>
   </field>

   <field><value>123</value><field>
 

Version:
2.0
Author:
Job Torres, Rodrigo Ruiz

Constructor Summary
GridXMLComparator()
          Creates an instance.
 
Method Summary
 boolean compare(org.w3c.dom.Document d1, org.w3c.dom.Document d2)
          Compares the given documents.
 boolean compare(org.w3c.dom.Element elem1, org.w3c.dom.Element elem2)
          Compares the given elements.
 boolean compare(java.io.File f1, java.io.File f2)
          Compares two XML documents for equivalency.
 boolean compare(java.io.Reader xml1, java.io.Reader xml2)
          Compares two XML documents for equivalency.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GridXMLComparator

public GridXMLComparator()
Creates an instance.

Method Detail

compare

public boolean compare(java.io.Reader xml1,
                       java.io.Reader xml2)
                throws org.xml.sax.SAXException,
                       java.io.IOException,
                       javax.xml.parsers.ParserConfigurationException
Compares two XML documents for equivalency.

Parameters:
xml1 - A reader containing the first document to compare
xml2 - A reader containing the second document to compare
Returns:
true if both documents are equivalent; false otherwise
Throws:
org.xml.sax.SAXException - If a syntax error is found in one of the XML trees
java.io.IOException - If an I/O error occurs reading the sources
javax.xml.parsers.ParserConfigurationException - If the builder cannot be instantiated

compare

public boolean compare(java.io.File f1,
                       java.io.File f2)
                throws org.xml.sax.SAXException,
                       java.io.IOException,
                       javax.xml.parsers.ParserConfigurationException
Compares two XML documents for equivalency.

Parameters:
f1 - A file containing the first document to compare
f2 - A file containing the second document to compare
Returns:
true if both documents are equivalent; false otherwise
Throws:
org.xml.sax.SAXException - If a syntax error is found in one of the XML trees
java.io.IOException - If an I/O error occurs reading the sources
javax.xml.parsers.ParserConfigurationException - If the builder cannot be instantiated

compare

public boolean compare(org.w3c.dom.Document d1,
                       org.w3c.dom.Document d2)
Compares the given documents.

Parameters:
d1 - The first document
d2 - The second document
Returns:
true if they are "logically" equal

compare

public boolean compare(org.w3c.dom.Element elem1,
                       org.w3c.dom.Element elem2)
Compares the given elements.

Parameters:
elem1 - Left element to compare
elem2 - Right element to compare
Returns:
true if both elements are equivalent; false otherwise


Copyright © 2007-2008 Grid Systems, S.A.. All Rights Reserved.