KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > tools > anttasks > Compare


1 /*
2  * $Id: Compare.java,v 1.2.2.3 2003/02/25 15:19:58 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.tools.anttasks;
52
53 import org.apache.tools.ant.BuildException;
54
55 import java.io.*;
56 import java.text.DateFormat JavaDoc;
57 import java.util.ArrayList JavaDoc;
58 import java.util.Date JavaDoc;
59 import java.util.StringTokenizer JavaDoc;
60
61 /**
62  * This class is an extension of Ant, a script utility from
63  * jakarta.apache.org.
64  * It provides methods to compare two files
65  */

66
67 public class Compare {
68     private String JavaDoc referenceDirectory, testDirectory;
69     private String JavaDoc[] filenameList;
70     private String JavaDoc filenames;
71     private static boolean IDENTICAL_FILES = true;
72     private static boolean NOTIDENTICAL_FILES = false;
73     private BufferedInputStream oldfileInput;
74     private BufferedInputStream newfileInput;
75
76     // sets directory for test files
77
public void setTestDirectory(String JavaDoc testDirectory) {
78         if (!(testDirectory.endsWith("/") | testDirectory.endsWith("\\"))) {
79             testDirectory += File.separator;
80         }
81         this.testDirectory = testDirectory;
82     }
83
84     // sets directory for reference files
85
public void setReferenceDirectory(String JavaDoc referenceDirectory) {
86         if (!(referenceDirectory.endsWith("/")
87                 | referenceDirectory.endsWith("\\"))) {
88             referenceDirectory += File.separator;
89         }
90         this.referenceDirectory = referenceDirectory;
91     }
92
93     public void setFilenames(String JavaDoc filenames) {
94         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(filenames, ",");
95         ArrayList JavaDoc filenameListTmp = new ArrayList JavaDoc(20);
96         while (tokens.hasMoreTokens()) {
97             filenameListTmp.add(tokens.nextToken());
98         }
99         filenameList = new String JavaDoc[filenameListTmp.size()];
100         filenameList = (String JavaDoc[])filenameListTmp.toArray(filenameList);
101     }
102
103     private boolean compareBytes(File oldFile, File newFile) {
104         try {
105             oldfileInput =
106                 new BufferedInputStream(new FileInputStream(oldFile));
107             newfileInput =
108                 new BufferedInputStream(new FileInputStream(newFile));
109             int charactO = 0;
110             int charactN = 0;
111             boolean identical = true;
112
113             while (identical & (charactO != -1)) {
114                 if (charactO == charactN) {
115                     charactO = oldfileInput.read();
116                     charactN = newfileInput.read();
117                 } else {
118                     return NOTIDENTICAL_FILES;
119                 }
120             }
121             return IDENTICAL_FILES;
122         } catch (IOException io) {
123             System.err.println("Task Compare - Error: \n" + io.toString());
124         }
125         return NOTIDENTICAL_FILES;
126     }
127
128     private boolean compareFileSize(File oldFile, File newFile) {
129         if (oldFile.length() != newFile.length()) {
130             return NOTIDENTICAL_FILES;
131         } else {
132             return IDENTICAL_FILES;
133         }
134     } // end: compareBytes
135

136     private boolean filesExist(File oldFile, File newFile) {
137         if (!oldFile.exists()) {
138             System.err.println("Task Compare - ERROR: File "
139                                + referenceDirectory + oldFile.getName()
140                                + " doesn't exist!");
141             return false;
142         } else if (!newFile.exists()) {
143             System.err.println("Task Compare - ERROR: File " + testDirectory
144                                + newFile.getName() + " doesn't exist!");
145             return false;
146         } else {
147             return true;
148         }
149     }
150
151     public void writeHeader(PrintWriter results) {
152         String JavaDoc dateTime = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
153                 DateFormat.MEDIUM).format(new Date JavaDoc());
154         results.println("<html><head><title>Test Results</title></head><body>\n");
155         results.println("<h2>Compare Results<br>");
156         results.println("<font size='1'>created " + dateTime
157                         + "</font></h2>");
158         results.println("<table cellpadding='10' border='2'><thead><th align='center'>reference file</th><th align='center'>test file</th>"
159                         + "<th align='center'>identical?</th></thead>");
160
161
162     }
163
164     // main method of task compare
165
public void execute() throws BuildException {
166         boolean identical = false;
167         File oldFile;
168         File newFile;
169         try {
170             PrintWriter results =
171                 new PrintWriter(new FileWriter("results.html"), true);
172             this.writeHeader(results);
173             for (int i = 0; i < filenameList.length; i++) {
174                 oldFile = new File(referenceDirectory + filenameList[i]);
175                 newFile = new File(testDirectory + filenameList[i]);
176                 if (filesExist(oldFile, newFile)) {
177                     identical = compareFileSize(oldFile, newFile);
178                     if (identical) {
179                         identical = compareBytes(oldFile, newFile);
180                     }
181                     if (!identical) {
182                         System.out.println("Task Compare: \nFiles "
183                                            + referenceDirectory
184                                            + oldFile.getName() + " - "
185                                            + testDirectory
186                                            + newFile.getName()
187                                            + " are *not* identical.");
188                         results.println("<tr><td><a HREF='"
189                                         + referenceDirectory
190                                         + oldFile.getName() + "'>"
191                                         + oldFile.getName()
192                                         + "</a> </td><td> <a HREF='"
193                                         + testDirectory + newFile.getName()
194                                         + "'>" + newFile.getName() + "</a>"
195                                         + " </td><td><font color='red'>No</font></td></tr>");
196                     } else {
197                         results.println("<tr><td><a HREF='"
198                                         + referenceDirectory
199                                         + oldFile.getName() + "'>"
200                                         + oldFile.getName()
201                                         + "</a> </td><td> <a HREF='"
202                                         + testDirectory + newFile.getName()
203                                         + "'>" + newFile.getName() + "</a>"
204                                         + " </td><td>Yes</td></tr>");
205                     }
206                 }
207             }
208             results.println("</table></html>");
209         } catch (IOException ioe) {
210             System.err.println("ERROR: " + ioe);
211         }
212     } // end: execute()
213

214 }
215
216
Popular Tags