1 17 18 19 20 package org.apache.fop.tools.anttasks; 21 22 import java.util.Date ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 25 import java.io.BufferedInputStream ; 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 30 import org.apache.tools.ant.BuildException; 31 import java.text.DateFormat ; 32 33 38 39 public class FileCompare { 40 41 private String referenceDirectory, testDirectory; 42 private String [] filenameList; 43 private String filenames; 44 45 49 public void setTestDirectory(String testDirectory) { 50 if (!(testDirectory.endsWith("/") | testDirectory.endsWith("\\"))) { 51 testDirectory += File.separator; 52 } 53 this.testDirectory = testDirectory; 54 } 55 56 60 public void setReferenceDirectory(String referenceDirectory) { 61 if (!(referenceDirectory.endsWith("/") 62 | referenceDirectory.endsWith("\\"))) { 63 referenceDirectory += File.separator; 64 } 65 this.referenceDirectory = referenceDirectory; 66 } 67 68 72 public void setFilenames(String filenames) { 73 StringTokenizer tokens = new StringTokenizer (filenames, ","); 74 List filenameListTmp = new java.util.ArrayList (20); 75 while (tokens.hasMoreTokens()) { 76 filenameListTmp.add(tokens.nextToken()); 77 } 78 filenameList = new String [filenameListTmp.size()]; 79 filenameList = (String [])filenameListTmp.toArray(new String [0]); 80 } 81 82 88 public static boolean compareFiles(File f1, File f2) throws IOException { 89 return (compareFileSize(f1, f2) && compareBytes(f1, f2)); 90 } 91 92 96 private static boolean compareBytes(File file1, File file2) throws IOException { 97 BufferedInputStream file1Input = 98 new BufferedInputStream (new java.io.FileInputStream (file1)); 99 BufferedInputStream file2Input = 100 new BufferedInputStream (new java.io.FileInputStream (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 121 private static boolean compareFileSize(File oldFile, File newFile) { 122 if (oldFile.length() != newFile.length()) { 123 return false; 124 } else { 125 return true; 126 } 127 } 129 private boolean filesExist(File oldFile, File 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 results) { 145 String dateTime = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 146 DateFormat.MEDIUM).format(new Date ()); 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 161 public void execute() throws BuildException { 162 boolean identical = false; 163 File oldFile; 164 File newFile; 165 try { 166 PrintWriter results = 167 new PrintWriter (new java.io.FileWriter ("results.html"), true); 168 this.writeHeader(results); 169 for (int i = 0; i < filenameList.length; i++) { 170 oldFile = new File (referenceDirectory + filenameList[i]); 171 newFile = new File (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 ioe) { 206 System.err.println("ERROR: " + ioe); 207 } 208 } 210 } 211 212 | Popular Tags |