Browse Source

support for Touchable resource collections in <touch>

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@349637 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 19 years ago
parent
commit
3af551b1bb
4 changed files with 85 additions and 45 deletions
  1. +24
    -16
      docs/manual/CoreTasks/touch.html
  2. +6
    -0
      src/etc/testcases/taskdefs/touch.xml
  3. +48
    -29
      src/main/org/apache/tools/ant/taskdefs/Touch.java
  4. +7
    -0
      src/testcases/org/apache/tools/ant/taskdefs/TouchTest.java

+ 24
- 16
docs/manual/CoreTasks/touch.html View File

@@ -10,11 +10,14 @@

<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. 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)
or a <a href="../CoreTypes/filelist.html">Filelist</a> (since Ant 1.6).</p>

<p>Changes the modification time of a resource and possibly creates it
at the same time. In addition to working with a single file, this Task
can also work on <a href="../CoreTypes/resource.html">resource</a> and
resource collections (which also includes directories). Prior to Ant
1.7 only FileSet or <a href="../CoreTypes/filelist.html">Filelist</a>
(since Ant 1.6) have been supported.</p>

<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
@@ -25,8 +28,8 @@ or a <a href="../CoreTypes/filelist.html">Filelist</a> (since Ant 1.6).</p>
<tr>
<td valign="top">file</td>
<td valign="top">The name of the file.</td>
<td valign="top" align="center">Unless a nested fileset element
or a nested filelist element has been specified.</td>
<td valign="top" align="center">Unless a nested resource collection element
has been specified.</td>
</tr>
<tr>
<td valign="top">millis</td>
@@ -60,15 +63,20 @@ or a <a href="../CoreTypes/filelist.html">Filelist</a> (since Ant 1.6).</p>
</tr>
</table>
<h3>Parameters specified as nested elements</h3>
<h4>fileset</h4>
<p>You can use any number of nested <code>&lt;fileset&gt;</code>
elements to define the files for this task and refer to
<code>&lt;fileset&gt;</code>s defined elsewhere.</p>
<h4>filelist</h4>
<p><em>Since Ant 1.6</em></p>
<p>You can use any number of nested <code>&lt;filelist&gt;</code>
elements to define the files for this task and refer to
<code>&lt;filelist&gt;</code>s defined elsewhere.</p>
<h4>any resource collection</h4>

<p>You can use any number of nested resource collection elements to
define the resource for this task and refer to resources defined
elsewhere. <b>Note:</b> resource passed to this task must implement
the <code>org.apache.tools.ant.types.resources.Touchable</code>
interface, this is true for all filesystem-based resources like those
returned by path, fileset ot filelist.</p>

<p>For backwards compatibility directories matched by nested filesets
will be "touched" as well, use a &lt;type&gt; selector to suppress
this. This only applies to filesets nested into the task directly,
not to filesets nested into a path or any other resource
collection.</p>

<h4>mapper</h4>
<p><em>Since Ant 1.6.3,</em> a nested <a href="../CoreTypes/mapper.html">


+ 6
- 0
src/etc/testcases/taskdefs/touch.xml View File

@@ -53,6 +53,12 @@
</touch>
</target>

<target name="testResourceCollection">
<touch millis="1662256000000">
<file file="touchtest"/>
</touch>
</target>

<target name="testMappedFileset">
<touch file="touchtest" millis="${mappermillis}" />
<touch>


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

@@ -22,6 +22,7 @@ import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Locale;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
@@ -31,6 +32,11 @@ import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.FileList;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.Touchable;
import org.apache.tools.ant.types.resources.Union;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.FileNameMapper;

