View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems;
18  
19  import java.io.IOException;
20  import java.io.Reader;
21  
22  import javax.xml.parsers.ParserConfigurationException;
23  
24  import org.xml.sax.SAXException;
25  
26  /**
27   * "Logical" comparator for XML trees.
28   * <p>
29   * This class implements a "relaxed" comparison between two XML documents.
30   * Starting from the document root elements, a recursive comparison is performed
31   * for each pair of nodes, using the following criteria to determine their
32   * equivalency:
33   *
34   * <ol>
35   *   <li>XML comment clauses are ignored.</li>
36   *   <li>Text nodes among nested elements are ignored.</li>
37   *   <li>Both elements must have the same name, and same attribute set
38   *       (the attribute order is not important).</li>
39   *   <li>If both elements contain a single child of type TEXT, the contents
40   *       must be the same for both nodes.</li>
41   *   <li>Both elements must have the same number of child elements</li>
42   *   <li>For each child element in the "left" element, an equivalent element
43   *       must be present in the "right" one. This equivalence is determined
44   *       through a recursive call to the same comparison algorithm. The
45   *       order of children elements is not taken into account.</li>
46   * </ol>
47   *
48   * <b>NOTE</b>: Ignoring text nodes among elements allows the following two XML
49   * fragments to be considered as equivalent:
50   *
51   * <pre>
52   *   &lt;field>
53   *     &lt;value>123&lt;/value>
54   *   &lt;/field>
55   *
56   *   &ltfield>&lt;value>123&lt;/value>&lt;field>
57   * </pre>
58   *
59   * @author Job Torres
60   * @author Rodrigo Ruiz
61   * @version 2.0
62   * @deprecated This class has been replaced by {@link GridXMLComparator}
63   */
64  public class GridXMLComparer {
65  
66    /**
67     * Wrapped comparator. This class delegates all its methods in
68     * this instance.
69     */
70    private GridXMLComparator gxc = new GridXMLComparator();
71  
72    /**
73     * Creates an instance.
74     */
75    public GridXMLComparer() {
76    }
77  
78    /**
79     * Logically compares two XML documents.
80     *
81     * @param xml1 First document to compare
82     * @param xml2 Second document to compare
83     * @return true if both documents are equal, false otherwise
84     * @throws SAXException If a syntax error is found in one of the XML trees
85     * @throws IOException  If an I/O error occurs reading the sources
86     * @throws ParserConfigurationException If the builder cannot be instantiated
87     */
88    public boolean compareXMLs(Reader xml1, Reader xml2)
89      throws SAXException, IOException, ParserConfigurationException {
90      return gxc.compare(xml1, xml2);
91    }
92  }