KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > cli > DependencyClosure


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 DependencyClosure {
45     public static final String JavaDoc DEFAULT_START_INCLUDES = "//";
46     public static final String JavaDoc DEFAULT_LOGFILE = "System.out";
47
48     public static void showError(CommandLineUsage clu, String JavaDoc msg) {
49         System.err.println(msg);
50         showError(clu);
51     }
52
53     public static void showError(CommandLineUsage clu) {
54         System.err.println(clu);
55         System.err.println();
56         System.err.println("Defaults is text output to the console.");
57         System.err.println();
58     }
59
60     public static void showVersion() throws IOException {
61         Version version = new Version();
62         
63         System.err.print(version.getImplementationTitle());
64         System.err.print(" ");
65         System.err.print(version.getImplementationVersion());
66         System.err.print(" (c) ");
67         System.err.print(version.getCopyrightDate());
68         System.err.print(" ");
69         System.err.print(version.getCopyrightHolder());
70         System.err.println();
71         
72         System.err.print(version.getImplementationURL());
73         System.err.println();
74         
75         System.err.print("Compiled on ");
76         System.err.print(version.getImplementationDate());
77         System.err.println();
78     }
79
80     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
81         // Parsing the command line
82
CommandLine commandLine = new CommandLine(new AtLeastParameterStrategy(1));
83         commandLine.addMultipleValuesSwitch("start-includes", DEFAULT_START_INCLUDES);
84         commandLine.addMultipleValuesSwitch("start-excludes");
85         commandLine.addMultipleValuesSwitch("package-start-includes");
86         commandLine.addMultipleValuesSwitch("package-start-excludes");
87         commandLine.addMultipleValuesSwitch("class-start-includes");
88         commandLine.addMultipleValuesSwitch("class-start-excludes");
89         commandLine.addMultipleValuesSwitch("feature-start-includes");
90         commandLine.addMultipleValuesSwitch("feature-start-excludes");
91         commandLine.addMultipleValuesSwitch("stop-includes");
92         commandLine.addMultipleValuesSwitch("stop-excludes");
93         commandLine.addMultipleValuesSwitch("package-stop-includes");
94         commandLine.addMultipleValuesSwitch("package-stop-excludes");
95         commandLine.addMultipleValuesSwitch("class-stop-includes");
96         commandLine.addMultipleValuesSwitch("class-stop-excludes");
97         commandLine.addMultipleValuesSwitch("feature-stop-includes");
98         commandLine.addMultipleValuesSwitch("feature-stop-excludes");
99
100         commandLine.addOptionalValueSwitch("maximum-inbound-depth");
101         commandLine.addOptionalValueSwitch("maximum-outbound-depth");
102         
103         commandLine.addToggleSwitch("xml");
104         commandLine.addToggleSwitch("validate");
105         commandLine.addSingleValueSwitch("encoding", XMLPrinter.DEFAULT_ENCODING);
106         commandLine.addSingleValueSwitch("dtd-prefix", XMLPrinter.DEFAULT_DTD_PREFIX);
107         commandLine.addSingleValueSwitch("indent-text");
108         commandLine.addToggleSwitch("time");
109         commandLine.addSingleValueSwitch("out");
110         commandLine.addToggleSwitch("help");
111         commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE);
112         commandLine.addToggleSwitch("version");
113
114         CommandLineUsage usage = new CommandLineUsage("DependencyClosure");
115         commandLine.accept(usage);
116
117         try {
118             commandLine.parse(args);
119         } catch (IllegalArgumentException JavaDoc ex) {
120             showError(usage, ex.toString());
121             System.exit(1);
122         } catch (CommandLineException ex) {
123             showError(usage, ex.toString());
124             System.exit(1);
125         }
126
127         if (commandLine.getToggleSwitch("help")) {
128             showError(usage);
129         }
130         
131         if (commandLine.getToggleSwitch("version")) {
132             showVersion();
133         }
134
135         if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) {
136             System.exit(1);
137         }
138
139         VerboseListener verboseListener = new VerboseListener();
140         if (commandLine.isPresent("verbose")) {
141             if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) {
142                 verboseListener.setWriter(System.out);
143             } else {
144                 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose")));
145             }
146         }
147
148         /*
149          * Beginning of main processing
150          */

