001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependencyfinder.ant;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import javax.xml.parsers.*;
039:
040: import org.apache.tools.ant.*;
041: import org.apache.tools.ant.types.*;
042:
043: import org.xml.sax.*;
044:
045: import com.jeantessier.classreader.*;
046: import com.jeantessier.metrics.*;
047:
048: public class OOMetrics extends Task {
049: public static final String DEFAULT_PROJECT_NAME = "Project";
050: public static final String DEFAULT_SORT = "name";
051:
052: private String projectName = DEFAULT_PROJECT_NAME;
053: private File configuration;
054: private boolean csv = false;
055: private boolean txt = false;
056: private boolean xml = false;
057: private boolean validate = false;
058: private String encoding = com.jeantessier.metrics.XMLPrinter.DEFAULT_ENCODING;
059: private String dtdPrefix = com.jeantessier.metrics.XMLPrinter.DEFAULT_DTD_PREFIX;
060: private String indentText;
061: private boolean projectMetrics = false;
062: private boolean groupMetrics = false;
063: private boolean classMetrics = false;
064: private boolean methodMetrics = false;
065: private Path scopeIncludesList;
066: private Path scopeExcludesList;
067: private Path filterIncludesList;
068: private Path filterExcludesList;
069: private boolean showAllMetrics = false;
070: private boolean showEmptyMetrics = false;
071: private boolean showHiddenMeasurements = false;
072: private String sort = DEFAULT_SORT;
073: private boolean expand = false;
074: private boolean reverse = false;
075: private File destprefix;
076: private Path path;
077:
078: public String getProjectname() {
079: return projectName;
080: }
081:
082: public void setProjectname(String projectName) {
083: this .projectName = projectName;
084: }
085:
086: public File getConfiguration() {
087: return configuration;
088: }
089:
090: public void setConfiguration(File configuration) {
091: this .configuration = configuration;
092: }
093:
094: public boolean getCsv() {
095: return csv;
096: }
097:
098: public void setCsv(boolean csv) {
099: this .csv = csv;
100: }
101:
102: public boolean getTxt() {
103: return txt;
104: }
105:
106: public void setTxt(boolean txt) {
107: this .txt = txt;
108: }
109:
110: public boolean getXml() {
111: return xml;
112: }
113:
114: public void setXml(boolean xml) {
115: this .xml = xml;
116: }
117:
118: public boolean getValidate() {
119: return validate;
120: }
121:
122: public void setValidate(boolean validate) {
123: this .validate = validate;
124: }
125:
126: public String getEncoding() {
127: return encoding;
128: }
129:
130: public void setEncoding(String encoding) {
131: this .encoding = encoding;
132: }
133:
134: public String getDtdprefix() {
135: return dtdPrefix;
136: }
137:
138: public void setDtdprefix(String dtdPrefix) {
139: this .dtdPrefix = dtdPrefix;
140: }
141:
142: public String getIndenttext() {
143: return indentText;
144: }
145:
146: public void setIntenttext(String indentText) {
147: this .indentText = indentText;
148: }
149:
150: public boolean getProjectmetrics() {
151: return projectMetrics;
152: }
153:
154: public void setProjectmetrics(boolean projectMetrics) {
155: this .projectMetrics = projectMetrics;
156: }
157:
158: public boolean getGroupmetrics() {
159: return groupMetrics;
160: }
161:
162: public void setGroupmetrics(boolean groupMetrics) {
163: this .groupMetrics = groupMetrics;
164: }
165:
166: public boolean getClassmetrics() {
167: return classMetrics;
168: }
169:
170: public void setClassmetrics(boolean classMetrics) {
171: this .classMetrics = classMetrics;
172: }
173:
174: public boolean getMethodmetrics() {
175: return methodMetrics;
176: }
177:
178: public void setMethodmetrics(boolean methodMetrics) {
179: this .methodMetrics = methodMetrics;
180: }
181:
182: public void setAllmetrics(boolean allMetrics) {
183: setProjectmetrics(allMetrics);
184: setGroupmetrics(allMetrics);
185: setClassmetrics(allMetrics);
186: setMethodmetrics(allMetrics);
187: }
188:
189: public Path createScopeincludeslist() {
190: if (scopeIncludesList == null) {
191: scopeIncludesList = new Path(getProject());
192: }
193:
194: return scopeIncludesList;
195: }
196:
197: public Path getScopeincludeslist() {
198: return scopeIncludesList;
199: }
200:
201: public Path createScopeexcludeslist() {
202: if (scopeExcludesList == null) {
203: scopeExcludesList = new Path(getProject());
204: }
205:
206: return scopeExcludesList;
207: }
208:
209: public Path getScopeexcludeslist() {
210: return scopeExcludesList;
211: }
212:
213: public Path createFilterincludeslist() {
214: if (filterIncludesList == null) {
215: filterIncludesList = new Path(getProject());
216: }
217:
218: return filterIncludesList;
219: }
220:
221: public Path getFilterincludeslist() {
222: return filterIncludesList;
223: }
224:
225: public Path createFilterexcludeslist() {
226: if (filterExcludesList == null) {
227: filterExcludesList = new Path(getProject());
228: }
229:
230: return filterExcludesList;
231: }
232:
233: public Path getFilterexcludeslist() {
234: return filterExcludesList;
235: }
236:
237: public boolean getShowallmetrics() {
238: return showAllMetrics;
239: }
240:
241: public void setShowallmetrics(boolean showAllMetrics) {
242: this .showAllMetrics = showAllMetrics;
243: }
244:
245: public boolean getShowemptymetrics() {
246: return showEmptyMetrics;
247: }
248:
249: public void setShowemptymetrics(boolean showEmptyMetrics) {
250: this .showEmptyMetrics = showEmptyMetrics;
251: }
252:
253: public boolean getShowhiddenmeasurements() {
254: return showHiddenMeasurements;
255: }
256:
257: public void setShowhiddenmeasurements(boolean showHiddenMeasurements) {
258: this .showHiddenMeasurements = showHiddenMeasurements;
259: }
260:
261: public String getSort() {
262: return sort;
263: }
264:
265: public void setSort(String sort) {
266: this .sort = sort;
267: }
268:
269: public boolean getExpand() {
270: return expand;
271: }
272:
273: public void setExpand(boolean expand) {
274: this .expand = expand;
275: }
276:
277: public boolean getReverse() {
278: return reverse;
279: }
280:
281: public void setReverse(boolean reverse) {
282: this .reverse = reverse;
283: }
284:
285: public File getDestprefix() {
286: return destprefix;
287: }
288:
289: public void setDestprefix(File destprefix) {
290: this .destprefix = destprefix;
291: }
292:
293: public Path createPath() {
294: if (path == null) {
295: path = new Path(getProject());
296: }
297:
298: return path;
299: }
300:
301: public Path getPath() {
302: return path;
303: }
304:
305: public void execute() throws BuildException {
306: // first off, make sure that we've got what we need
307:
308: if (getConfiguration() == null) {
309: throw new BuildException("configuration must be set!");
310: }
311:
312: if (!getConfiguration().exists()) {
313: throw new BuildException("configuration does not exist!");
314: }
315:
316: if (!getConfiguration().isFile()) {
317: throw new BuildException("configuration is not a file!");
318: }
319:
320: if (getPath() == null) {
321: throw new BuildException("path must be set!");
322: }
323:
324: if (getDestprefix() == null) {
325: throw new BuildException("destprefix must be set!");
326: }
327:
328: try {
329: log("Reading classes from path " + getPath());
330:
331: VerboseListener verboseListener = new VerboseListener(this );
332:
333: MetricsFactory factory = new MetricsFactory(
334: getProjectname(), new MetricsConfigurationLoader(
335: getValidate()).load(getConfiguration()
336: .getAbsolutePath()));
337:
338: ClassfileLoader loader = new AggregatingClassfileLoader();
339: loader.addLoadListener(verboseListener);
340: loader.load(Arrays.asList(getPath().list()));
341:
342: com.jeantessier.metrics.MetricsGatherer gatherer = new com.jeantessier.metrics.MetricsGatherer(
343: projectName, factory);
344: gatherer.addMetricsListener(verboseListener);
345: if (getScopeincludeslist() != null
346: || getScopeexcludeslist() != null) {
347: gatherer
348: .setScopeIncludes(createCollection(
349: getScopeincludeslist(),
350: getScopeexcludeslist()));
351: }
352: if (getFilterincludeslist() != null
353: || getFilterexcludeslist() != null) {
354: gatherer.setFilterIncludes(createCollection(
355: getFilterincludeslist(),
356: getFilterexcludeslist()));
357: }
358: gatherer.visitClassfiles(loader.getAllClassfiles());
359:
360: if (getShowallmetrics()) {
361: for (Metrics metrics : gatherer.getMetricsFactory()
362: .getAllClassMetrics()) {
363: gatherer.getMetricsFactory().includeClassMetrics(
364: metrics);
365: }
366:
367: for (Metrics metrics : gatherer.getMetricsFactory()
368: .getAllMethodMetrics()) {
369: gatherer.getMetricsFactory().includeMethodMetrics(
370: metrics);
371: }
372: }
373:
374: if (getCsv()) {
375: printCSVFiles(gatherer.getMetricsFactory());
376: } else if (getTxt()) {
377: printTextFile(gatherer.getMetricsFactory());
378: } else if (getXml()) {
379: printXMLFile(gatherer.getMetricsFactory());
380: }
381: } catch (SAXException ex) {
382: throw new BuildException(ex);
383: } catch (ParserConfigurationException ex) {
384: throw new BuildException(ex);
385: } catch (IOException ex) {
386: throw new BuildException(ex);
387: }
388: }
389:
390: private Collection<String> createCollection(Path includes,
391: Path excludes) throws IOException {
392: Collection<String> result = new HashSet<String>();
393:
394: if (includes != null) {
395: String[] filenames = includes.list();
396: for (String filename : filenames) {
397: BufferedReader reader = new BufferedReader(
398: new FileReader(filename));
399: String line;
400: while ((line = reader.readLine()) != null) {
401: result.add(line);
402: }
403: reader.close();
404: }
405: }
406:
407: if (excludes != null) {
408: String[] filenames = excludes.list();
409: for (String filename : filenames) {
410: BufferedReader reader = new BufferedReader(
411: new FileReader(filename));
412: String line;
413: while ((line = reader.readLine()) != null) {
414: result.remove(line);
415: }
416: reader.close();
417: }
418: }
419:
420: return result;
421: }
422:
423: private void printCSVFiles(MetricsFactory factory)
424: throws IOException {
425: MetricsComparator comparator = new MetricsComparator(getSort());
426: if (getReverse()) {
427: comparator.reverse();
428: }
429:
430: List<Metrics> metrics;
431: com.jeantessier.metrics.Printer printer;
432:
433: if (getProjectmetrics()) {
434: String filename = getDestprefix().getAbsolutePath()
435: + "_project.csv";
436: log("Saving metrics to " + filename);
437:
438: PrintWriter out = new PrintWriter(new FileWriter(filename));
439:
440: metrics = new ArrayList<Metrics>(factory
441: .getProjectMetrics());
442: Collections.sort(metrics, comparator);
443: printer = new com.jeantessier.metrics.CSVPrinter(out,
444: factory.getConfiguration().getProjectMeasurements());
445: printer.setShowEmptyMetrics(getShowemptymetrics());
446: printer
447: .setShowHiddenMeasurements(getShowhiddenmeasurements());
448: if (getIndenttext() != null) {
449: printer.setIndentText(getIndenttext());
450: }
451:
452: printer.visitMetrics(metrics);
453:
454: out.close();
455: }
456:
457: if (getGroupmetrics()) {
458: String filename = getDestprefix().getAbsolutePath()
459: + "_groups.csv";
460: log("Saving metrics to " + filename);
461:
462: PrintWriter out = new PrintWriter(new FileWriter(filename));
463:
464: metrics = new ArrayList<Metrics>(factory.getGroupMetrics());
465: Collections.sort(metrics, comparator);
466: printer = new com.jeantessier.metrics.CSVPrinter(out,
467: factory.getConfiguration().getGroupMeasurements());
468: printer.setShowEmptyMetrics(getShowemptymetrics());
469: printer
470: .setShowHiddenMeasurements(getShowhiddenmeasurements());
471: if (getIndenttext() != null) {
472: printer.setIndentText(getIndenttext());
473: }
474:
475: printer.visitMetrics(metrics);
476:
477: out.close();
478: }
479:
480: if (getClassmetrics()) {
481: String filename = getDestprefix().getAbsolutePath()
482: + "_classes.csv";
483: log("Saving metrics to " + filename);
484:
485: PrintWriter out = new PrintWriter(new FileWriter(filename));
486:
487: metrics = new ArrayList<Metrics>(factory.getClassMetrics());
488: Collections.sort(metrics, comparator);
489: printer = new com.jeantessier.metrics.CSVPrinter(out,
490: factory.getConfiguration().getClassMeasurements());
491: printer.setShowEmptyMetrics(getShowemptymetrics());
492: printer
493: .setShowHiddenMeasurements(getShowhiddenmeasurements());
494: if (getIndenttext() != null) {
495: printer.setIndentText(getIndenttext());
496: }
497:
498: printer.visitMetrics(metrics);
499:
500: out.close();
501: }
502:
503: if (getMethodmetrics()) {
504: String filename = getDestprefix().getAbsolutePath()
505: + "_methods.csv";
506: log("Saving metrics to " + filename);
507:
508: PrintWriter out = new PrintWriter(new FileWriter(filename));
509:
510: metrics = new ArrayList<Metrics>(factory.getMethodMetrics());
511: Collections.sort(metrics, comparator);
512: printer = new com.jeantessier.metrics.CSVPrinter(out,
513: factory.getConfiguration().getMethodMeasurements());
514: printer.setShowEmptyMetrics(getShowemptymetrics());
515: printer
516: .setShowHiddenMeasurements(getShowhiddenmeasurements());
517: if (getIndenttext() != null) {
518: printer.setIndentText(getIndenttext());
519: }
520:
521: printer.visitMetrics(metrics);
522:
523: out.close();
524: }
525: }
526:
527: private void printTextFile(MetricsFactory factory)
528: throws IOException {
529: MetricsComparator comparator = new MetricsComparator(getSort());
530: if (getReverse()) {
531: comparator.reverse();
532: }
533:
534: String filename = getDestprefix().getAbsolutePath() + ".txt";
535: log("Saving metrics to " + filename);
536:
537: PrintWriter out = new PrintWriter(new FileWriter(filename));
538:
539: List<Metrics> metrics;
540:
541: if (getProjectmetrics()) {
542: out.println("Project metrics");
543: out.println("---------------");
544: metrics = new ArrayList<Metrics>(factory
545: .getProjectMetrics());
546: Collections.sort(metrics, comparator);
547: com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(
548: out, factory.getConfiguration()
549: .getProjectMeasurements());
550: printer.setShowEmptyMetrics(getShowemptymetrics());
551: printer
552: .setShowHiddenMeasurements(getShowhiddenmeasurements());
553: printer.setExpandCollectionMeasurements(getExpand());
554: if (getIndenttext() != null) {
555: printer.setIndentText(getIndenttext());
556: }
557:
558: printer.visitMetrics(metrics);
559:
560: out.println();
561: }
562:
563: if (getGroupmetrics()) {
564: out.println("Group metrics");
565: out.println("-------------");
566: metrics = new ArrayList<Metrics>(factory.getGroupMetrics());
567: Collections.sort(metrics, comparator);
568: com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(
569: out, factory.getConfiguration()
570: .getGroupMeasurements());
571: printer.setShowEmptyMetrics(getShowemptymetrics());
572: printer
573: .setShowHiddenMeasurements(getShowhiddenmeasurements());
574: printer.setExpandCollectionMeasurements(getExpand());
575: if (getIndenttext() != null) {
576: printer.setIndentText(getIndenttext());
577: }
578:
579: printer.visitMetrics(metrics);
580:
581: out.println();
582: }
583:
584: if (getClassmetrics()) {
585: out.println("Class metrics");
586: out.println("-------------");
587: metrics = new ArrayList<Metrics>(factory.getClassMetrics());
588: Collections.sort(metrics, comparator);
589: com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(
590: out, factory.getConfiguration()
591: .getClassMeasurements());
592: printer.setShowEmptyMetrics(getShowemptymetrics());
593: printer
594: .setShowHiddenMeasurements(getShowhiddenmeasurements());
595: printer.setExpandCollectionMeasurements(getExpand());
596: if (getIndenttext() != null) {
597: printer.setIndentText(getIndenttext());
598: }
599:
600: printer.visitMetrics(metrics);
601:
602: out.println();
603: }
604:
605: if (getMethodmetrics()) {
606: out.println("Method metrics");
607: out.println("--------------");
608: metrics = new ArrayList<Metrics>(factory.getMethodMetrics());
609: Collections.sort(metrics, comparator);
610: com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(
611: out, factory.getConfiguration()
612: .getMethodMeasurements());
613: printer.setShowEmptyMetrics(getShowemptymetrics());
614: printer
615: .setShowHiddenMeasurements(getShowhiddenmeasurements());
616: printer.setExpandCollectionMeasurements(getExpand());
617: if (getIndenttext() != null) {
618: printer.setIndentText(getIndenttext());
619: }
620:
621: printer.visitMetrics(metrics);
622:
623: out.println();
624: }
625:
626: out.close();
627: }
628:
629: private void printXMLFile(MetricsFactory factory)
630: throws IOException {
631: MetricsComparator comparator = new MetricsComparator(getSort());
632: if (getReverse()) {
633: comparator.reverse();
634: }
635:
636: String filename = getDestprefix().getAbsolutePath() + ".xml";
637: log("Saving metrics to " + filename);
638:
639: PrintWriter out = new PrintWriter(new FileWriter(filename));
640:
641: List<Metrics> metrics = new ArrayList<Metrics>(factory
642: .getProjectMetrics());
643: Collections.sort(metrics, comparator);
644: com.jeantessier.metrics.Printer printer = new com.jeantessier.metrics.XMLPrinter(
645: out, factory.getConfiguration(), getEncoding(),
646: getDtdprefix());
647: printer.setShowEmptyMetrics(getShowemptymetrics());
648: printer.setShowHiddenMeasurements(getShowhiddenmeasurements());
649: if (getIndenttext() != null) {
650: printer.setIndentText(getIndenttext());
651: }
652:
653: printer.visitMetrics(metrics);
654:
655: out.close();
656: }
657: }
|