KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > cli > 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.cli;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.log4j.*;
39
40 import com.jeantessier.commandline.*;
41 import com.jeantessier.dependency.*;
42 import com.jeantessier.dependencyfinder.*;
43
44 public class DependencyMetrics {
45     public static final String JavaDoc DEFAULT_INCLUDES = "//";
46     public static final String JavaDoc DEFAULT_SCOPE_INCLUDES = "//";
47     public static final String JavaDoc DEFAULT_FILTER_INCLUDES = "//";
48     public static final String JavaDoc DEFAULT_LOGFILE = "System.out";
49
50     public static void showError(CommandLineUsage clu, String JavaDoc msg) {
51         System.err.println(msg);
52         showError(clu);
53     }
54
55     public static void showError(CommandLineUsage clu) {
56         System.err.println(clu);
57         System.err.println();
58         System.err.println("-all shorthand for the combination:");
59         System.err.println(" -package-scope");
60         System.err.println(" -class-scope");
61         System.err.println(" -feature-scope");
62         System.err.println(" -package-filter");
63         System.err.println(" -class-filter");
64         System.err.println(" -feature-filter");
65         System.err.println();
66         System.err.println("-p2p shorthand for the combination:");
67         System.err.println(" -package-scope");
68         System.err.println(" -package-filter");
69         System.err.println();
70         System.err.println("-c2p shorthand for the combination:");
71         System.err.println(" -class-scope");
72         System.err.println(" -package-filter");
73         System.err.println();
74         System.err.println("-c2c shorthand for the combination:");
75         System.err.println(" -class-scope");
76         System.err.println(" -class-filter");
77         System.err.println();
78         System.err.println("-f2f shorthand for the combination:");
79         System.err.println(" -feature-scope");
80         System.err.println(" -feature-filter");
81         System.err.println();
82         System.err.println("-includes \"str\" shorthand for the combination:");
83         System.err.println(" -scope-includes \"str\"");
84         System.err.println(" -filter-includes \"str\"");
85         System.err.println();
86         System.err.println("-excludes \"str\" shorthand for the combination:");
87         System.err.println(" -scope-excludes \"str\"");
88         System.err.println(" -filter-excludes \"str\"");
89         System.err.println();
90         System.err.println("-chart-all shorthand for the combination:");
91         System.err.println(" -chart-classes-per-package");
92         System.err.println(" -chart-features-per-class");
93         System.err.println(" -chart-inbounds-per-package");
94         System.err.println(" -chart-outbounds-per-package");
95         System.err.println(" -chart-inbounds-per-class");
96         System.err.println(" -chart-outbounds-per-class");
97         System.err.println(" -chart-inbounds-per-feature");
98         System.err.println(" -chart-outbounds-per-feature");
99         System.err.println();
100         System.err.println("-chart-inbounds shorthand for the combination:");
101         System.err.println(" -chart-inbounds-per-package");
102         System.err.println(" -chart-inbounds-per-class");
103         System.err.println(" -chart-inbounds-per-feature");
104         System.err.println();
105         System.err.println("-chart-outbounds shorthand for the combination:");
106         System.err.println(" -chart-outbounds-per-package");
107         System.err.println(" -chart-outbounds-per-class");
108         System.err.println(" -chart-outbounds-per-feature");
109         System.err.println();
110         System.err.println("-chart-packages shorthand for the combination:");
111         System.err.println(" -chart-classes-per-package");
112         System.err.println(" -chart-inbounds-per-package");
113         System.err.println(" -chart-outbounds-per-package");
114         System.err.println();
115         System.err.println("-chart-classes shorthand for the combination:");
116         System.err.println(" -chart-features-per-class");
117         System.err.println(" -chart-inbounds-per-class");
118         System.err.println(" -chart-outbounds-per-class");
119         System.err.println();
120         System.err.println("-chart-features shorthand for the combination:");
121         System.err.println(" -chart-inbounds-per-feature");
122         System.err.println(" -chart-outbounds-per-feature");
123         System.err.println();
124         System.err.println("If no files are specified, it processes the current directory.");
125         System.err.println();
126         System.err.println("If file is a directory, it is recusively scanned for files");
127         System.err.println("ending in \".class\".");
128         System.err.println();
129         System.err.println("Defaults is text output to the console.");
130         System.err.println();
131     }
132
133     public static void showVersion() throws IOException {
134         Version version = new Version();
135         
136         System.err.print(version.getImplementationTitle());
137         System.err.print(" ");
138         System.err.print(version.getImplementationVersion());
139         System.err.print(" (c) ");
140         System.err.print(version.getCopyrightDate());
141         System.err.print(" ");
142         System.err.print(version.getCopyrightHolder());
143         System.err.println();
144         
145         System.err.print(version.getImplementationURL());
146         System.err.println();
147         
148         System.err.print("Compiled on ");
149         System.err.print(version.getImplementationDate());
150         System.err.println();
151     }
152
153     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
154         // Parsing the command line
155
CommandLine commandLine = new CommandLine(new AtLeastParameterStrategy(1));
156         commandLine.addMultipleValuesSwitch("scope-includes", DEFAULT_SCOPE_INCLUDES);
157         commandLine.addMultipleValuesSwitch("scope-excludes");
158         commandLine.addToggleSwitch("package-scope");
159         commandLine.addMultipleValuesSwitch("package-scope-includes");
160         commandLine.addMultipleValuesSwitch("package-scope-excludes");
161         commandLine.addToggleSwitch("class-scope");
162         commandLine.addMultipleValuesSwitch("class-scope-includes");
163         commandLine.addMultipleValuesSwitch("class-scope-excludes");
164         commandLine.addToggleSwitch("feature-scope");
165         commandLine.addMultipleValuesSwitch("feature-scope-includes");
166         commandLine.addMultipleValuesSwitch("feature-scope-excludes");
167         commandLine.addMultipleValuesSwitch("filter-includes", DEFAULT_FILTER_INCLUDES);
168         commandLine.addMultipleValuesSwitch("filter-excludes");
169         commandLine.addToggleSwitch("package-filter");
170         commandLine.addMultipleValuesSwitch("package-filter-includes");
171         commandLine.addMultipleValuesSwitch("package-filter-excludes");
172         commandLine.addToggleSwitch("class-filter");
173         commandLine.addMultipleValuesSwitch("class-filter-includes");
174         commandLine.addMultipleValuesSwitch("class-filter-excludes");
175         commandLine.addToggleSwitch("feature-filter");
176         commandLine.addMultipleValuesSwitch("feature-filter-includes");
177         commandLine.addMultipleValuesSwitch("feature-filter-excludes");
178
179         commandLine.addToggleSwitch("all");
180         commandLine.addToggleSwitch("p2p");
181         commandLine.addToggleSwitch("c2p");
182         commandLine.addToggleSwitch("c2c");
183         commandLine.addToggleSwitch("f2f");
184         commandLine.addMultipleValuesSwitch("includes", DEFAULT_INCLUDES);
185         commandLine.addMultipleValuesSwitch("excludes");
186
187         commandLine.addToggleSwitch("list");
188         commandLine.addToggleSwitch("chart-classes-per-package");
189         commandLine.addToggleSwitch("chart-features-per-class");
190         commandLine.addToggleSwitch("chart-inbounds-per-package");
191         commandLine.addToggleSwitch("chart-outbounds-per-package");
192         commandLine.addToggleSwitch("chart-inbounds-per-class");
193         commandLine.addToggleSwitch("chart-outbounds-per-class");
194         commandLine.addToggleSwitch("chart-inbounds-per-feature");
195         commandLine.addToggleSwitch("chart-outbounds-per-feature");
196         commandLine.addToggleSwitch("chart-inbounds");
197         commandLine.addToggleSwitch("chart-outbounds");
198         commandLine.addToggleSwitch("chart-packages");
199         commandLine.addToggleSwitch("chart-classes");
200         commandLine.addToggleSwitch("chart-features");
201         commandLine.addToggleSwitch("chart-all");
202         commandLine.addToggleSwitch("time");
203         commandLine.addToggleSwitch("validate");
204         commandLine.addSingleValueSwitch("out");
205         commandLine.addToggleSwitch("help");
206         commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE);
207         commandLine.addToggleSwitch("version");
208
209         CommandLineUsage usage = new CommandLineUsage("DependencyMetrics");
210         commandLine.accept(usage);
211
212         try {
213             commandLine.parse(args);
214         } catch (IllegalArgumentException JavaDoc ex) {
215             showError(usage, ex.toString());
216             System.exit(1);
217         } catch (CommandLineException ex) {
218             showError(usage, ex.toString());
219             System.exit(1);
220         }
221
222         if (commandLine.getToggleSwitch("help")) {
223             showError(usage);
224         }
225         
226         if (commandLine.getToggleSwitch("version")) {
227             showVersion();
228         }
229
230         if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) {
231             System.exit(1);
232         }
233
234         VerboseListener verboseListener = new VerboseListener();
235         if (commandLine.isPresent("verbose")) {
236             if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) {
237                 verboseListener.setWriter(System.out);
238             } else {
239                 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose")));
240             }
241         }
242
243         /*
244          * Beginning of main processing
245          */

