1 | /*
|
---|
2 | * Copyright 2002-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 java.io.File;
|
---|
21 | import java.text.DateFormat;
|
---|
22 | import java.text.SimpleDateFormat;
|
---|
23 | import java.text.ParseException;
|
---|
24 | import java.util.Locale;
|
---|
25 |
|
---|
26 | import org.apache.tools.ant.Project;
|
---|
27 | import org.apache.tools.ant.taskdefs.condition.Os;
|
---|
28 | import org.apache.tools.ant.types.EnumeratedAttribute;
|
---|
29 | import org.apache.tools.ant.types.Parameter;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Selector that chooses files based on their last modified date.
|
---|
33 | *
|
---|
34 | * @since 1.5
|
---|
35 | */
|
---|
36 | public class DateSelector extends BaseExtendSelector {
|
---|
37 |
|
---|
38 | private long millis = -1;
|
---|
39 | private String dateTime = null;
|
---|
40 | private boolean includeDirs = false;
|
---|
41 | private int granularity = 0;
|
---|
42 | private int cmp = 2;
|
---|
43 | private String pattern;
|
---|
44 | /** Key to used for parameterized custom selector */
|
---|
45 | public static final String MILLIS_KEY = "millis";
|
---|
46 | /** Key to used for parameterized custom selector */
|
---|
47 | public static final String DATETIME_KEY = "datetime";
|
---|
48 | /** Key to used for parameterized custom selector */
|
---|
49 | public static final String CHECKDIRS_KEY = "checkdirs";
|
---|
50 | /** Key to used for parameterized custom selector */
|
---|
51 | public static final String GRANULARITY_KEY = "granularity";
|
---|
52 | /** Key to used for parameterized custom selector */
|
---|
53 | public static final String WHEN_KEY = "when";
|
---|
54 | /** Key to used for parameterized custom selector */
|
---|
55 | public static final String PATTERN_KEY = "pattern";
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Creates a new <code>DateSelector</code> instance.
|
---|
59 | *
|
---|
60 | */
|
---|
61 | public DateSelector() {
|
---|
62 | if (Os.isFamily("dos")) {
|
---|
63 | granularity = 2000;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * @return a string describing this object
|
---|
69 | */
|
---|
70 | public String toString() {
|
---|
71 | StringBuffer buf = new StringBuffer("{dateselector date: ");
|
---|
72 | buf.append(dateTime);
|
---|
73 | buf.append(" compare: ");
|
---|
74 | if (cmp == 0) {
|
---|
75 | buf.append("before");
|
---|
76 | } else if (cmp == 1) {
|
---|
77 | buf.append("after");
|
---|
78 | } else {
|
---|
79 | buf.append("equal");
|
---|
80 | }
|
---|
81 | buf.append(" granularity: ");
|
---|
82 | buf.append(granularity);
|
---|
83 | if (pattern != null) {
|
---|
84 | buf.append(" pattern: ").append(pattern);
|
---|
85 | }
|
---|
86 | buf.append("}");
|
---|
87 | return buf.toString();
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * For users that prefer to express time in milliseconds since 1970
|
---|
92 | *
|
---|
93 | * @param millis the time to compare file's last modified date to,
|
---|
94 | * expressed in milliseconds
|
---|
95 | */
|
---|
96 | public void setMillis(long millis) {
|
---|
97 | this.millis = millis;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Returns the millisecond value the selector is set for.
|
---|
102 | * @return the millisecond value
|
---|
103 | */
|
---|
104 | public long getMillis() {
|
---|
105 | if (dateTime != null) {
|
---|
106 | validate();
|
---|
107 | }
|
---|
108 | return millis;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Sets the date. The user must supply it in MM/DD/YYYY HH:MM AM_PM
|
---|
113 | * format
|
---|
114 | *
|
---|
115 | * @param dateTime a string in MM/DD/YYYY HH:MM AM_PM format
|
---|
116 | */
|
---|
117 | public void setDatetime(String dateTime) {
|
---|
118 | this.dateTime = dateTime;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Should we be checking dates on directories?
|
---|
123 | *
|
---|
124 | * @param includeDirs whether to check the timestamp on directories
|
---|
125 | */
|
---|
126 | public void setCheckdirs(boolean includeDirs) {
|
---|
127 | this.includeDirs = includeDirs;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Sets the number of milliseconds leeway we will give before we consider
|
---|
132 | * a file not to have matched a date.
|
---|
133 | * @param granularity the number of milliconds leeway
|
---|
134 | */
|
---|
135 | public void setGranularity(int granularity) {
|
---|
136 | this.granularity = granularity;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Sets the type of comparison to be done on the file's last modified
|
---|
141 | * date.
|
---|
142 | *
|
---|
143 | * @param cmp The comparison to perform, an EnumeratedAttribute
|
---|
144 | */
|
---|
145 | public void setWhen(TimeComparisons cmp) {
|
---|
146 | this.cmp = cmp.getIndex();
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Sets the pattern to be used for the SimpleDateFormat
|
---|
151 | *
|
---|
152 | * @param pattern the pattern that defines the date format
|
---|
153 | */
|
---|
154 | public void setPattern(String pattern) {
|
---|
155 | this.pattern = pattern;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * When using this as a custom selector, this method will be called.
|
---|
160 | * It translates each parameter into the appropriate setXXX() call.
|
---|
161 | *
|
---|
162 | * @param parameters the complete set of parameters for this selector
|
---|
163 | */
|
---|
164 | public void setParameters(Parameter[] parameters) {
|
---|
165 | super.setParameters(parameters);
|
---|
166 | if (parameters != null) {
|
---|
167 | for (int i = 0; i < parameters.length; i++) {
|
---|
168 | String paramname = parameters[i].getName();
|
---|
169 | if (MILLIS_KEY.equalsIgnoreCase(paramname)) {
|
---|
170 | try {
|
---|
171 | setMillis(new Long(parameters[i].getValue()
|
---|
172 | ).longValue());
|
---|
173 | } catch (NumberFormatException nfe) {
|
---|
174 | setError("Invalid millisecond setting "
|
---|
175 | + parameters[i].getValue());
|
---|
176 | }
|
---|
177 | } else if (DATETIME_KEY.equalsIgnoreCase(paramname)) {
|
---|
178 | setDatetime(parameters[i].getValue());
|
---|
179 | } else if (CHECKDIRS_KEY.equalsIgnoreCase(paramname)) {
|
---|
180 | setCheckdirs(Project.toBoolean(parameters[i].getValue()));
|
---|
181 | } else if (GRANULARITY_KEY.equalsIgnoreCase(paramname)) {
|
---|
182 | try {
|
---|
183 | setGranularity(new Integer(parameters[i].getValue()
|
---|
184 | ).intValue());
|
---|
185 | } catch (NumberFormatException nfe) {
|
---|
186 | setError("Invalid granularity setting "
|
---|
187 | + parameters[i].getValue());
|
---|
188 | }
|
---|
189 | } else if (WHEN_KEY.equalsIgnoreCase(paramname)) {
|
---|
190 | TimeComparisons cmp = new TimeComparisons();
|
---|
191 | cmp.setValue(parameters[i].getValue());
|
---|
192 | setWhen(cmp);
|
---|
193 | } else if (PATTERN_KEY.equalsIgnoreCase(paramname)) {
|
---|
194 | setPattern(parameters[i].getValue());
|
---|
195 | } else {
|
---|
196 | setError("Invalid parameter " + paramname);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * This is a consistency check to ensure the selector's required
|
---|
204 | * values have been set.
|
---|
205 | */
|
---|
206 | public void verifySettings() {
|
---|
207 | if (dateTime == null && millis < 0) {
|
---|
208 | setError("You must provide a datetime or the number of "
|
---|
209 | + "milliseconds.");
|
---|
210 | } else if (millis < 0 && dateTime != null) {
|
---|
211 | // check millis and only set it once.
|
---|
212 | DateFormat df = ((pattern == null)
|
---|
213 | ? DateFormat.getDateTimeInstance(
|
---|
214 | DateFormat.SHORT, DateFormat.SHORT, Locale.US)
|
---|
215 | : new SimpleDateFormat(pattern));
|
---|
216 |
|
---|
217 | try {
|
---|
218 | setMillis(df.parse(dateTime).getTime());
|
---|
219 | if (millis < 0) {
|
---|
220 | setError("Date of " + dateTime
|
---|
221 | + " results in negative milliseconds value"
|
---|
222 | + " relative to epoch (January 1, 1970, 00:00:00 GMT).");
|
---|
223 | }
|
---|
224 | } catch (ParseException pe) {
|
---|
225 | setError("Date of " + dateTime
|
---|
226 | + " Cannot be parsed correctly. It should be in"
|
---|
227 | + ((pattern == null)
|
---|
228 | ? " MM/DD/YYYY HH:MM AM_PM" : pattern) + " format.");
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * The heart of the matter. This is where the selector gets to decide
|
---|
235 | * on the inclusion of a file in a particular fileset.
|
---|
236 | *
|
---|
237 | * @param basedir the base directory the scan is being done from
|
---|
238 | * @param filename is the name of the file to check
|
---|
239 | * @param file is a java.io.File object the selector can use
|
---|
240 | * @return whether the file should be selected or not
|
---|
241 | */
|
---|
242 | public boolean isSelected(File basedir, String filename, File file) {
|
---|
243 |
|
---|
244 | validate();
|
---|
245 |
|
---|
246 | if (file.isDirectory() && (!includeDirs)) {
|
---|
247 | return true;
|
---|
248 | }
|
---|
249 | if (cmp == 0) {
|
---|
250 | return ((file.lastModified() - granularity) < millis);
|
---|
251 | } else if (cmp == 1) {
|
---|
252 | return ((file.lastModified() + granularity) > millis);
|
---|
253 | } else {
|
---|
254 | return (Math.abs(file.lastModified() - millis) <= granularity);
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Enumerated attribute with the values for time comparison.
|
---|
260 | * <p>
|
---|
261 | */
|
---|
262 | public static class TimeComparisons extends EnumeratedAttribute {
|
---|
263 | /**
|
---|
264 | * @return the values as an array of strings
|
---|
265 | */
|
---|
266 | public String[] getValues() {
|
---|
267 | return new String[]{"before", "after", "equal"};
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|