KickJava   Java API By Example, From Geeks To Geeks.

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


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.classreader.*;
41 import com.jeantessier.commandline.*;
42 import com.jeantessier.dependency.*;
43 import com.jeantessier.dependencyfinder.*;
44
45 public class DependencyExtractor {
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("If no files are specified, it processes the current directory.");
57         System.err.println();
58         System.err.println("If file is a directory, it is recusively scanned for files");
59         System.err.println("ending in \".class\".");
60         System.err.println();
61         System.err.println("Defaults is text output to the console.");
62         System.err.println();
63     }
64
65     public static void showVersion() throws IOException {
66         Version version = new Version();
67         
68         System.err.print(version.getImplementationTitle());
69         System.err.print(" ");
70         System.err.print(version.getImplementationVersion());
71         System.err.print(" (c) ");
72         System.err.print(version.getCopyrightDate());
73         System.err.print(" ");
74         System.err.print(version.getCopyrightHolder());
75         System.err.println();
76         
77         System.err.print(version.getImplementationURL());
78         System.err.println();
79         
80         System.err.print("Compiled on ");
81         System.err.print(version.getImplementationDate());
82         System.err.println();
83     }
84
85     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
86         // Parsing the command line
87
CommandLine commandLine = new CommandLine();
88         commandLine.addToggleSwitch("xml");
89         commandLine.addToggleSwitch("maximize");
90         commandLine.addToggleSwitch("minimize");
91         commandLine.addSingleValueSwitch("encoding", com.jeantessier.dependency.XMLPrinter.DEFAULT_ENCODING);
92         commandLine.addSingleValueSwitch("dtd-prefix", com.jeantessier.dependency.XMLPrinter.DEFAULT_DTD_PREFIX);
93         commandLine.addSingleValueSwitch("indent-text");
94         commandLine.addToggleSwitch("time");
95         commandLine.addSingleValueSwitch("out");
96         commandLine.addToggleSwitch("help");
97         commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE);
98         commandLine.addToggleSwitch("version");
99
100         CommandLineUsage usage = new CommandLineUsage("DependencyExtractor");
101         commandLine.accept(usage);
102
103         try {
104             commandLine.parse(args);
105         } catch (IllegalArgumentException JavaDoc ex) {
106             showError(usage, ex.toString());
107             System.exit(1);
108         } catch (CommandLineException ex) {
109             showError(usage, ex.toString());
110             System.exit(1);
111         }
112
113         if (commandLine.getToggleSwitch("help")) {
114             showError(usage);
115         }
116         
117         if (commandLine.getToggleSwitch("version")) {
118             showVersion();
119         }
120
121         if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) {
122             System.exit(1);
123         }
124
125         VerboseListener verboseListener = new VerboseListener();
126         if (commandLine.isPresent("verbose")) {
127             if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) {
128                 verboseListener.setWriter(System.out);
129             } else {
130                 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose")));
131             }
132         }
133
134         /*
135          * Beginning of main processing
136          */

137
138         Date start = new Date();
139
140         List parameters = commandLine.getParameters();
141         if (parameters.size() == 0) {
142             parameters.add(".");
143         }
144
145         NodeFactory factory = new NodeFactory();
146         CodeDependencyCollector collector = new CodeDependencyCollector(factory);
147         
148         ClassfileLoader loader = new TransientClassfileLoader();
149         loader.addLoadListener(new LoadListenerVisitorAdapter(collector));
150         loader.addLoadListener(verboseListener);
151         loader.load(parameters);
152
153         if (commandLine.isPresent("minimize")) {
154             LinkMinimizer minimizer = new LinkMinimizer();
155             minimizer.traverseNodes(factory.getPackages().values());
156         } else if (commandLine.isPresent("maximize")) {
157             LinkMaximizer maximizer = new LinkMaximizer();
158             maximizer.traverseNodes(factory.getPackages().values());
159         }
160
161         verboseListener.print("Printing the graph ...");
162
163         PrintWriter out;
164         if (commandLine.isPresent("out")) {
165             out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out")));
166         } else {
167             out = new PrintWriter(System.out);
168         }
169             
170         com.jeantessier.dependency.Printer printer;
171         if (commandLine.getToggleSwitch("xml")) {
172             printer = new com.jeantessier.dependency.XMLPrinter(out, commandLine.getSingleSwitch("encoding"), commandLine.getSingleSwitch("dtd-prefix"));
173         } else {
174             printer = new com.jeantessier.dependency.TextPrinter(out);
175         }
176             
177         if (commandLine.isPresent("indent-text")) {
178             printer.setIndentText(commandLine.getSingleSwitch("indent-text"));
179         }
180
181         printer.traverseNodes(factory.getPackages().values());
182
183         out.close();
184
185         Date end = new Date();
186
187         if (commandLine.getToggleSwitch("time")) {
188             System.err.println(DependencyExtractor.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs.");
189         }
190
191         verboseListener.close();
192     }
193 }
194
Popular Tags