KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > DifferentSelector


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
19 package org.apache.tools.ant.types.selectors;
20
21 import org.apache.tools.ant.util.FileUtils;
22 import org.apache.tools.ant.BuildException;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * This selector selects files against a mapped set of target files, selecting
29  * all those files which are different.
30  * Files with different lengths are deemed different
31  * automatically
32  * Files with identical timestamps are viewed as matching by
33  * default, unless you specify otherwise.
34  * Contents are compared if the lengths are the same
35  * and the timestamps are ignored or the same,
36  * except if you decide to ignore contents to gain speed.
37  * <p>
38  * This is a useful selector to work with programs and tasks that don't handle
39  * dependency checking properly; Even if a predecessor task always creates its
40  * output files, followup tasks can be driven off copies made with a different
41  * selector, so their dependencies are driven on the absolute state of the
42  * files, not a timestamp.
43  * <p>
44  * Clearly, however, bulk file comparisons is inefficient; anything that can
45  * use timestamps is to be preferred. If this selector must be used, use it
46  * over as few files as possible, perhaps following it with an &lt;uptodate;&gt
47  * to keep the descendent routines conditional.
48  *
49  */

50 public class DifferentSelector extends MappingSelector {
51
52     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
53
54     private boolean ignoreFileTimes = true;
55     private boolean ignoreContents = false;
56
57
58     /**
59      * This flag tells the selector to ignore file times in the comparison
60      * @param ignoreFileTimes if true ignore file times
61      */

62     public void setIgnoreFileTimes(boolean ignoreFileTimes) {
63         this.ignoreFileTimes = ignoreFileTimes;
64     }
65     /**
66      * This flag tells the selector to ignore contents
67      * @param ignoreContents if true ignore contents
68      * @since ant 1.6.3
69      */

70     public void setIgnoreContents(boolean ignoreContents) {
71         this.ignoreContents = ignoreContents;
72     }
73     /**
74      * this test is our selection test that compared the file with the destfile
75      * @param srcfile the source file
76      * @param destfile the destination file
77      * @return true if the files are different
78      */

79     protected boolean selectionTest(File JavaDoc srcfile, File JavaDoc destfile) {
80
81         //if either of them is missing, they are different
82
if (srcfile.exists() != destfile.exists()) {
83             return true;
84         }
85
86         if (srcfile.length() != destfile.length()) {
87             // different size =>different files
88
return true;
89         }
90
91         if (!ignoreFileTimes) {
92             //same date if dest timestamp is within granularity of the srcfile
93
boolean sameDate;
94             sameDate = destfile.lastModified() >= srcfile.lastModified() - granularity
95                     && destfile.lastModified() <= srcfile.lastModified() + granularity;
96
97             // different dates => different files
98
if (!sameDate) {
99                 return true;
100             }
101         }
102         if (!ignoreContents) {
103             //here do a bulk comparison
104
try {
105                 return !FILE_UTILS.contentEquals(srcfile, destfile);
106             } catch (IOException JavaDoc e) {
107                 throw new BuildException("while comparing " + srcfile + " and "
108                         + destfile, e);
109             }
110         } else {
111             return false;
112         }
113     }
114 }
115
Popular Tags