FileDocCategorySizeDatePackage
Touch.javaAPI DocApache Ant 1.7012752Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs

Touch

public class Touch extends org.apache.tools.ant.Task
Touch a file and/or fileset(s) and/or filelist(s); corresponds to the Unix touch command.

If the file to touch doesn't exist, an empty one is created.

since
Ant 1.1
ant.task
category="filesystem"

Fields Summary
private static final DateFormatFactory
DEFAULT_DF_FACTORY
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
private File
file
private long
millis
private String
dateTime
private Vector
filesets
private org.apache.tools.ant.types.resources.Union
resources
private boolean
dateTimeConfigured
private boolean
mkdirs
private boolean
verbose
private org.apache.tools.ant.util.FileNameMapper
fileNameMapper
private DateFormatFactory
dfFactory
Constructors Summary
public Touch()
Construct a new Touch task.


              
      
    
Methods Summary
public voidadd(org.apache.tools.ant.types.ResourceCollection rc)
Add a collection of resources to touch.

param
rc the collection to add.
since
Ant 1.7

        resources.add(rc);
    
public voidadd(org.apache.tools.ant.util.FileNameMapper fileNameMapper)
Add a FileNameMapper.

param
fileNameMapper the FileNameMapper to add.
since
Ant 1.6.3
throws
BuildException if multiple mappers are added.

        if (this.fileNameMapper != null) {
            throw new BuildException("Only one mapper may be added to the "
                + getTaskName() + " task.");
        }
        this.fileNameMapper = fileNameMapper;
    
public voidaddConfiguredMapper(org.apache.tools.ant.types.Mapper mapper)
Add a Mapper.

param
mapper the Mapper to add.
since
Ant 1.6.3

        add(mapper.getImplementation());
    
public voidaddFilelist(org.apache.tools.ant.types.FileList list)
Add a filelist to touch.

param
list the Filelist to add.

        add(list);
    
public voidaddFileset(org.apache.tools.ant.types.FileSet set)
Add a set of files to touch.

param
set the Fileset to add.

        filesets.add(set);
        add(set);
    
protected synchronized voidcheckConfiguration()
Check that this task has been configured properly.

throws
BuildException if configuration errors are detected.
since
Ant 1.6.3

        if (file == null && resources.size() == 0) {
            throw new BuildException("Specify at least one source"
                                   + "--a file or resource collection.");
        }
        if (file != null && file.exists() && file.isDirectory()) {
            throw new BuildException("Use a resource collection to touch directories.");
        }
        if (dateTime != null && !dateTimeConfigured) {
            long workmillis = millis;
            DateFormat df = dfFactory.getPrimaryFormat();
            ParseException pe = null;
            try {
                workmillis = df.parse(dateTime).getTime();
            } catch (ParseException peOne) {
                df = dfFactory.getFallbackFormat();
                if (df == null) {
                    pe = peOne;
                } else {
                    try {
                        workmillis = df.parse(dateTime).getTime();
                    } catch (ParseException peTwo) {
                        pe = peTwo;
                    }
                }
            }
            if (pe != null) {
                throw new BuildException(pe.getMessage(), pe, getLocation());
            }
            if (workmillis < 0) {
                throw new BuildException("Date of " + dateTime
                                         + " results in negative "
                                         + "milliseconds value "
                                         + "relative to epoch "
                                         + "(January 1, 1970, "
                                         + "00:00:00 GMT).");
            }
            log("Setting millis to " + workmillis + " from datetime attribute",
                ((millis < 0) ? Project.MSG_DEBUG : Project.MSG_VERBOSE));
            setMillis(workmillis);
            //only set if successful to this point:
            dateTimeConfigured = true;
        }
    
public voidexecute()
Execute the touch operation.

throws
BuildException if an error occurs.

        checkConfiguration();
        touch();
    
private longgetTimestamp()

        return (millis < 0) ? System.currentTimeMillis() : millis;
    
public voidsetDatetime(java.lang.String dateTime)
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". Optional, default=now.

param
dateTime the String date in the specified format.

        if (this.dateTime != null) {
            log("Resetting datetime attribute to " + dateTime, Project.MSG_VERBOSE);
        }
        this.dateTime = dateTime;
        dateTimeConfigured = false;
    
public voidsetFile(java.io.File file)
Sets a single source file to touch. If the file does not exist an empty file will be created.

param
file the File to touch.

        this.file = file;
    
public voidsetMillis(long millis)
Set the new modification time of file(s) touched in milliseconds since midnight Jan 1 1970. Optional, default=now.

param
millis the long timestamp to use.

        this.millis = millis;
    
public voidsetMkdirs(boolean mkdirs)
Set whether nonexistent parent directories should be created when touching new files.

param
mkdirs boolean whether to create parent directories.
since
Ant 1.6.3

        this.mkdirs = mkdirs;
    
public voidsetPattern(java.lang.String pattern)
Set the format of the datetime attribute.

param
pattern the SimpleDateFormat-compatible format pattern.
since
Ant 1.6.3

        dfFactory = new DateFormatFactory() {
            public DateFormat getPrimaryFormat() {
                return new SimpleDateFormat(pattern);
            }
            public DateFormat getFallbackFormat() {
                return null;
            }
        };
    
public voidsetVerbose(boolean verbose)
Set whether the touch task will report every file it creates; defaults to true.

param
verbose boolean flag.
since
Ant 1.6.3

        this.verbose = verbose;
    
protected voidtouch()
Does the actual work; assumes everything has been checked by now.

throws
BuildException if an error occurs.

        long defaultTimestamp = getTimestamp();

        if (file != null) {
            touch(new FileResource(file.getParentFile(), file.getName()),
                  defaultTimestamp);
        }
        // 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[] srcDirs = ds.getIncludedDirectories();

            for (int j = 0; j < srcDirs.length; j++) {
                touch(new FileResource(fromDir, srcDirs[j]), defaultTimestamp);
            }
        }
    
protected voidtouch(java.io.File file)
Touch a single file with the current timestamp (this.millis). This method does not interact with any nested mappers and remains for reasons of backwards-compatibility only.

param
file file to touch
throws
BuildException on error
deprecated
since 1.6.x.

        touch(file, getTimestamp());
    
private voidtouch(org.apache.tools.ant.types.Resource r, long defaultTimestamp)

        if (fileNameMapper == null) {
            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(r.getName());
            if (mapped != null && mapped.length > 0) {
                long modTime = (r.isExists()) ? r.getLastModified()
                    : defaultTimestamp;
                for (int i = 0; i < mapped.length; i++) {
                    touch(getProject().resolveFile(mapped[i]), modTime);
                }
            }
        }
    
private voidtouch(java.io.File file, long modTime)

        if (!file.exists()) {
            log("Creating " + file,
                ((verbose) ? Project.MSG_INFO : Project.MSG_VERBOSE));
            try {
                FILE_UTILS.createNewFile(file, mkdirs);
            } catch (IOException ioe) {
                throw new BuildException("Could not create " + file, ioe,
                                         getLocation());
            }
        }
        if (!file.canWrite()) {
            throw new BuildException("Can not change modification date of "
                                     + "read-only file " + file);
        }
        FILE_UTILS.setFileLastModified(file, modTime);