1 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 ; 57 import java.util.ArrayList ; 58 import java.util.Date ; 59 import java.util.StringTokenizer ; 60 61 66 67 public class Compare { 68 private String referenceDirectory, testDirectory; 69 private String [] filenameList; 70 private String 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 public void setTestDirectory(String testDirectory) { 78 if (!(testDirectory.endsWith("/") | testDirectory.endsWith("\\"))) { 79 testDirectory += File.separator; 80 } 81 this.testDirectory = testDirectory; 82 } 83 84 public void setReferenceDirectory(String referenceDirectory) { 86 if (!(referenceDirectory.endsWith("/") 87 | referenceDirectory.endsWith("\\"))) { 88 referenceDirectory += File.separator; 89 } 90 this.referenceDirectory = referenceDirectory; 91 } 92 93 public void setFilenames(String filenames) { 94 StringTokenizer tokens = new StringTokenizer (filenames, ","); 95 ArrayList filenameListTmp = new ArrayList (20); 96 while (tokens.hasMoreTokens()) { 97 filenameListTmp.add(tokens.nextToken()); 98 } 99 filenameList = new String [filenameListTmp.size()]; 100 filenameList = (String [])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 } 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 dateTime = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 153 DateFormat.MEDIUM).format(new Date ()); 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 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 } 214 } 215 216 | Popular Tags |