1 19 20 package soot.toolkits.astmetrics.DataHandlingApplication; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.FileReader ; 26 import java.io.FilenameFilter ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.io.OutputStreamWriter ; 30 import java.io.PrintWriter ; 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.Collections ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Vector ; 37 38 import soot.CompilationDeathException; 39 40 import org.w3c.dom.*; 41 42 import javax.xml.parsers.DocumentBuilderFactory ; 43 import javax.xml.parsers.DocumentBuilder ; 44 import org.xml.sax.SAXException ; 45 import org.xml.sax.SAXParseException ; 46 47 public class ProcessData { 48 49 private static final int CLASSNAMESIZE=15; 51 52 private static final int CLASS =0; 53 private static final int BENCHMARK =1; 54 55 private static String metricListFileName=null; 56 57 58 private static ArrayList xmlFileList = new ArrayList (); 59 60 61 private static int aggregationMechanism =-1; 62 63 64 private static OutputStream streamOut; 65 private static PrintWriter bench; 66 67 70 public static void main(String [] args) { 71 int argLength =args.length; 72 if(argLength ==0){ 73 printIntro(); 74 useHelp(); 75 System.exit(1); 76 } 77 78 if(args[0].equals("--help")){ 79 printHelp(); 80 System.exit(1); 81 } 82 else if(args[0].equals("-metricList")){ 83 metricListFileName(args); 84 System.out.println("A list of metrics will be stored in: "+metricListFileName); 85 86 readXMLFileNames(2,args); 87 88 try{ 89 OutputStream streamOut = new FileOutputStream (metricListFileName); 90 PrintWriter writerOut = new PrintWriter (new OutputStreamWriter (streamOut)); 91 writeMetricLists(writerOut); 92 93 writerOut.flush(); 94 streamOut.close(); 95 } catch (IOException e) { 96 throw new CompilationDeathException("Cannot output file " + metricListFileName); 97 } 98 99 } 100 else if (args[0].equals("-tables")){ 101 metricListFileName(args); 102 System.out.println("Will read column table headings from: "+metricListFileName); 103 104 aggregationOption(args); 106 if(aggregationMechanism == ProcessData.BENCHMARK){ 107 System.out.println("Aggregating over benchmarks...each row is one of the xml files"); 108 System.out.println("Only one tex file with the name"+metricListFileName+".tex will be created"); 109 } 110 else if (aggregationMechanism == ProcessData.CLASS){ 111 System.out.println("Aggregating over class...each row is one class..."); 112 System.out.println("Each benchmark (xml file) will have its own tex file"); 113 } 114 115 readXMLFileNames(3,args); 116 117 generateMetricsTables(); 119 } 120 else{ 121 System.out.println("Incorrect argument number 1: expecting -metricList or -tables"); 122 System.exit(1); 123 } 124 125 } 126 127 128 129 132 private static void aggregationOption(String [] args){ 133 if(args.length < 3){ 134 System.out.println("Expecting -class or -benchmark at argument number 3"); 135 System.exit(1); 136 } 137 if(args[2].equals("-class")){ 138 aggregationMechanism = ProcessData.CLASS; 139 } 140 else if(args[2].equals("-benchmark")){ 141 aggregationMechanism = ProcessData.BENCHMARK; 142 } 143 else{ 144 System.out.println("Expecting -class or -benchmark at argument number 3"); 145 System.exit(1); 146 } 147 } 148 149 153 private static void readXMLFileNames(int startIndex, String args[]){ 154 if(args.length<startIndex+1){ 155 System.out.println("Expecting an xml file OR * symbol as argument number"+(startIndex+1)); 156 System.exit(1); 157 } 158 159 if(args[startIndex].equals("*")){ 161 System.out.println("Will read all xml files from directory"); 162 163 readStar(); 165 } 166 else{ 167 for(int i=startIndex;i<args.length;i++){ 168 String temp = args[i]; 169 if(!temp.endsWith(".xml")){ 170 } 173 else{ 174 xmlFileList.add(temp); 175 } 176 } 177 } 178 179 Iterator it = xmlFileList.iterator(); 180 while(it.hasNext()){ 181 System.out.println("Will be reading: "+(String )it.next()); 182 } 183 } 184 185 186 187 190 private static void metricListFileName(String [] args){ 191 if(args.length < 2 ){ 192 System.out.println("Expecting name of metricList as argumnet number 2"); 193 System.exit(1); 194 } 195 metricListFileName = args[1]; 196 197 } 198 199 public static void printHelp(){ 200 printIntro(); 201 System.out.println("There are two main modes of execution"); 202 System.out.println("To execute the program the first argument should be one of these modes"); 203 System.out.println("-metricList and -tables"); 204 System.out.println("\n\n The -metricList mode"); 205 System.out.println("The argument at location 1 should be name of a file where the list of metrics will be stored"); 206 System.out.println("All arguments following argument 1 have to be xml files to be processed"); 207 System.out.println("If argument at location 2 is * then the current directory is searched and all xml files will be processed"); 208 209 System.out.println("\n\n The -tables mode"); 210 System.out.println("The argument at location 1 should be name of a file where the list of metrics are stored"); 211 System.out.println("These metrics will become the COLUMNS in the tables created"); 212 System.out.println("Argument at location 2 is the choice of aggregation"); 213 System.out.println("\t -class for class level metrics"); 214 System.out.println("\t -benchmark for benchmark level metrics"); 215 System.out.println("Each xml file is considered to be a benchmark with a bunch of classes in it"); 216 217 System.out.println("All arguments following argument 2 have to be xml files to be processed"); 218 System.out.println("If argument at location 3 is * then the current directory is searched and all xml files will be processed"); 219 } 220 221 222 public static void printIntro(){ 223 System.out.println("Welcome to the processData application"); 224 System.out.println("The application is an xml document parser."); 225 System.out.println("Its primary aim is to create pretty tex tables"); 226 } 227 228 public static void useHelp(){ 229 System.out.println("Use the --help flag for more details"); 230 } 231 232 233 236 private static void readStar(){ 237 String curDir = System.getProperty("user.dir"); 238 System.out.println("Current system directory is"+curDir); 239 File dir = new File (curDir); 240 241 String [] children = dir.list(); 242 if (children == null) { 243 } else { 245 246 FilenameFilter filter = new FilenameFilter () { 247 public boolean accept(File dir, String name) { 248 return name.endsWith(".xml"); 249 } 250 }; 251 children = dir.list(filter); 252 253 for(int i=0;i<children.length;i++) 254 xmlFileList.add(children[i]); 255 } 256 } 257 258 259 260 private static void writeMetricLists(PrintWriter out){ 261 ArrayList metricList = new ArrayList (); 262 263 Iterator it = xmlFileList.iterator(); 264 while(it.hasNext()){ 265 String fileName = (String )it.next(); 266 try { 267 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 268 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 269 Document doc = docBuilder.parse (new File (fileName)); 270 System.out.println("Retrieving Metric List from xml file: "+fileName); 271 doc.getDocumentElement ().normalize (); 273 274 NodeList metrics = doc.getElementsByTagName("Metric"); 275 276 for(int s=0; s<metrics.getLength() ; s++){ 277 Node metricNode = metrics.item(s); 278 if(metricNode.getNodeType() == Node.ELEMENT_NODE){ 279 280 Element metricElement = (Element)metricNode; 281 NodeList metricName = metricElement.getElementsByTagName("MetricName"); 282 Element name = (Element)metricName.item(0); 283 284 NodeList textFNList = name.getChildNodes(); 285 if(!metricList.contains(((Node)textFNList.item(0)).getNodeValue().trim())) 287 metricList.add(((Node)textFNList.item(0)).getNodeValue().trim()); 288 289 } } }catch (SAXParseException err) { 292 System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); 293 System.out.println(" " + err.getMessage ()); 294 } 295 catch (SAXException e) { 296 Exception x = e.getException (); 297 ((x == null) ? e : x).printStackTrace (); 298 } 299 catch (Throwable t) { 300 t.printStackTrace (); 301 } 302 } 303 304 it = metricList.iterator(); 305 while(it.hasNext()){ 306 out.println(it.next()); 307 } 308 System.out.println(metricListFileName+ " created."); 309 } 310 311 312 313 private static void generateMetricsTables(){ 314 315 Vector columns = new Vector (); 316 317 320 try{ 321 FileReader file = new FileReader (metricListFileName); 322 BufferedReader fileInput = new BufferedReader (file); 323 String text; 324 325 while( (text = fileInput.readLine()) != null){ 327 columns.add(text); 329 } 330 fileInput.close(); 331 } 332 catch(Exception e){ 333 System.out.println("Exception while reading from metricList"+metricListFileName); 334 System.exit(1); 335 } 336 337 338 339 340 341 342 343 String newClassName=""; 344 if(aggregationMechanism == ProcessData.BENCHMARK){ 345 347 newClassName = metricListFileName+".tex"; 348 349 System.out.println("Creating tex file"+newClassName+" from metrics info"); 350 351 bench = openWriteFile(newClassName); 352 353 printTexTableHeader(bench,"Benchmarks",columns); 354 } 355 356 357 358 359 Iterator it = xmlFileList.iterator(); 360 while(it.hasNext()){ 361 String fileName = (String )it.next(); 362 363 try{ 364 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 365 366 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 367 Document doc = docBuilder.parse (new File (fileName)); 368 System.out.println("Gethering metric info from from xml file: "+fileName); 369 doc.getDocumentElement ().normalize (); 371 372 373 if(aggregationMechanism == ProcessData.BENCHMARK){ 374 375 380 381 if(fileName.endsWith(".xml")) 383 bench.print(fileName.substring(0,fileName.length()-4)); 384 else 385 bench.print(fileName); 386 387 388 HashMap aggregatedValues = new HashMap (); 389 390 Iterator tempIt = columns.iterator(); 391 while(tempIt.hasNext()){ 392 aggregatedValues.put(tempIt.next(),new Integer (0)); 393 } 394 395 aggregateXMLFileMetrics(doc,aggregatedValues); 396 397 tempIt = columns.iterator(); 399 while(tempIt.hasNext()){ 400 int val = ((Integer )aggregatedValues.get(tempIt.next())).intValue(); 401 bench.print("&"+val); 402 if(tempIt.hasNext()) 403 bench.print(" "); 404 else 405 bench.println("\\\\"); 406 } 407 408 409 410 411 412 413 414 415 } 416 else if (aggregationMechanism == ProcessData.CLASS){ 417 getClassMetrics(fileName,doc,columns); 418 } 419 else{ 420 System.out.println("Unknown aggregation Mechanism"); 421 System.exit(1); 422 } 423 }catch (SAXParseException err) { 424 System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); 425 System.out.println(" " + err.getMessage ()); 426 } 427 catch (SAXException e) { 428 Exception x = e.getException (); 429 ((x == null) ? e : x).printStackTrace (); 430 } 431 catch (Throwable t) { 432 t.printStackTrace (); 433 } 434 435 } 436 437 if(aggregationMechanism == ProcessData.BENCHMARK){ 438 printTexTableFooter(bench,""); 439 closeWriteFile(bench,newClassName); 440 } 441 442 443 444 445 446 447 448 449 } 450 451 452 private static PrintWriter openWriteFile(String fileName){ 453 PrintWriter writerOut; 454 try{ 455 streamOut = new FileOutputStream (fileName); 456 writerOut = new PrintWriter (new OutputStreamWriter (streamOut)); 457 } catch (IOException e) { 458 throw new CompilationDeathException("Cannot output file " + fileName); 459 } 460 return writerOut; 461 } 462 463 464 private static void closeWriteFile(PrintWriter writerOut,String fileName){ 465 try{ 466 writerOut.flush(); 467 streamOut.close(); 468 } catch (IOException e) { 469 throw new CompilationDeathException("Cannot output file " + fileName); 470 } 471 } 472 473 474 475 476 481 private static void aggregateXMLFileMetrics(Document doc, HashMap aggregated){ 482 483 NodeList metrics = doc.getElementsByTagName("Metric"); 484 485 for(int s=0; s<metrics.getLength() ; s++){ 486 Node metricNode = metrics.item(s); 487 if(metricNode.getNodeType() == Node.ELEMENT_NODE){ 488 489 Element metricElement = (Element)metricNode; 490 NodeList metricName = metricElement.getElementsByTagName("MetricName"); 491 Element name = (Element)metricName.item(0); 492 493 NodeList textFNList = name.getChildNodes(); 494 String tempName = ((Node)textFNList.item(0)).getNodeValue().trim(); 495 496 Object tempObj = aggregated.get(tempName); 497 if( tempObj == null){ 498 continue; 500 } 501 502 Integer valSoFar = (Integer )tempObj; 503 504 506 NodeList value = metricElement.getElementsByTagName("Value"); 507 Element name1 = (Element)value.item(0); 508 509 NodeList textFNList1 = name1.getChildNodes(); 510 String valToPrint = ((Node)textFNList1.item(0)).getNodeValue().trim(); 511 int temp = Integer.parseInt(valToPrint); 512 513 aggregated.put(tempName, new Integer (valSoFar.intValue()+temp) ); 514 515 } } } 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 private static void getClassMetrics(String fileName, Document doc,Vector columns){ 541 String newClassName = fileName; 543 if(newClassName.endsWith(".xml")) 544 newClassName = newClassName.substring(0,newClassName.length()-4); 545 546 newClassName += ".tex"; 547 548 System.out.println("Creating tex file"+newClassName+" from metrics info in file"+fileName); 549 550 PrintWriter writerOut = openWriteFile(newClassName); 551 printTexTableHeader(writerOut,"Classes",columns); 552 553 554 555 556 557 561 ArrayList classNames = new ArrayList (); 562 HashMap classData = new HashMap (); 563 564 NodeList classes = doc.getElementsByTagName("Class"); 566 567 for(int cl=0;cl<classes.getLength();cl++){ 568 Node classNode = classes.item(cl); 570 if(classNode.getNodeType() == Node.ELEMENT_NODE){ 571 572 Element classElement = (Element)classNode; 573 574 577 NodeList classNameNodeList = classElement.getElementsByTagName("ClassName"); 578 Element classNameElement = (Element)classNameNodeList.item(0); 579 580 NodeList classNameTextFNList = classNameElement.getChildNodes(); 581 String className = ((Node)classNameTextFNList.item(0)).getNodeValue().trim(); 582 583 585 className = className.replace('_','-'); 586 if(className.length()>CLASSNAMESIZE){ 587 className = className.substring(0,CLASSNAMESIZE); 589 classNames.add(className); 590 } 591 else{ 592 classNames.add(className); 594 } 595 596 System.out.print("\nclassName "+className); 597 598 599 600 String data = " "; 601 602 603 606 NodeList metrics = classElement.getElementsByTagName("Metric"); 607 608 int columnIndex=0; 610 for(int s=0; s<metrics.getLength() && columnIndex < columns.size(); s++){ 611 Node metricNode = metrics.item(s); 612 if(metricNode.getNodeType() == Node.ELEMENT_NODE){ 613 614 Element metricElement = (Element)metricNode; 615 NodeList metricName = metricElement.getElementsByTagName("MetricName"); 616 Element name = (Element)metricName.item(0); 617 618 NodeList textFNList = name.getChildNodes(); 619 String tempName = ((Node)textFNList.item(0)).getNodeValue().trim(); 620 621 625 if(! tempName.equals(columns.elementAt(columnIndex))){ 626 continue; 628 } 629 630 631 632 634 NodeList value = metricElement.getElementsByTagName("Value"); 635 Element name1 = (Element)value.item(0); 636 637 NodeList textFNList1 = name1.getChildNodes(); 638 String valToPrint = ((Node)textFNList1.item(0)).getNodeValue().trim(); 639 System.out.print(" " + valToPrint); 640 data += "&"+valToPrint; 642 643 644 645 columnIndex++; 646 if(columns.size()>columnIndex){ 647 data += " "; 649 } 650 else{ 651 data += "\\\\"; 653 } 654 655 } } 658 659 660 661 662 663 classData.put(className,data); 664 665 666 667 } } 670 674 675 Collections.sort(classNames); 676 677 Iterator tempIt = classNames.iterator(); 678 while(tempIt.hasNext()){ 679 String className = (String )tempIt.next(); 680 String data = (String )classData.get(className); 681 writerOut.print(className); 682 writerOut.println(data); 683 } 684 printTexTableFooter(writerOut,fileName); 685 686 closeWriteFile(writerOut,metricListFileName); 687 } 688 689 690 private static void printTexTableFooter(PrintWriter out, String tableCaption){ 691 out.println(""); 692 out.println("\\hline"); 693 out.println("\\end{tabular}"); 694 out.println("\\caption{ ..."+tableCaption+"..... }"); 695 out.println("\\end{table}"); 696 } 697 698 699 700 private static void printTexTableHeader(PrintWriter out, String rowHeading, Vector columns){ 701 out.println("\\begin{table}"); 702 out.print("\\begin{tabular}{"); 703 704 for(int i =0;i<=columns.size();i++) 705 out.print("|l"); 706 707 out.println("|}"); 708 out.println("\\hline"); 709 710 out.print(rowHeading+" "); 711 712 Iterator it = columns.iterator(); 713 while(it.hasNext()){ 714 out.print("&"+(String )it.next()); 715 if(it.hasNext()) 716 out.print(" "); 717 } 718 out.println("\\\\"); 719 720 out.println("\\hline"); 721 } 722 } 723 | Popular Tags |