001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependencyfinder.ant;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import org.apache.tools.ant.*;
039:
040: import com.jeantessier.diff.*;
041:
042: public class ListDiff extends Task {
043: private String name = "";
044: private File oldFile;
045: private String oldLabel;
046: private File newFile;
047: private String newLabel;
048: private boolean compress = false;
049: private String encoding = ListDiffPrinter.DEFAULT_ENCODING;
050: private String dtdPrefix = ListDiffPrinter.DEFAULT_DTD_PREFIX;
051: private String indentText;
052: private File destfile;
053:
054: public String getName() {
055: return name;
056: }
057:
058: public void setName(String name) {
059: this .name = name;
060: }
061:
062: public File getOld() {
063: return oldFile;
064: }
065:
066: public void setOld(File oldFile) {
067: this .oldFile = oldFile;
068: }
069:
070: public String getOldlabel() {
071: return oldLabel;
072: }
073:
074: public void setOldlabel(String oldLabel) {
075: this .oldLabel = oldLabel;
076: }
077:
078: public File getNew() {
079: return newFile;
080: }
081:
082: public void setNew(File newFile) {
083: this .newFile = newFile;
084: }
085:
086: public String getNewlabel() {
087: return newLabel;
088: }
089:
090: public void setNewlabel(String newLabel) {
091: this .newLabel = newLabel;
092: }
093:
094: public boolean getCompress() {
095: return compress;
096: }
097:
098: public void setCompress(boolean compress) {
099: this .compress = compress;
100: }
101:
102: public String getEncoding() {
103: return encoding;
104: }
105:
106: public void setEncoding(String encoding) {
107: this .encoding = encoding;
108: }
109:
110: public String getDtdprefix() {
111: return dtdPrefix;
112: }
113:
114: public void setDtdprefix(String dtdPrefix) {
115: this .dtdPrefix = dtdPrefix;
116: }
117:
118: public String getIndenttext() {
119: return indentText;
120: }
121:
122: public void setIntenttext(String indentText) {
123: this .indentText = indentText;
124: }
125:
126: public File getDestfile() {
127: return destfile;
128: }
129:
130: public void setDestfile(File destfile) {
131: this .destfile = destfile;
132: }
133:
134: public void execute() throws BuildException {
135: // first off, make sure that we've got what we need
136:
137: if (getOld() == null) {
138: throw new BuildException("old must be set!");
139: }
140:
141: if (!getOld().exists()) {
142: throw new BuildException("old does not exist!");
143: }
144:
145: if (!getOld().isFile()) {
146: throw new BuildException("old is not a file!");
147: }
148:
149: if (getNew() == null) {
150: throw new BuildException("new must be set!");
151: }
152:
153: if (!getNew().exists()) {
154: throw new BuildException("new does not exist!");
155: }
156:
157: if (!getNew().isFile()) {
158: throw new BuildException("new is not a file!");
159: }
160:
161: if (getDestfile() == null) {
162: throw new BuildException("destfile must be set!");
163: }
164:
165: try {
166: String line;
167:
168: log("Loading old list from " + getOld().getAbsolutePath());
169: Collection<String> oldAPI = new TreeSet<String>();
170: BufferedReader oldIn = new BufferedReader(new FileReader(
171: getOld()));
172: while ((line = oldIn.readLine()) != null) {
173: oldAPI.add(line);
174: }
175: oldIn.close();
176:
177: log("Loading new list from " + getNew().getAbsolutePath());
178: Collection<String> newAPI = new TreeSet<String>();
179: BufferedReader newIn = new BufferedReader(new FileReader(
180: getNew()));
181: while ((line = newIn.readLine()) != null) {
182: newAPI.add(line);
183: }
184: newIn.close();
185:
186: log("Comparing old and new lists ...");
187:
188: ListDiffPrinter printer = new ListDiffPrinter(
189: 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: for (String name : oldAPI) {
198: if (!newAPI.contains(name)) {
199: printer.remove(name);
200: }
201: }
202:
203: for (String name : newAPI) {
204: if (!oldAPI.contains(name)) {
205: printer.add(name);
206: }
207: }
208:
209: log("Saving difference report to "
210: + getDestfile().getAbsolutePath());
211:
212: PrintWriter out = new PrintWriter(new FileWriter(
213: getDestfile()));
214: out.print(printer);
215: out.close();
216: } catch (IOException ex) {
217: throw new BuildException(ex);
218: }
219: }
220: }
|