Search in sources :

Example 1 with FileNameMapper

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);
    }
}
Also used : FileNameMapper(org.apache.tools.ant.util.FileNameMapper) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper)

Example 2 with FileNameMapper

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;
}
Also used : Mapper(org.apache.tools.ant.types.Mapper) Task(org.apache.tools.ant.Task) FilterChain(org.apache.tools.ant.types.FilterChain) FilterSetCollection(org.apache.tools.ant.types.FilterSetCollection) HashMap(java.util.HashMap) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) FileResource(org.apache.tools.ant.types.resources.FileResource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FilterSet(org.apache.tools.ant.types.FilterSet) SourceFileScanner(org.apache.tools.ant.util.SourceFileScanner) Vector(java.util.Vector) FileSet(org.apache.tools.ant.types.FileSet) Map(java.util.Map) Project(org.apache.tools.ant.Project) Hashtable(java.util.Hashtable) FileUtils(org.apache.tools.ant.util.FileUtils) Resource(org.apache.tools.ant.types.Resource) LinkedHashtable(org.apache.tools.ant.util.LinkedHashtable) Set(java.util.Set) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) File(java.io.File) List(java.util.List) FileProvider(org.apache.tools.ant.types.resources.FileProvider) ResourceUtils(org.apache.tools.ant.util.ResourceUtils) HashMap(java.util.HashMap) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) ArrayList(java.util.ArrayList) FileResource(org.apache.tools.ant.types.resources.FileResource) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 3 with FileNameMapper

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);
}
Also used : Mapper(org.apache.tools.ant.types.Mapper) FileNameMapper(org.apache.tools.ant.util.FileNameMapper)

Example 4 with FileNameMapper

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;
    }
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.tools.ant.types.Resource) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) Union(org.apache.tools.ant.types.resources.Union) StringTokenizer(java.util.StringTokenizer) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) Resources(org.apache.tools.ant.types.resources.Resources) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 5 with FileNameMapper

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();
}
Also used : Properties(java.util.Properties) Iterator(java.util.Iterator) Enumeration(java.util.Enumeration) RegexpMatcher(org.apache.tools.ant.util.regexp.RegexpMatcher) Set(java.util.Set) HashMap(java.util.HashMap) BuildException(org.apache.tools.ant.BuildException) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) Stack(java.util.Stack) ArrayList(java.util.ArrayList) RegexpMatcherFactory(org.apache.tools.ant.util.regexp.RegexpMatcherFactory) HashSet(java.util.HashSet) PropertyResource(org.apache.tools.ant.types.resources.PropertyResource) List(java.util.List) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) MappedResource(org.apache.tools.ant.types.resources.MappedResource) Map(java.util.Map) Entry(java.util.Map.Entry) Optional(java.util.Optional) Project(org.apache.tools.ant.Project) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) PropertyResource(org.apache.tools.ant.types.resources.PropertyResource) MappedResource(org.apache.tools.ant.types.resources.MappedResource) PropertyResource(org.apache.tools.ant.types.resources.PropertyResource) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) MappedResource(org.apache.tools.ant.types.resources.MappedResource)

Aggregations

FileNameMapper (org.apache.tools.ant.util.FileNameMapper)24 BuildException (org.apache.tools.ant.BuildException)16 File (java.io.File)9 IdentityMapper (org.apache.tools.ant.util.IdentityMapper)8 Resource (org.apache.tools.ant.types.Resource)6 IOException (java.io.IOException)5 Project (org.apache.tools.ant.Project)5 Mapper (org.apache.tools.ant.types.Mapper)5 FlatFileNameMapper (org.apache.tools.ant.util.FlatFileNameMapper)5 MergingMapper (org.apache.tools.ant.util.MergingMapper)5 List (java.util.List)4 GlobPatternMapper (org.apache.tools.ant.util.GlobPatternMapper)4 SourceFileScanner (org.apache.tools.ant.util.SourceFileScanner)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Stream (java.util.stream.Stream)3 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)3 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)3 FileResource (org.apache.tools.ant.types.resources.FileResource)3 HashSet (java.util.HashSet)2