xref: /aoo41x/main/xmerge/workben/XmlDiff.java (revision 04ea5bd4)
1*04ea5bd4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*04ea5bd4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*04ea5bd4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*04ea5bd4SAndrew Rist  * distributed with this work for additional information
6*04ea5bd4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*04ea5bd4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*04ea5bd4SAndrew Rist  * "License"); you may not use this file except in compliance
9*04ea5bd4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*04ea5bd4SAndrew Rist  *
11*04ea5bd4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*04ea5bd4SAndrew Rist  *
13*04ea5bd4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*04ea5bd4SAndrew Rist  * software distributed under the License is distributed on an
15*04ea5bd4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*04ea5bd4SAndrew Rist  * KIND, either express or implied.  See the License for the
17*04ea5bd4SAndrew Rist  * specific language governing permissions and limitations
18*04ea5bd4SAndrew Rist  * under the License.
19*04ea5bd4SAndrew Rist  *
20*04ea5bd4SAndrew Rist  *************************************************************/
21*04ea5bd4SAndrew Rist 
22*04ea5bd4SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import java.io.FileWriter;
25cdf0e10cSrcweir import java.io.InputStream;
26cdf0e10cSrcweir import java.io.FileInputStream;
27cdf0e10cSrcweir import java.io.BufferedInputStream;
28cdf0e10cSrcweir import java.io.BufferedWriter;
29cdf0e10cSrcweir import java.io.IOException;
30cdf0e10cSrcweir import java.io.FileNotFoundException;
31cdf0e10cSrcweir import java.io.OutputStream;
32cdf0e10cSrcweir import java.io.OutputStreamWriter;
33cdf0e10cSrcweir import java.io.Writer;
34cdf0e10cSrcweir import java.io.PrintWriter;
35cdf0e10cSrcweir import java.util.Vector;
36cdf0e10cSrcweir import java.util.Properties;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import javax.xml.parsers.DocumentBuilderFactory;
39cdf0e10cSrcweir import javax.xml.parsers.DocumentBuilder;
40cdf0e10cSrcweir import javax.xml.parsers.ParserConfigurationException;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir import org.w3c.dom.Node;
43cdf0e10cSrcweir import org.w3c.dom.Document;
44cdf0e10cSrcweir import org.w3c.dom.NodeList;
45cdf0e10cSrcweir import org.xml.sax.SAXException;
46cdf0e10cSrcweir import org.xml.sax.SAXParseException;
47cdf0e10cSrcweir /**
48cdf0e10cSrcweir  *  This class will diff 2 Xml files.
49cdf0e10cSrcweir  *
50cdf0e10cSrcweir  *  @author   Stephen Mak
51cdf0e10cSrcweir  */
52cdf0e10cSrcweir 
53cdf0e10cSrcweir public final class XmlDiff {
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     private static final String PROPSFILE = "XmlDiff.properties";
56cdf0e10cSrcweir     private static final String FILE1 = "XmlDiff.file1";
57cdf0e10cSrcweir     private static final String FILE2 = "XmlDiff.file2";
58cdf0e10cSrcweir     private static final String OUTPUT= "XmlDiff.output";
59cdf0e10cSrcweir     private static final String IGNORE_TAGS= "XmlDiff.tags";
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     private Properties props_ = null;
62cdf0e10cSrcweir     private static PrintWriter writer_ = null;
63cdf0e10cSrcweir     private String[] tags_ = null;
64cdf0e10cSrcweir     private String file1_ = null;
65cdf0e10cSrcweir     private String file2_ = null;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     /**
68cdf0e10cSrcweir      *  Constructor.  Load the properties file.
69cdf0e10cSrcweir      */
70cdf0e10cSrcweir 
XmlDiff()71cdf0e10cSrcweir     public XmlDiff() throws IOException {
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         Class c = this.getClass();
74cdf0e10cSrcweir         InputStream is = c.getResourceAsStream(PROPSFILE);
75cdf0e10cSrcweir         BufferedInputStream bis = new BufferedInputStream(is);
76cdf0e10cSrcweir         props_ = new Properties();
77cdf0e10cSrcweir         props_.load(bis);
78cdf0e10cSrcweir         bis.close();
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         String file1 = props_.getProperty(FILE1, "");
81cdf0e10cSrcweir         String file2 = props_.getProperty(FILE2, "");
82cdf0e10cSrcweir         String tagsString = props_.getProperty(IGNORE_TAGS, "");
83cdf0e10cSrcweir         String output = props_.getProperty("debug.output", "System.out");
84cdf0e10cSrcweir         setOutput(output);
85cdf0e10cSrcweir         tags_ = parseTags(tagsString);
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     /**
89cdf0e10cSrcweir      * diff 2 xml, but overwrite the property file's file1/2 setting with
90cdf0e10cSrcweir      * the input argument
91cdf0e10cSrcweir      */
diff(String file1, String file2)92cdf0e10cSrcweir     public boolean diff(String file1, String file2)  throws IOException {
93cdf0e10cSrcweir         file1_ = file1;
94cdf0e10cSrcweir         file2_ = file2;
95cdf0e10cSrcweir         return diff();
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
diff()98cdf0e10cSrcweir     public boolean diff() throws IOException {
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         boolean result = false;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         writer_.println("parsing "+ file1_ + "...");
103cdf0e10cSrcweir         // parse the Xml file
104cdf0e10cSrcweir         Document doc1 = parseXml(file1_);
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         writer_.println("parsing "+ file1_ + "...");
107cdf0e10cSrcweir         Document doc2 = parseXml(file2_);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         if (doc1 != null && doc2 != null) {
110cdf0e10cSrcweir           writer_.println("diffing "+ file1_ + " & " + file2_ + "...");
111cdf0e10cSrcweir           result = compareNode(doc1, doc2);
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir         return result;
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
diffLog(String errMsg, Node node1, Node node2)116cdf0e10cSrcweir     private void diffLog(String errMsg, Node node1, Node node2) {
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         String node1Str = "";
119cdf0e10cSrcweir         String node2Str = "";
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         if (node1 != null) {
122cdf0e10cSrcweir             node1Str = "[Type]:" + nodeInfo(node1) +
123cdf0e10cSrcweir                        " [Name]:" + node1.getNodeName();
124cdf0e10cSrcweir             if (node1.getNodeValue() != null)
125cdf0e10cSrcweir                 node1Str += " [Value]:" + node1.getNodeValue();
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         if (node2 != null) {
129cdf0e10cSrcweir             node2Str = "[Type]:" + nodeInfo(node2) +
130cdf0e10cSrcweir                        " [Name]:" + node2.getNodeName();
131cdf0e10cSrcweir             if (node2.getNodeValue() != null)
132cdf0e10cSrcweir                 node2Str += " [Value]:" + node2.getNodeValue();
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         writer_.println(errMsg);
136cdf0e10cSrcweir         writer_.println("  Node1 - " + node1Str);
137cdf0e10cSrcweir         writer_.println("  Node2 - " + node2Str);
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
nodeInfo(Node node)140cdf0e10cSrcweir     private String nodeInfo(Node node) {
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         String str = null;
143cdf0e10cSrcweir         switch (node.getNodeType()) {
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         	case Node.ELEMENT_NODE:
146cdf0e10cSrcweir         	    str = "ELEMENT";
147cdf0e10cSrcweir         	    break;
148cdf0e10cSrcweir         	case Node.ATTRIBUTE_NODE:
149cdf0e10cSrcweir         	    str = "ATTRIBUTE";
150cdf0e10cSrcweir         	    break;
151cdf0e10cSrcweir         	case Node.TEXT_NODE:
152cdf0e10cSrcweir         	    str = "TEXT";
153cdf0e10cSrcweir         	    break;
154cdf0e10cSrcweir         	case Node.CDATA_SECTION_NODE:
155cdf0e10cSrcweir         	    str = "CDATA_SECTION";
156cdf0e10cSrcweir         	    break;
157cdf0e10cSrcweir         	case Node.ENTITY_REFERENCE_NODE:
158cdf0e10cSrcweir         	    str = "ENTITY_REFERENCE";
159cdf0e10cSrcweir         	    break;
160cdf0e10cSrcweir         	case Node.ENTITY_NODE:
161cdf0e10cSrcweir         	    str = "ENTITY";
162cdf0e10cSrcweir         	    break;
163cdf0e10cSrcweir         	case Node.PROCESSING_INSTRUCTION_NODE:
164cdf0e10cSrcweir         	    str = "PROCESSING_INSTRUCTION";
165cdf0e10cSrcweir         	    break;
166cdf0e10cSrcweir         	case Node.COMMENT_NODE:
167cdf0e10cSrcweir         	    str = "COMMENT";
168cdf0e10cSrcweir         	    break;
169cdf0e10cSrcweir         	case Node.DOCUMENT_NODE:
170cdf0e10cSrcweir         	    str = "DOCUMENT";
171cdf0e10cSrcweir         	    break;
172cdf0e10cSrcweir         	case Node.DOCUMENT_TYPE_NODE:
173cdf0e10cSrcweir         	    str = "DOCUMENT_TYPE";
174cdf0e10cSrcweir         	    break;
175cdf0e10cSrcweir         	case Node.DOCUMENT_FRAGMENT_NODE:
176cdf0e10cSrcweir         	    str = "DOCUMENT_FRAGMENT";
177cdf0e10cSrcweir         	    break;
178cdf0e10cSrcweir         	case Node.NOTATION_NODE:
179cdf0e10cSrcweir         	    str = "NOTATION";
180cdf0e10cSrcweir         	    break;
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir         return str;
183cdf0e10cSrcweir     }
184cdf0e10cSrcweir 
ignoreTag(String nodeName)185cdf0e10cSrcweir     private boolean ignoreTag(String nodeName) {
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         if (tags_ != null) {
189cdf0e10cSrcweir             for (int i = 0; i < tags_.length; i++) {
190cdf0e10cSrcweir                 if (tags_[i].equals(nodeName))
191cdf0e10cSrcweir                     return true;
192cdf0e10cSrcweir             }
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir         return false;
195cdf0e10cSrcweir     }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir     // for future use if we want to compare attributes
attributesEqual(Node node1, Node node2)198cdf0e10cSrcweir     private boolean attributesEqual(Node node1, Node node2) {
199cdf0e10cSrcweir         return true;
200cdf0e10cSrcweir     }
201cdf0e10cSrcweir 
compareNode(Node node1, Node node2)202cdf0e10cSrcweir     private boolean compareNode(Node node1, Node node2) {
203cdf0e10cSrcweir         boolean equal = false;
204cdf0e10cSrcweir 
205cdf0e10cSrcweir         while (true) {
206cdf0e10cSrcweir 
207cdf0e10cSrcweir             if (node1 == null && node2 == null) {
208cdf0e10cSrcweir                 equal = true;
209cdf0e10cSrcweir                 break;
210cdf0e10cSrcweir             } else if (node1 == null || node2 == null) {
211cdf0e10cSrcweir                 diffLog("DIFF: one of the node is null", node1, node2);
212cdf0e10cSrcweir                 break;
213cdf0e10cSrcweir             }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir             if (node1.getNodeType() != node2.getNodeType()) {
216cdf0e10cSrcweir                 diffLog("DIFF: nodetype is different", node1, node2);
217cdf0e10cSrcweir                 break;
218cdf0e10cSrcweir             }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir             if (node1.getNodeName() == null && node2.getNodeName() == null) {
221cdf0e10cSrcweir                 // empty
222cdf0e10cSrcweir             } else if (node1.getNodeName() == null ||
223cdf0e10cSrcweir                        node2.getNodeName() == null) {
224cdf0e10cSrcweir                 diffLog("DIFF: one of the nodeName is null", node1, node2);
225cdf0e10cSrcweir                 break;
226cdf0e10cSrcweir             } else if (!node1.getNodeName().equals(node2.getNodeName())) {
227cdf0e10cSrcweir                 diffLog("DIFF: nodeName is different", node1, node2);
228cdf0e10cSrcweir                 break;
229cdf0e10cSrcweir             }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir             if (ignoreTag(node1.getNodeName())) {
232cdf0e10cSrcweir                 diffLog("DIFF: Some tag(s) is ignored", node1, node2);
233cdf0e10cSrcweir                 equal = true;
234cdf0e10cSrcweir                 break;
235cdf0e10cSrcweir             }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir             if (node1.getNodeValue() == null && node2.getNodeValue() == null) {
238cdf0e10cSrcweir                 // empty
239cdf0e10cSrcweir             } else if (node1.getNodeValue() == null ||
240cdf0e10cSrcweir                        node2.getNodeValue() == null) {
241cdf0e10cSrcweir                 diffLog("DIFF: one of the nodevalue is null", node1, node2);
242cdf0e10cSrcweir                 break;
243cdf0e10cSrcweir             } else if (!node1.getNodeValue().equals(node2.getNodeValue())) {
244cdf0e10cSrcweir                 diffLog("DIFF: nodeValue is different", node1, node2);
245cdf0e10cSrcweir                 break;
246cdf0e10cSrcweir             }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir             // try to compare attributes if necessary
249cdf0e10cSrcweir             if (!attributesEqual(node1, node2))
250cdf0e10cSrcweir                 break;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir             NodeList node1Children = node1.getChildNodes();
253cdf0e10cSrcweir             NodeList node2Children = node2.getChildNodes();
254cdf0e10cSrcweir 
255cdf0e10cSrcweir             // number of children have to be the same
256cdf0e10cSrcweir             if (node1Children == null && node2Children == null) {
257cdf0e10cSrcweir                 equal = true;
258cdf0e10cSrcweir                 break;
259cdf0e10cSrcweir             }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir             if (node1Children == null || node2Children == null) {
262cdf0e10cSrcweir                 diffLog("DIFF: one node's children is null", node1, node2);
263cdf0e10cSrcweir                 break;
264cdf0e10cSrcweir             }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir             if (node1Children.getLength() != node2Children.getLength())  {
267cdf0e10cSrcweir                 diffLog("DIFF: num of children is different", node1, node2);
268cdf0e10cSrcweir                 break;
269cdf0e10cSrcweir             }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir             // compare all the childrens
272cdf0e10cSrcweir             equal = true;
273cdf0e10cSrcweir 
274cdf0e10cSrcweir             for (int i = 0; i < node1Children.getLength(); i++) {
275cdf0e10cSrcweir                 if (!compareNode(node1Children.item(i),
276cdf0e10cSrcweir                                  node2Children.item(i))) {
277cdf0e10cSrcweir                     equal = false;
278cdf0e10cSrcweir                     break;
279cdf0e10cSrcweir                 }
280cdf0e10cSrcweir             }
281cdf0e10cSrcweir             break;
282cdf0e10cSrcweir         }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir         return equal;
285cdf0e10cSrcweir     }
286cdf0e10cSrcweir 
parseXml(String filename)287cdf0e10cSrcweir     private Document parseXml (String filename) throws IOException {
288cdf0e10cSrcweir 
289cdf0e10cSrcweir         Document w3cDocument = null;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir         FileInputStream fis;
292cdf0e10cSrcweir 
293cdf0e10cSrcweir         try {
294cdf0e10cSrcweir             fis = new FileInputStream(filename);
295cdf0e10cSrcweir         } catch (FileNotFoundException ex) {
296cdf0e10cSrcweir             ex.printStackTrace(writer_);
297cdf0e10cSrcweir             writer_.println(ex.getMessage());
298cdf0e10cSrcweir             return w3cDocument;
299cdf0e10cSrcweir         }
300cdf0e10cSrcweir 
301cdf0e10cSrcweir         /** factory for DocumentBuilder objects */
302cdf0e10cSrcweir         DocumentBuilderFactory factory = null;
303cdf0e10cSrcweir         factory = DocumentBuilderFactory.newInstance();
304cdf0e10cSrcweir         factory.setNamespaceAware(true);
305cdf0e10cSrcweir         factory.setValidating(false);
306cdf0e10cSrcweir 
307cdf0e10cSrcweir         /** DocumentBuilder object */
308cdf0e10cSrcweir         DocumentBuilder builder = null;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir         try {
311cdf0e10cSrcweir             builder = factory.newDocumentBuilder();
312cdf0e10cSrcweir         } catch (ParserConfigurationException ex) {
313cdf0e10cSrcweir             ex.printStackTrace(writer_);
314cdf0e10cSrcweir             writer_.println(ex.getMessage());
315cdf0e10cSrcweir             return null;
316cdf0e10cSrcweir         }
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 
319cdf0e10cSrcweir         builder.setErrorHandler(
320cdf0e10cSrcweir             new org.xml.sax.ErrorHandler() {
321cdf0e10cSrcweir                 // ignore fatal errors (an exception is guaranteed)
322cdf0e10cSrcweir                 public void fatalError(SAXParseException e)
323cdf0e10cSrcweir                     throws SAXException {
324cdf0e10cSrcweir                     throw e;
325cdf0e10cSrcweir                 }
326cdf0e10cSrcweir 
327cdf0e10cSrcweir                 public void error(SAXParseException e)
328cdf0e10cSrcweir                     throws SAXParseException {
329cdf0e10cSrcweir                     // make sure validation error is thrown.
330cdf0e10cSrcweir                     throw e;
331cdf0e10cSrcweir                 }
332cdf0e10cSrcweir 
333cdf0e10cSrcweir                 public void warning(SAXParseException e)
334cdf0e10cSrcweir                     throws SAXParseException {
335cdf0e10cSrcweir                 }
336cdf0e10cSrcweir             }
337cdf0e10cSrcweir         );
338cdf0e10cSrcweir 
339cdf0e10cSrcweir         try {
340cdf0e10cSrcweir             w3cDocument = builder.parse(fis);
341cdf0e10cSrcweir             w3cDocument.getDocumentElement().normalize();
342cdf0e10cSrcweir         } catch (SAXException ex) {
343cdf0e10cSrcweir             ex.printStackTrace(writer_);
344cdf0e10cSrcweir             writer_.println(ex.getMessage());
345cdf0e10cSrcweir             return w3cDocument;
346cdf0e10cSrcweir         }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir         return w3cDocument;
349cdf0e10cSrcweir     }
350cdf0e10cSrcweir 
parseTags(String tagsString)351cdf0e10cSrcweir     private String [] parseTags(String tagsString) {
352cdf0e10cSrcweir         Vector tagsVector = new Vector();
353cdf0e10cSrcweir         if (tagsString.length() == 0)
354cdf0e10cSrcweir             return null;
355cdf0e10cSrcweir 
356cdf0e10cSrcweir         int start = 0;
357cdf0e10cSrcweir         int end = 0;
358cdf0e10cSrcweir         // break the tag string into a vector of strings by words
359cdf0e10cSrcweir         for (end = tagsString.indexOf(" ", start);
360cdf0e10cSrcweir              end != -1 ;
361cdf0e10cSrcweir              start = end + 1, end = tagsString.indexOf(" ", start)) {
362cdf0e10cSrcweir             tagsVector.add(tagsString.substring(start,end));
363cdf0e10cSrcweir         }
364cdf0e10cSrcweir 
365cdf0e10cSrcweir         tagsVector.add(tagsString.substring(start,tagsString.length()));
366cdf0e10cSrcweir 
367cdf0e10cSrcweir         // convert the vector to array
368cdf0e10cSrcweir         String[] tags= new String[tagsVector.size()];
369cdf0e10cSrcweir         tagsVector.copyInto(tags);
370cdf0e10cSrcweir 
371cdf0e10cSrcweir         return tags;
372cdf0e10cSrcweir     }
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 
375cdf0e10cSrcweir     /**
376cdf0e10cSrcweir      *  Set the output to the specified argument.
377cdf0e10cSrcweir      *  This method is only used internally to prevent
378cdf0e10cSrcweir      *  invalid string parameter.
379cdf0e10cSrcweir      *
380cdf0e10cSrcweir      *  @param   str   output specifier
381cdf0e10cSrcweir      */
setOutput(String str)382cdf0e10cSrcweir     private static void setOutput(String str) {
383cdf0e10cSrcweir 
384cdf0e10cSrcweir         if (writer_ == null) {
385cdf0e10cSrcweir 
386cdf0e10cSrcweir             if (str.equals("System.out")) {
387cdf0e10cSrcweir 
388cdf0e10cSrcweir                 setOutput(System.out);
389cdf0e10cSrcweir 
390cdf0e10cSrcweir             } else if (str.equals("System.err")) {
391cdf0e10cSrcweir 
392cdf0e10cSrcweir                 setOutput(System.err);
393cdf0e10cSrcweir 
394cdf0e10cSrcweir             } else {
395cdf0e10cSrcweir 
396cdf0e10cSrcweir                 try {
397cdf0e10cSrcweir 
398cdf0e10cSrcweir                     setOutput(new FileWriter(str));
399cdf0e10cSrcweir 
400cdf0e10cSrcweir                 } catch (IOException e) {
401cdf0e10cSrcweir 
402cdf0e10cSrcweir                     e.printStackTrace(System.err);
403cdf0e10cSrcweir                 }
404cdf0e10cSrcweir             }
405cdf0e10cSrcweir         }
406cdf0e10cSrcweir     }
407cdf0e10cSrcweir 
408cdf0e10cSrcweir     /**
409cdf0e10cSrcweir      *  Set the output to an OutputStream object.
410cdf0e10cSrcweir      *
411cdf0e10cSrcweir      *  @param   stream   OutputStream object
412cdf0e10cSrcweir      */
413cdf0e10cSrcweir 
setOutput(OutputStream stream)414cdf0e10cSrcweir     private static void setOutput(OutputStream stream) {
415cdf0e10cSrcweir 
416cdf0e10cSrcweir         setOutput(new OutputStreamWriter(stream));
417cdf0e10cSrcweir     }
418cdf0e10cSrcweir 
419cdf0e10cSrcweir     /**
420cdf0e10cSrcweir      *  Set the Writer object to manage the output.
421cdf0e10cSrcweir      *
422cdf0e10cSrcweir      *  @param   w   Writer object to write out
423cdf0e10cSrcweir      */
424cdf0e10cSrcweir 
setOutput(Writer w)425cdf0e10cSrcweir     private static void setOutput(Writer w) {
426cdf0e10cSrcweir 
427cdf0e10cSrcweir         if (writer_ != null) {
428cdf0e10cSrcweir 
429cdf0e10cSrcweir             writer_.close();
430cdf0e10cSrcweir         }
431cdf0e10cSrcweir 
432cdf0e10cSrcweir         writer_ = new PrintWriter(new BufferedWriter(w), true);
433cdf0e10cSrcweir     }
434cdf0e10cSrcweir 
main(String args[])435cdf0e10cSrcweir     public static void main(String args[]) throws IOException {
436cdf0e10cSrcweir 
437cdf0e10cSrcweir         if (args.length != 0 && args.length != 2) {
438cdf0e10cSrcweir             System.out.println("Usage: XmlDiff [<file1> <file2>].");
439cdf0e10cSrcweir             return;
440cdf0e10cSrcweir         }
441cdf0e10cSrcweir 
442cdf0e10cSrcweir         XmlDiff xmldiff = new XmlDiff();
443cdf0e10cSrcweir 
444cdf0e10cSrcweir         boolean same = false;
445cdf0e10cSrcweir         if (args.length == 2) {
446cdf0e10cSrcweir             same = xmldiff.diff(args[0], args[1]);
447cdf0e10cSrcweir         } else {
448cdf0e10cSrcweir             same = xmldiff.diff();
449cdf0e10cSrcweir         }
450cdf0e10cSrcweir 
451cdf0e10cSrcweir         System.out.println("Diff result: " + same);
452cdf0e10cSrcweir     }
453cdf0e10cSrcweir }
454cdf0e10cSrcweir 
455