Browse Source

Make <touch> work on <fileset>s

Submitted by:	Michael J. Sikorsky <mj@servidium.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268897 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
e6e450dc16
3 changed files with 82 additions and 31 deletions
  1. +2
    -0
      WHATSNEW
  2. +10
    -2
      docs/manual/CoreTasks/touch.html
  3. +70
    -29
      src/main/org/apache/tools/ant/taskdefs/Touch.java

+ 2
- 0
WHATSNEW View File

@@ -10,6 +10,8 @@ Changes that could break older environments:
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:
--------------



+ 10
- 2
docs/manual/CoreTasks/touch.html View File

@@ -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 @@ of now works, all other cases will emit a warning.</p>
<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 @@ modification time to the current time.</p>
<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> &lt;touch datetime=&quot;09/10/1974 4:30 pm&quot;&gt;
&lt;fileset dir=&quot;src_dir&quot;/&gt;
&lt;/touch&gt;</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 &copy; 2000,2001 Apache Software Foundation. All rights
Reserved.</p>


+ 70
- 29
src/main/org/apache/tools/ant/taskdefs/Touch.java View File

@@ -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.lang.reflect.Method;
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:stefan.bodewig@megabit.net">Stefan Bodewig</a>
* <p>Note: Setting the modification time of files is not supported in
* JDK 1.1.</p>
*
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
* @author <a href="mailto:mj@servidium.com">Michael J. Sikorsky</a>
* @author <a href="mailto:shaw@servidium.com">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 @@ public class Touch extends Task {
}

/**
* 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 @@ public class Touch extends Task {
}
}

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;
}


Loading…
Cancel
Save