KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > ant > OOMetrics


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependencyfinder.ant;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.tools.ant.*;
39 import org.apache.tools.ant.types.*;
40
41 import org.xml.sax.*;
42
43 import com.jeantessier.classreader.*;
44 import com.jeantessier.metrics.*;
45
46 public class OOMetrics extends Task {
47     public static final String JavaDoc DEFAULT_PROJECT_NAME = "Project";
48     public static final String JavaDoc DEFAULT_SORT = "name";
49
50     private String JavaDoc projectName = DEFAULT_PROJECT_NAME;
51     private File configuration;
52     private boolean csv = false;
53     private boolean txt = false;
54     private boolean xml = false;
55     private boolean validate = false;
56     private String JavaDoc encoding = com.jeantessier.metrics.XMLPrinter.DEFAULT_ENCODING;
57     private String JavaDoc dtdPrefix = com.jeantessier.metrics.XMLPrinter.DEFAULT_DTD_PREFIX;
58     private String JavaDoc indentText;
59     private boolean projectMetrics = false;
60     private boolean groupMetrics = false;
61     private boolean classMetrics = false;
62     private boolean methodMetrics = false;
63     private Path scopeIncludesList;
64     private Path scopeExcludesList;
65     private Path filterIncludesList;
66     private Path filterExcludesList;
67     private boolean showAllMetrics = false;
68     private boolean showEmptyMetrics = false;
69     private boolean showHiddenMeasurements = false;
70     private String JavaDoc sort = DEFAULT_SORT;
71     private boolean expand = false;
72     private boolean reverse = false;
73     private File destprefix;
74     private Path path;
75
76     public String JavaDoc getProjectname() {
77         return projectName;
78     }
79     
80     public void setProjectname(String JavaDoc projectName) {
81         this.projectName = projectName;
82     }
83
84     public File getConfiguration() {
85         return configuration;
86     }
87     
88     public void setConfiguration(File configuration) {
89         this.configuration = configuration;
90     }
91
92     public boolean getCsv() {
93         return csv;
94     }
95     
96     public void setCsv(boolean csv) {
97         this.csv = csv;
98     }
99
100     public boolean getTxt() {
101         return txt;
102     }
103     
104     public void setTxt(boolean txt) {
105         this.txt = txt;
106     }
107
108     public boolean getXml() {
109         return xml;
110     }
111
112     public void setXml(boolean xml) {
113         this.xml = xml;
114     }
115
116     public boolean getValidate() {
117         return validate;
118     }
119
120     public void setValidate(boolean validate) {
121         this.validate = validate;
122     }
123
124     public String JavaDoc getEncoding() {
125         return encoding;
126     }
127     
128     public void setEncoding(String JavaDoc encoding) {
129         this.encoding = encoding;
130     }
131
132     public String JavaDoc getDtdprefix() {
133         return dtdPrefix;
134     }
135     
136     public void setDtdprefix(String JavaDoc dtdPrefix) {
137         this.dtdPrefix = dtdPrefix;
138     }
139
140     public String JavaDoc getIndenttext() {
141         return indentText;
142     }
143     
144     public void setIntenttext(String JavaDoc indentText) {
145         this.indentText = indentText;
146     }
147
148     public boolean getProjectmetrics() {
149         return projectMetrics;
150     }
151     
152     public void setProjectmetrics(boolean projectMetrics) {
153         this.projectMetrics = projectMetrics;
154     }
155
156     public boolean getGroupmetrics() {
157         return groupMetrics;
158     }
159     
160     public void setGroupmetrics(boolean groupMetrics) {
161         this.groupMetrics = groupMetrics;
162     }
163
164     public boolean getClassmetrics() {
165         return classMetrics;
166     }
167     
168     public void setClassmetrics(boolean classMetrics) {
169         this.classMetrics = classMetrics;
170     }
171
172     public boolean getMethodmetrics() {
173         return methodMetrics;
174     }
175     
176     public void setMethodmetrics(boolean methodMetrics) {
177         this.methodMetrics = methodMetrics;
178     }
179
180     public void setAllmetrics(boolean allMetrics) {
181         setProjectmetrics(allMetrics);
182         setGroupmetrics(allMetrics);
183         setClassmetrics(allMetrics);
184         setMethodmetrics(allMetrics);
185     }
186     
187     public Path createScopeincludeslist() {
188         if (scopeIncludesList == null) {
189             scopeIncludesList = new Path(getProject());
190         }
191
192         return scopeIncludesList;
193     }
194     
195     public Path getScopeincludeslist() {
196         return scopeIncludesList;
197     }
198     
199     public Path createScopeexcludeslist() {
200         if (scopeExcludesList == null) {
201             scopeExcludesList = new Path(getProject());
202         }
203
204         return scopeExcludesList;
205     }
206     
207     public Path getScopeexcludeslist() {
208         return scopeExcludesList;
209     }
210     
211     public Path createFilterincludeslist() {
212         if (filterIncludesList == null) {
213             filterIncludesList = new Path(getProject());
214         }
215
216         return filterIncludesList;
217     }
218     
219     public Path getFilterincludeslist() {
220         return filterIncludesList;
221     }
222     
223     public Path createFilterexcludeslist() {
224         if (filterExcludesList == null) {
225             filterExcludesList = new Path(getProject());
226         }
227
228         return filterExcludesList;
229     }
230     
231     public Path getFilterexcludeslist() {
232         return filterExcludesList;
233     }
234
235     public boolean getShowallmetrics() {
236         return showAllMetrics;
237     }
238     
239     public void setShowallmetrics(boolean showAllMetrics) {
240         this.showAllMetrics = showAllMetrics;
241     }
242
243     public boolean getShowemptymetrics() {
244         return showEmptyMetrics;
245     }
246     
247     public void setShowemptymetrics(boolean showEmptyMetrics) {
248         this.showEmptyMetrics = showEmptyMetrics;
249     }
250
251     public boolean getShowhiddenmeasurements() {
252         return showHiddenMeasurements;
253     }
254     
255     public void setShowhiddenmeasurements(boolean showHiddenMeasurements) {
256         this.showHiddenMeasurements = showHiddenMeasurements;
257     }
258     
259     public String JavaDoc getSort() {
260         return sort;
261     }
262     
263     public void setSort(String JavaDoc sort) {
264         this.sort = sort;
265     }
266
267     public boolean getExpand() {
268         return expand;
269     }
270     
271     public void setExpand(boolean expand) {
272         this.expand = expand;
273     }
274
275     public boolean getReverse() {
276         return reverse;
277     }
278     
279     public void setReverse(boolean reverse) {
280         this.reverse = reverse;
281     }
282
283     public File getDestprefix() {
284         return destprefix;
285     }
286     
287     public void setDestprefix(File destprefix) {
288         this.destprefix = destprefix;
289     }
290     
291     public Path createPath() {
292         if (path == null) {
293             path = new Path(getProject());
294         }
295
296         return path;
297     }
298     
299     public Path getPath() {
300         return path;
301     }
302     
303     public void execute() throws BuildException {
304         // first off, make sure that we've got what we need
305

306         if (getConfiguration() == null) {
307             throw new BuildException("configuration must be set!");
308         }
309         
310         if (!getConfiguration().exists()) {
311             throw new BuildException("configuration does not exist!");
312         }
313         
314         if (!getConfiguration().isFile()) {
315             throw new BuildException("configuration is not a file!");
316         }
317
318         if (getPath() == null) {
319             throw new BuildException("path must be set!");
320         }
321
322         if (getDestprefix() == null) {
323             throw new BuildException("destprefix must be set!");
324         }
325
326         try {
327             log("Reading classes from path " + getPath());
328
329             VerboseListener verboseListener = new VerboseListener(this);
330             
331             MetricsFactory factory = new MetricsFactory(getProjectname(), new MetricsConfigurationLoader(getValidate()).load(getConfiguration().getAbsolutePath()));
332             
333             ClassfileLoader loader = new AggregatingClassfileLoader();
334             loader.addLoadListener(verboseListener);
335             loader.load(Arrays.asList(getPath().list()));
336             
337             com.jeantessier.metrics.MetricsGatherer gatherer = new com.jeantessier.metrics.MetricsGatherer(projectName, factory);
338             gatherer.addMetricsListener(verboseListener);
339             if (getScopeincludeslist() != null || getScopeexcludeslist() != null) {
340                 gatherer.setScopeIncludes(createCollection(getScopeincludeslist(), getScopeexcludeslist()));
341             }
342             if (getFilterincludeslist() != null || getFilterexcludeslist() != null) {
343                 gatherer.setFilterIncludes(createCollection(getFilterincludeslist(), getFilterexcludeslist()));
344             }
345             gatherer.visitClassfiles(loader.getAllClassfiles());
346         
347             if (getShowallmetrics()) {
348                 Iterator i;
349                 
350                 i = gatherer.getMetricsFactory().getAllClassMetrics().iterator();
351                 while (i.hasNext()) {
352                     gatherer.getMetricsFactory().includeClassMetrics((Metrics) i.next());
353                 }
354                 
355                 i = gatherer.getMetricsFactory().getAllMethodMetrics().iterator();
356                 while (i.hasNext()) {
357                     gatherer.getMetricsFactory().includeMethodMetrics((Metrics) i.next());
358                 }
359             }
360             
361             if (getCsv()) {
362                 printCSVFiles(gatherer.getMetricsFactory());
363             } else if (getTxt()) {
364                 printTextFile(gatherer.getMetricsFactory());
365             } else if (getXml()) {
366                 printXMLFile(gatherer.getMetricsFactory());
367             }
368         } catch (SAXException ex) {
369             throw new BuildException(ex);
370         } catch (IOException ex) {
371             throw new BuildException(ex);
372         }
373     }
374
375     private Collection createCollection(Path includes, Path excludes) throws IOException {
376         Collection result = new HashSet();
377
378         if (includes != null) {
379             String JavaDoc[] filenames = includes.list();
380             for (int i=0; i<filenames.length; i++) {
381                 BufferedReader reader = new BufferedReader(new FileReader(filenames[i]));
382                 String JavaDoc line;
383                 while ((line = reader.readLine()) != null) {
384                     result.add(line);
385                 }
386                 reader.close();
387             }
388         }
389         
390         if (excludes != null) {
391             String JavaDoc[] filenames = excludes.list();
392             for (int i=0; i<filenames.length; i++) {
393                 BufferedReader reader = new BufferedReader(new FileReader(filenames[i]));
394                 String JavaDoc line;
395                 while ((line = reader.readLine()) != null) {
396                     result.remove(line);
397                 }
398                 reader.close();
399             }
400         }
401         
402         return result;
403     }
404
405     private void printCSVFiles(MetricsFactory factory) throws IOException {
406         MetricsComparator comparator = new MetricsComparator(getSort());
407         if (getReverse()) {
408             comparator.reverse();
409         }
410
411         List metrics;
412         Iterator i;
413         com.jeantessier.metrics.Printer printer;
414
415         if (getProjectmetrics()) {
416             String JavaDoc filename = getDestprefix().getAbsolutePath() + "_project.csv";
417             log("Saving metrics to " + filename);
418
419             PrintWriter out = new PrintWriter(new FileWriter(filename));
420             
421             metrics = new ArrayList(factory.getProjectMetrics());
422             Collections.sort(metrics, comparator);
423             printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getProjectMeasurements());
424             printer.setShowEmptyMetrics(getShowemptymetrics());
425             printer.setShowHiddenMeasurements(getShowhiddenmeasurements());
426             if (getIndenttext() != null) {
427                 printer.setIndentText(getIndenttext());
428             }
429
430             printer.visitMetrics(metrics);
431
432             out.close();
433         }
434
435         if (getGroupmetrics()) {
436             String JavaDoc filename = getDestprefix().getAbsolutePath() + "_groups.csv";
437             log("Saving metrics to " + filename);
438
439             PrintWriter out = new PrintWriter(new FileWriter(filename));
440
441             metrics = new ArrayList(factory.getGroupMetrics());
442             Collections.sort(metrics, comparator);
443             printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getGroupMeasurements());
444             printer.setShowEmptyMetrics(getShowemptymetrics());
445             printer.setShowHiddenMeasurements(getShowhiddenmeasurements());
446             if (getIndenttext() != null) {
447                 printer.setIndentText(getIndenttext());
448             }
449
450             printer.visitMetrics(metrics);
451
452             out.close();
453         }
454
455         if (getClassmetrics()) {
456             String JavaDoc filename = getDestprefix().getAbsolutePath() + "_classes.csv";
457             log("Saving metrics to " + filename);
458
459             PrintWriter out = new PrintWriter(new FileWriter(filename));
460
461             metrics = new ArrayList(factory.getClassMetrics());
462             Collections.sort(metrics, comparator);
463             printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getClassMeasurements());
464             printer.setShowEmptyMetrics(getShowemptymetrics());
465             printer.setShowHiddenMeasurements(getShowhiddenmeasurements());
466             if (getIndenttext() != null) {
467                 printer.setIndentText(getIndenttext());
468             }
469
470             printer.visitMetrics(metrics);
471
472             out.close();
473         }
474
475         if (getMethodmetrics()) {
476             String JavaDoc filename = getDestprefix().getAbsolutePath() + "_methods.csv";
477             log("Saving metrics to " + filename);
478
479             PrintWriter out = new PrintWriter(new FileWriter(filename));
480
481             metrics = new ArrayList(factory.getMethodMetrics());
482             Collections.sort(metrics, comparator);
483             printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getMethodMeasurements());
484             printer.setShowEmptyMetrics(getShowemptymetrics());
485             printer.setShowHiddenMeasurements(getShowhiddenmeasurements());
486             if (getIndenttext() != null) {
487                 printer.setIndentText(getIndenttext());
488             }
489
490             printer.visitMetrics(metrics);
491
492             out.close();
493         }
494     }
495
496     private void printTextFile(MetricsFactory factory) throws IOException {
497         MetricsComparator comparator = new MetricsComparator(getSort());
498         if (getReverse()) {
499             comparator.reverse();
500         }
501
502         String JavaDoc filename = getDestprefix().getAbsolutePath() + ".txt";
503         log("Saving metrics to " + filename);
504         
505         PrintWriter out = new PrintWriter(new FileWriter(filename));
506
507         List metrics;
508         Iterator i;
509
510         if (getProjectmetrics()) {
511             out.println("Project metrics");
512             out.println("---------------");
513             metrics = new ArrayList(factory.getProjectMetrics());
514             Collections.sort(metrics, comparator);
515             com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getProjectMeasurements());
516             printer.setShowEmptyMetrics(getShowemptymetrics());
517             printer.setShowHiddenMeasurements(getShowhiddenmeasurements());
518             printer.setExpandCollectionMeasurements(getExpand());
519             if (getIndenttext() != null) {
520                 printer.setIndentText(getIndenttext());
521             }
522
523             printer.visitMetrics(metrics);
524
525             out.println();
526         }
527
528         if (getGroupmetrics()) {
529             out.println("Group metrics");
530             out.println("-------------");
531             metrics = new ArrayList(factory.getGroupMetrics());
532             Collections.sort(metrics, comparator);
533             com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getGroupMeasurements());
534             printer.setShowEmptyMetrics(getShowemptymetrics());
535             printer.setShowHiddenMeasurements(getShowhiddenmeasurements());
536             printer.setExpandCollectionMeasurements(getExpand());
537             if (getIndenttext() != null) {
538                 printer.setIndentText(getIndenttext());
539             }
540
541             printer.visitMetrics(metrics);
542
543             out.println();
544         }
545
546         if (getClassmetrics()) {
547             out.println("Class metrics");
548             out.println("-------------");
549             metrics = new ArrayList(factory.getClassMetrics());
550             Collections.sort(metrics, comparator);
551             com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getClassMeasurements());
552             printer.setShowEmptyMetrics(getShowemptymetrics());
553             printer.setShowHiddenMeasurements(getShowhiddenmeasurements());
554             printer.setExpandCollectionMeasurements(getExpand());
555             if (getIndenttext() != null) {
556                 printer.setIndentText(getIndenttext());
557             }
558
559             printer.visitMetrics(metrics);
560
561             out.println();
562         }
563         
564         if (getMethodmetrics()) {
565             out.println("Method metrics");
566             out.println("--------------");
567             metrics = new ArrayList(factory.getMethodMetrics());
568             Collections.sort(metrics, comparator);
569             com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getMethodMeasurements());
570             printer.setShowEmptyMetrics(getShowemptymetrics());
571             printer.setShowHiddenMeasurements(getShowhiddenmeasurements());
572             printer.setExpandCollectionMeasurements(getExpand());
573             if (getIndenttext() != null) {
574                 printer.setIndentText(getIndenttext());
575             }
576
577             printer.visitMetrics(metrics);
578
579             out.println();
580         }
581         
582         out.close();
583     }
584
585     private void printXMLFile(MetricsFactory factory) throws IOException {
586         MetricsComparator comparator = new MetricsComparator(getSort());
587         if (getReverse()) {
588             comparator.reverse();
589         }
590
591         String JavaDoc filename = getDestprefix().getAbsolutePath() + ".xml";
592         log("Saving metrics to " + filename);
593         
594         PrintWriter out = new PrintWriter(new FileWriter(filename));
595
596         List metrics = new ArrayList(factory.getProjectMetrics());
597         Collections.sort(metrics, comparator);
598         com.jeantessier.metrics.Printer printer = new com.jeantessier.metrics.XMLPrinter(out, factory.getConfiguration(), getEncoding(), getDtdprefix());
599         printer.setShowEmptyMetrics(getShowemptymetrics());
600         printer.setShowHiddenMeasurements(getShowhiddenmeasurements());
601         if (getIndenttext() != null) {
602             printer.setIndentText(getIndenttext());
603         }
604
605         printer.visitMetrics(metrics);
606
607         out.close();
608     }
609 }
610
Popular Tags