KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > selectors > Date


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.types.resources.selectors;
19
20 import java.text.DateFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.types.Resource;
27 import org.apache.tools.ant.types.TimeComparison;
28 import org.apache.tools.ant.util.FileUtils;
29
30 /**
31  * Date ResourceSelector. Based on the date FileSelector, with the most
32  * notable difference being the lack of support for the includedirs attribute.
33  * It is recommended that the effect of includeDirs = "false" be achieved for
34  * resources by enclosing a "dir" Type ResourceSelector and a Date
35  * ResourceSelector in an Or ResourceSelector.
36  * @since Ant 1.7
37  */

38 public class Date implements ResourceSelector {
39     private static final String JavaDoc MILLIS_OR_DATETIME
40         = "Either the millis or the datetime attribute must be set.";
41     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
42
43     private Long JavaDoc millis = null;
44     private String JavaDoc dateTime = null;
45     private String JavaDoc pattern = null;
46     private TimeComparison when = TimeComparison.EQUAL;
47     private long granularity = FILE_UTILS.getFileTimestampGranularity();
48
49     /**
50      * Set the date/time in milliseconds since 1970.
51      * @param m the number of millis.
52      */

53     public synchronized void setMillis(long m) {
54         millis = new Long JavaDoc(m);
55     }
56
57     /**
58      * Get the date/time in ms.
59      * @return long number of millis since 1970.
60      */

61     public synchronized long getMillis() {
62         return millis == null ? -1L : millis.longValue();
63     }
64
65     /**
66      * Set the date and time as a String.
67      * @param s the date & time to use.
68      */

69     public synchronized void setDateTime(String JavaDoc s) {
70         dateTime = s;
71         millis = null;
72     }
73
74     /**
75      * Get the date & time in String format.
76      * @return a String representing a date & time.
77      */

78     public synchronized String JavaDoc getDatetime() {
79         return dateTime;
80     }
81
82     /**
83      * Set the granularity to use for this ResourceSelector.
84      * @param g the timestamp granularity.
85      */

86     public synchronized void setGranularity(long g) {
87         granularity = g;
88     }
89
90     /**
91      * Get the timestamp granularity used by this ResourceSelector.
92      * @return the long granularity.
93      */

94     public synchronized long getGranularity() {
95         return granularity;
96     }
97
98     /**
99      * Set the optional pattern to use with the datetime attribute.
100      * @param p the SimpleDateFormat-compatible pattern string.
101      */

102     public synchronized void setPattern(String JavaDoc p) {
103         pattern = p;
104     }
105
106     /**
107      * Get the pattern for use with the datetime attribute.
108      * @return a SimpleDateFormat-compatible pattern string.
109      */

110     public synchronized String JavaDoc getPattern() {
111         return pattern;
112     }
113
114     /**
115      * Set the comparison mode.
116      * @param c a TimeComparison object.
117      */

118     public synchronized void setWhen(TimeComparison c) {
119         when = c;
120     }
121
122     /**
123      * Get the comparison mode.
124      * @return a TimeComparison object.
125      */

126     public synchronized TimeComparison getWhen() {
127         return when;
128     }
129
130     /**
131      * Return true if this Resource is selected.
132      * @param r the Resource to check.
133      * @return whether the Resource was selected.
134      */

135     public synchronized boolean isSelected(Resource r) {
136         if (dateTime == null && millis == null) {
137             throw new BuildException(MILLIS_OR_DATETIME);
138         }
139         if (millis == null) {
140             DateFormat JavaDoc df = ((pattern == null)
141                 ? DateFormat.getDateTimeInstance(
142                     DateFormat.SHORT, DateFormat.SHORT, Locale.US)
143                 : new SimpleDateFormat JavaDoc(pattern));
144             try {
145                 long m = df.parse(dateTime).getTime();
146                 if (m < 0) {
147                     throw new BuildException("Date of " + dateTime
148                         + " results in negative milliseconds value"
149                         + " relative to epoch (January 1, 1970, 00:00:00 GMT).");
150                 }
151                 setMillis(m);
152             } catch (ParseException JavaDoc pe) {
153                 throw new BuildException("Date of " + dateTime
154                         + " Cannot be parsed correctly. It should be in"
155                         + (pattern == null
156                         ? " MM/DD/YYYY HH:MM AM_PM" : pattern) + " format.");
157             }
158         }
159         return when.evaluate(r.getLastModified(), millis.longValue(), granularity);
160     }
161
162 }
163
Popular Tags