01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.tools.ant.taskdefs.condition;
19:
20: import java.io.File;
21: import java.io.IOException;
22: import org.apache.tools.ant.BuildException;
23: import org.apache.tools.ant.util.FileUtils;
24:
25: /**
26: * Compares two files for equality based on size and
27: * content. Timestamps are not at all looked at.
28: *
29: * @since Ant 1.5
30: */
31:
32: public class FilesMatch implements Condition {
33:
34: /**
35: * Helper that provides the file comparison method.
36: */
37: private static final FileUtils FILE_UTILS = FileUtils
38: .getFileUtils();
39:
40: /**
41: * files to compare
42: */
43: private File file1, file2;
44:
45: private boolean textfile = false;
46:
47: /**
48: * Sets the File1 attribute
49: *
50: * @param file1 The new File1 value
51: */
52: public void setFile1(File file1) {
53: this .file1 = file1;
54: }
55:
56: /**
57: * Sets the File2 attribute
58: *
59: * @param file2 The new File2 value
60: */
61: public void setFile2(File file2) {
62: this .file2 = file2;
63: }
64:
65: /**
66: * Set whether to ignore line endings when comparing files.
67: * @param textfile whether to ignore line endings.
68: */
69: public void setTextfile(boolean textfile) {
70: this .textfile = textfile;
71: }
72:
73: /**
74: * comparison method of the interface
75: *
76: * @return true if the files are equal
77: * @exception BuildException if it all went pear-shaped
78: */
79: public boolean eval() throws BuildException {
80:
81: //validate
82: if (file1 == null || file2 == null) {
83: throw new BuildException(
84: "both file1 and file2 are required in "
85: + "filesmatch");
86: }
87:
88: //#now match the files
89: boolean matches = false;
90: try {
91: matches = FILE_UTILS.contentEquals(file1, file2, textfile);
92: } catch (IOException ioe) {
93: throw new BuildException("when comparing files: "
94: + ioe.getMessage(), ioe);
95: }
96: return matches;
97: }
98: }
|