View Javadoc

1   /*
2    * org.osjava.jardiff.ant.JarDiffTask
3    *
4    * $Id: IOThread.java 1952 2005-08-28 18:03:41Z cybertiger $
5    * $URL: https://svn.osjava.org/svn/osjava/trunk/osjava-nio/src/java/org/osjava/nio/IOThread.java $
6    * $Rev: 1952 $
7    * $Date: 2005-08-28 18:03:41 +0000 (Sun, 28 Aug 2005) $
8    * $Author: cybertiger $
9    *
10   * Copyright (c) 2005, Antony Riley
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions are met:
15   *
16   * + Redistributions of source code must retain the above copyright notice,
17   *   this list of conditions and the following disclaimer.
18   *
19   * + Redistributions in binary form must reproduce the above copyright notice,
20   *   this list of conditions and the following disclaimer in the documentation
21   *   and/or other materials provided with the distribution.
22   *
23   * + Neither the name JarDiff nor the names of its contributors may
24   *   be used to endorse or promote products derived from this software without
25   *   specific prior written permission.
26   *
27   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37   * POSSIBILITY OF SUCH DAMAGE.
38   */
39  package org.osjava.jardiff.ant;
40  
41  import org.apache.tools.ant.BuildException;
42  import org.apache.tools.ant.Task;
43  import java.io.File;
44  import java.io.FileOutputStream;
45  import java.io.IOException;
46  import java.net.URL;
47  import java.util.Set;
48  import java.util.HashSet;
49  import javax.xml.transform.ErrorListener;
50  import javax.xml.transform.Transformer;
51  import javax.xml.transform.TransformerConfigurationException;
52  import javax.xml.transform.TransformerException;
53  import javax.xml.transform.TransformerFactory;
54  import javax.xml.transform.sax.SAXTransformerFactory;
55  import javax.xml.transform.sax.TransformerHandler;
56  import javax.xml.transform.stream.StreamResult;
57  import javax.xml.transform.stream.StreamSource;
58  
59  import org.osjava.jardiff.JarDiff;
60  import org.osjava.jardiff.DiffException;
61  import org.osjava.jardiff.StreamDiffHandler;
62  import org.osjava.jardiff.SimpleDiffCriteria;
63  
64  /***
65   * Process two jarfiles generating a public API difference report.
66   * This is useful for keeping track of API changes between versions of a 
67   * project.
68   *
69   * @author <a href="mailto:antony@cyberiantiger.org">Antony Riley</a>
70   */
71  public class JarDiffTask extends Task {
72  
73      /***
74       * The jarfile this diff is from.
75       */
76      private File fromJar = null;
77  
78      /***
79       * The jarfile this diff is to.
80       */
81      private File toJar = null;
82  
83      /***
84       * The file to write the report to.
85       */
86      private File out = null;
87  
88      /***
89       * The name to use for the from version.
90       */
91      private String fromName = null;
92  
93      /***
94       * The name to use for the to verison.
95       */
96      private String toName = null;
97  
98      /***
99       * Force output, even if the existing output is newer than
100      * the jar files.
101      */
102     private boolean force = false;
103 
104     /***
105      * Run the task, generating the jardiff report.
106      *
107      * @throws BuildException When there is an error creating the diff,
108      *                        When there is a problem with the xml parser,
109      *                        When there is a problem with the xslt transformer
110      *                        When the attributes specified are invalid.
111      */
112     public void execute() throws BuildException {
113         try {
114             if(fromJar == null) {
115                 throw new BuildException("no fromjar file specified",
116                         getLocation());
117             }
118             if(toJar == null) {
119                 throw new BuildException("no tojar file specified",
120                         getLocation());
121             }
122             if(out == null) {
123                 throw new BuildException("no out file specified", 
124                         getLocation());
125             }
126             if(fromName == null) {
127                 fromName = fromJar.getName();
128             }
129             if(toName == null) {
130                 toName = toJar.getName();
131             }
132             if(!fromJar.exists() || !fromJar.isFile() || !fromJar.canRead()) {
133                 String msg = "fromjar is not a file, or cannot be read";
134                 throw new BuildException(msg, getLocation());
135             }
136             if(!toJar.exists() || !toJar.isFile() || !toJar.canRead()) {
137                 String msg = "tojar is not a file, or cannot be read";
138                 throw new BuildException(msg, getLocation());
139             }
140             long outModified = out.lastModified();
141             long oldModified = fromJar.lastModified();
142             long newModified = toJar.lastModified();
143             if(force || oldModified > outModified || newModified > outModified)
144             {
145                 log("Writing xml api diff to "+out);
146                 JarDiff jd = new JarDiff();
147                 jd.setOldVersion(fromName);
148                 jd.setNewVersion(toName);
149                 jd.loadOldClasses(fromJar);
150                 jd.loadNewClasses(toJar);
151                 jd.diff(
152                         new StreamDiffHandler(new FileOutputStream(out)),
153                         new SimpleDiffCriteria()
154                        );
155             }
156         } catch (DiffException de) {
157             throw new BuildException(de);
158         } catch (IOException ioe) {
159             throw new BuildException(ioe);
160         }
161     }
162 
163     /***
164      * Set the from jar file.
165      * Required attribute.
166      *
167      * @param fromJar a jar file.
168      */
169     public void setFromjar(File fromJar) {
170         this.fromJar = fromJar;
171     }
172 
173     /***
174      * Set the to jar file.
175      * Required attribute.
176      * @param toJar a jar file.
177      */
178     public void setTojar(File toJar) {
179         this.toJar = toJar;
180     }
181 
182     /***
183      * Set the out file.
184      * Required attribute.
185      *
186      * @param out an output file.
187      */
188     public void setOut(File out) {
189         this.out = out;
190     }
191 
192     /***
193      * Set the from jar visible name.
194      * Optional attribute.
195      * Defaults to the filename of fromjar.
196      *
197      * @param fromName a visible name.
198      */
199     public void setFromname(String fromName) {
200         this.fromName = fromName;
201     }
202 
203     /***
204      * Set the to jar visible name.
205      * Optional attribute.
206      * Defaults to the filename of tojar.
207      *
208      * @param toName a visible name.
209      */
210     public void setToname(String toName) {
211         this.toName = toName;
212     }
213 
214     /***
215      * Force output even if there is an existing diff file which is
216      * newer than the source jar files.
217      * Optional attribute.
218      * Defaults to false.
219      *
220      * @param force true to force output, false otherwise
221      */
222     public void setForce(boolean force) {
223         this.force = force;
224     }
225 }