KickJava   Java API By Example, From Geeks To Geeks.

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


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.dependency.*;
44
45 public class DependencyMetrics extends GraphTask {
46     private String JavaDoc scopeIncludes = "//";
47     private String JavaDoc scopeExcludes = "";
48     private boolean packageScope;
49     private String JavaDoc packageScopeIncludes = "";
50     private String JavaDoc packageScopeExcludes = "";
51     private boolean classScope;
52     private String JavaDoc classScopeIncludes = "";
53     private String JavaDoc classScopeExcludes = "";
54     private boolean featureScope;
55     private String JavaDoc featureScopeIncludes = "";
56     private String JavaDoc featureScopeExcludes = "";
57     private String JavaDoc filterIncludes = "//";
58     private String JavaDoc filterExcludes = "";
59     private boolean packageFilter;
60     private String JavaDoc packageFilterIncludes = "";
61     private String JavaDoc packageFilterExcludes = "";
62     private boolean classFilter;
63     private String JavaDoc classFilterIncludes = "";
64     private String JavaDoc classFilterExcludes = "";
65     private boolean featureFilter;
66     private String JavaDoc featureFilterIncludes = "";
67     private String JavaDoc featureFilterExcludes = "";
68
69     private boolean list = false;
70     private boolean chartClassesPerPackage = false;
71     private boolean chartFeaturesPerClass = false;
72     private boolean chartInboundsPerPackage = false;
73     private boolean chartOutboundsPerPackage = false;
74     private boolean chartInboundsPerClass = false;
75     private boolean chartOutboundsPerClass = false;
76     private boolean chartInboundsPerFeature = false;
77     private boolean chartOutboundsPerFeature = false;
78
79     public String JavaDoc getScopeincludes() {
80         return scopeIncludes;
81     }
82
83     public void setScopeincludes(String JavaDoc scopeIncludes) {
84         this.scopeIncludes = scopeIncludes;
85     }
86     
87     public String JavaDoc getScopeexcludes() {
88         return scopeExcludes;
89     }
90
91     public void setScopeexcludes(String JavaDoc scopeExcludes) {
92         this.scopeExcludes = scopeExcludes;
93     }
94
95     public boolean getPackagescope() {
96         return packageScope;
97     }
98
99     public void setPackagescope(boolean packageScope) {
100         this.packageScope = packageScope;
101     }
102     
103     public String JavaDoc getPackagescopeincludes() {
104         return packageScopeIncludes;
105     }
106
107     public void setPackagescopeincludes(String JavaDoc packageScopeIncludes) {
108         this.packageScopeIncludes = packageScopeIncludes;
109     }
110     
111     public String JavaDoc getPackagescopeexcludes() {
112         return packageScopeExcludes;
113     }
114
115     public void setPackagescopeexcludes(String JavaDoc packageScopeExcludes) {
116         this.packageScopeExcludes = packageScopeExcludes;
117     }
118
119     public boolean getClassscope() {
120         return classScope;
121     }
122
123     public void setClassscope(boolean classScope) {
124         this.classScope = classScope;
125     }
126     
127     public String JavaDoc getClassscopeincludes() {
128         return classScopeIncludes;
129     }
130
131     public void setClassscopeincludes(String JavaDoc classScopeIncludes) {
132         this.classScopeIncludes = classScopeIncludes;
133     }
134     
135     public String JavaDoc getClassscopeexcludes() {
136         return classScopeExcludes;
137     }
138
139     public void setClassscopeexcludes(String JavaDoc classScopeExcludes) {
140         this.classScopeExcludes = classScopeExcludes;
141     }
142
143     public boolean getFeaturescope() {
144         return featureScope;
145     }
146
147     public void setFeaturescope(boolean featureScope) {
148         this.featureScope = featureScope;
149     }
150     
151     public String JavaDoc getFeaturescopeincludes() {
152         return featureScopeIncludes;
153     }
154
155     public void setFeaturescopeincludes(String JavaDoc featureScopeIncludes) {
156         this.featureScopeIncludes = featureScopeIncludes;
157     }
158     
159     public String JavaDoc getFeaturescopeexcludes() {
160         return featureScopeExcludes;
161     }
162
163     public void setFeaturescopeexcludes(String JavaDoc featureScopeExcludes) {
164         this.featureScopeExcludes = featureScopeExcludes;
165     }
166
167     public String JavaDoc getFilterincludes() {
168         return filterIncludes;
169     }
170
171     public void setFilterincludes(String JavaDoc filterIncludes) {
172         this.filterIncludes = filterIncludes;
173     }
174     
175     public String JavaDoc getFilterexcludes() {
176         return filterExcludes;
177     }
178
179     public void setFilterexcludes(String JavaDoc filterExcludes) {
180         this.filterExcludes = filterExcludes;
181     }
182
183     public boolean getPackagefilter() {
184         return packageFilter;
185     }
186
187     public void setPackagefilter(boolean packageFilter) {
188         this.packageFilter = packageFilter;
189     }
190     
191     public String JavaDoc getPackagefilterincludes() {
192         return packageFilterIncludes;
193     }
194
195     public void setPackagefilterincludes(String JavaDoc packageFilterIncludes) {
196         this.packageFilterIncludes = packageFilterIncludes;
197     }
198     
199     public String JavaDoc getPackagefilterexcludes() {
200         return packageFilterExcludes;
201     }
202
203     public void setPackagefilterexcludes(String JavaDoc packageFilterExcludes) {
204         this.packageFilterExcludes = packageFilterExcludes;
205     }
206
207     public boolean getClassfilter() {
208         return classFilter;
209     }
210
211     public void setClassfilter(boolean classFilter) {
212         this.classFilter = classFilter;
213     }
214     
215     public String JavaDoc getClassfilterincludes() {
216         return classFilterIncludes;
217     }
218
219     public void setClassfilterincludes(String JavaDoc classFilterIncludes) {
220         this.classFilterIncludes = classFilterIncludes;
221     }
222     
223     public String JavaDoc getClassfilterexcludes() {
224         return classFilterExcludes;
225     }
226
227     public void setClassfilterexcludes(String JavaDoc classFilterExcludes) {
228         this.classFilterExcludes = classFilterExcludes;
229     }
230
231     public boolean getFeaturefilter() {
232         return featureFilter;
233     }
234
235     public void setFeaturefilter(boolean featureFilter) {
236         this.featureFilter = featureFilter;
237     }
238     
239     public String JavaDoc getFeaturefilterincludes() {
240         return featureFilterIncludes;
241     }
242
243     public void setFeaturefilterincludes(String JavaDoc featureFilterIncludes) {
244         this.featureFilterIncludes = featureFilterIncludes;
245     }
246     
247     public String JavaDoc getFeaturefilterexcludes() {
248         return featureFilterExcludes;
249     }
250
251     public void setFeaturefilterexcludes(String JavaDoc featureFilterExcludes) {
252         this.featureFilterExcludes = featureFilterExcludes;
253     }
254
255     public void setAll(boolean value) {
256         setPackagescope(value);
257         setClassscope(value);
258         setFeaturescope(value);
259         setPackagefilter(value);
260         setClassfilter(value);
261         setFeaturefilter(value);
262     }
263         
264     public void setP2p(boolean value) {
265         setPackagescope(value);
266         setPackagefilter(value);
267     }
268     
269     public void setC2p(boolean value) {
270         setClassscope(value);
271         setPackagefilter(value);
272     }
273
274     public void setC2c(boolean value) {
275         setClassscope(value);
276         setClassfilter(value);
277     }
278
279     public void setF2f(boolean value) {
280         setFeaturescope(value);
281         setFeaturefilter(value);
282     }
283
284     public void setIncludes(String JavaDoc value) {
285         setScopeincludes(value);
286         setFilterincludes(value);
287     }
288
289     public void setExcludes(String JavaDoc value) {
290         setScopeexcludes(value);
291         setFilterexcludes(value);
292     }
293
294     public boolean getList() {
295         return list;
296     }
297     
298     public void setList(boolean list) {
299         this.list = list;
300     }
301
302     public boolean getChartclassesperpackage() {
303         return chartClassesPerPackage;
304     }
305     
306     public void setChartclassesperpackage(boolean chartClassesPerPackage) {
307         this.chartClassesPerPackage = chartClassesPerPackage;
308     }
309
310     public boolean getChartfeaturesperclass() {
311         return chartFeaturesPerClass;
312     }
313     
314     public void setChartfeaturesperclass(boolean chartFeaturesPerClass) {
315         this.chartFeaturesPerClass = chartFeaturesPerClass;
316     }
317
318     public boolean getChartinboundsperpackage() {
319         return chartInboundsPerPackage;
320     }
321     
322     public void setChartinboundsperpackage(boolean chartInboundsPerPackage) {
323         this.chartInboundsPerPackage = chartInboundsPerPackage;
324     }
325
326     public boolean getChartoutboundsperpackage() {
327         return chartOutboundsPerPackage;
328     }
329     
330     public void setChartoutboundsperpackage(boolean chartOutboundsPerPackage) {
331         this.chartOutboundsPerPackage = chartOutboundsPerPackage;
332     }
333
334     public boolean getChartinboundsperclass() {
335         return chartInboundsPerClass;
336     }
337     
338     public void setChartinboundsperclass(boolean chartInboundsPerClass) {
339         this.chartInboundsPerClass = chartInboundsPerClass;
340     }
341
342     public boolean getChartoutboundsperclass() {
343         return chartOutboundsPerClass;
344     }
345     
346     public void setChartoutboundsperclass(boolean chartOutboundsPerClass) {
347         this.chartOutboundsPerClass = chartOutboundsPerClass;
348     }
349     
350     public boolean getChartinboundsperfeature() {
351         return chartInboundsPerFeature;
352     }
353     
354     public void setChartinboundsperfeature(boolean chartInboundsPerFeature) {
355         this.chartInboundsPerFeature = chartInboundsPerFeature;
356     }
357
358     public boolean getChartoutboundsperfeature() {
359         return chartOutboundsPerFeature;
360     }
361     
362     public void setChartoutboundsperfeature(boolean chartOutboundsPerFeature) {
363         this.chartOutboundsPerFeature = chartOutboundsPerFeature;
364     }
365
366     public void setChartinbounds(boolean chartInbounds) {
367         setChartinboundsperpackage(chartInbounds);
368         setChartinboundsperclass(chartInbounds);
369         setChartinboundsperfeature(chartInbounds);
370     }
371
372     public void setChartoutbounds(boolean chartOutbounds) {
373         setChartoutboundsperpackage(chartOutbounds);
374         setChartoutboundsperclass(chartOutbounds);
375         setChartoutboundsperfeature(chartOutbounds);
376     }
377
378     public void setChartpackages(boolean chartPackages) {
379         setChartclassesperpackage(chartPackages);
380         setChartinboundsperpackage(chartPackages);
381         setChartoutboundsperpackage(chartPackages);
382     }
383
384     public void setChartclasses(boolean chartClasses) {
385         setChartfeaturesperclass(chartClasses);
386         setChartinboundsperclass(chartClasses);
387         setChartoutboundsperclass(chartClasses);
388     }
389
390     public void setChartfeatures(boolean chartFeatures) {
391         setChartinboundsperfeature(chartFeatures);
392         setChartoutboundsperfeature(chartFeatures);
393     }
394
395     public void setChartall(boolean chartAll) {
396         setChartclassesperpackage(chartAll);
397         setChartfeaturesperclass(chartAll);
398         setChartinboundsperpackage(chartAll);
399         setChartoutboundsperpackage(chartAll);
400         setChartinboundsperclass(chartAll);
401         setChartoutboundsperclass(chartAll);
402         setChartinboundsperfeature(chartAll);
403         setChartoutboundsperfeature(chartAll);
404     }
405     
406     public void execute() throws BuildException {
407         // first off, make sure that we've got what we need
408
validateParameters();
409
410         VerboseListener verboseListener = new VerboseListener(this);
411
412         try {
413             NodeFactory factory = new NodeFactory();
414             
415             String JavaDoc[] filenames = getSrc().list();
416             for (int i=0; i<filenames.length; i++) {
417                 log("Reading graph from " + filenames[i]);
418                 
419                 Collection packages = Collections.EMPTY_LIST;
420                 
421                 if (filenames[i].endsWith(".xml")) {
422                     NodeLoader loader = new NodeLoader(factory, getValidate());
423                     loader.addDependencyListener(verboseListener);
424                     loader.load(filenames[i]);
425                 }
426             }
427
428             log("Saving metrics report to " + getDestfile().getAbsolutePath());
429             
430             PrintWriter out = new PrintWriter(new FileWriter(getDestfile()));
431
432             MetricsReport reporter = new MetricsReport(out);
433             
434             reporter.setListingElements(getList());
435             reporter.setChartingClassesPerPackage(getChartclassesperpackage());
436             reporter.setChartingFeaturesPerClass(getChartfeaturesperclass());
437             reporter.setChartingInboundsPerPackage(getChartinboundsperpackage());
438             reporter.setChartingOutboundsPerPackage(getChartoutboundsperpackage());
439             reporter.setChartingInboundsPerClass(getChartinboundsperclass());
440             reporter.setChartingOutboundsPerClass(getChartoutboundsperclass());
441             reporter.setChartingInboundsPerFeature(getChartinboundsperfeature());
442             reporter.setChartingOutboundsPerFeature(getChartoutboundsperfeature());
443
444             MetricsGatherer metrics = new MetricsGatherer(getStrategy());
445             metrics.traverseNodes(factory.getPackages().values());
446             reporter.process(metrics);
447
448             out.close();
449         } catch (SAXException ex) {
450             throw new BuildException(ex);
451         } catch (IOException ex) {
452             throw new BuildException(ex);
453         }
454     }
455
456     private SelectionCriteria getScopeCriteria() throws BuildException {
457         RegularExpressionSelectionCriteria result = new RegularExpressionSelectionCriteria();
458
459         result.setMatchingPackages(getPackagescope());
460         result.setMatchingClasses(getClassscope());
461         result.setMatchingFeatures(getFeaturescope());
462
463         result.setGlobalIncludes(getScopeincludes());
464         result.setGlobalExcludes(getScopeexcludes());
465         result.setPackageIncludes(getPackagescopeincludes());
466         result.setPackageExcludes(getPackagescopeexcludes());
467         result.setClassIncludes(getClassscopeincludes());
468         result.setClassExcludes(getClassscopeexcludes());
469         result.setFeatureIncludes(getFeaturescopeincludes());
470         result.setFeatureExcludes(getFeaturescopeexcludes());
471
472         return result;
473     }
474
475     private SelectionCriteria getFilterCriteria() throws BuildException {
476         RegularExpressionSelectionCriteria result = new RegularExpressionSelectionCriteria();
477
478         result.setMatchingPackages(getPackagefilter());
479         result.setMatchingClasses(getClassfilter());
480         result.setMatchingFeatures(getFeaturefilter());
481
482         result.setGlobalIncludes(getFilterincludes());
483         result.setGlobalExcludes(getFilterexcludes());
484         result.setPackageIncludes(getPackagefilterincludes());
485         result.setPackageExcludes(getPackagefilterexcludes());
486         result.setClassIncludes(getClassfilterincludes());
487         result.setClassExcludes(getClassfilterexcludes());
488         result.setFeatureIncludes(getFeaturefilterincludes());
489         result.setFeatureExcludes(getFeaturefilterexcludes());
490
491         return result;
492     }
493     
494     private TraversalStrategy getStrategy() throws BuildException {
495         return new SelectiveTraversalStrategy(getScopeCriteria(), getFilterCriteria());
496     }
497 }
498
Popular Tags