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.IOException;
25cdf0e10cSrcweir import java.io.File;
26cdf0e10cSrcweir import java.util.zip.*;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir public class XmlWrapper
29cdf0e10cSrcweir {
main(String args[])30cdf0e10cSrcweir     public static void main(String args[]) throws IOException
31cdf0e10cSrcweir     {
32cdf0e10cSrcweir         System.out.println("args.length is " + args.length);
33cdf0e10cSrcweir         if (args.length < 2) {
34cdf0e10cSrcweir             System.out.println("Usage: java XmlWrapper [<zipfile1> <zipfile2>].");
35cdf0e10cSrcweir             //return;
36cdf0e10cSrcweir 		 	System.exit(-1);
37cdf0e10cSrcweir 
38cdf0e10cSrcweir         }
39cdf0e10cSrcweir 
40cdf0e10cSrcweir         XmlWrapper w = new XmlWrapper();
41cdf0e10cSrcweir         File currdirfp = null;
42cdf0e10cSrcweir         try {
43cdf0e10cSrcweir          currdirfp = new File(".");
44cdf0e10cSrcweir         } catch (Exception fx) {
45cdf0e10cSrcweir          System.out.println("Could not get File instance for current directory \n");
46cdf0e10cSrcweir          //return;
47cdf0e10cSrcweir 		 System.exit(-1);
48cdf0e10cSrcweir         }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir         File f1 = null;
51cdf0e10cSrcweir         File f2 = null;
52cdf0e10cSrcweir         String fname1,fname2;
53cdf0e10cSrcweir         try {
54cdf0e10cSrcweir          f1 = File.createTempFile("xmlcomp", ".tmp", currdirfp);
55cdf0e10cSrcweir          f2 = File.createTempFile("xmlcomp", ".tmp", currdirfp);
56cdf0e10cSrcweir         } catch (Exception tx) {
57cdf0e10cSrcweir          System.out.println("Could not create TempFile ");
58cdf0e10cSrcweir          System.out.println("Exception: " + tx.toString());
59cdf0e10cSrcweir          //return;
60cdf0e10cSrcweir 		 System.exit(-1);
61cdf0e10cSrcweir         }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir         fname1 = f1.getAbsolutePath();
64cdf0e10cSrcweir         fname2 = f2.getAbsolutePath();
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         // get content.xml file from zip file and copy it to temporary
67cdf0e10cSrcweir         // filename
68cdf0e10cSrcweir         XmlZipExtract xw1 = new XmlZipExtract(args[0]);
69cdf0e10cSrcweir         try {
70cdf0e10cSrcweir           xw1.getContentXml(fname1);
71cdf0e10cSrcweir         } catch (ZipException e) {
72cdf0e10cSrcweir          System.out.println("Exception: file is not a ZIP file: " + args[0]);
73cdf0e10cSrcweir          f1.delete();
74cdf0e10cSrcweir          f2.delete();
75cdf0e10cSrcweir          //return;
76cdf0e10cSrcweir 		 System.exit(-1);
77cdf0e10cSrcweir         } catch (Exception e) {
78cdf0e10cSrcweir          System.out.println("Exception: Could not extract XML from " + args[0]);
79cdf0e10cSrcweir          System.out.println("Exception: " + e.toString());
80cdf0e10cSrcweir          f1.delete();
81cdf0e10cSrcweir          f2.delete();
82cdf0e10cSrcweir          //return;
83cdf0e10cSrcweir 		 System.exit(-1);
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         // get content.xml file from zip file and copy it to temporary
87cdf0e10cSrcweir         // filename
88cdf0e10cSrcweir         XmlZipExtract xw2 = new XmlZipExtract(args[1]);
89cdf0e10cSrcweir         try {
90cdf0e10cSrcweir          xw2.getContentXml(fname2);
91cdf0e10cSrcweir         } catch (ZipException e) {
92cdf0e10cSrcweir          System.out.println("Exception: file is not a ZIP file: " + args[0]);
93cdf0e10cSrcweir          f1.delete();
94cdf0e10cSrcweir          f2.delete();
95cdf0e10cSrcweir          //return;
96cdf0e10cSrcweir 		 System.exit(-1);
97cdf0e10cSrcweir         } catch (Exception ex) {
98cdf0e10cSrcweir          System.out.println(ex.getMessage());
99cdf0e10cSrcweir          System.out.println("Exception: Could not extract XML from " + args[1]);
100cdf0e10cSrcweir          System.out.println("Exception: " + ex.toString());
101cdf0e10cSrcweir          f1.delete();
102cdf0e10cSrcweir          f2.delete();
103cdf0e10cSrcweir          //return;
104cdf0e10cSrcweir 		 System.exit(-1);
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         boolean same = false;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         try
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir            XmlDiff xmldiff = new XmlDiff();
112cdf0e10cSrcweir 
113cdf0e10cSrcweir            if (args.length == 2) {
114cdf0e10cSrcweir                same = xmldiff.diff(fname1, fname2);
115cdf0e10cSrcweir            } else {
116cdf0e10cSrcweir                same = xmldiff.diff();
117cdf0e10cSrcweir            }
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir         catch (Exception ex)
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir          System.out.println("XmlDiff failed");
122cdf0e10cSrcweir          System.out.println("Exception: " + ex.toString());
123cdf0e10cSrcweir          f1.delete();
124cdf0e10cSrcweir          f2.delete();
125cdf0e10cSrcweir          //return;
126cdf0e10cSrcweir 		 System.exit(-1);
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         System.out.println("Diff result: " + same);
130cdf0e10cSrcweir         if (same)
131cdf0e10cSrcweir         {
132cdf0e10cSrcweir           System.out.println("XMLDIFFRESULT:PASSED");
133cdf0e10cSrcweir         } else {
134cdf0e10cSrcweir           System.out.println("XMLDIFFRESULT:FAILED");
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         f1.delete();
138cdf0e10cSrcweir         f2.delete();
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 		if (same)
141cdf0e10cSrcweir 		{
142cdf0e10cSrcweir 			System.exit(2);
143cdf0e10cSrcweir 		}
144cdf0e10cSrcweir 		else
145cdf0e10cSrcweir 		{
146cdf0e10cSrcweir 			System.exit(3);
147cdf0e10cSrcweir 		}
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir }
150