KickJava   Java API By Example, From Geeks To Geeks.

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


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.dependencyfinder.*;
43
44 public class ListSymbols {
45     public static final String JavaDoc DEFAULT_LOGFILE = "System.out";
46
47     public static void showError(CommandLineUsage clu, String JavaDoc msg) {
48         System.err.println(msg);
49         showError(clu);
50     }
51
52     public static void showError(CommandLineUsage clu) {
53         System.err.println(clu);
54         System.err.println();
55         System.err.println("If no files are specified, it processes the current directory.");
56         System.err.println();
57     }
58
59     public static void showVersion() throws IOException {
60         Version version = new Version();
61         
62         System.err.print(version.getImplementationTitle());
63         System.err.print(" ");
64         System.err.print(version.getImplementationVersion());
65         System.err.print(" (c) ");
66         System.err.print(version.getCopyrightDate());
67         System.err.print(" ");
68         System.err.print(version.getCopyrightHolder());
69         System.err.println();
70         
71         System.err.print(version.getImplementationURL());
72         System.err.println();
73         
74         System.err.print("Compiled on ");
75         System.err.print(version.getImplementationDate());
76         System.err.println();
77     }
78
79     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
80         // Parsing the command line
81
CommandLine commandLine = new CommandLine();
82         commandLine.addToggleSwitch("class-names");
83         commandLine.addToggleSwitch("field-names");
84         commandLine.addToggleSwitch("method-names");
85         commandLine.addToggleSwitch("local-names");
86         commandLine.addToggleSwitch("time");
87         commandLine.addSingleValueSwitch("out");
88         commandLine.addToggleSwitch("help");
89         commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE);
90         commandLine.addToggleSwitch("version");
91
92         CommandLineUsage usage = new CommandLineUsage("ListSymbols");
93         commandLine.accept(usage);
94
95         try {
96             commandLine.parse(args);
97         } catch (IllegalArgumentException JavaDoc ex) {
98             showError(usage, ex.toString());
99             System.exit(1);
100         } catch (CommandLineException ex) {
101             showError(usage, ex.toString());
102             System.exit(1);
103         }
104
105         if (commandLine.getToggleSwitch("help")) {
106             showError(usage);
107         }
108         
109         if (commandLine.getToggleSwitch("version")) {
110             showVersion();
111         }
112
113         if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) {
114             System.exit(1);
115         }
116
117         VerboseListener verboseListener = new VerboseListener();
118         if (commandLine.isPresent("verbose")) {
119             if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) {
120                 verboseListener.setWriter(System.out);
121             } else {
122                 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose")));
123             }
124         }
125
126         /*
127          * Beginning of main processing
128          */

129
130         Date start = new Date();
131
132         List parameters = commandLine.getParameters();
133         if (parameters.size() == 0) {
134             parameters.add(".");
135         }
136
137         SymbolGatherer collector = new SymbolGatherer();
138
139         // Since SymbolGatherer lists everything by default,
140
// we turn them all off if any of the switches are
141
// present. This way, if you pass nothing, you get
142
// the default behavior and the tool shows everything.
143
// If you pass in one or more, you only see symbols
144
// of the kind(s) you specified.
145
if (commandLine.isPresent("class-names") || commandLine.isPresent("field-names") || commandLine.isPresent("method-names") || commandLine.isPresent("local-names")) {
146             collector.setCollectingClassNames(false);
147             collector.setCollectingFieldNames(false);
148             collector.setCollectingMethodNames(false);
149             collector.setCollectingLocalNames(false);
150         }
151
152         if (commandLine.isPresent("class-names")) {
153             collector.setCollectingClassNames(true);
154         }
155
156         if (commandLine.isPresent("field-names")) {
157             collector.setCollectingFieldNames(true);
158         }
159
160         if (commandLine.isPresent("method-names")) {
161             collector.setCollectingMethodNames(true);
162         }
163
164         if (commandLine.isPresent("local-names")) {
165             collector.setCollectingLocalNames(true);
166         }
167         
168         ClassfileLoader loader = new TransientClassfileLoader();
169         loader.addLoadListener(new LoadListenerVisitorAdapter(collector));
170         loader.addLoadListener(verboseListener);
171         loader.load(parameters);
172
173         PrintWriter out;
174         if (commandLine.isPresent("out")) {
175             out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out")));
176         } else {
177             out = new PrintWriter(new OutputStreamWriter(System.out));
178         }
179
180         Iterator i = collector.getCollection().iterator();
181         while (i.hasNext()) {
182             out.println(i.next());
183         }
184
185         Date end = new Date();
186
187         if (commandLine.getToggleSwitch("time")) {
188             System.err.println(ListSymbols.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs.");
189         }
190
191         out.close();
192
193         verboseListener.close();
194     }
195 }
196
Popular Tags