blob: 29de48e1054bb886a0c01d297b5928cdfc6f29be [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
2 * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17package com.sun.ejte.ccl.reporter;
18
19import org.xml.sax.*;
20import org.xml.sax.ext.*;
21import org.xml.sax.helpers.*;
22import java.io.*;
23import java.util.*;
24
25public class ParseML{
David Matějčekf4dc06a2021-05-17 12:10:57 +020026 private String vendorParserClass =
Vinay Vishal57171472018-09-18 20:22:00 +053027 "org.apache.xerces.parsers.SAXParser";
28 public static boolean debug = false;
29 boolean inTestcase = false;
30 boolean inTestcaseID = false;
31 boolean gotStatus = false;
32 static Hashtable hash = null;
33 static Vector[] fileDiffs = null;
34 String key = "";
35 String value = "";
36
37 public static void main(String args[]){
38 ParseML pml = new ParseML();
39 if(args.length<2){
40 pml.usage();
41 }
42 hash = new Hashtable();
43 int no_of_files = args.length;
44 if(debug){
45 System.out.println("number of files:"+no_of_files);
46 }
47 Hashtable[] filehash = new Hashtable[no_of_files];
48 fileDiffs = new Vector[no_of_files];
David Matějčekf4dc06a2021-05-17 12:10:57 +020049 for(int i = 0; i<args.length; i++){
Vinay Vishal57171472018-09-18 20:22:00 +053050 filehash[i] = new Hashtable();
51 fileDiffs[i] = new Vector();
52 fileDiffs[i].add("</u><b>File:"+args[i]+"</b></u><br>\n"); // add the filename to the vector
53 System.out.println("parsing file #"+i);
54 filehash[i] = pml.buildTree(args[i]);
55 hash = null;
56 hash = new Hashtable();
57 if(debug){
58 System.out.print("========================");
59 System.out.println("Displaying hashtable # "+i);
60 pml.displayHash(filehash[i]);
61 }
David Matějčekf4dc06a2021-05-17 12:10:57 +020062 }
Vinay Vishal57171472018-09-18 20:22:00 +053063 pml.diffHash(filehash);
64 pml.displayVectorArr(fileDiffs);
65 System.out.print("writing diffs to file...");
66 pml.writeDiffs(fileDiffs);
67 System.out.println("done");
68 }
69 public Hashtable buildTree(String xmlURI){
70 //hash = null;
71 try{
72 //Initialize a reader
David Matějčekf4dc06a2021-05-17 12:10:57 +020073 XMLReader reader =
Vinay Vishal57171472018-09-18 20:22:00 +053074 XMLReaderFactory.createXMLReader(vendorParserClass);
75 //Register Content Handler
76 ContentHandler myContentHandler = new MyContentHandler();
77 reader.setContentHandler(myContentHandler);
78
79 //Parse
David Matějčekf4dc06a2021-05-17 12:10:57 +020080 InputSource inputSource =
Vinay Vishal57171472018-09-18 20:22:00 +053081 new InputSource(new java.io.FileInputStream(new java.io.File(xmlURI)));
82 reader.parse(inputSource);
83
84 } catch(Throwable th){
85 th.printStackTrace();
86 }
87 return hash;
88 }
89
90 class MyContentHandler implements ContentHandler{
91 private Locator locator;
92 public void setDocumentLocator(Locator locator){
93 this.locator = locator;
94 }
95 public void startDocument() throws SAXException{
96 //initialize a 2D vector here
97 }
98 public void endDocument() throws SAXException{
99 }
100 public void processingInstruction(String target, String data)
101 throws SAXException{
102 // add target and data to the 2D vector
103 }
104 public void startPrefixMapping(String prefix, String uri){
105 }
106 public void endPrefixMapping(String prefix){
107 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200108 public void startElement(String namespaceURI, String localName,
Vinay Vishal57171472018-09-18 20:22:00 +0530109 String qName, Attributes atts)
110 throws SAXException{
111 // add the local name into vector
112 if(localName.equals("testcase")){
113 inTestcase = true;
114 }
115 if(localName.equals("id") && (inTestcase)){
116 inTestcaseID = true;
117 if(debug){
118 System.out.println("inside testcase id.");
119 }
120 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200121 // assuming that when its time to get the status, the value for the keys would have been obtained.
Vinay Vishal57171472018-09-18 20:22:00 +0530122 if(localName.equals("status") && (inTestcase)){
123 //get attributes: pass/fail
124 if(debug){
David Matějčekf4dc06a2021-05-17 12:10:57 +0200125 System.out.println("getting testcase status...");
Vinay Vishal57171472018-09-18 20:22:00 +0530126 }
127 value = atts.getValue(0).trim();
128 if((key!=null) && (!(key.equals(""))) && (value!=null) && (!value.equals(""))){
129 if(debug){
David Matějčekf4dc06a2021-05-17 12:10:57 +0200130 System.out.println("Key["+key+"] has value["+value+"]");
Vinay Vishal57171472018-09-18 20:22:00 +0530131 }
132 hash.put(key,value);
133 gotStatus = true;
David Matějčekf4dc06a2021-05-17 12:10:57 +0200134 key = "";
Vinay Vishal57171472018-09-18 20:22:00 +0530135 value = "";
David Matějčekf4dc06a2021-05-17 12:10:57 +0200136 }
Vinay Vishal57171472018-09-18 20:22:00 +0530137 else{
138 if(key == null || key.equals("")){
139 System.out.println("invalid key!");
140 }
141 if(value== null || value.equals("")){
142 System.out.println("invalid value!");
David Matějčekf4dc06a2021-05-17 12:10:57 +0200143 }
Vinay Vishal57171472018-09-18 20:22:00 +0530144 }
145 }
146 }
147 public void endElement(String namespaceURI, String localName, String qName)
148 throws SAXException{
149 if(localName.equals("testcase")){
150 inTestcase = false;
151 if(debug){
David Matějčekf4dc06a2021-05-17 12:10:57 +0200152 System.out.println("Outside testcase tag");
Vinay Vishal57171472018-09-18 20:22:00 +0530153 }
154 }
155 if(localName.equals("id") && (inTestcase)){
156 inTestcaseID = false;
157 if(debug){
David Matějčekf4dc06a2021-05-17 12:10:57 +0200158 System.out.println("Outside testcase-ID tag");
Vinay Vishal57171472018-09-18 20:22:00 +0530159 } /*
160 key = key.trim();
161 value = value.trim();
David Matějčekf4dc06a2021-05-17 12:10:57 +0200162 if((key!=null) && (value!=null) &&
Vinay Vishal57171472018-09-18 20:22:00 +0530163 (!(value.equals(""))) && (!(key.equals("")))){
164
165 } */
166 }
167 }
168 public void characters(char[] ch, int start, int length)
169 throws SAXException{
170 String s = new String(ch, start, length);
171 if(debug){
172 System.out.print("\nvalue of start index= "+start+", ");
173 System.out.print("length of charectars = "+length+", ");
174 System.out.println("string = ["+s+"]");
175 }
176 if(inTestcaseID){
177 key += s.trim();
178 if((key!=null) && (!key.equals(""))){
179 if(debug){
180 System.out.println("TestCase ID:"+key);
181 }
182 }
183 }
184 }
185 public void ignorableWhitespace(char[] ch, int start, int length)
186 throws SAXException{
187 }
188 public void skippedEntity(String name) throws SAXException{
189 System.out.println("Skipped entity is:"+name);
David Matějčekf4dc06a2021-05-17 12:10:57 +0200190 }
Vinay Vishal57171472018-09-18 20:22:00 +0530191 }
192 /*======= End of Inner Class Definition ================*/
193
David Matějčekf4dc06a2021-05-17 12:10:57 +0200194 public void displayHash(Hashtable hashtable){
Vinay Vishal57171472018-09-18 20:22:00 +0530195 for (Enumeration e = hashtable.keys() ; e.hasMoreElements() ;) {
196 String keyval = (String)e.nextElement();
197 System.out.println(keyval +":"+hashtable.get(keyval));
198 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200199 }
Vinay Vishal57171472018-09-18 20:22:00 +0530200
201 public void displayVector(Vector v){
202 for(int i=0; i<v.size(); i++){
203 System.out.println((String)v.get(i));
204 }
205 }
206
207 public void displayVectorArr(Vector[] v){
208 for (int i = 0; i<v.length; i++){
209 displayVector(v[i]);
210 }
211 }
212
213 public void writeDiffs(Vector[] v){
214 try{
215 File inputfile = new File("fileDiffs.html");
216 FileWriter fw = new FileWriter(inputfile);
David Matějčekf4dc06a2021-05-17 12:10:57 +0200217 String filecontent = "<h3>File Diff Output</h3><hr>\n";
218 for(int i = 0; i<v.length ; i++){
Vinay Vishal57171472018-09-18 20:22:00 +0530219 for(int j = 0; j<v[i].size(); j++){
220 filecontent += v[i].get(j)+"<br>\n";
221 }
222 filecontent += "<hr>\n";
223 }
224 fw.write(filecontent);
225 fw.close();
226 } catch(FileNotFoundException fnfe){
227 System.out.println("File is not present. \n"+
228 "Please check the file name and try again");
229 } catch(Exception ex){
230 ex.printStackTrace();
David Matějčekf4dc06a2021-05-17 12:10:57 +0200231 }
Vinay Vishal57171472018-09-18 20:22:00 +0530232 }
233
234 public Vector getKeyList(Hashtable[] hashes){
235 int hashtables = hashes.length;
236 Vector allkeys= new Vector();
237 Vector[] hashVecs = new Vector[hashtables];
David Matějčekf4dc06a2021-05-17 12:10:57 +0200238 for(int i=0; i<hashtables; i++){
Vinay Vishal57171472018-09-18 20:22:00 +0530239 hashVecs[i] = new Vector((Collection)hashes[i].keySet());
240 }
241 allkeys = getUnion(hashVecs);
242 return allkeys;
243 }
244
245 public Vector getUnion(Vector[] v){
246 Vector union = new Vector();
247 for(int i=0; i<v.length; i++){
248 for(int j=0; j<v[i].size(); j++){
249 if(!union.contains(v[i].get(j))){
250 union.add(v[i].get(j));
251 }//end if
252 }//end inner-for
253 }// end outer-for
254 return union;
255 }// end method getUnion
256
257 public void diffHash(Hashtable[] hashes){
258 int hashtables = hashes.length;
259 if(debug){
260 System.out.println("total hashtables to diff:"+hashtables);
261 }
262 int bigHash = 0; // take the first hashtable as the golden file.
David Matějčekf4dc06a2021-05-17 12:10:57 +0200263 /*Get a list of most keys*/
Vinay Vishal57171472018-09-18 20:22:00 +0530264 Vector keylist = new Vector();
265 keylist = getKeyList(hashes);
266 /*Start comparing all other hashtable elements to this golden list of testcases*/
267
268 int totalkeys = keylist.size();
269 if(debug){
270 System.out.println("Total number of testcases gathered: "+totalkeys);
271 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200272 for(int keyno=0; keyno<totalkeys; keyno++){
Vinay Vishal57171472018-09-18 20:22:00 +0530273 String keyObj = (String)keylist.get(keyno);
274 Object val = null;
275 Object bigHashVal = null;
276 // for all hashtables in the array...
277 if(debug){
278 System.out.println("checking out key:"+keyObj);
279 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200280 for(int i = 0; i<hashtables ; i++){
281 if(i==bigHash){
Vinay Vishal57171472018-09-18 20:22:00 +0530282 continue;
283 }
284 // key exists
285 if((bigHashVal=hashes[bigHash].get(keyObj))!=null){ // key exists in bigHash
286 if((val = hashes[i].get(keyObj))!=null){
287 if (val.equals(bigHashVal)){
288 // key and value pair match the golden file
289 continue;
290 } else {
291 // key exists but the values are different
David Matějčekf4dc06a2021-05-17 12:10:57 +0200292 // add it into both the vectors, if not already there.
Vinay Vishal57171472018-09-18 20:22:00 +0530293 String diffObj = "<font color=blue>"+keyObj+
294 " has an inconsistent status:"+(String)val+
David Matějčekf4dc06a2021-05-17 12:10:57 +0200295 "</font>";
Vinay Vishal57171472018-09-18 20:22:00 +0530296 String bigdiffObj = "<font color=blue>"+keyObj+
297 " has an inconsistent status:"+(String)bigHashVal+
298 "</font>";
299 if(!fileDiffs[i].contains(diffObj)){
300 fileDiffs[i].add(diffObj);
301 }
302 if(!fileDiffs[bigHash].contains(bigdiffObj)){
303 fileDiffs[bigHash].add(bigdiffObj);
304 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200305 }
Vinay Vishal57171472018-09-18 20:22:00 +0530306 } else {
307 // key doesn't exist
David Matějčekf4dc06a2021-05-17 12:10:57 +0200308 /*add it into the vector for smaller tables, if not already there.*/
Vinay Vishal57171472018-09-18 20:22:00 +0530309 String diffObj = "<font color=red>"+keyObj+
310 " testcase is missing </font>";
311 if(!fileDiffs[i].contains(diffObj)){
312 fileDiffs[i].add(diffObj);
313 }// end missing-key if
314 } // end missing-key else
315 // end key-exists if in bighash
316 } else {
317 // add the missing key into the vector for bighash
318 String bigdiffObj = "<font color=red>"+keyObj+
319 " testcase is missing </font>";
320 if(!fileDiffs[bigHash].contains(bigdiffObj)){
321 fileDiffs[bigHash].add(bigdiffObj);
David Matějčekf4dc06a2021-05-17 12:10:57 +0200322 }// end missing-key if
Vinay Vishal57171472018-09-18 20:22:00 +0530323 }
324 } // end for-loop for going through the hashtable array
325 } // end for-loop for keys in the bigHash hashtable
David Matějčekf4dc06a2021-05-17 12:10:57 +0200326 } // end diff-hash method
Vinay Vishal57171472018-09-18 20:22:00 +0530327
David Matějčekf4dc06a2021-05-17 12:10:57 +0200328 public void usage(){
Vinay Vishal57171472018-09-18 20:22:00 +0530329 String usageStr = "Usage:\n java ParseML <xml_file_1> <xml_file_2> <xml_file_3>..."+
330 "\n\tThis Utility will let you 'diff' multiple xml"+
331 "\n\t files and produce a fileDiff.html file in the "+
332 "\n\t directory where this file is run from."+
333 "\n\tDiff results are produced only by matching the contents "+
334 "\n\tof the xml trees and not by comparing text characters."+
335 "\n\n\tThis program takes arguments of two or more 'well_formed_xml' files.";
336 System.out.println(usageStr);
337 System.exit(0);
338 }
339}