KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > ant > ListDiff


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.diff.*;
42
43 public class ListDiff extends Task {
44     private String JavaDoc name = "";
45     private File oldFile;
46     private String JavaDoc oldLabel;
47     private File newFile;
48     private String JavaDoc newLabel;
49     private boolean compress = false;
50     private String JavaDoc encoding = ListDiffPrinter.DEFAULT_ENCODING;
51     private String JavaDoc dtdPrefix = ListDiffPrinter.DEFAULT_DTD_PREFIX;
52     private String JavaDoc indentText;
53     private File destfile;
54
55     public String JavaDoc getName() {
56         return name;
57     }
58     
59     public void setName(String JavaDoc name) {
60         this.name = name;
61     }
62     
63     public File getOld() {
64         return oldFile;
65     }
66     
67     public void setOld(File oldFile) {
68         this.oldFile = oldFile;
69     }
70
71     public String JavaDoc getOldlabel() {
72         return oldLabel;
73     }
74     
75     public void setOldlabel(String JavaDoc oldLabel) {
76         this.oldLabel = oldLabel;
77     }
78     
79     public File getNew() {
80         return newFile;
81     }
82     
83     public void setNew(File newFile) {
84         this.newFile = newFile;
85     }
86
87     public String JavaDoc getNewlabel() {
88         return newLabel;
89     }
90     
91     public void setNewlabel(String JavaDoc newLabel) {
92         this.newLabel = newLabel;
93     }
94
95     public boolean getCompress() {
96         return compress;
97     }
98
99     public void setCompress(boolean compress) {
100         this.compress = compress;
101     }
102     
103     public String JavaDoc getEncoding() {
104         return dtdPrefix;
105     }
106     
107     public void setEncoding(String JavaDoc encoding) {
108         this.encoding = encoding;
109     }
110     
111     public String JavaDoc getDtdprefix() {
112         return dtdPrefix;
113     }
114     
115     public void setDtdprefix(String JavaDoc dtdPrefix) {
116         this.dtdPrefix = dtdPrefix;
117     }
118
119     public String JavaDoc getIndenttext() {
120         return indentText;
121     }
122     
123     public void setIntenttext(String JavaDoc indentText) {
124         this.indentText = indentText;
125     }
126
127     public File getDestfile() {
128         return destfile;
129     }
130     
131     public void setDestfile(File destfile) {
132         this.destfile = destfile;
133     }
134     
135     public void execute() throws BuildException {
136         // first off, make sure that we've got what we need
137

138         if (getOld() == null) {
139             throw new BuildException("old must be set!");
140         }
141         
142         if (!getOld().exists()) {
143             throw new BuildException("old does not exist!");
144         }
145         
146         if (!getOld().isFile()) {
147             throw new BuildException("old is not a file!");
148         }
149
150         if (getNew() == null) {
151             throw new BuildException("new must be set!");
152         }
153         
154         if (!getNew().exists()) {
155             throw new BuildException("new does not exist!");
156         }
157         
158         if (!getNew().isFile()) {
159             throw new BuildException("new is not a file!");
160         }
161
162         if (getDestfile() == null) {
163             throw new BuildException("destfile must be set!");
164         }
165
166         VerboseListener verboseListener = new VerboseListener(this);
167
168         try {
169             String JavaDoc line;
170             
171             log("Loading old list from " + getOld().getAbsolutePath());
172             Collection oldAPI = new TreeSet();
173             BufferedReader oldIn = new BufferedReader(new FileReader(getOld()));
174             while((line = oldIn.readLine()) != null) {
175                 oldAPI.add(line);
176             }
177             oldIn.close();
178             
179             log("Loading new list from " + getNew().getAbsolutePath());
180             Collection newAPI = new TreeSet();
181             BufferedReader newIn = new BufferedReader(new FileReader(getNew()));
182             while((line = newIn.readLine()) != null) {
183                 newAPI.add(line);
184             }
185             newIn.close();
186             
187             log("Comparing old and new lists ...");
188
189             ListDiffPrinter printer = new ListDiffPrinter(getCompress(), getEncoding(), getDtdprefix());
190             printer.setName(getName());
191             printer.setOldVersion(getOldlabel());
192             printer.setNewVersion(getNewlabel());
193             if (getIndenttext() != null) {
194                 printer.setIndentText(getIndenttext());
195             }
196             
197             Iterator i;
198             
199             i = oldAPI.iterator();
200             while (i.hasNext()) {
201                 line = (String JavaDoc) i.next();
202                 if (!newAPI.contains(line)) {
203                     printer.remove(line);
204                 }
205             }
206             
207             i = newAPI.iterator();
208             while (i.hasNext()) {
209                 line = (String JavaDoc) i.next();
210                 if (!oldAPI.contains(line)) {
211                     printer.add(line);
212                 }
213             }
214
215             log("Saving difference report to " + getDestfile().getAbsolutePath());
216
217             PrintWriter out = new PrintWriter(new FileWriter(getDestfile()));
218             out.print(printer);
219             out.close();
220         } catch (IOException ex) {
221             throw new BuildException(ex);
222         }
223     }
224 }
225
Popular Tags