@@ -78,7 +84,7 @@ public class Touch extends Task {
private long millis = -1;
private String dateTime;
private Vector filesets = new Vector();
private Vector filelists = new Vector();
private Union resources = new Union();
private boolean dateTimeConfigured;
private boolean mkdirs;
private boolean verbose = true;
@@ -188,7 +194,8 @@ public class Touch extends Task {
* @param set the <code>Fileset</code> to add.
*/
public void addFileset(FileSet set) {
filesets.addElement(set);
filesets.add(set);
add(set);
}

/**
@@ -196,7 +203,16 @@ public class Touch extends Task {
* @param list the <code>Filelist</code> to add.
*/
public void addFilelist(FileList list) {
filelists.addElement(list);
add(list);
}

/**
* Add a collection of resources to touch.
*
* @since Ant 1.7
*/
public void add(ResourceCollection rc) {
resources.add(rc);
}

/**
@@ -205,12 +221,12 @@ public class Touch extends Task {
* @since Ant 1.6.3
*/
protected synchronized void checkConfiguration() throws BuildException {
if (file == null && filesets.size() + filelists.size() == 0) {
if (file == null && resources.size() == 0) {
throw new BuildException("Specify at least one source"
+ "--a file, filelist or a fileset.");
+ "--a file or resource collection.");
}
if (file != null && file.exists() && file.isDirectory()) {
throw new BuildException("Use a fileset to touch directories.");
throw new BuildException("Use a resource collection to touch directories.");
}
if (dateTime != null && !dateTimeConfigured) {
long workmillis = millis;
@@ -266,33 +282,31 @@ public class Touch extends Task {
long defaultTimestamp = getTimestamp();

if (file != null) {
touch(file.getParentFile(), file.getName(), defaultTimestamp);
touch(new FileResource(file.getParentFile(), file.getName()),
defaultTimestamp);
}
// deal with the filesets
// deal with the resource collections
Iterator iter = resources.iterator();
while (iter.hasNext()) {
Resource r = (Resource) iter.next();
if (!(r instanceof Touchable)) {
throw new BuildException("Can't touch " + r);
}
touch(r, defaultTimestamp);
}

// deal with filesets in a special way since the task
// originally also used the directories and Union won't return
// them.
for (int i = 0; i < filesets.size(); i++) {
FileSet fs = (FileSet) filesets.elementAt(i);
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
File fromDir = fs.getDir(getProject());

String[] srcFiles = ds.getIncludedFiles();
String[] srcDirs = ds.getIncludedDirectories();

for (int j = 0; j < srcFiles.length; j++) {
touch(fromDir, srcFiles[j], defaultTimestamp);
}
for (int j = 0; j < srcDirs.length; j++) {
touch(fromDir, srcDirs[j], defaultTimestamp);
}
}
// deal with the filelists
for (int i = 0; i < filelists.size(); i++) {
FileList fl = (FileList) filelists.elementAt(i);
File fromDir = fl.getDir(getProject());

String[] srcFiles = fl.getFiles(getProject());

for (int j = 0; j < srcFiles.length; j++) {
touch(fromDir, srcFiles[j], defaultTimestamp);
touch(new FileResource(fromDir, srcDirs[j]), defaultTimestamp);
}
}
}
@@ -313,14 +327,19 @@ public class Touch extends Task {
return (millis < 0) ? System.currentTimeMillis() : millis;
}

private void touch(File fromDir, String filename, long defaultTimestamp) {
File f = FILE_UTILS.resolveFile(fromDir, filename);
private void touch(Resource r, long defaultTimestamp) {
if (fileNameMapper == null) {
touch(f, defaultTimestamp);
if (r instanceof FileResource) {
// use this to create file and deal with non-writable files
touch(((FileResource) r).getFile(), defaultTimestamp);
} else {
((Touchable) r).touch(defaultTimestamp);
}
} else {
String[] mapped = fileNameMapper.mapFileName(filename);
String[] mapped = fileNameMapper.mapFileName(r.getName());
if (mapped != null && mapped.length > 0) {
long modTime = (f.exists()) ? f.lastModified() : defaultTimestamp;
long modTime = (r.isExists()) ? r.getLastModified()
: defaultTimestamp;
for (int i = 0; i < mapped.length; i++) {
touch(getProject().resolveFile(mapped[i]), modTime);
}


+ 7
- 0
src/testcases/org/apache/tools/ant/taskdefs/TouchTest.java View File

@@ -105,6 +105,13 @@ public class TouchTest extends BuildFileTest {
touchFile("testFileset", 946080000000L);
}

/**
* test the resource collection
*/
public void testResourceCollection() {
touchFile("testResourceCollection", 1662256000000L);
}

/**
* test the mapped file set
*/


Loading…
Cancel
Save