KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: FileCompare.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.tools.anttasks;
21
22 import java.util.Date JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import java.io.BufferedInputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29
30 import org.apache.tools.ant.BuildException;
31 import java.text.DateFormat JavaDoc;
32
33 /**
34  * This class is an extension of Ant, a script utility from
35  * http://ant.apache.org.
36  * It provides methods to compare two files.
37  */

38
39 public class FileCompare {
40     
41     private String JavaDoc referenceDirectory, testDirectory;
42     private String JavaDoc[] filenameList;
43     private String JavaDoc filenames;
44
45     /**
46      * Sets directory for test files.
47      * @param testDirectory the test directory
48      */

49     public void setTestDirectory(String JavaDoc testDirectory) {
50         if (!(testDirectory.endsWith("/") | testDirectory.endsWith("\\"))) {
51             testDirectory += File.separator;
52         }
53         this.testDirectory = testDirectory;
54     }
55
56     /**
57      * Sets directory for reference files.
58      * @param referenceDirectory the reference directory
59      */

60     public void setReferenceDirectory(String JavaDoc referenceDirectory) {
61         if (!(referenceDirectory.endsWith("/")
62                 | referenceDirectory.endsWith("\\"))) {
63             referenceDirectory += File.separator;
64         }
65         this.referenceDirectory = referenceDirectory;
66     }
67
68     /**
69      * Sets the comma-separated list of files to process.
70      * @param filenames list of files, comma-separated
71      */

72     public void setFilenames(String JavaDoc filenames) {
73         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(filenames, ",");
74         List JavaDoc filenameListTmp = new java.util.ArrayList JavaDoc(20);
75         while (tokens.hasMoreTokens()) {
76             filenameListTmp.add(tokens.nextToken());
77         }
78         filenameList = new String JavaDoc[filenameListTmp.size()];
79         filenameList = (String JavaDoc[])filenameListTmp.toArray(new String JavaDoc[0]);
80     }
81
82     /**
83      * Compares two files to see if they are equal
84      * @param f1 first file to compare
85      * @param f2 second file to compare
86      * @return true if files are same, false otherwise
87      */

88     public static boolean compareFiles(File JavaDoc f1, File JavaDoc f2) throws IOException JavaDoc {
89         return (compareFileSize(f1, f2) && compareBytes(f1, f2));
90     }
91
92     /**
93      * Compare the contents of two files.
94      * @param true if files are same byte-by-byte, false otherwise
95      */

96     private static boolean compareBytes(File JavaDoc file1, File JavaDoc file2) throws IOException JavaDoc {
97         BufferedInputStream JavaDoc file1Input =
98             new BufferedInputStream JavaDoc(new java.io.FileInputStream JavaDoc(file1));
99         BufferedInputStream JavaDoc file2Input =
100             new BufferedInputStream JavaDoc(new java.io.FileInputStream JavaDoc(file2));
101
102         int charact1 = 0;
103         int charact2 = 0;
104
105         while (charact1 != -1) {
106             if (charact1 == charact2) {
107                 charact1 = file1Input.read();
108                 charact2 = file2Input.read();
109             } else {
110                 return false;
111             }
112         }
113
114         return true;
115     }
116
117     /**
118      * Does a file size compare of two files
119      * @param true if files are same length, false otherwise
120      */

121     private static boolean compareFileSize(File JavaDoc oldFile, File JavaDoc newFile) {
122         if (oldFile.length() != newFile.length()) {
123             return false;
124         } else {
125             return true;
126         }
127     } // end: compareBytes
128

129     private boolean filesExist(File JavaDoc oldFile, File JavaDoc newFile) {
130         if (!oldFile.exists()) {
131             System.err.println("Task Compare - ERROR: File "
132                                + referenceDirectory + oldFile.getName()
133                                + " doesn't exist!");
134             return false;
135         } else if (!newFile.exists()) {
136             System.err.println("Task Compare - ERROR: File " + testDirectory
137                                + newFile.getName() + " doesn't exist!");
138             return false;
139         } else {
140             return true;
141         }
142     }
143
144     private void writeHeader(PrintWriter JavaDoc results) {
145         String JavaDoc dateTime = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
146                 DateFormat.MEDIUM).format(new Date JavaDoc());
147         results.println("<html><head><title>Test Results</title></head><body>\n");
148         results.println("<h2>Compare Results<br>");
149         results.println("<font size='1'>created " + dateTime
150                         + "</font></h2>");
151         results.println("<table cellpadding='10' border='2'><thead>"
152                         + "<th align='center'>reference file</th>"
153                         + "<th align='center'>test file</th>"
154                         + "<th align='center'>identical?</th></thead>");
155     }
156
157     /**
158      * Main method of task compare
159      * @throws BuildException If the execution fails.
160      */

161     public void execute() throws BuildException {
162         boolean identical = false;
163         File JavaDoc oldFile;
164         File JavaDoc newFile;
165         try {
166             PrintWriter JavaDoc results =
167                 new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc("results.html"), true);
168             this.writeHeader(results);
169             for (int i = 0; i < filenameList.length; i++) {
170                 oldFile = new File JavaDoc(referenceDirectory + filenameList[i]);
171                 newFile = new File JavaDoc(testDirectory + filenameList[i]);
172                 if (filesExist(oldFile, newFile)) {
173                     identical = compareFileSize(oldFile, newFile);
174                     if (identical) {
175                         identical = compareBytes(oldFile, newFile);
176                     }
177                     if (!identical) {
178                         System.out.println("Task Compare: \nFiles "
179                                            + referenceDirectory
180                                            + oldFile.getName() + " - "
181                                            + testDirectory
182                                            + newFile.getName()
183                                            + " are *not* identical.");
184                         results.println("<tr><td><a HREF='"
185                                         + referenceDirectory
186                                         + oldFile.getName() + "'>"
187                                         + oldFile.getName()
188                                         + "</a> </td><td> <a HREF='"
189                                         + testDirectory + newFile.getName()
190                                         + "'>" + newFile.getName() + "</a>"
191                                         + " </td><td><font color='red'>No</font></td></tr>");
192                     } else {
193                         results.println("<tr><td><a HREF='"
194                                         + referenceDirectory
195                                         + oldFile.getName() + "'>"
196                                         + oldFile.getName()
197                                         + "</a> </td><td> <a HREF='"
198                                         + testDirectory + newFile.getName()
199                                         + "'>" + newFile.getName() + "</a>"
200                                         + " </td><td>Yes</td></tr>");
201                     }
202                 }
203             }
204             results.println("</table></html>");
205         } catch (IOException JavaDoc ioe) {
206             System.err.println("ERROR: " + ioe);
207         }
208     } // end: execute()
209

210 }
211
212
Popular Tags