Search in sources :

Example 1 with FilterSet

use of org.apache.tools.ant.types.FilterSet in project ant by apache.

the class Copy method doResourceOperations.

/**
 * Actually does the resource copies.
 * This is a good method for subclasses to override.
 * @param map a map of source resource to array of destination files.
 * @since Ant 1.7
 */
protected void doResourceOperations(final Map<Resource, String[]> map) {
    if (!map.isEmpty()) {
        log("Copying " + map.size() + " resource" + (map.size() == 1 ? "" : "s") + " to " + destDir.getAbsolutePath());
        for (final Map.Entry<Resource, String[]> e : map.entrySet()) {
            final Resource fromResource = e.getKey();
            for (final String toFile : e.getValue()) {
                try {
                    log("Copying " + fromResource + " to " + toFile, verbosity);
                    final FilterSetCollection executionFilters = new FilterSetCollection();
                    if (filtering) {
                        executionFilters.addFilterSet(getProject().getGlobalFilterSet());
                    }
                    for (final FilterSet filterSet : filterSets) {
                        executionFilters.addFilterSet(filterSet);
                    }
                    ResourceUtils.copyResource(fromResource, new FileResource(destDir, toFile), executionFilters, filterChains, forceOverwrite, preserveLastModified, /* append: */
                    false, inputEncoding, outputEncoding, getProject(), getForce());
                } catch (final IOException ioe) {
                    String msg = "Failed to copy " + fromResource + " to " + toFile + " due to " + getDueTo(ioe);
                    final File targetFile = new File(toFile);
                    if (!(ioe instanceof ResourceUtils.ReadOnlyTargetFileException) && targetFile.exists() && !targetFile.delete()) {
                        msg += " and I couldn't delete the corrupt " + toFile;
                    }
                    if (failonerror) {
                        throw new BuildException(msg, ioe, getLocation());
                    }
                    log(msg, Project.MSG_ERR);
                }
            }
        }
    }
}
Also used : FilterSet(org.apache.tools.ant.types.FilterSet) FilterSetCollection(org.apache.tools.ant.types.FilterSetCollection) ResourceUtils(org.apache.tools.ant.util.ResourceUtils) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 2 with FilterSet

use of org.apache.tools.ant.types.FilterSet in project ant by apache.

the class Copy method createFilterSet.

/**
 * Add a filterset.
 * @return a filter set object.
 */
public FilterSet createFilterSet() {
    final FilterSet filterSet = new FilterSet();
    filterSets.addElement(filterSet);
    return filterSet;
}
Also used : FilterSet(org.apache.tools.ant.types.FilterSet)

Example 3 with FilterSet

use of org.apache.tools.ant.types.FilterSet in project ant by apache.

the class Copy method doFileOperations.

/**
 * Actually does the file (and possibly empty directory) copies.
 * This is a good method for subclasses to override.
 */
protected void doFileOperations() {
    if (!fileCopyMap.isEmpty()) {
        log("Copying " + fileCopyMap.size() + " file" + (fileCopyMap.size() == 1 ? "" : "s") + " to " + destDir.getAbsolutePath());
        for (final Map.Entry<String, String[]> e : fileCopyMap.entrySet()) {
            final String fromFile = e.getKey();
            for (final String toFile : e.getValue()) {
                if (fromFile.equals(toFile)) {
                    log("Skipping self-copy of " + fromFile, verbosity);
                    continue;
                }
                try {
                    log("Copying " + fromFile + " to " + toFile, verbosity);
                    final FilterSetCollection executionFilters = new FilterSetCollection();
                    if (filtering) {
                        executionFilters.addFilterSet(getProject().getGlobalFilterSet());
                    }
                    for (final FilterSet filterSet : filterSets) {
                        executionFilters.addFilterSet(filterSet);
                    }
                    fileUtils.copyFile(new File(fromFile), new File(toFile), executionFilters, filterChains, forceOverwrite, preserveLastModified, /* append: */
                    false, inputEncoding, outputEncoding, getProject(), getForce());
                } catch (final IOException ioe) {
                    String msg = "Failed to copy " + fromFile + " to " + toFile + " due to " + getDueTo(ioe);
                    final File targetFile = new File(toFile);
                    if (!(ioe instanceof ResourceUtils.ReadOnlyTargetFileException) && targetFile.exists() && !targetFile.delete()) {
                        msg += " and I couldn't delete the corrupt " + toFile;
                    }
                    if (failonerror) {
                        throw new BuildException(msg, ioe, getLocation());
                    }
                    log(msg, Project.MSG_ERR);
                }
            }
        }
    }
    if (includeEmpty) {
        int createCount = 0;
        for (final String[] dirs : dirCopyMap.values()) {
            for (String dir : dirs) {
                final File d = new File(dir);
                if (!d.exists()) {
                    if (!d.mkdirs() && !d.isDirectory()) {
                        log("Unable to create directory " + d.getAbsolutePath(), Project.MSG_ERR);
                    } else {
                        createCount++;
                    }
                }
            }
        }
        if (createCount > 0) {
            log("Copied " + dirCopyMap.size() + " empty director" + (dirCopyMap.size() == 1 ? "y" : "ies") + " to " + createCount + " empty director" + (createCount == 1 ? "y" : "ies") + " under " + destDir.getAbsolutePath());
        }
    }
}
Also used : FilterSet(org.apache.tools.ant.types.FilterSet) FilterSetCollection(org.apache.tools.ant.types.FilterSetCollection) ResourceUtils(org.apache.tools.ant.util.ResourceUtils) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Aggregations

FilterSet (org.apache.tools.ant.types.FilterSet)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BuildException (org.apache.tools.ant.BuildException)2 FilterSetCollection (org.apache.tools.ant.types.FilterSetCollection)2 ResourceUtils (org.apache.tools.ant.util.ResourceUtils)2 Resource (org.apache.tools.ant.types.Resource)1 FileResource (org.apache.tools.ant.types.resources.FileResource)1