KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
22 import java.text.DateFormat JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.text.ParseException JavaDoc;
25 import java.util.Locale JavaDoc;
26
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.types.Parameter;
29 import org.apache.tools.ant.types.TimeComparison;
30 import org.apache.tools.ant.util.FileUtils;
31
32 /**
33  * Selector that chooses files based on their last modified date.
34  *
35  * @since 1.5
36  */

37 public class DateSelector extends BaseExtendSelector {
38
39     /** Utilities used for file operations */
40     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
41
42     private long millis = -1;
43     private String JavaDoc dateTime = null;
44     private boolean includeDirs = false;
45     private long granularity = 0;
46     private String JavaDoc pattern;
47     private TimeComparison when = TimeComparison.EQUAL;
48
49     /** Key to used for parameterized custom selector */
50     public static final String JavaDoc MILLIS_KEY = "millis";
51     /** Key to used for parameterized custom selector */
52     public static final String JavaDoc DATETIME_KEY = "datetime";
53     /** Key to used for parameterized custom selector */
54     public static final String JavaDoc CHECKDIRS_KEY = "checkdirs";
55     /** Key to used for parameterized custom selector */
56     public static final String JavaDoc GRANULARITY_KEY = "granularity";
57     /** Key to used for parameterized custom selector */
58     public static final String JavaDoc WHEN_KEY = "when";
59     /** Key to used for parameterized custom selector */
60     public static final String JavaDoc PATTERN_KEY = "pattern";
61
62     /**
63      * Creates a new <code>DateSelector</code> instance.
64      *
65      */

66     public DateSelector() {
67         granularity = FILE_UTILS.getFileTimestampGranularity();
68     }
69
70     /**
71      * @return a string describing this object
72      */

73     public String JavaDoc toString() {
74         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("{dateselector date: ");
75         buf.append(dateTime);
76         buf.append(" compare: ").append(when.getValue());
77         buf.append(" granularity: ");
78         buf.append(granularity);
79         if (pattern != null) {
80             buf.append(" pattern: ").append(pattern);
81         }
82         buf.append("}");
83         return buf.toString();
84     }
85
86     /**
87      * Set the time; for users who prefer to express time in ms since 1970.
88      *
89      * @param millis the time to compare file's last modified date to,
90      * expressed in milliseconds.
91      */

92     public void setMillis(long millis) {
93         this.millis = millis;
94     }
95
96     /**
97      * Returns the millisecond value the selector is set for.
98      * @return the millisecond value.
99      */

100     public long getMillis() {
101         if (dateTime != null) {
102             validate();
103         }
104         return millis;
105     }
106
107     /**
108      * Sets the date. The user must supply it in MM/DD/YYYY HH:MM AM_PM format,
109      * unless an alternate pattern is specified via the pattern attribute.
110      *
111      * @param dateTime a formatted date <code>String</code>.
112      */

113     public void setDatetime(String JavaDoc dateTime) {
114         this.dateTime = dateTime;
115         millis = -1;
116     }
117
118     /**
119      * Set whether to check dates on directories.
120      *
121      * @param includeDirs whether to check the timestamp on directories.
122      */

123     public void setCheckdirs(boolean includeDirs) {
124         this.includeDirs = includeDirs;
125     }
126
127     /**
128      * Sets the number of milliseconds leeway we will give before we consider
129      * a file not to have matched a date.
130      * @param granularity the number of milliseconds leeway.
131      */

132     public void setGranularity(int granularity) {
133         this.granularity = granularity;
134     }
135
136     /**
137      * Sets the type of comparison to be done on the file's last modified
138      * date.
139      *
140      * @param tcmp The comparison to perform, an EnumeratedAttribute.
141      */

142     public void setWhen(TimeComparisons tcmp) {
143         setWhen((TimeComparison) tcmp);
144     }
145
146     /**
147      * Set the comparison type.
148      * @param t TimeComparison object.
149      */

150     public void setWhen(TimeComparison t) {
151         when = t;
152     }
153
154     /**
155      * Sets the pattern to be used for the SimpleDateFormat.
156      *
157      * @param pattern the pattern that defines the date format.
158      */

159     public void setPattern(String JavaDoc pattern) {
160         this.pattern = pattern;
161     }
162
163     /**
164      * When using this as a custom selector, this method will be called.
165      * It translates each parameter into the appropriate setXXX() call.
166      *
167      * @param parameters the complete set of parameters for this selector.
168      */

169     public void setParameters(Parameter[] parameters) {
170         super.setParameters(parameters);
171         if (parameters != null) {
172             for (int i = 0; i < parameters.length; i++) {
173                 String JavaDoc paramname = parameters[i].getName();
174                 if (MILLIS_KEY.equalsIgnoreCase(paramname)) {
175                     try {
176                         setMillis(new Long JavaDoc(parameters[i].getValue()
177                         ).longValue());
178                     } catch (NumberFormatException JavaDoc nfe) {
179                         setError("Invalid millisecond setting "
180                                 + parameters[i].getValue());
181                     }
182                 } else if (DATETIME_KEY.equalsIgnoreCase(paramname)) {
183                     setDatetime(parameters[i].getValue());
184                 } else if (CHECKDIRS_KEY.equalsIgnoreCase(paramname)) {
185                     setCheckdirs(Project.toBoolean(parameters[i].getValue()));
186                 } else if (GRANULARITY_KEY.equalsIgnoreCase(paramname)) {
187                     try {
188                         setGranularity(new Integer JavaDoc(parameters[i].getValue()
189                         ).intValue());
190                     } catch (NumberFormatException JavaDoc nfe) {
191                         setError("Invalid granularity setting "
192                             + parameters[i].getValue());
193                     }
194                 } else if (WHEN_KEY.equalsIgnoreCase(paramname)) {
195                     setWhen(new TimeComparison(parameters[i].getValue()));
196                 } else if (PATTERN_KEY.equalsIgnoreCase(paramname)) {
197                     setPattern(parameters[i].getValue());
198                 } else {
199                     setError("Invalid parameter " + paramname);
200                 }
201             }
202         }
203     }
204
205     /**
206      * This is a consistency check to ensure the selector's required
207      * values have been set.
208      */

209     public void verifySettings() {
210         if (dateTime == null && millis < 0) {
211             setError("You must provide a datetime or the number of "
212                     + "milliseconds.");
213         } else if (millis < 0 && dateTime != null) {
214             // check millis and only set it once.
215
DateFormat JavaDoc df = ((pattern == null)
216                 ? DateFormat.getDateTimeInstance(
217                     DateFormat.SHORT, DateFormat.SHORT, Locale.US)
218                 : new SimpleDateFormat JavaDoc(pattern));
219
220             try {
221                 setMillis(df.parse(dateTime).getTime());
222                 if (millis < 0) {
223                     setError("Date of " + dateTime
224                         + " results in negative milliseconds value"
225                         + " relative to epoch (January 1, 1970, 00:00:00 GMT).");
226                 }
227             } catch (ParseException JavaDoc pe) {
228                 setError("Date of " + dateTime
229                         + " Cannot be parsed correctly. It should be in"
230                         + ((pattern == null)
231                         ? " MM/DD/YYYY HH:MM AM_PM" : pattern) + " format.");
232             }
233         }
234     }
235
236     /**
237      * The heart of the matter. This is where the selector gets to decide
238      * on the inclusion of a file in a particular fileset.
239      *
240      * @param basedir the base directory from which the scan is being performed.
241      * @param filename is the name of the file to check.
242      * @param file is a java.io.File object the selector can use.
243      * @return whether the file is selected.
244      */

245     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
246
247         validate();
248
249         return (file.isDirectory() && !includeDirs)
250             || when.evaluate(file.lastModified(), millis, granularity);
251     }
252
253     /**
254      * Enumerated attribute with the values for time comparison.
255      * <p>
256      */

257     public static class TimeComparisons extends TimeComparison {
258     }
259
260 }
261
262
263
Popular Tags