Search in sources :

Example 1 with Touchable

use of org.apache.tools.ant.types.resources.Touchable in project ant by apache.

the class ResourceUtils method copyResource.

/**
 * Convenience method to copy content from one Resource to another
 * specifying whether token filtering must be used, whether filter chains
 * must be used, whether newer destination files may be overwritten and
 * whether the last modified time of <code>dest</code> file should be made
 * equal to the last modified time of <code>source</code>.
 *
 * @param source the Resource to copy from.
 *                   Must not be <code>null</code>.
 * @param dest   the Resource to copy to.
 *                 Must not be <code>null</code>.
 * @param filters the collection of filters to apply to this copy.
 * @param filterChains filterChains to apply during the copy.
 * @param overwrite Whether or not the destination Resource should be
 *                  overwritten if it already exists.
 * @param preserveLastModified Whether or not the last modified time of
 *                             the destination Resource should be set to that
 *                             of the source.
 * @param append Whether to append to an Appendable Resource.
 * @param inputEncoding the encoding used to read the files.
 * @param outputEncoding the encoding used to write the files.
 * @param project the project instance.
 * @param force whether read-only target files will be overwritten
 *
 * @throws IOException if the copying fails.
 *
 * @since Ant 1.8.2
 */
public static void copyResource(final Resource source, final Resource dest, final FilterSetCollection filters, final Vector<FilterChain> filterChains, final boolean overwrite, final boolean preserveLastModified, final boolean append, final String inputEncoding, final String outputEncoding, final Project project, final boolean force) throws IOException {
    if (!overwrite && !SelectorUtils.isOutOfDate(source, dest, FileUtils.getFileUtils().getFileTimestampGranularity())) {
        return;
    }
    final boolean filterSetsAvailable = (filters != null && filters.hasFilters());
    final boolean filterChainsAvailable = (filterChains != null && !filterChains.isEmpty());
    String effectiveInputEncoding;
    if (source instanceof StringResource) {
        effectiveInputEncoding = ((StringResource) source).getEncoding();
    } else {
        effectiveInputEncoding = inputEncoding;
    }
    File destFile = null;
    if (dest.as(FileProvider.class) != null) {
        destFile = dest.as(FileProvider.class).getFile();
    }
    if (destFile != null && destFile.isFile() && !destFile.canWrite()) {
        if (!force) {
            throw new ReadOnlyTargetFileException(destFile);
        }
        if (!FILE_UTILS.tryHardToDelete(destFile)) {
            throw new IOException("failed to delete read-only destination file " + destFile);
        }
    }
    if (filterSetsAvailable) {
        copyWithFilterSets(source, dest, filters, filterChains, append, effectiveInputEncoding, outputEncoding, project);
    } else if (filterChainsAvailable || (effectiveInputEncoding != null && !effectiveInputEncoding.equals(outputEncoding)) || (effectiveInputEncoding == null && outputEncoding != null)) {
        copyWithFilterChainsOrTranscoding(source, dest, filterChains, append, effectiveInputEncoding, outputEncoding, project);
    } else {
        boolean copied = false;
        if (source.as(FileProvider.class) != null && destFile != null && !append) {
            final File sourceFile = source.as(FileProvider.class).getFile();
            try {
                copyUsingFileChannels(sourceFile, destFile, project);
                copied = true;
            } catch (final IOException ex) {
                String msg = "Attempt to copy " + sourceFile + " to " + destFile + " using NIO Channels" + " failed due to '" + ex.getMessage() + "'.  Falling back to streams.";
                if (project != null) {
                    project.log(msg, Project.MSG_WARN);
                } else {
                    System.err.println(msg);
                }
            }
        }
        if (!copied) {
            copyUsingStreams(source, dest, append, project);
        }
    }
    if (preserveLastModified) {
        final Touchable t = dest.as(Touchable.class);
        if (t != null) {
            setLastModified(t, source.getLastModified());
        }
    }
}
Also used : StringResource(org.apache.tools.ant.types.resources.StringResource) FileProvider(org.apache.tools.ant.types.resources.FileProvider) IOException(java.io.IOException) File(java.io.File) Touchable(org.apache.tools.ant.types.resources.Touchable)

Example 2 with Touchable

use of org.apache.tools.ant.types.resources.Touchable in project ant by apache.

the class Touch method touch.

/**
 * Does the actual work; assumes everything has been checked by now.
 * @throws BuildException if an error occurs.
 */
protected void touch() throws BuildException {
    long defaultTimestamp = getTimestamp();
    if (file != null) {
        touch(new FileResource(file.getParentFile(), file.getName()), defaultTimestamp);
    }
    if (resources == null) {
        return;
    }
    // deal with the resource collections
    for (Resource r : resources) {
        Touchable t = r.as(Touchable.class);
        if (t == null) {
            throw new BuildException("Can't touch " + r);
        }
        touch(r, defaultTimestamp);
    }
    // them.
    for (FileSet fs : filesets) {
        DirectoryScanner ds = fs.getDirectoryScanner(getProject());
        File fromDir = fs.getDir(getProject());
        for (String srcDir : ds.getIncludedDirectories()) {
            touch(new FileResource(fromDir, srcDir), defaultTimestamp);
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) Touchable(org.apache.tools.ant.types.resources.Touchable)

Aggregations

File (java.io.File)2 Touchable (org.apache.tools.ant.types.resources.Touchable)2 IOException (java.io.IOException)1 BuildException (org.apache.tools.ant.BuildException)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 FileSet (org.apache.tools.ant.types.FileSet)1 Resource (org.apache.tools.ant.types.Resource)1 FileProvider (org.apache.tools.ant.types.resources.FileProvider)1 FileResource (org.apache.tools.ant.types.resources.FileResource)1 StringResource (org.apache.tools.ant.types.resources.StringResource)1