use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.
the class Copy method scan.
/**
* Compares source files to destination files to see if they should be
* copied.
*
* @param fromDir The source directory.
* @param toDir The destination directory.
* @param files A list of files to copy.
* @param dirs A list of directories to copy.
*/
protected void scan(final File fromDir, final File toDir, final String[] files, final String[] dirs) {
final FileNameMapper mapper = getMapper();
buildMap(fromDir, toDir, files, mapper, fileCopyMap);
if (includeEmpty) {
buildMap(fromDir, toDir, dirs, mapper, dirCopyMap);
}
}
use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.
the class Copy method buildMap.
/**
* Create a map of resources to copy.
*
* @param fromResources The source resources.
* @param toDir the destination directory.
* @param mapper a <code>FileNameMapper</code> value.
* @return a map of source resource to array of destination files.
* @since Ant 1.7
*/
protected Map<Resource, String[]> buildMap(final Resource[] fromResources, final File toDir, final FileNameMapper mapper) {
final Map<Resource, String[]> map = new HashMap<>();
Resource[] toCopy;
if (forceOverwrite) {
final List<Resource> v = new ArrayList<>();
for (Resource rc : fromResources) {
if (mapper.mapFileName(rc.getName()) != null) {
v.add(rc);
}
}
toCopy = v.toArray(new Resource[v.size()]);
} else {
toCopy = ResourceUtils.selectOutOfDateSources(this, fromResources, mapper, name -> new FileResource(toDir, name), granularity);
}
for (Resource rc : toCopy) {
final String[] mappedFiles = mapper.mapFileName(rc.getName());
if (mappedFiles == null || mappedFiles.length == 0) {
throw new BuildException("Can't copy a resource without a" + " name if the mapper doesn't" + " provide one.");
}
if (!enableMultipleMappings) {
map.put(rc, new String[] { new File(toDir, mappedFiles[0]).getAbsolutePath() });
} else {
// reuse the array created by the mapper
for (int k = 0; k < mappedFiles.length; k++) {
mappedFiles[k] = new File(toDir, mappedFiles[k]).getAbsolutePath();
}
map.put(rc, mappedFiles);
}
}
return map;
}
use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.
the class XSLTProcess method add.
/**
* Adds a nested filenamemapper.
* @param fileNameMapper the mapper to add
* @exception BuildException if more than one mapper is defined
* @since Ant 1.7.0
*/
public void add(final FileNameMapper fileNameMapper) throws BuildException {
final Mapper mapper = new Mapper(getProject());
mapper.add(fileNameMapper);
addMapper(mapper);
}
use of org.apache.tools.ant.util.FileNameMapper 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.util.FileNameMapper in project ant by apache.
the class PropertySet method iterator.
/**
* Fulfill the ResourceCollection interface.
* @return an Iterator of Resources.
* @since Ant 1.7
*/
@Override
public Iterator<Resource> iterator() {
if (isReference()) {
return getRef().iterator();
}
dieOnCircularReference();
Stream<Resource> result = getPropertyNames(getEffectiveProperties()).stream().map(name -> new PropertyResource(getProject(), name));
Optional<FileNameMapper> m = Optional.ofNullable(getMapper()).map(Mapper::getImplementation);
if (m.isPresent()) {
result = result.map(p -> new MappedResource(p, m.get()));
}
return result.iterator();
}
Aggregations