Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.sun.ejte.ccl.reporter; |
| 18 | |
| 19 | import org.xml.sax.*; |
| 20 | import org.xml.sax.ext.*; |
| 21 | import org.xml.sax.helpers.*; |
| 22 | import java.io.*; |
| 23 | import java.util.*; |
| 24 | |
| 25 | public class ParseML{ |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 26 | private String vendorParserClass = |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 27 | "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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 49 | for(int i = 0; i<args.length; i++){ |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 50 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 62 | } |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 63 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 73 | XMLReader reader = |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 74 | XMLReaderFactory.createXMLReader(vendorParserClass); |
| 75 | //Register Content Handler |
| 76 | ContentHandler myContentHandler = new MyContentHandler(); |
| 77 | reader.setContentHandler(myContentHandler); |
| 78 | |
| 79 | //Parse |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 80 | InputSource inputSource = |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 81 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 108 | public void startElement(String namespaceURI, String localName, |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 109 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 121 | // assuming that when its time to get the status, the value for the keys would have been obtained. |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 122 | if(localName.equals("status") && (inTestcase)){ |
| 123 | //get attributes: pass/fail |
| 124 | if(debug){ |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 125 | System.out.println("getting testcase status..."); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 126 | } |
| 127 | value = atts.getValue(0).trim(); |
| 128 | if((key!=null) && (!(key.equals(""))) && (value!=null) && (!value.equals(""))){ |
| 129 | if(debug){ |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 130 | System.out.println("Key["+key+"] has value["+value+"]"); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 131 | } |
| 132 | hash.put(key,value); |
| 133 | gotStatus = true; |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 134 | key = ""; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 135 | value = ""; |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 136 | } |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 137 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 143 | } |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 144 | } |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 152 | System.out.println("Outside testcase tag"); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | if(localName.equals("id") && (inTestcase)){ |
| 156 | inTestcaseID = false; |
| 157 | if(debug){ |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 158 | System.out.println("Outside testcase-ID tag"); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 159 | } /* |
| 160 | key = key.trim(); |
| 161 | value = value.trim(); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 162 | if((key!=null) && (value!=null) && |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 163 | (!(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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 190 | } |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 191 | } |
| 192 | /*======= End of Inner Class Definition ================*/ |
| 193 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 194 | public void displayHash(Hashtable hashtable){ |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 195 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 199 | } |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 200 | |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 217 | String filecontent = "<h3>File Diff Output</h3><hr>\n"; |
| 218 | for(int i = 0; i<v.length ; i++){ |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 219 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 231 | } |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 232 | } |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 238 | for(int i=0; i<hashtables; i++){ |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 239 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 263 | /*Get a list of most keys*/ |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 264 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 272 | for(int keyno=0; keyno<totalkeys; keyno++){ |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 273 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 280 | for(int i = 0; i<hashtables ; i++){ |
| 281 | if(i==bigHash){ |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 282 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 292 | // add it into both the vectors, if not already there. |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 293 | String diffObj = "<font color=blue>"+keyObj+ |
| 294 | " has an inconsistent status:"+(String)val+ |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 295 | "</font>"; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 296 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 305 | } |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 306 | } else { |
| 307 | // key doesn't exist |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 308 | /*add it into the vector for smaller tables, if not already there.*/ |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 309 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 322 | }// end missing-key if |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 323 | } |
| 324 | } // end for-loop for going through the hashtable array |
| 325 | } // end for-loop for keys in the bigHash hashtable |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 326 | } // end diff-hash method |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 327 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 328 | public void usage(){ |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 329 | 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 | } |