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