001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types.resources.selectors;
019:
020: import java.text.DateFormat;
021: import java.text.SimpleDateFormat;
022: import java.text.ParseException;
023: import java.util.Locale;
024:
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.types.Resource;
027: import org.apache.tools.ant.types.TimeComparison;
028: import org.apache.tools.ant.util.FileUtils;
029:
030: /**
031: * Date ResourceSelector. Based on the date FileSelector, with the most
032: * notable difference being the lack of support for the includedirs attribute.
033: * It is recommended that the effect of includeDirs = "false" be achieved for
034: * resources by enclosing a "dir" Type ResourceSelector and a Date
035: * ResourceSelector in an Or ResourceSelector.
036: * @since Ant 1.7
037: */
038: public class Date implements ResourceSelector {
039: private static final String MILLIS_OR_DATETIME = "Either the millis or the datetime attribute must be set.";
040: private static final FileUtils FILE_UTILS = FileUtils
041: .getFileUtils();
042:
043: private Long millis = null;
044: private String dateTime = null;
045: private String pattern = null;
046: private TimeComparison when = TimeComparison.EQUAL;
047: private long granularity = FILE_UTILS.getFileTimestampGranularity();
048:
049: /**
050: * Set the date/time in milliseconds since 1970.
051: * @param m the number of millis.
052: */
053: public synchronized void setMillis(long m) {
054: millis = new Long(m);
055: }
056:
057: /**
058: * Get the date/time in ms.
059: * @return long number of millis since 1970.
060: */
061: public synchronized long getMillis() {
062: return millis == null ? -1L : millis.longValue();
063: }
064:
065: /**
066: * Set the date and time as a String.
067: * @param s the date & time to use.
068: */
069: public synchronized void setDateTime(String s) {
070: dateTime = s;
071: millis = null;
072: }
073:
074: /**
075: * Get the date & time in String format.
076: * @return a String representing a date & time.
077: */
078: public synchronized String getDatetime() {
079: return dateTime;
080: }
081:
082: /**
083: * Set the granularity to use for this ResourceSelector.
084: * @param g the timestamp granularity.
085: */
086: public synchronized void setGranularity(long g) {
087: granularity = g;
088: }
089:
090: /**
091: * Get the timestamp granularity used by this ResourceSelector.
092: * @return the long granularity.
093: */
094: public synchronized long getGranularity() {
095: return granularity;
096: }
097:
098: /**
099: * 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 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 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 df = ((pattern == null) ? DateFormat
141: .getDateTimeInstance(DateFormat.SHORT,
142: DateFormat.SHORT, Locale.US)
143: : new SimpleDateFormat(pattern));
144: try {
145: long m = df.parse(dateTime).getTime();
146: if (m < 0) {
147: throw new BuildException(
148: "Date of "
149: + dateTime
150: + " results in negative milliseconds value"
151: + " relative to epoch (January 1, 1970, 00:00:00 GMT).");
152: }
153: setMillis(m);
154: } catch (ParseException pe) {
155: throw new BuildException(
156: "Date of "
157: + dateTime
158: + " Cannot be parsed correctly. It should be in"
159: + (pattern == null ? " MM/DD/YYYY HH:MM AM_PM"
160: : pattern) + " format.");
161: }
162: }
163: return when.evaluate(r.getLastModified(), millis.longValue(),
164: granularity);
165: }
166:
167: }
|