use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.
the class Copy method execute.
/**
* Perform the copy operation.
* @exception BuildException if an error occurs.
*/
@Override
public void execute() throws BuildException {
// may be altered in validateAttributes
final File savedFile = file;
final File savedDestFile = destFile;
final File savedDestDir = destDir;
ResourceCollection savedRc = null;
if (file == null && destFile != null && rcs.size() == 1) {
// will be removed in validateAttributes
savedRc = rcs.elementAt(0);
}
try {
// make sure we don't have an illegal set of options
try {
validateAttributes();
} catch (final BuildException e) {
if (failonerror || !getMessage(e).equals(MSG_WHEN_COPYING_EMPTY_RC_TO_FILE)) {
throw e;
} else {
log("Warning: " + getMessage(e), Project.MSG_ERR);
return;
}
}
// deal with the single file
copySingleFile();
// deal with the ResourceCollections
/* for historical and performance reasons we have to do
things in a rather complex way.
(1) Move is optimized to move directories if a fileset
has been included completely, therefore FileSets need a
special treatment. This is also required to support
the failOnError semantic (skip filesets with broken
basedir but handle the remaining collections).
(2) We carry around a few protected methods that work
on basedirs and arrays of names. To optimize stuff, all
resources with the same basedir get collected in
separate lists and then each list is handled in one go.
*/
final Map<File, List<String>> filesByBasedir = new HashMap<>();
final Map<File, List<String>> dirsByBasedir = new HashMap<>();
final Set<File> baseDirs = new HashSet<>();
final List<Resource> nonFileResources = new ArrayList<>();
for (ResourceCollection rc : rcs) {
// Step (1) - beware of the ZipFileSet
if (rc instanceof FileSet && rc.isFilesystemOnly()) {
final FileSet fs = (FileSet) rc;
DirectoryScanner ds;
try {
ds = fs.getDirectoryScanner(getProject());
} catch (final BuildException e) {
if (failonerror || !getMessage(e).endsWith(DirectoryScanner.DOES_NOT_EXIST_POSTFIX)) {
throw e;
}
if (!quiet) {
log("Warning: " + getMessage(e), Project.MSG_ERR);
}
continue;
}
final File fromDir = fs.getDir(getProject());
final String[] srcFiles = ds.getIncludedFiles();
final String[] srcDirs = ds.getIncludedDirectories();
if (!flatten && mapperElement == null && ds.isEverythingIncluded() && !fs.hasPatterns()) {
completeDirMap.put(fromDir, destDir);
}
add(fromDir, srcFiles, filesByBasedir);
add(fromDir, srcDirs, dirsByBasedir);
baseDirs.add(fromDir);
} else {
if (!rc.isFilesystemOnly() && !supportsNonFileResources()) {
throw new BuildException("Only FileSystem resources are supported.");
}
for (final Resource r : rc) {
if (!r.isExists()) {
final String message = "Warning: Could not find resource " + r.toLongString() + " to copy.";
if (!failonerror) {
if (!quiet) {
log(message, Project.MSG_ERR);
}
} else {
throw new BuildException(message);
}
continue;
}
File baseDir = NULL_FILE_PLACEHOLDER;
String name = r.getName();
final FileProvider fp = r.as(FileProvider.class);
if (fp != null) {
final FileResource fr = ResourceUtils.asFileResource(fp);
baseDir = getKeyFile(fr.getBaseDir());
if (fr.getBaseDir() == null) {
name = fr.getFile().getAbsolutePath();
}
}
// files.
if (r.isDirectory() || fp != null) {
add(baseDir, name, r.isDirectory() ? dirsByBasedir : filesByBasedir);
baseDirs.add(baseDir);
} else {
// a not-directory file resource
// needs special treatment
nonFileResources.add(r);
}
}
}
}
iterateOverBaseDirs(baseDirs, dirsByBasedir, filesByBasedir);
// do all the copy operations now...
try {
doFileOperations();
} catch (final BuildException e) {
if (!failonerror) {
if (!quiet) {
log("Warning: " + getMessage(e), Project.MSG_ERR);
}
} else {
throw e;
}
}
if (!nonFileResources.isEmpty() || singleResource != null) {
final Resource[] nonFiles = nonFileResources.toArray(new Resource[nonFileResources.size()]);
// restrict to out-of-date resources
final Map<Resource, String[]> map = scan(nonFiles, destDir);
if (singleResource != null) {
map.put(singleResource, new String[] { destFile.getAbsolutePath() });
}
try {
doResourceOperations(map);
} catch (final BuildException e) {
if (!failonerror) {
if (!quiet) {
log("Warning: " + getMessage(e), Project.MSG_ERR);
}
} else {
throw e;
}
}
}
} finally {
// clean up again, so this instance can be used a second
// time
singleResource = null;
file = savedFile;
destFile = savedDestFile;
destDir = savedDestDir;
if (savedRc != null) {
rcs.insertElementAt(savedRc, 0);
}
fileCopyMap.clear();
dirCopyMap.clear();
completeDirMap.clear();
}
}
use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.
the class PathConvert method execute.
/**
* Do the execution.
* @throws BuildException if something is invalid.
*/
@Override
public void execute() throws BuildException {
Resources savedPath = path;
// may be altered in validateSetup
String savedPathSep = pathSep;
// may be altered in validateSetup
String savedDirSep = dirSep;
try {
// If we are a reference, create a Path from the reference
if (isReference()) {
Object o = refid.getReferencedObject(getProject());
if (!(o instanceof ResourceCollection)) {
throw new BuildException("refid '%s' does not refer to a resource collection.", refid.getRefId());
}
getPath().add((ResourceCollection) o);
}
// validate our setup
validateSetup();
// Currently, we deal with only two path formats: Unix and Windows
// And Unix is everything that is not Windows
// (with the exception for NetWare and OS/2 below)
// for NetWare and OS/2, piggy-back on Windows, since here and
// in the apply code, the same assumptions can be made as with
// windows - that \\ is an OK separator, and do comparisons
// case-insensitive.
String fromDirSep = onWindows ? "\\" : "/";
StringBuilder rslt = new StringBuilder();
ResourceCollection resources = isPreserveDuplicates() ? path : new Union(path);
List<String> ret = new ArrayList<>();
FileNameMapper mapperImpl = mapper == null ? new IdentityMapper() : mapper.getImplementation();
for (Resource r : resources) {
String[] mapped = mapperImpl.mapFileName(String.valueOf(r));
for (int m = 0; mapped != null && m < mapped.length; ++m) {
ret.add(mapped[m]);
}
}
boolean first = true;
for (String string : ret) {
// Apply the path prefix map
String elem = mapElement(string);
if (!first) {
rslt.append(pathSep);
}
first = false;
StringTokenizer stDirectory = new StringTokenizer(elem, fromDirSep, true);
while (stDirectory.hasMoreTokens()) {
String token = stDirectory.nextToken();
rslt.append(fromDirSep.equals(token) ? dirSep : token);
}
}
// unless setonempty == false
if (setonempty || rslt.length() > 0) {
String value = rslt.toString();
if (property == null) {
log(value);
} else {
log("Set property " + property + " = " + value, Project.MSG_VERBOSE);
getProject().setNewProperty(property, value);
}
}
} finally {
path = savedPath;
dirSep = savedDirSep;
pathSep = savedPathSep;
}
}
use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.
the class ResourceContains method resolveRefid.
private void resolveRefid() {
try {
if (getProject() == null) {
throw new BuildException("Cannot retrieve refid; project unset");
}
Object o = getProject().getReference(refid);
if (!(o instanceof Resource)) {
if (o instanceof ResourceCollection) {
ResourceCollection rc = (ResourceCollection) o;
if (rc.size() == 1) {
o = rc.iterator().next();
}
} else {
throw new BuildException("Illegal value at '%s': %s", refid, o);
}
}
this.resource = (Resource) o;
} finally {
refid = null;
}
}
use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.
the class Difference method getCollection.
/**
* Calculate the difference of the nested ResourceCollections.
* @return a Collection of Resources.
*/
@Override
protected Collection<Resource> getCollection() {
List<ResourceCollection> rcs = getResourceCollections();
int size = rcs.size();
if (size < 2) {
throw new BuildException("The difference of %d resource %s is undefined.", size, size == 1 ? "collection" : "collections");
}
Set<Resource> hs = new HashSet<>();
List<Resource> al = new ArrayList<>();
for (ResourceCollection rc : rcs) {
for (Resource r : rc) {
if (hs.add(r)) {
al.add(r);
} else {
al.remove(r);
}
}
}
return al;
}
use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.
the class ResourceUtils method selectOutOfDateSources.
/**
* Tells which source files should be reprocessed based on the
* last modification date of target files.
* @param logTo where to send (more or less) interesting output.
* @param source array of resources bearing relative path and last
* modification date.
* @param mapper filename mapper indicating how to find the target
* files.
* @param targets object able to map as a resource a relative path
* at <b>destination</b>.
* @param granularity The number of milliseconds leeway to give
* before deciding a target is out of date.
* @return array containing the source files which need to be
* copied or processed, because the targets are out of date or do
* not exist.
* @since Ant 1.6.2
*/
public static Resource[] selectOutOfDateSources(final ProjectComponent logTo, final Resource[] source, final FileNameMapper mapper, final ResourceFactory targets, final long granularity) {
final Union u = new Union();
u.addAll(Arrays.asList(source));
final ResourceCollection rc = selectOutOfDateSources(logTo, u, mapper, targets, granularity);
return rc.size() == 0 ? new Resource[0] : ((Union) rc).listResources();
}
Aggregations