use of org.apache.tools.ant.types.resources.Union 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.resources.Union in project ant by apache.
the class Path method add.
/**
* Add a nested <code>ResourceCollection</code>.
* @param c the ResourceCollection to add.
* @since Ant 1.7
*/
public void add(ResourceCollection c) {
checkChildrenAllowed();
if (c == null) {
return;
}
if (union == null) {
union = new Union();
union.setProject(getProject());
union.setCache(cache);
}
union.add(c);
setChecked(false);
}
use of org.apache.tools.ant.types.resources.Union 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();
}
use of org.apache.tools.ant.types.resources.Union in project ant by apache.
the class ResourceUtils method selectSources.
/**
* Tells which sources should be reprocessed because the given
* selector selects at least one target.
*
* @param logTo where to send (more or less) interesting output.
* @param source ResourceCollection.
* @param mapper filename mapper indicating how to find the target Resources.
* @param targets object able to map a relative path as a Resource.
* @param selector returns a selector that is applied to target
* files. If it selects at least one target the source will be
* added to the returned collection.
* @return ResourceCollection.
* @since Ant 1.8.0
*/
public static ResourceCollection selectSources(final ProjectComponent logTo, ResourceCollection source, final FileNameMapper mapper, final ResourceFactory targets, final ResourceSelectorProvider selector) {
if (source.isEmpty()) {
logTo.log("No sources found.", Project.MSG_VERBOSE);
return Resources.NONE;
}
source = Union.getInstance(source);
final Union result = new Union();
for (final Resource sr : source) {
String srName = sr.getName();
srName = srName == null ? srName : srName.replace('/', File.separatorChar);
String[] targetnames = null;
try {
targetnames = mapper.mapFileName(srName);
} catch (final Exception e) {
logTo.log("Caught " + e + " mapping resource " + sr, Project.MSG_VERBOSE);
}
if (targetnames == null || targetnames.length == 0) {
logTo.log(sr + " skipped - don\'t know how to handle it", Project.MSG_VERBOSE);
continue;
}
final Union targetColl = new Union();
for (String targetname : targetnames) {
if (targetname == null) {
targetname = "(no name)";
}
targetColl.add(targets.getResource(targetname.replace(File.separatorChar, '/')));
}
// find the out-of-date targets:
final Restrict r = new Restrict();
r.add(selector.getTargetSelectorForSource(sr));
r.add(targetColl);
if (r.size() > 0) {
result.add(sr);
final Resource t = r.iterator().next();
logTo.log(sr.getName() + " added as " + t.getName() + (t.isExists() ? " is outdated." : " doesn\'t exist."), Project.MSG_VERBOSE);
continue;
}
// log uptodateness of all targets:
logTo.log(sr.getName() + " omitted as " + targetColl.toString() + (targetColl.size() == 1 ? " is" : " are ") + " up to date.", Project.MSG_VERBOSE);
}
return result;
}
use of org.apache.tools.ant.types.resources.Union in project ant by apache.
the class ImportTask method execute.
@Override
public void execute() {
if (file == null && resources.isEmpty()) {
throw new BuildException("import requires file attribute or at least one nested resource");
}
if (getOwningTarget() == null || !getOwningTarget().getName().isEmpty()) {
throw new BuildException("import only allowed as a top-level task");
}
ProjectHelper helper = getProject().getReference(ProjectHelper.PROJECTHELPER_REFERENCE);
if (helper == null) {
// this happens if the projecthelper was not registered with the project.
throw new BuildException("import requires support in ProjectHelper");
}
if (helper.getImportStack().isEmpty()) {
// helper that doesn't set the import.
throw new BuildException("import requires support in ProjectHelper");
}
if (getLocation() == null || getLocation().getFileName() == null) {
throw new BuildException("Unable to get location of import task");
}
Union resourcesToImport = new Union(getProject(), resources);
Resource fromFileAttribute = getFileAttributeResource();
if (fromFileAttribute != null) {
resources.add(fromFileAttribute);
}
for (Resource r : resourcesToImport) {
importResource(helper, r);
}
}
Aggregations