diff --git a/WHATSNEW b/WHATSNEW index 0a785878e..f7042cb1a 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -836,6 +836,9 @@ Other changes: arbitrary JDBC connection properties. Bugzilla Report 33452. + * A new islastmodified condition can check the last modified date of + resources. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTasks/conditions.html b/docs/manual/CoreTasks/conditions.html index e49c07f92..e0ad40caf 100644 --- a/docs/manual/CoreTasks/conditions.html +++ b/docs/manual/CoreTasks/conditions.html @@ -1007,5 +1007,55 @@ is redundant and will be ignored.

 <hasfreespace partition="c:" needed="100M"/>
 
+ +

islastmodified

+ +

Tests the last modified date of a resource. Since Ant +1.8.0

+ + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
millisSpecifies the expected modification time of the resource + in milliseconds since midnight Jan 1 1970.Exactly one of the + two.
datetimeSpecifies the expected modification time of the + resource. The special value "now" indicates the + current time.
patternSimpleDateFormat-compatible pattern string. + Defaults to MM/DD/YYYY HH:MM AM_or_PM or MM/DD/YYYY HH:MM:SS AM_or_PM. + No
modeHow to compare the timestamp. Accepted values + are "equals", "before", "not-before", "after" and "not-after". + No, defaults to "equals".
+ +

The actual resource to test is specified as a nested element.

+ +

+ An example: +

+
+<islastmodified dateTime="08/18/2009 04:41:19 AM" mode="not-before">
+  <file file="${file}"/>
+</islastmodified>
+
diff --git a/src/main/org/apache/tools/ant/taskdefs/Touch.java b/src/main/org/apache/tools/ant/taskdefs/Touch.java index 80d4babed..a4f344ef8 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Touch.java +++ b/src/main/org/apache/tools/ant/taskdefs/Touch.java @@ -54,12 +54,12 @@ import org.apache.tools.ant.util.FileNameMapper; */ public class Touch extends Task { - private interface DateFormatFactory { + public interface DateFormatFactory { DateFormat getPrimaryFormat(); DateFormat getFallbackFormat(); } - private static final DateFormatFactory DEFAULT_DF_FACTORY + public static final DateFormatFactory DEFAULT_DF_FACTORY = new DateFormatFactory() { /* * The initial version used DateFormat.SHORT for the diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/IsLastModified.java b/src/main/org/apache/tools/ant/taskdefs/condition/IsLastModified.java new file mode 100644 index 000000000..1881f868d --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/condition/IsLastModified.java @@ -0,0 +1,193 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.taskdefs.condition; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.taskdefs.Touch; +import org.apache.tools.ant.types.EnumeratedAttribute; +import org.apache.tools.ant.types.Resource; + +/** + * Condition that makes assertions about the last modified date of a + * resource. + * + * @since Ant 1.8.0 + */ +public class IsLastModified extends ProjectComponent implements Condition { + private long millis = -1; + private String dateTime = null; + private Touch.DateFormatFactory dfFactory = Touch.DEFAULT_DF_FACTORY; + private Resource resource; + private CompareMode mode = CompareMode.EQUALS; + + /** + * Set the new modification time of file(s) touched + * in milliseconds since midnight Jan 1 1970. + * @param millis the long timestamp to use. + */ + public void setMillis(long millis) { + this.millis = millis; + } + + /** + * Set the new modification time of file(s) touched + * in the format "MM/DD/YYYY HH:MM AM or PM" + * or "MM/DD/YYYY HH:MM:SS AM or PM". + * @param dateTime the String date in the specified format. + */ + public void setDatetime(String dateTime) { + this.dateTime = dateTime; + } + + /** + * Set the format of the datetime attribute. + * @param pattern the SimpleDateFormat-compatible + * format pattern. + */ + public void setPattern(final String pattern) { + dfFactory = new Touch.DateFormatFactory() { + public DateFormat getPrimaryFormat() { + return new SimpleDateFormat(pattern); + } + public DateFormat getFallbackFormat() { + return null; + } + }; + } + + /** + * The resource to test. + */ + public void add(Resource r) { + if (resource != null) { + throw new BuildException("only one resource can be tested"); + } + resource = r; + } + + /** + * The type of comparison to test. + */ + public void setMode(CompareMode mode) { + this.mode = mode; + } + + /** + * Argument validation. + */ + protected void validate() throws BuildException { + if (millis >= 0 && dateTime != null) { + throw new BuildException("Only one of dateTime and millis can be" + + " set"); + } + if (millis < 0 && dateTime == null) { + throw new BuildException("millis or dateTime is required"); + } + if (resource == null) { + throw new BuildException("resource is required"); + } + } + + /** + * Calculate timestamp as millis either based on millis or + * dateTime (and pattern) attribute. + */ + protected long getMillis() throws BuildException { + if (millis >= 0) { + return millis; + } + if ("now".equalsIgnoreCase(dateTime)) { + return System.currentTimeMillis(); + } + DateFormat df = dfFactory.getPrimaryFormat(); + ParseException pe = null; + try { + return df.parse(dateTime).getTime(); + } catch (ParseException peOne) { + df = dfFactory.getFallbackFormat(); + if (df == null) { + pe = peOne; + } else { + try { + return df.parse(dateTime).getTime(); + } catch (ParseException peTwo) { + pe = peTwo; + } + } + } + if (pe != null) { + throw new BuildException(pe.getMessage(), pe, getLocation()); + } + /* NOTREACHED */ + return 0; + } + + public boolean eval() throws BuildException { + validate(); + long expected = getMillis(); + long actual = resource.getLastModified(); + if (CompareMode.EQUALS_TEXT.equals(mode.getValue())) { + return expected == actual; + } + if (CompareMode.BEFORE_TEXT.equals(mode.getValue())) { + return expected > actual; + } + if (CompareMode.NOT_BEFORE_TEXT.equals(mode.getValue())) { + return expected <= actual; + } + if (CompareMode.AFTER_TEXT.equals(mode.getValue())) { + return expected < actual; + } + if (CompareMode.NOT_AFTER_TEXT.equals(mode.getValue())) { + return expected >= actual; + } + throw new BuildException("Unknown mode " + mode.getValue()); + } + + public static class CompareMode extends EnumeratedAttribute { + private static final String EQUALS_TEXT = "equals"; + private static final String BEFORE_TEXT = "before"; + private static final String AFTER_TEXT = "after"; + private static final String NOT_BEFORE_TEXT = "not-before"; + private static final String NOT_AFTER_TEXT = "not-after"; + + private static final CompareMode EQUALS = new CompareMode(EQUALS_TEXT); + + public CompareMode() { + this(EQUALS_TEXT); + } + + public CompareMode(String s) { + super(); + setValue(s); + } + + public String[] getValues() { + return new String[] { + EQUALS_TEXT, BEFORE_TEXT, AFTER_TEXT, NOT_BEFORE_TEXT, + NOT_AFTER_TEXT, + }; + } + } +} diff --git a/src/main/org/apache/tools/ant/types/conditions/antlib.xml b/src/main/org/apache/tools/ant/types/conditions/antlib.xml index 7f9a1e754..f6cf3eaab 100644 --- a/src/main/org/apache/tools/ant/types/conditions/antlib.xml +++ b/src/main/org/apache/tools/ant/types/conditions/antlib.xml @@ -21,7 +21,7 @@ --> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +