1 | /*
|
---|
2 | * Copyright 2000-2004 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 |
|
---|
18 | package org.apache.tools.ant.types.selectors;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.util.FileUtils;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 |
|
---|
23 | import java.io.File;
|
---|
24 | import java.io.IOException;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * This selector selects files against a mapped set of target files, selecting
|
---|
28 | * all those files which are different.
|
---|
29 | * Files with different lengths are deemed different
|
---|
30 | * automatically
|
---|
31 | * Files with identical timestamps are viewed as matching by
|
---|
32 | * default, unless you specify otherwise.
|
---|
33 | * Contents are compared if the lengths are the same
|
---|
34 | * and the timestamps are ignored or the same,
|
---|
35 | * except if you decide to ignore contents to gain speed.
|
---|
36 | * <p>
|
---|
37 | * This is a useful selector to work with programs and tasks that don't handle
|
---|
38 | * dependency checking properly; Even if a predecessor task always creates its
|
---|
39 | * output files, followup tasks can be driven off copies made with a different
|
---|
40 | * selector, so their dependencies are driven on the absolute state of the
|
---|
41 | * files, not a timestamp.
|
---|
42 | * <p>
|
---|
43 | * Clearly, however, bulk file comparisons is inefficient; anything that can
|
---|
44 | * use timestamps is to be preferred. If this selector must be used, use it
|
---|
45 | * over as few files as possible, perhaps following it with an <uptodate;>
|
---|
46 | * to keep the descendent routines conditional.
|
---|
47 | *
|
---|
48 | */
|
---|
49 | public class DifferentSelector extends MappingSelector {
|
---|
50 |
|
---|
51 | private FileUtils fileUtils = FileUtils.newFileUtils();
|
---|
52 |
|
---|
53 | private boolean ignoreFileTimes = true;
|
---|
54 | private boolean ignoreContents = false;
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * This flag tells the selector to ignore file times in the comparison
|
---|
59 | * @param ignoreFileTimes if true ignore file times
|
---|
60 | */
|
---|
61 | public void setIgnoreFileTimes(boolean ignoreFileTimes) {
|
---|
62 | this.ignoreFileTimes = ignoreFileTimes;
|
---|
63 | }
|
---|
64 | /**
|
---|
65 | * This flag tells the selector to ignore contents
|
---|
66 | * @param ignoreContents if true ignore contents
|
---|
67 | * @since ant 1.6.3
|
---|
68 | */
|
---|
69 | public void setIgnoreContents(boolean ignoreContents) {
|
---|
70 | this.ignoreContents = ignoreContents;
|
---|
71 | }
|
---|
72 | /**
|
---|
73 | * this test is our selection test that compared the file with the destfile
|
---|
74 | * @param srcfile the source file
|
---|
75 | * @param destfile the destination file
|
---|
76 | * @return true if the files are different
|
---|
77 | */
|
---|
78 | protected boolean selectionTest(File srcfile, File destfile) {
|
---|
79 |
|
---|
80 | //if either of them is missing, they are different
|
---|
81 | if (srcfile.exists() != destfile.exists()) {
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (srcfile.length() != destfile.length()) {
|
---|
86 | // different size =>different files
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (!ignoreFileTimes) {
|
---|
91 | //same date if dest timestamp is within granularity of the srcfile
|
---|
92 | boolean sameDate;
|
---|
93 | sameDate = destfile.lastModified() >= srcfile.lastModified() - granularity
|
---|
94 | && destfile.lastModified() <= srcfile.lastModified() + granularity;
|
---|
95 |
|
---|
96 | // different dates => different files
|
---|
97 | if (!sameDate) {
|
---|
98 | return true;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | if (!ignoreContents) {
|
---|
102 | //here do a bulk comparison
|
---|
103 | try {
|
---|
104 | return !fileUtils.contentEquals(srcfile, destfile);
|
---|
105 | } catch (IOException e) {
|
---|
106 | throw new BuildException("while comparing " + srcfile + " and "
|
---|
107 | + destfile, e);
|
---|
108 | }
|
---|
109 | } else {
|
---|
110 | return false;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|