KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > astmetrics > DataHandlingApplication > ProcessData


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2006 Nomair A. Naeem
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.toolkits.astmetrics.DataHandlingApplication;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.FilenameFilter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 import soot.CompilationDeathException;
39
40 import org.w3c.dom.*;
41
42 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
43 import javax.xml.parsers.DocumentBuilder JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45 import org.xml.sax.SAXParseException JavaDoc;
46
47 public class ProcessData {
48     
49     //when printing class names what is the max size of each class name
50
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 JavaDoc metricListFileName=null;
56     
57     
58     private static ArrayList JavaDoc xmlFileList = new ArrayList JavaDoc();
59
60     
61     private static int aggregationMechanism =-1;
62     
63     
64     private static OutputStream JavaDoc streamOut;
65     private static PrintWriter JavaDoc bench;
66     
67     /**
68      * @param args
69      */

70     public static void main(String JavaDoc[] 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 JavaDoc streamOut = new FileOutputStream JavaDoc(metricListFileName);
90                 PrintWriter JavaDoc writerOut = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(streamOut));
91                 writeMetricLists(writerOut);
92                 
93                 writerOut.flush();
94                 streamOut.close();
95             } catch (IOException JavaDoc 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             //read aggregation option
105
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             //TODO: hello
118
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     /*
130      * check if there is a 3rd argument and it should be either -class or -benchmark
131      */

132     private static void aggregationOption(String JavaDoc[] 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     /*
150      * Check if there is a 3rd argument if not complain
151      * @param startIndex is the first args element where we expect to have an xml file or a *
152      */

153     private static void readXMLFileNames(int startIndex, String JavaDoc 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         //check if its a *
160
if(args[startIndex].equals("*")){
161             System.out.println("Will read all xml files from directory");
162             
163             //READ DIRECTORY STRUCTURE
164
readStar();
165         }
166         else{
167             for(int i=startIndex;i<args.length;i++){
168                 String JavaDoc temp = args[i];
169                 if(!temp.endsWith(".xml")){
170                     //System.out.println("Argument number "+(startIndex+1) + ": '" + temp+"' is not a valid xml file");
171
//System.exit(1);
172
}
173                 else{
174                     xmlFileList.add(temp);
175                 }
176             }
177         }
178         
179         Iterator JavaDoc it = xmlFileList.iterator();
180         while(it.hasNext()){
181             System.out.println("Will be reading: "+(String JavaDoc)it.next());
182         }
183     }
184     
185     
186     
187     /*
188      * Check if args1 exists and if so store the metric List FileName
189      */

190     private static void metricListFileName(String JavaDoc[] 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     /*
234      * Read all the xml files from current directory into the xmlFileList arraylist
235      */

236     private static void readStar(){
237         String JavaDoc curDir = System.getProperty("user.dir");
238         System.out.println("Current system directory is"+curDir);
239         File JavaDoc dir = new File JavaDoc(curDir);
240             
241         String JavaDoc[] children = dir.list();
242         if (children == null) {
243             // Either dir does not exist or is not a directory
244
} else {
245     
246             FilenameFilter JavaDoc filter = new FilenameFilter JavaDoc() {
247                 public boolean accept(File JavaDoc dir, String JavaDoc 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 JavaDoc out){
261         ArrayList JavaDoc metricList = new ArrayList JavaDoc();
262         
263         Iterator JavaDoc it = xmlFileList.iterator();
264         while(it.hasNext()){
265             String JavaDoc fileName = (String JavaDoc)it.next();
266             try {
267                 DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory.newInstance();
268                 DocumentBuilder JavaDoc docBuilder = docBuilderFactory.newDocumentBuilder();
269                 Document doc = docBuilder.parse (new File JavaDoc(fileName));
270                 System.out.println("Retrieving Metric List from xml file: "+fileName);
271                 // normalize text representation
272
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                         //System.out.println("MetricName: " + ((Node)textFNList.item(0)).getNodeValue().trim());
286
if(!metricList.contains(((Node)textFNList.item(0)).getNodeValue().trim()))
287                             metricList.add(((Node)textFNList.item(0)).getNodeValue().trim());
288
289                     }//end of if clause
290
}//end of for loop with s var
291
}catch (SAXParseException JavaDoc err) {
292                 System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
293                 System.out.println(" " + err.getMessage ());
294             }
295             catch (SAXException JavaDoc e) {
296                 Exception JavaDoc x = e.getException ();
297                 ((x == null) ? e : x).printStackTrace ();
298             }
299             catch (Throwable JavaDoc 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 JavaDoc columns = new Vector JavaDoc();
316         
317         /*
318          * create the columns which are the metriclist
319          */

320         try{
321             FileReader JavaDoc file = new FileReader JavaDoc(metricListFileName);
322             BufferedReader JavaDoc fileInput = new BufferedReader JavaDoc(file);
323             String JavaDoc text;
324                     
325             //System.out.println("Columns");
326
while( (text = fileInput.readLine()) != null){
327                 //System.out.print(text+"\t");
328
columns.add(text);
329             }
330             fileInput.close();
331         }
332         catch(Exception JavaDoc 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 JavaDoc newClassName="";
344         if(aggregationMechanism == ProcessData.BENCHMARK){
345             //TODO: create a metricListfileName.xml with metric info
346

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 JavaDoc it = xmlFileList.iterator();
360         while(it.hasNext()){
361             String JavaDoc fileName = (String JavaDoc)it.next();
362
363             try{
364                 DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory.newInstance();
365
366                 DocumentBuilder JavaDoc docBuilder = docBuilderFactory.newDocumentBuilder();
367                 Document doc = docBuilder.parse (new File JavaDoc(fileName));
368                 System.out.println("Gethering metric info from from xml file: "+fileName);
369                 // normalize text representation
370
doc.getDocumentElement ().normalize ();
371
372                 
373                 if(aggregationMechanism == ProcessData.BENCHMARK){
374
375                     /*
376                      * TODO:
377                      * so .tex file is already open with the header and all
378                      * writer is in field bench
379                      */

380                     
381                     //print the name of the xml file as the name of the benchmark
382
if(fileName.endsWith(".xml"))
383                         bench.print(fileName.substring(0,fileName.length()-4));
384                     else
385                         bench.print(fileName);
386                     
387                     
388                     HashMap JavaDoc aggregatedValues = new HashMap JavaDoc();
389                     
390                     Iterator JavaDoc tempIt = columns.iterator();
391                     while(tempIt.hasNext()){
392                         aggregatedValues.put(tempIt.next(),new Integer JavaDoc(0));
393                     }
394
395                     aggregateXMLFileMetrics(doc,aggregatedValues);
396                     
397                     //at this point the hashmap contains aggregatedValue of all columns
398
tempIt = columns.iterator();
399                     while(tempIt.hasNext()){
400                         int val = ((Integer JavaDoc)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 JavaDoc err) {
424                 System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
425                 System.out.println(" " + err.getMessage ());
426             }
427             catch (SAXException JavaDoc e) {
428                 Exception JavaDoc x = e.getException ();
429                 ((x == null) ? e : x).printStackTrace ();
430             }
431             catch (Throwable JavaDoc 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 JavaDoc openWriteFile(String JavaDoc fileName){
453         PrintWriter JavaDoc writerOut;
454         try{
455             streamOut = new FileOutputStream JavaDoc(fileName);
456             writerOut = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(streamOut));
457         } catch (IOException JavaDoc e) {
458             throw new CompilationDeathException("Cannot output file " + fileName);
459         }
460         return writerOut;
461     }
462     
463     
464     private static void closeWriteFile(PrintWriter JavaDoc writerOut,String JavaDoc fileName){
465         try{
466             writerOut.flush();
467             streamOut.close();
468         } catch (IOException JavaDoc e) {
469             throw new CompilationDeathException("Cannot output file " + fileName);
470         }
471     }
472     
473     
474     
475     
476     /*
477      * This method is called for each xml class sent as input.
478      *
479      * Should read all of its metrics and add them to the aggregated values
480      */

481     private static void aggregateXMLFileMetrics(Document doc, HashMap JavaDoc 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 JavaDoc tempName = ((Node)textFNList.item(0)).getNodeValue().trim();
495                     
496                 Object JavaDoc tempObj = aggregated.get(tempName);
497                 if( tempObj == null){
498                     //not found in hashmap so we dont care about this metric
499
continue;
500                 }
501                 
502                 Integer JavaDoc valSoFar = (Integer JavaDoc)tempObj;
503                 
504                 //We get to this point only if the metric is important
505

506                 NodeList value = metricElement.getElementsByTagName("Value");
507                 Element name1 = (Element)value.item(0);
508
509                 NodeList textFNList1 = name1.getChildNodes();
510                 String JavaDoc valToPrint = ((Node)textFNList1.item(0)).getNodeValue().trim();
511                 int temp = Integer.parseInt(valToPrint);
512                 
513                 aggregated.put(tempName, new Integer JavaDoc(valSoFar.intValue()+temp) );
514                 
515             }//end of if metricNode is an element_Node
516
}//end of for loop with s var
517
}
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 JavaDoc fileName, Document doc,Vector JavaDoc columns){
541         //create a tex file with name (fileName - xml + tex)
542
String JavaDoc 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 JavaDoc writerOut = openWriteFile(newClassName);
551         printTexTableHeader(writerOut,"Classes",columns);
552         
553         
554         
555         
556         
557         /*
558          * In order to print all class info alphabetically
559          * we will create a map of className to data to be displayed
560          */

561         ArrayList JavaDoc classNames = new ArrayList JavaDoc();
562         HashMap JavaDoc classData = new HashMap JavaDoc();
563         
564         //each row is a class the name is obtained from the tag Class
565
NodeList classes = doc.getElementsByTagName("Class");
566         
567         for(int cl=0;cl<classes.getLength();cl++){
568             //going through each class node
569
Node classNode = classes.item(cl);
570             if(classNode.getNodeType() == Node.ELEMENT_NODE){
571             
572                 Element classElement = (Element)classNode;
573                 
574                 /*
575                  * Class Name
576                  */

577                 NodeList classNameNodeList = classElement.getElementsByTagName("ClassName");
578                 Element classNameElement = (Element)classNameNodeList.item(0);
579
580                 NodeList classNameTextFNList = classNameElement.getChildNodes();
581                 String JavaDoc className = ((Node)classNameTextFNList.item(0)).getNodeValue().trim();
582                 
583                 //writerOut.println("");
584

585                 className = className.replace('_','-');
586                 if(className.length()>CLASSNAMESIZE){
587                     //writerOut.print(className.substring(0,CLASSNAMESIZE)+" ");
588
className = className.substring(0,CLASSNAMESIZE);
589                     classNames.add(className);
590                 }
591                 else{
592                     //writerOut.print(className+" ");
593
classNames.add(className);
594                 }
595                 
596                 System.out.print("\nclassName "+className);
597                 
598     
599                 
600                 String JavaDoc data = " ";
601
602                 
603                 /*
604                  * Metrics
605                  */

606                 NodeList metrics = classElement.getElementsByTagName("Metric");
607
608                 int columnIndex=0; //which one we are printing right now
609

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 JavaDoc tempName = ((Node)textFNList.item(0)).getNodeValue().trim();
620                     
621                         /*
622                          * If the name of this metric is not the next column name in the columns
623                          * simply skip over it and continue
624                          */

625                         if(! tempName.equals(columns.elementAt(columnIndex))){
626                             //System.out.println("here");
627
continue;
628                         }
629
630                         
631                     
632                         //We get to this point only if the metric name is the same as the column name
633

634                         NodeList value = metricElement.getElementsByTagName("Value");
635                         Element name1 = (Element)value.item(0);
636
637                         NodeList textFNList1 = name1.getChildNodes();
638                         String JavaDoc valToPrint = ((Node)textFNList1.item(0)).getNodeValue().trim();
639                         System.out.print(" " + valToPrint);
640                         //writerOut.print("&"+valToPrint);
641
data += "&"+valToPrint;
642                         
643                         
644                         
645                         columnIndex++;
646                         if(columns.size()>columnIndex){
647                             //writerOut.print(" ");
648
data += " ";
649                         }
650                         else{
651                             //writerOut.println("\\\\");
652
data += "\\\\";
653                         }
654                         
655                     }//end of if metricNode is an element_Node
656
}//end of for loop with s var
657

658                 
659                 
660                 
661                 
662                 
663                 classData.put(className,data);
664                     
665                 
666                 
667             }//end of if classNode is an element_Node
668
}//end of for loop with cl
669

670         //writerOut.println();
671
//writerOut.println();
672
//writerOut.println();
673

674         
675         Collections.sort(classNames);
676         
677         Iterator JavaDoc tempIt = classNames.iterator();
678         while(tempIt.hasNext()){
679             String JavaDoc className = (String JavaDoc)tempIt.next();
680             String JavaDoc data = (String JavaDoc)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 JavaDoc out, String JavaDoc 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 JavaDoc out, String JavaDoc rowHeading, Vector JavaDoc 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 JavaDoc it = columns.iterator();
713         while(it.hasNext()){
714             out.print("&"+(String JavaDoc)it.next());
715             if(it.hasNext())
716                 out.print(" ");
717         }
718         out.println("\\\\");
719         
720         out.println("\\hline");
721     }
722 }
723
Popular Tags