KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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