KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > ant > 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.ant;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.tools.ant.*;
39 import org.apache.tools.ant.types.*;
40
41 import com.jeantessier.classreader.*;
42 import com.jeantessier.dependency.*;
43 import com.jeantessier.diff.*;
44
45 public class JarJarDiff extends Task {
46     private String JavaDoc name = "";
47     private Path oldPath;
48     private String JavaDoc oldLabel;
49     private File oldDocumentation = new File("old_documentation.txt");
50     private Path newPath;
51     private String JavaDoc newLabel;
52     private File newDocumentation = new File("new_documentation.txt");
53     private String JavaDoc encoding = Report.DEFAULT_ENCODING;
54     private String JavaDoc dtdPrefix = Report.DEFAULT_DTD_PREFIX;
55     private String JavaDoc indentText;
56     private File destfile;
57
58     public String JavaDoc getName() {
59         return name;
60     }
61     
62     public void setName(String JavaDoc name) {
63         this.name = name;
64     }
65     
66     public Path createOld() {
67         if (oldPath == null) {
68             oldPath = new Path(getProject());
69         }
70
71         return oldPath;
72     }
73     
74     public Path getOld() {
75         return oldPath;
76     }
77
78     public String JavaDoc getOldlabel() {
79         return oldLabel;
80     }
81     
82     public void setOldlabel(String JavaDoc oldLabel) {
83         this.oldLabel = oldLabel;
84     }
85
86     public File getOlddocumentation() {
87         return oldDocumentation;
88     }
89     
90     public void setOlddocumentation(File oldDocumentation) {
91         this.oldDocumentation = oldDocumentation;
92     }
93     
94     public Path createNew() {
95         if (newPath == null) {
96             newPath = new Path(getProject());
97         }
98
99         return newPath;
100     }
101     
102     public Path getNew() {
103         return newPath;
104     }
105
106     public String JavaDoc getNewlabel() {
107         return newLabel;
108     }
109     
110     public void setNewlabel(String JavaDoc newLabel) {
111         this.newLabel = newLabel;
112     }
113
114     public File getNewdocumentation() {
115         return newDocumentation;
116     }
117     
118     public void setNewdocumentation(File newDocumentation) {
119         this.newDocumentation = newDocumentation;
120     }
121
122     public String JavaDoc getEncoding() {
123         return encoding;
124     }
125     
126     public void setEncoding(String JavaDoc encoding) {
127         this.encoding = encoding;
128     }
129
130     public String JavaDoc getDtdprefix() {
131         return dtdPrefix;
132     }
133     
134     public void setDtdprefix(String JavaDoc dtdPrefix) {
135         this.dtdPrefix = dtdPrefix;
136     }
137
138     public String JavaDoc getIndenttext() {
139         return indentText;
140     }
141     
142     public void setIntenttext(String JavaDoc indentText) {
143         this.indentText = indentText;
144     }
145
146     public File getDestfile() {
147         return destfile;
148     }
149     
150     public void setDestfile(File destfile) {
151         this.destfile = destfile;
152     }
153     
154     public void execute() throws BuildException {
155         // first off, make sure that we've got what we need
156

157         if (getOld() == null) {
158             throw new BuildException("old must be set!");
159         }
160
161         if (getNew() == null) {
162             throw new BuildException("new must be set!");
163         }
164
165         if (getDestfile() == null) {
166             throw new BuildException("destfile must be set!");
167         }
168
169         VerboseListener verboseListener = new VerboseListener(this);
170
171         try {
172             // Collecting data, first classfiles from JARs,
173
// then package/class trees using NodeFactory.
174

175             log("Loading old classes from path " + getOld());
176             Validator oldValidator = new ListBasedValidator(getOlddocumentation());
177             ClassfileLoader oldJar = new AggregatingClassfileLoader();
178             oldJar.addLoadListener(verboseListener);
179             oldJar.load(Arrays.asList(getOld().list()));
180             
181             log("Loading new classes from path " + getNew());
182             Validator newValidator = new ListBasedValidator(getNewdocumentation());
183             ClassfileLoader newJar = new AggregatingClassfileLoader();
184             newJar.addLoadListener(verboseListener);
185             newJar.load(Arrays.asList(getNew().list()));
186             
187             // Starting to compare, first at package level,
188
// then descending to class level for packages
189
// that are in both the old and the new codebase.
190

191             log("Comparing old and new classes ...");
192             
193             String JavaDoc name = getName();
194             String JavaDoc oldLabel = (getOldlabel() != null) ? getOldlabel() : getOld().toString();
195             String JavaDoc newLabel = (getNewlabel() != null) ? getNewlabel() : getNew().toString();
196             
197             DifferencesFactory factory = new DifferencesFactory(oldValidator, newValidator);
198             Differences differences = factory.createJarDifferences(name, oldLabel, oldJar, newLabel, newJar);
199             
200             log("Saving difference report to " + getDestfile().getAbsolutePath());
201             
202             com.jeantessier.diff.Printer printer = new Report(getEncoding(), getDtdprefix());
203             if (getIndenttext() != null) {
204                 printer.setIndentText(getIndenttext());
205             }
206             
207             differences.accept(printer);
208             
209             PrintWriter out = new PrintWriter(new FileWriter(getDestfile()));
210             out.print(printer);
211             out.close();
212         } catch (IOException ex) {
213             throw new BuildException(ex);
214         }
215     }
216 }
217
Popular Tags