246
247         Date start = new Date();
248
249         NodeFactory factory = new NodeFactory();
250         
251         Iterator i = commandLine.getParameters().iterator();
252         while (i.hasNext()) {
253             String JavaDoc filename = (String JavaDoc) i.next();
254             Logger.getLogger(DependencyMetrics.class).info("Reading " + filename);
255             verboseListener.print("Reading " + filename);
256
257             if (filename.endsWith(".xml")) {
258                 NodeLoader loader = new NodeLoader(factory, commandLine.getToggleSwitch("validate"));
259                 loader.addDependencyListener(verboseListener);
260                 loader.load(filename);
261             }
262
263             Logger.getLogger(DependencyMetrics.class).info("Read \"" + filename + "\".");
264         }
265         
266         PrintWriter out;
267         if (commandLine.isPresent("out")) {
268             out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out")));
269         } else {
270             out = new PrintWriter(new OutputStreamWriter(System.out));
271         }
272
273         MetricsReport reporter = new MetricsReport(out);
274         
275         reporter.setListingElements(commandLine.getToggleSwitch("list"));
276         reporter.setChartingClassesPerPackage(commandLine.getToggleSwitch("chart-classes-per-package"));
277         reporter.setChartingFeaturesPerClass(commandLine.getToggleSwitch("chart-features-per-class"));
278         reporter.setChartingInboundsPerPackage(commandLine.getToggleSwitch("chart-inbounds-per-package"));
279         reporter.setChartingOutboundsPerPackage(commandLine.getToggleSwitch("chart-outbounds-per-package"));
280         reporter.setChartingInboundsPerClass(commandLine.getToggleSwitch("chart-inbounds-per-class"));
281         reporter.setChartingOutboundsPerClass(commandLine.getToggleSwitch("chart-outbounds-per-class"));
282         reporter.setChartingInboundsPerFeature(commandLine.getToggleSwitch("chart-inbounds-per-feature"));
283         reporter.setChartingOutboundsPerFeature(commandLine.getToggleSwitch("chart-outbounds-per-feature"));
284
285         if (commandLine.getToggleSwitch("chart-all")) {
286             reporter.setChartingClassesPerPackage(true);
287             reporter.setChartingFeaturesPerClass(true);
288             reporter.setChartingInboundsPerPackage(true);
289             reporter.setChartingOutboundsPerPackage(true);
290             reporter.setChartingInboundsPerClass(true);
291             reporter.setChartingOutboundsPerClass(true);
292             reporter.setChartingInboundsPerFeature(true);
293             reporter.setChartingOutboundsPerFeature(true);
294         }
295         
296         if (commandLine.getToggleSwitch("chart-inbounds")) {
297             reporter.setChartingInboundsPerPackage(true);
298             reporter.setChartingInboundsPerClass(true);
299             reporter.setChartingInboundsPerFeature(true);
300         }
301         
302         if (commandLine.getToggleSwitch("chart-outbounds")) {
303             reporter.setChartingOutboundsPerPackage(true);
304             reporter.setChartingOutboundsPerClass(true);
305             reporter.setChartingOutboundsPerFeature(true);
306         }
307         
308         if (commandLine.getToggleSwitch("chart-packages")) {
309             reporter.setChartingClassesPerPackage(true);
310             reporter.setChartingInboundsPerPackage(true);
311             reporter.setChartingOutboundsPerPackage(true);
312         }
313         
314         if (commandLine.getToggleSwitch("chart-classes")) {
315             reporter.setChartingFeaturesPerClass(true);
316             reporter.setChartingInboundsPerClass(true);
317             reporter.setChartingOutboundsPerClass(true);
318         }
319         
320         if (commandLine.getToggleSwitch("chart-features")) {
321             reporter.setChartingInboundsPerFeature(true);
322             reporter.setChartingOutboundsPerFeature(true);
323         }
324
325         RegularExpressionSelectionCriteria scopeCriteria = new RegularExpressionSelectionCriteria();
326         
327         scopeCriteria.setMatchingPackages(commandLine.getToggleSwitch("package-scope"));
328         scopeCriteria.setMatchingClasses(commandLine.getToggleSwitch("class-scope"));
329         scopeCriteria.setMatchingFeatures(commandLine.getToggleSwitch("feature-scope"));
330
331         if (commandLine.isPresent("scope-includes") || (!commandLine.isPresent("package-scope-includes") && !commandLine.isPresent("class-scope-includes") && !commandLine.isPresent("feature-scope-includes"))) {
332             // Only use the default if nothing else has been specified.
333
scopeCriteria.setGlobalIncludes(commandLine.getMultipleSwitch("scope-includes"));
334         }
335         scopeCriteria.setGlobalExcludes(commandLine.getMultipleSwitch("scope-excludes"));
336         scopeCriteria.setPackageIncludes(commandLine.getMultipleSwitch("package-scope-includes"));
337         scopeCriteria.setPackageExcludes(commandLine.getMultipleSwitch("package-scope-excludes"));
338         scopeCriteria.setClassIncludes(commandLine.getMultipleSwitch("class-scope-includes"));
339         scopeCriteria.setClassExcludes(commandLine.getMultipleSwitch("class-scope-excludes"));
340         scopeCriteria.setFeatureIncludes(commandLine.getMultipleSwitch("feature-scope-includes"));
341         scopeCriteria.setFeatureExcludes(commandLine.getMultipleSwitch("feature-scope-excludes"));
342
343         RegularExpressionSelectionCriteria filterCriteria = new RegularExpressionSelectionCriteria();
344
345         filterCriteria.setMatchingPackages(commandLine.getToggleSwitch("package-filter"));
346         filterCriteria.setMatchingClasses(commandLine.getToggleSwitch("class-filter"));
347         filterCriteria.setMatchingFeatures(commandLine.getToggleSwitch("feature-filter"));
348         
349         if (commandLine.isPresent("filter-includes") || (!commandLine.isPresent("package-filter-includes") && !commandLine.isPresent("class-filter-includes") && !commandLine.isPresent("feature-filter-includes"))) {
350             // Only use the default if nothing else has been specified.
351
filterCriteria.setGlobalIncludes(commandLine.getMultipleSwitch("filter-includes"));
352         }
353         filterCriteria.setGlobalExcludes(commandLine.getMultipleSwitch("filter-excludes"));
354         filterCriteria.setPackageIncludes(commandLine.getMultipleSwitch("package-filter-includes"));
355         filterCriteria.setPackageExcludes(commandLine.getMultipleSwitch("package-filter-excludes"));
356         filterCriteria.setClassIncludes(commandLine.getMultipleSwitch("class-filter-includes"));
357         filterCriteria.setClassExcludes(commandLine.getMultipleSwitch("class-filter-excludes"));
358         filterCriteria.setFeatureIncludes(commandLine.getMultipleSwitch("feature-filter-includes"));
359         filterCriteria.setFeatureExcludes(commandLine.getMultipleSwitch("feature-filter-excludes"));
360     
361         if (commandLine.getToggleSwitch("all")) {
362             scopeCriteria.setMatchingPackages(true);
363             scopeCriteria.setMatchingClasses(true);
364             scopeCriteria.setMatchingFeatures(true);
365             filterCriteria.setMatchingPackages(true);
366             filterCriteria.setMatchingClasses(true);
367             filterCriteria.setMatchingFeatures(true);
368         }
369     
370         if (commandLine.getToggleSwitch("p2p")) {
371             scopeCriteria.setMatchingPackages(true);
372             filterCriteria.setMatchingPackages(true);
373         }
374     
375         if (commandLine.getToggleSwitch("c2p")) {
376             scopeCriteria.setMatchingClasses(true);
377             filterCriteria.setMatchingPackages(true);
378         }
379     
380         if (commandLine.getToggleSwitch("c2c")) {
381             scopeCriteria.setMatchingClasses(true);
382             filterCriteria.setMatchingClasses(true);
383         }
384     
385         if (commandLine.getToggleSwitch("f2f")) {
386             scopeCriteria.setMatchingFeatures(true);
387             filterCriteria.setMatchingFeatures(true);
388         }
389     
390         if (commandLine.isPresent("includes")) {
391             scopeCriteria.setGlobalIncludes(commandLine.getMultipleSwitch("includes"));
392             filterCriteria.setGlobalIncludes(commandLine.getMultipleSwitch("includes"));
393         }
394     
395         if (commandLine.isPresent("excludes")) {
396             scopeCriteria.setGlobalExcludes(commandLine.getMultipleSwitch("excludes"));
397             filterCriteria.setGlobalExcludes(commandLine.getMultipleSwitch("excludes"));
398         }
399
400         Logger.getLogger(DependencyMetrics.class).info("Reporting on " + factory.getPackages().size() + " package(s) ...");
401         verboseListener.print("Reporting on " + factory.getPackages().size() + " package(s) ...");
402
403         MetricsGatherer metrics = new MetricsGatherer(new SelectiveTraversalStrategy(scopeCriteria, filterCriteria));
404         metrics.traverseNodes(factory.getPackages().values());
405         reporter.process(metrics);
406
407         out.close();
408         
409         Date end = new Date();
410
411         if (commandLine.getToggleSwitch("time")) {
412             System.err.println(DependencyMetrics.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs.");
413         }
414
415         verboseListener.close();
416     }
417 }
418
Popular Tags