KickJava   Java API By Example, From Geeks To Geeks.

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


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 ClassFinder {
45     public static final String JavaDoc DEFAULT_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("If no files are specified, it processes the current directory.");
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();
83         commandLine.addMultipleValuesSwitch("includes", DEFAULT_INCLUDES);
84         commandLine.addMultipleValuesSwitch("excludes");
85         commandLine.addToggleSwitch("time");
86         commandLine.addSingleValueSwitch("out");
87         commandLine.addToggleSwitch("help");
88         commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE);
89         commandLine.addToggleSwitch("version");
90
91         CommandLineUsage usage = new CommandLineUsage("ClassFinder");
92         commandLine.accept(usage);
93
94         try {
95             commandLine.parse(args);
96         } catch (IllegalArgumentException JavaDoc ex) {
97             showError(usage, ex.toString());
98             System.exit(1);
99         } catch (CommandLineException ex) {
100             showError(usage, ex.toString());
101             System.exit(1);
102         }
103
104         if (commandLine.getToggleSwitch("help")) {
105             showError(usage);
106         }
107         
108         if (commandLine.getToggleSwitch("version")) {
109             showVersion();
110         }
111
112         if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) {
113             System.exit(1);
114         }
115
116         VerboseListener verboseListener = new VerboseListener();
117         if (commandLine.isPresent("verbose")) {
118             if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) {
119                 verboseListener.setWriter(System.out);
120             } else {
121                 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose")));
122             }
123         }
124
125         /*
126          * Beginning of main processing
127          */

128
129         Date start = new Date();
130
131         PrintWriter out;
132         if (commandLine.isPresent("out")) {
133             out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out")));
134         } else {
135             out = new PrintWriter(new OutputStreamWriter(System.out));
136         }
137
138         List parameters = commandLine.getParameters();
139         if (parameters.size() == 0) {
140             parameters.add(".");
141         }
142
143         ClassMatcher matcher = new ClassMatcher(commandLine.getMultipleSwitch("includes"), commandLine.getMultipleSwitch("excludes"));
144         
145         ClassfileLoader loader = new TransientClassfileLoader();
146         loader.addLoadListener(matcher);
147         loader.addLoadListener(verboseListener);
148         loader.load(parameters);
149
150         Iterator i = matcher.getResults().entrySet().iterator();
151         while (i.hasNext()) {
152             Map.Entry entry = (Map.Entry) i.next();
153             out.print(entry.getKey());
154             out.print(": ");
155
156             Iterator j = ((List) entry.getValue()).iterator();
157             while (j.hasNext()) {
158                 out.print(j.next());
159                 if (j.hasNext()) {
160                     out.print(", ");
161                 }
162             }
163             
164             out.println();
165         }
166         
167         Date end = new Date();
168
169         if (commandLine.getToggleSwitch("time")) {
170             System.err.println(ClassFinder.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs.");
171         }
172
173         out.close();
174
175         verboseListener.close();
176     }
177 }
178
Popular Tags