KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > FilesMatch


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
21 import java.io.IOException JavaDoc;
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.getFileUtils();
38
39     /**
40      * files to compare
41      */

42     private File JavaDoc file1, file2;
43
44     private boolean textfile = false;
45
46
47     /**
48      * Sets the File1 attribute
49      *
50      * @param file1 The new File1 value
51      */

52     public void setFile1(File JavaDoc file1) {
53         this.file1 = file1;
54     }
55
56
57     /**
58      * Sets the File2 attribute
59      *
60      * @param file2 The new File2 value
61      */

62     public void setFile2(File JavaDoc file2) {
63         this.file2 = file2;
64     }
65
66     /**
67      * Set whether to ignore line endings when comparing files.
68      * @param textfile whether to ignore line endings.
69      */

70     public void setTextfile(boolean textfile) {
71         this.textfile = textfile;
72     }
73
74     /**
75      * comparison method of the interface
76      *
77      * @return true if the files are equal
78      * @exception BuildException if it all went pear-shaped
79      */

80     public boolean eval()
81         throws BuildException {
82
83         //validate
84
if (file1 == null || file2 == null) {
85             throw new BuildException("both file1 and file2 are required in "
86                                      + "filesmatch");
87         }
88
89         //#now match the files
90
boolean matches = false;
91         try {
92             matches = FILE_UTILS.contentEquals(file1, file2, textfile);
93         } catch (IOException JavaDoc ioe) {
94             throw new BuildException("when comparing files: "
95                 + ioe.getMessage(), ioe);
96         }
97         return matches;
98     }
99 }
100
101
Popular Tags