View Javadoc

1   /*
2    * 
3    * 
4    */
5   package zipdiff;
6   
7   import java.io.File;
8   import java.util.HashSet;
9   import java.util.Set;
10  
11  import zipdiff.output.*;
12  
13  import org.apache.commons.cli.*;
14  
15  /***
16   * 
17   * Provides a command line interface to zipdiff
18   * 
19   * @author Sean C. Sullivan, J.Stewart
20   * 
21   */
22  public class Main {
23      private static final int EXITCODE_ERROR = 2;
24  	private static final int EXITCODE_DIFF = 1;
25      private static final String OPTION_COMPARE_CRC_VALUES = "comparecrcvalues";
26      private static final String OPTION_COMPARE_TIMESTAMPS = "comparetimestamps";
27      private static final String OPTION_IGNORE_CVS_FILES = "ignorecvsfiles";
28      private static final String OPTION_OUTPUT_FILE = "outputfile";
29      private static final String OPTION_FILE1 = "file1";
30      private static final String OPTION_FILE2 = "file2";
31      private static final String OPTION_REGEX = "regex";
32      private static final String OPTION_EXIT_WITH_ERROR_ON_DIFF = "exitwitherrorondifference";
33  	private static final String OPTION_VERBOSE = "verbose";
34      private static final Options options;
35  
36      // static initializer
37      static {
38          options = new Options();
39  
40          Option compareTS =
41              new Option(OPTION_COMPARE_TIMESTAMPS, OPTION_COMPARE_TIMESTAMPS, false, "Compare timestamps");
42          compareTS.setRequired(false);
43  
44          Option compareCRC =
45              new Option(OPTION_COMPARE_CRC_VALUES, OPTION_COMPARE_CRC_VALUES, false, "Compare CRC values");
46          compareCRC.setRequired(false);
47  
48          Option file1 = new Option(OPTION_FILE1, OPTION_FILE1, true, "<filename> first file to compare");
49          file1.setRequired(true);
50  
51          Option file2 = new Option(OPTION_FILE2, OPTION_FILE2, true, "<filename> second file to compare");
52          file2.setRequired(true);
53  
54          Option outputFileOption =
55          	new Option(
56          			OPTION_OUTPUT_FILE,
57  					OPTION_OUTPUT_FILE,
58  					true,
59  					"output filename");
60          outputFileOption.setRequired(false);
61          
62          Option regex =
63              new Option(
64                  OPTION_REGEX,
65                  OPTION_REGEX,
66                  true,
67                  "regular expression to match files to exclude e.g. (?i)meta-inf.*");
68          regex.setRequired(false);
69  
70          Option ignoreCVSFilesOption =
71          	new Option(
72          		OPTION_IGNORE_CVS_FILES,
73  				OPTION_IGNORE_CVS_FILES,
74  				false,
75  				"ignore CVS files");
76          ignoreCVSFilesOption.setRequired(false);
77          
78  		Option exitWithError = 
79  			new Option(
80  				OPTION_EXIT_WITH_ERROR_ON_DIFF,
81  				OPTION_EXIT_WITH_ERROR_ON_DIFF,
82  				false,
83  				"if a difference is found then exit with error " + EXITCODE_DIFF);
84  				
85  		Option verboseOption = 
86  			new Option(
87  				OPTION_VERBOSE,
88  				OPTION_VERBOSE,
89  				false,
90  				"verbose mode");
91  		
92          options.addOption(compareTS);
93          options.addOption(compareCRC);
94          options.addOption(file1);
95          options.addOption(file2);
96          options.addOption(regex);
97          options.addOption(ignoreCVSFilesOption);
98          options.addOption(exitWithError);
99          options.addOption(verboseOption);
100         options.addOption(outputFileOption);
101     }
102 
103     private static void checkFile(java.io.File f) {
104         String filename = f.toString();
105 
106         if (!f.exists()) {
107             System.err.println("'" + filename + "' does not exist");
108             System.exit(EXITCODE_ERROR);
109         }
110 
111         if (!f.canRead()) {
112             System.err.println("'" + filename + "' is not readable");
113             System.exit(EXITCODE_ERROR);
114         }
115 
116         if (f.isDirectory()) {
117             System.err.println("'" + filename + "' is a directory");
118             System.exit(EXITCODE_ERROR);
119         }
120 
121     }
122 
123     private static void writeOutputFile(String filename, Differences d)
124     	throws java.io.IOException
125     {
126 		Builder builder = null;
127 		if (filename.endsWith(".html"))
128 		{
129 			builder = new HtmlBuilder();
130 		}
131 		else if (filename.endsWith(".xml"))
132 		{
133 			builder = new XmlBuilder();
134 		}
135 		else 
136 		{
137 			builder = new TextBuilder();
138 		}
139 		builder.build(filename, d);
140 		
141     }
142  
143 	/***
144 	 * 
145 	 * The command line interface to zipdiff utility
146 	 * 
147 	 * @param args The command line parameters
148 	 * 
149 	 */
150     public static void main(String[] args) {
151         CommandLineParser parser = new GnuParser();
152 
153         try {
154             CommandLine line = parser.parse(options, args);
155 
156             String filename1 = null;
157             String filename2 = null;
158 
159             filename1 = line.getOptionValue(OPTION_FILE1);
160             filename2 = line.getOptionValue(OPTION_FILE2);
161 
162             File f1 = new File(filename1);
163             File f2 = new File(filename2);
164 
165             checkFile(f1);
166             checkFile(f2);
167 
168             System.out.println("File 1 = " + f1);
169             System.out.println("File 2 = " + f2);
170 
171             DifferenceCalculator calc = new DifferenceCalculator(f1, f2);
172 
173             String regularExpression = null;
174 
175             // todo - calc.setFilenamesToIgnore();
176 
177             if (line.hasOption(OPTION_COMPARE_CRC_VALUES)) {
178                 calc.setCompareCRCValues(true);
179             } else {
180                 calc.setCompareCRCValues(false);
181             }
182 
183             if (line.hasOption(OPTION_IGNORE_CVS_FILES)) {
184             	calc.setIgnoreCVSFiles(true);
185             } else {
186             	calc.setIgnoreCVSFiles(false);
187             }
188             
189             if (line.hasOption(OPTION_COMPARE_TIMESTAMPS)) {
190                 calc.setIgnoreTimestamps(false);
191             } else {
192                 calc.setIgnoreTimestamps(true);
193             }
194 
195             if (line.hasOption(OPTION_REGEX)) {
196                 regularExpression = line.getOptionValue(OPTION_REGEX);
197                 Set regexSet = new HashSet();
198                 regexSet.add(regularExpression);
199 
200                 calc.setFilenameRegexToIgnore(regexSet);
201             }
202             
203             boolean exitWithErrorOnDiff = false;
204             if (line.hasOption(OPTION_EXIT_WITH_ERROR_ON_DIFF)) {
205             	exitWithErrorOnDiff = true;
206             }
207             
208             Differences d = calc.getDifferences();
209             
210             if (line.hasOption(OPTION_OUTPUT_FILE))
211             {
212             	String outputFilename = line.getOptionValue(OPTION_OUTPUT_FILE);
213             	writeOutputFile(outputFilename, d);
214             }
215             
216             
217             if (d.hasDifferences()) {
218             	if (line.hasOption(OPTION_VERBOSE)) {
219 					System.out.println(d);
220 					System.out.println(d.getFilename1() + " and " + d.getFilename2() + " are different.");
221             	}
222                 if (exitWithErrorOnDiff) {
223                 	System.exit(EXITCODE_DIFF); 
224                 }
225             } else {
226                 System.out.println("No differences found.");
227             }
228         } catch (ParseException pex) {
229             System.err.println(pex.getMessage());
230             HelpFormatter formatter = new HelpFormatter();
231             formatter.printHelp("zipdiff.Main [options] ", options);
232             System.exit(EXITCODE_ERROR);
233         } catch (Exception ex) {
234             ex.printStackTrace();
235             System.exit(EXITCODE_ERROR);
236         }
237 
238     }
239 
240 }