1 | /*
|
---|
2 | * Copyright 2002-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.condition;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import java.io.IOException;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.util.FileUtils;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Compares two files for bitwise equality based on size and
|
---|
26 | * content. Timestamps are not at all looked at.
|
---|
27 | *
|
---|
28 | * @since Ant 1.5
|
---|
29 | */
|
---|
30 |
|
---|
31 | public class FilesMatch implements Condition {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * files to compare
|
---|
35 | */
|
---|
36 | private File file1, file2;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Helper that provides the file comparison method.
|
---|
40 | */
|
---|
41 | private FileUtils fu = FileUtils.newFileUtils();
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Sets the File1 attribute
|
---|
45 | *
|
---|
46 | * @param file1 The new File1 value
|
---|
47 | */
|
---|
48 | public void setFile1(File file1) {
|
---|
49 | this.file1 = file1;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Sets the File2 attribute
|
---|
55 | *
|
---|
56 | * @param file2 The new File2 value
|
---|
57 | */
|
---|
58 | public void setFile2(File file2) {
|
---|
59 | this.file2 = file2;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * comparison method of the interface
|
---|
64 | *
|
---|
65 | * @return true if the files are equal
|
---|
66 | * @exception BuildException if it all went pear-shaped
|
---|
67 | */
|
---|
68 | public boolean eval()
|
---|
69 | throws BuildException {
|
---|
70 |
|
---|
71 | //validate
|
---|
72 | if (file1 == null || file2 == null) {
|
---|
73 | throw new BuildException("both file1 and file2 are required in "
|
---|
74 | + "filesmatch");
|
---|
75 | }
|
---|
76 |
|
---|
77 | //#now match the files
|
---|
78 | boolean matches = false;
|
---|
79 | try {
|
---|
80 | matches = fu.contentEquals(file1, file2);
|
---|
81 | } catch (IOException ioe) {
|
---|
82 | throw new BuildException("when comparing files: "
|
---|
83 | + ioe.getMessage(), ioe);
|
---|
84 | }
|
---|
85 | return matches;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|