KickJava   Java API By Example, From Geeks To Geeks.

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


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

139
140         Date start = new Date();
141
142         // Collecting data, first classfiles from JARs,
143
// then package/class trees using NodeFactory.
144

145         Validator oldValidator = new ListBasedValidator(commandLine.getSingleSwitch("old-documentation"));
146         ClassfileLoader oldJar = new AggregatingClassfileLoader();
147         oldJar.addLoadListener(verboseListener);
148         oldJar.load(commandLine.getMultipleSwitch("old"));
149
150         Validator newValidator = new ListBasedValidator(commandLine.getSingleSwitch("new-documentation"));
151         ClassfileLoader newJar = new AggregatingClassfileLoader();
152         newJar.addLoadListener(verboseListener);
153         newJar.load(commandLine.getMultipleSwitch("new"));
154
155         // Starting to compare, first at package level,
156
// then descending to class level for packages
157
// that are in both the old and the new codebase.
158

159         Logger.getLogger(JarJarDiff.class).info("Comparing ...");
160         verboseListener.print("Comparing ...");
161
162         String JavaDoc name = commandLine.getSingleSwitch("name");
163         String JavaDoc oldLabel = commandLine.isPresent("old-label") ? commandLine.getSingleSwitch("old-label") : commandLine.getSwitch("old").toString();
164         String JavaDoc newLabel = commandLine.isPresent("new-label") ? commandLine.getSingleSwitch("new-label") : commandLine.getSwitch("new").toString();
165
166         DifferencesFactory factory = new DifferencesFactory(oldValidator, newValidator);
167         Differences differences = factory.createJarDifferences(name, oldLabel, oldJar, newLabel, newJar);
168
169         Logger.getLogger(JarJarDiff.class).info("Printing results ...");
170         verboseListener.print("Printing results ...");
171
172         PrintWriter out;
173         if (commandLine.isPresent("out")) {
174             out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out")));
175         } else {
176             out = new PrintWriter(new OutputStreamWriter(System.out));
177         }
178
179         com.jeantessier.diff.Printer printer = new Report(commandLine.getSingleSwitch("encoding"), commandLine.getSingleSwitch("dtd-prefix"));
180         if (commandLine.isPresent("indent-text")) {
181             printer.setIndentText(commandLine.getSingleSwitch("indent-text"));
182         }
183
184         differences.accept(printer);
185         out.print(printer);
186
187         Date end = new Date();
188
189         if (commandLine.getToggleSwitch("time")) {
190             System.err.println(JarJarDiff.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs.");
191         }
192
193         out.close();
194
195         verboseListener.close();
196     }
197 }
198
Popular Tags