001 /*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005
006 package org.util.xml.tool;
007
008 import java.io.File;
009 import java.io.FileInputStream;
010 import java.io.StringReader;
011 import javax.swing.JFileChooser;
012 import org.util.xml.element.Element;
013 import org.util.xml.parse.ElementParser;
014
015 /**
016 *
017 * @author masaru
018 */
019 public class XMLComparator {
020
021 public static boolean isSameAsXML(File a, File b) throws Exception {
022 ElementParser ap = new ElementParser(new FileInputStream(a));
023 ap.parse();
024 ElementParser bp = new ElementParser(new FileInputStream(b));
025 bp.parse();
026 return isSameAsXML(ap.getResult(), bp.getResult());
027 }
028
029 public static boolean isSameAsXML(String a, String b) throws Exception {
030 ElementParser ap = new ElementParser(new StringReader(a));
031 ap.parse();
032 ElementParser bp = new ElementParser(new StringReader(b));
033 bp.parse();
034 return isSameAsXML(ap.getResult(), bp.getResult());
035 }
036
037 public static boolean isSameAsXML(Element[] a, Element[] b) {
038 if(a.length!=b.length) return false;
039 for(int i=0;i<a.length;i++)
040 if(!isSameAsXML(a[i], b[i])) return false;
041 return true;
042 }
043
044 public static boolean isSameAsXML(Element a, Element b) {
045 String as = a.toString();
046 String bs = b.toString();
047 return as.equals(bs);
048 }
049
050 public static void main(String[] args) throws Exception {
051 JFileChooser fc = new JFileChooser();
052 if(fc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
053 throw new Exception("canceled to select file!");
054 File file1 = fc.getSelectedFile();
055
056 if(fc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
057 throw new Exception("canceled to select file!");
058 File file2 = fc.getSelectedFile();
059 System.out.println("file1: "+file1.getAbsolutePath());
060 System.out.println("file2: "+file2.getAbsolutePath());
061 System.out.println("file1 == file2: "+isSameAsXML(file1, file2));
062 }
063 }