bodewig 01/03/29 01:37:13
Modified: . WHATSNEW
docs/manual/CoreTasks touch.html
src/main/org/apache/tools/ant/taskdefs Touch.java
Log:
Make <touch> work on <fileset>s
Submitted by: Michael J. Sikorsky <[EMAIL PROTECTED]>
Revision Changes Path
1.94 +2 -0 jakarta-ant/WHATSNEW
Index: WHATSNEW
===================================================================
RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
retrieving revision 1.93
retrieving revision 1.94
diff -u -r1.93 -r1.94
--- WHATSNEW 2001/03/19 13:18:30 1.93
+++ WHATSNEW 2001/03/29 09:37:06 1.94
@@ -10,6 +10,8 @@
rmic to use a new compiler a lot easier but may break custom
versions of this task that rely on the old implementation.
+* <touch> can now work on <fileset>s
+
Other changes:
--------------
1.3 +10 -2 jakarta-ant/docs/manual/CoreTasks/touch.html
Index: touch.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/touch.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- touch.html 2001/02/13 12:31:52 1.2
+++ touch.html 2001/03/29 09:37:10 1.3
@@ -10,7 +10,9 @@
<h2><a name="touch">Touch</a></h2>
<h3>Description</h3>
<p>Changes the modification time of a file and possibly creates it at
-the same time.</p>
+the same time. In addition to working with a single file, this Task
+can also work a <a href="../CoreTypes/fileset.html">Fileset</a> (which
+also includes directories).</p>
<p>For JDK 1.1 only the creation of new files with a modification time
of now works, all other cases will emit a warning.</p>
<h3>Parameters</h3>
@@ -23,7 +25,8 @@
<tr>
<td valign="top">file</td>
<td valign="top">the name of the file</td>
- <td valign="top" align="center">Yes</td>
+ <td valign="top" align="center">unless a nested fileset element
+ has been specified.</td>
</tr>
<tr>
<td valign="top">millis</td>
@@ -48,6 +51,11 @@
<p>creates <code>myfile</code> if it doesn't exist and changes the
modification time to Jun, 28 2000 2:02 pm (14:02 for those used to 24
hour times).</p>
+<pre> <touch datetime="09/10/1974 4:30 pm">
+ <fileset dir="src_dir"/>
+ </touch></pre>
+<p>changes the modification time to Oct, 09 1974 4:30 pm of all files and
directories
+ found in <code>src_dir</code>. </p>
<hr>
<p align="center">Copyright © 2000,2001 Apache Software Foundation. All
rights
Reserved.</p>
1.7 +70 -29
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Touch.java
Index: Touch.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Touch.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Touch.java 2001/03/19 13:05:45 1.6
+++ Touch.java 2001/03/29 09:37:12 1.7
@@ -55,6 +55,8 @@
package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.*;
+import org.apache.tools.ant.types.*;
+import org.apache.tools.ant.util.*;
import java.io.File;
import java.io.FileOutputStream;
@@ -64,25 +66,33 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
+import java.util.Enumeration;
import java.util.Locale;
+import java.util.Vector;
/**
- * Touch a file - corresponds to the Unix touch command.
+ * Touch a file and/or fileset(s) -- corresponds to the Unix touch command.
*
* <p>If the file to touch doesn't exist, an empty one is
- * created. Setting the modification time of files is not supported in
- * JDK 1.1.
+ * created. </p>
*
- * @author <a href="mailto:[EMAIL PROTECTED]">Stefan Bodewig</a>
+ * <p>Note: Setting the modification time of files is not supported in
+ * JDK 1.1.</p>
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Stefan Bodewig</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Michael J. Sikorsky</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Robert Shaw</a>
*/
public class Touch extends Task {
private File file; // required
private long millis = -1;
private String dateTime;
+ private Vector filesets = new Vector();
/**
- * The name of the file to touch.
+ * Sets a single source file to touch. If the file does not exist
+ * an empty file will be created.
*/
public void setFile(File file) {
this.file = file;
@@ -103,18 +113,25 @@
}
/**
- * Do the work.
- *
- * @exception BuildException Thrown in unrecoverable error.
+ * Adds a set of files (nested fileset attribute).
*/
+ public void addFileset(FileSet set) {
+ filesets.addElement(set);
+ }
+
+ /**
+ * Execute the touch operation.
+ */
public void execute() throws BuildException {
- if (file.exists() && project.getJavaVersion() == Project.JAVA_1_1) {
- log("Cannot change the modification time of "
- + file + " in JDK 1.1",
- Project.MSG_WARN);
- return;
+ if (file == null && filesets.size() == 0) {
+ throw
+ new BuildException("Specify at least one source - a file or
a fileset.");
}
-
+
+ if (file != null && file.exists() && file.isDirectory()) {
+ throw new BuildException("Use a fileset to touch directories.");
+ }
+
if (dateTime != null) {
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT,
@@ -126,30 +143,54 @@
}
}
- if (millis >= 0 && project.getJavaVersion() == Project.JAVA_1_1) {
- log(file + " will be created but its modification time cannot be
set in JDK 1.1",
- Project.MSG_WARN);
- }
-
touch();
}
/**
* Does the actual work. Entry point for Untar and Expand as well.
*/
- void touch() throws BuildException {
- if (!file.exists()) {
- log("Creating "+file, Project.MSG_INFO);
- try {
- FileOutputStream fos = new FileOutputStream(file);
- fos.write(new byte[0]);
- fos.close();
- } catch (IOException ioe) {
- throw new BuildException("Could not create "+file, ioe,
- location);
+ protected void touch() throws BuildException {
+ if (file != null) {
+ if (!file.exists()) {
+ log("Creating "+file, Project.MSG_INFO);
+ try {
+ FileOutputStream fos = new FileOutputStream(file);
+ fos.write(new byte[0]);
+ fos.close();
+ } catch (IOException ioe) {
+ throw new BuildException("Could not create "+file, ioe,
+ location);
+ }
}
+ touch(file);
}
+ if (millis >= 0 && project.getJavaVersion() == Project.JAVA_1_1) {
+ log("modification time of files cannot be set in JDK 1.1",
+ Project.MSG_WARN);
+ return;
+ }
+
+ // deal with the filesets
+ for (int i=0; i < filesets.size(); i++) {
+ FileSet fs = (FileSet) filesets.elementAt(i);
+ DirectoryScanner ds = fs.getDirectoryScanner(project);
+ File fromDir = fs.getDir(project);
+
+ String[] srcFiles = ds.getIncludedFiles();
+ String[] srcDirs = ds.getIncludedDirectories();
+
+ for(int j=0; j < srcFiles.length ; j++) {
+ touch(new File(fromDir, srcFiles[j]));
+ }
+
+ for(int j=0; j < srcDirs.length ; j++) {
+ touch(new File(fromDir, srcDirs[j]));
+ }
+ }
+ }
+
+ protected void touch(File file) {
if (project.getJavaVersion() == Project.JAVA_1_1) {
return;
}