Search in sources :

Example 16 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.

the class MappedResourceCollection method getCollection.

private Collection<Resource> getCollection() {
    FileNameMapper m = mapper == null ? new IdentityMapper() : mapper.getImplementation();
    Stream<MappedResource> stream;
    if (enableMultipleMappings) {
        stream = nested.stream().flatMap(r -> Stream.of(m.mapFileName(r.getName())).filter(Objects::nonNull).map(MergingMapper::new).map(mm -> new MappedResource(r, mm)));
    } else {
        stream = nested.stream().map(r -> new MappedResource(r, m));
    }
    return stream.collect(Collectors.toList());
}
Also used : DataType(org.apache.tools.ant.types.DataType) Mapper(org.apache.tools.ant.types.Mapper) Resource(org.apache.tools.ant.types.Resource) Iterator(java.util.Iterator) Collection(java.util.Collection) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) Collectors(java.util.stream.Collectors) File(java.io.File) Stack(java.util.Stack) Objects(java.util.Objects) MergingMapper(org.apache.tools.ant.util.MergingMapper) Stream(java.util.stream.Stream) Project(org.apache.tools.ant.Project) Reference(org.apache.tools.ant.types.Reference) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) Objects(java.util.Objects) FileNameMapper(org.apache.tools.ant.util.FileNameMapper)

Example 17 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.

the class MapperTest method testNested.

@Test
public void testNested() {
    Mapper mapper1 = new Mapper(project);
    Mapper.MapperType mt = new Mapper.MapperType();
    mt.setValue("glob");
    mapper1.setType(mt);
    mapper1.setFrom("from*");
    mapper1.setTo("to*");
    // mix element types
    FileNameMapper mapper2 = new FlatFileNameMapper();
    FileNameMapper mapper3 = new MergingMapper();
    mapper3.setTo("mergefile");
    Mapper container = new Mapper(project);
    container.addConfiguredMapper(mapper1);
    container.add(mapper2);
    container.add(mapper3);
    FileNameMapper fileNameMapper = container.getImplementation();
    String[] targets = fileNameMapper.mapFileName("fromfilename");
    assertNotNull("no filenames mapped", targets);
    assertEquals("wrong number of filenames mapped", 3, targets.length);
    List list = Arrays.asList(targets);
    assertTrue("cannot find expected target \"tofilename\"", list.contains("tofilename"));
    assertTrue("cannot find expected target \"fromfilename\"", list.contains("fromfilename"));
    assertTrue("cannot find expected target \"mergefile\"", list.contains("mergefile"));
}
Also used : GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper) MergingMapper(org.apache.tools.ant.util.MergingMapper) ChainedMapper(org.apache.tools.ant.util.ChainedMapper) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper) List(java.util.List) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper) MergingMapper(org.apache.tools.ant.util.MergingMapper) Test(org.junit.Test)

Example 18 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.

the class MapperTest method testChained.

@Test
public void testChained() {
    // a --> b --> c --- def
    // \-- ghi
    FileNameMapper mapperAB = new GlobPatternMapper();
    mapperAB.setFrom("a");
    mapperAB.setTo("b");
    FileNameMapper mapperBC = new GlobPatternMapper();
    mapperBC.setFrom("b");
    mapperBC.setTo("c");
    // implicit composite
    Mapper mapperCX = new Mapper(project);
    FileNameMapper mapperDEF = new GlobPatternMapper();
    mapperDEF.setFrom("c");
    mapperDEF.setTo("def");
    FileNameMapper mapperGHI = new GlobPatternMapper();
    mapperGHI.setFrom("c");
    mapperGHI.setTo("ghi");
    mapperCX.add(mapperDEF);
    mapperCX.add(mapperGHI);
    Mapper chained = new Mapper(project);
    chained.setClassname(ChainedMapper.class.getName());
    chained.add(mapperAB);
    chained.add(mapperBC);
    chained.addConfiguredMapper(mapperCX);
    FileNameMapper fileNameMapper = chained.getImplementation();
    String[] targets = fileNameMapper.mapFileName("a");
    assertNotNull("no filenames mapped", targets);
    assertEquals("wrong number of filenames mapped", 2, targets.length);
    List list = Arrays.asList(targets);
    assertTrue("cannot find expected target \"def\"", list.contains("def"));
    assertTrue("cannot find expected target \"ghi\"", list.contains("ghi"));
    targets = fileNameMapper.mapFileName("z");
    assertNull(targets);
}
Also used : GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper) MergingMapper(org.apache.tools.ant.util.MergingMapper) ChainedMapper(org.apache.tools.ant.util.ChainedMapper) GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) ChainedMapper(org.apache.tools.ant.util.ChainedMapper) List(java.util.List) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper) Test(org.junit.Test)

Example 19 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.

the class Untar method expandStream.

/**
 * @since Ant 1.7
 */
private void expandStream(String name, InputStream stream, File dir) throws IOException {
    try (TarInputStream tis = new TarInputStream(compression.decompress(name, new BufferedInputStream(stream)), getEncoding())) {
        log("Expanding: " + name + " into " + dir, Project.MSG_INFO);
        boolean empty = true;
        FileNameMapper mapper = getMapper();
        TarEntry te;
        while ((te = tis.getNextEntry()) != null) {
            empty = false;
            extractFile(FileUtils.getFileUtils(), null, dir, tis, te.getName(), te.getModTime(), te.isDirectory(), mapper);
        }
        if (empty && getFailOnEmptyArchive()) {
            throw new BuildException("archive '%s' is empty", name);
        }
        log("expand complete", Project.MSG_VERBOSE);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) TarInputStream(org.apache.tools.tar.TarInputStream) TarEntry(org.apache.tools.tar.TarEntry) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) BuildException(org.apache.tools.ant.BuildException)

Example 20 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.

the class UpToDate method scanDir.

/**
 * Scan a directory for files to check for "up to date"ness
 * @param srcDir the directory
 * @param files the files to scan for
 * @return true if the files are up to date
 */
protected boolean scanDir(File srcDir, String[] files) {
    SourceFileScanner sfs = new SourceFileScanner(this);
    FileNameMapper mapper = getMapper();
    File dir = srcDir;
    if (mapperElement == null) {
        dir = null;
    }
    return sfs.restrict(files, srcDir, dir, mapper).length == 0;
}
Also used : SourceFileScanner(org.apache.tools.ant.util.SourceFileScanner) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) File(java.io.File)

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