151
152         Date start = new Date();
153
154         NodeFactory factory = new NodeFactory();
155         
156         Iterator i = commandLine.getParameters().iterator();
157         while (i.hasNext()) {
158             String JavaDoc filename = (String JavaDoc) i.next();
159             Logger.getLogger(DependencyClosure.class).info("Reading " + filename);
160             verboseListener.print("Reading " + filename);
161
162             if (filename.endsWith(".xml")) {
163                 NodeLoader loader = new NodeLoader(factory, commandLine.getToggleSwitch("validate"));
164                 loader.addDependencyListener(verboseListener);
165                 loader.load(filename);
166             }
167
168             Logger.getLogger(DependencyClosure.class).info("Read \"" + filename + "\".");
169         }
170
171         RegularExpressionSelectionCriteria startCriteria = new RegularExpressionSelectionCriteria();
172         
173         if (commandLine.isPresent("start-includes") || (!commandLine.isPresent("package-start-includes") && !commandLine.isPresent("class-start-includes") && !commandLine.isPresent("feature-start-includes"))) {
174             // Only use the default if nothing else has been specified.
175
startCriteria.setGlobalIncludes(commandLine.getMultipleSwitch("start-includes"));
176         }
177         startCriteria.setGlobalExcludes(commandLine.getMultipleSwitch("start-excludes"));
178         startCriteria.setPackageIncludes(commandLine.getMultipleSwitch("package-start-includes"));
179         startCriteria.setPackageExcludes(commandLine.getMultipleSwitch("package-start-excludes"));
180         startCriteria.setClassIncludes(commandLine.getMultipleSwitch("class-start-includes"));
181         startCriteria.setClassExcludes(commandLine.getMultipleSwitch("class-start-excludes"));
182         startCriteria.setFeatureIncludes(commandLine.getMultipleSwitch("feature-start-includes"));
183         startCriteria.setFeatureExcludes(commandLine.getMultipleSwitch("feature-start-excludes"));
184
185         RegularExpressionSelectionCriteria stopCriteria = new RegularExpressionSelectionCriteria();
186
187         stopCriteria.setGlobalIncludes(commandLine.getMultipleSwitch("stop-includes"));
188         stopCriteria.setGlobalExcludes(commandLine.getMultipleSwitch("stop-excludes"));
189         stopCriteria.setPackageIncludes(commandLine.getMultipleSwitch("package-stop-includes"));
190         stopCriteria.setPackageExcludes(commandLine.getMultipleSwitch("package-stop-excludes"));
191         stopCriteria.setClassIncludes(commandLine.getMultipleSwitch("class-stop-includes"));
192         stopCriteria.setClassExcludes(commandLine.getMultipleSwitch("class-stop-excludes"));
193         stopCriteria.setFeatureIncludes(commandLine.getMultipleSwitch("feature-stop-includes"));
194         stopCriteria.setFeatureExcludes(commandLine.getMultipleSwitch("feature-stop-excludes"));
195
196         TransitiveClosure selector = new TransitiveClosure(startCriteria, stopCriteria);
197
198         try {
199             if (commandLine.isPresent("maximum-inbound-depth")) {
200                 selector.setMaximumInboundDepth(Long.parseLong(commandLine.getSingleSwitch("maximum-inbound-depth")));
201             }
202         } catch (NumberFormatException JavaDoc ex) {
203             selector.setMaximumInboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
204         }
205
206         try {
207             if (commandLine.isPresent("maximum-outbound-depth")) {
208                 selector.setMaximumOutboundDepth(Long.parseLong(commandLine.getSingleSwitch("maximum-outbound-depth")));
209             }
210         } catch (NumberFormatException JavaDoc ex) {
211             selector.setMaximumOutboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
212         }
213
214         Logger.getLogger(DependencyClosure.class).info("Operating on " + factory.getPackages().values().size() + " package(s) ...");
215
216         selector.traverseNodes(factory.getPackages().values());
217
218         Logger.getLogger(DependencyClosure.class).info("Reporting " + selector.getFactory().getPackages().values().size() + " package(s) ...");
219     
220         verboseListener.print("Printing the graph ...");
221
222         PrintWriter out;
223         if (commandLine.isPresent("out")) {
224             out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out")));
225         } else {
226             out = new PrintWriter(System.out);
227         }
228
229         Printer printer;
230         if (commandLine.isPresent("xml")) {
231             printer = new XMLPrinter(out, commandLine.getSingleSwitch("encoding"), commandLine.getSingleSwitch("dtd-prefix"));
232         } else {
233             printer = new TextPrinter(out);
234         }
235
236         if (commandLine.isPresent("indent-text")) {
237             printer.setIndentText(commandLine.getSingleSwitch("indent-text"));
238         }
239
240         printer.traverseNodes(selector.getFactory().getPackages().values());
241
242         out.close();
243
244         Date end = new Date();
245
246         if (commandLine.getToggleSwitch("time")) {
247             System.err.println(DependencyClosure.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs.");
248         }
249
250         verboseListener.close();
251     }
252 }
253
Popular Tags