Search in sources :

Example 1 with GlobPatternMapper

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

the class MapperTest method testCircularReferenceCheck.

@Test
public void testCircularReferenceCheck() {
    Mapper m = new Mapper(project);
    project.addReference("dummy", m);
    m.setRefid(new Reference(project, "dummy"));
    try {
        m.getImplementation();
        fail("Can make Mapper a Reference to itself.");
    } catch (BuildException be) {
        assertEquals("This data type contains a circular reference.", be.getMessage());
    }
    // dummy1 --> dummy2 --> dummy3 --> dummy1
    Mapper m1 = new Mapper(project);
    project.addReference("dummy1", m1);
    m1.setRefid(new Reference(project, "dummy2"));
    Mapper m2 = new Mapper(project);
    project.addReference("dummy2", m2);
    m2.setRefid(new Reference(project, "dummy3"));
    Mapper m3 = new Mapper(project);
    project.addReference("dummy3", m3);
    m3.setRefid(new Reference(project, "dummy1"));
    try {
        m1.getImplementation();
        fail("Can make circular reference.");
    } catch (BuildException be) {
        assertEquals("This data type contains a circular reference.", be.getMessage());
    }
    // dummy1 --> dummy2 --> dummy3
    // (which holds a glob mapper from "*.java" to "*.class"
    m1 = new Mapper(project);
    project.addReference("dummy1", m1);
    m1.setRefid(new Reference(project, "dummy2"));
    m2 = new Mapper(project);
    project.addReference("dummy2", m2);
    m2.setRefid(new Reference(project, "dummy3"));
    m3 = new Mapper(project);
    project.addReference("dummy3", m3);
    Mapper.MapperType mt = new Mapper.MapperType();
    mt.setValue("glob");
    m3.setType(mt);
    m3.setFrom("*.java");
    m3.setTo("*.class");
    FileNameMapper fmm = m1.getImplementation();
    assertTrue("should be glob", fmm instanceof GlobPatternMapper);
    String[] result = fmm.mapFileName("a.java");
    assertEquals("a.java should match", 1, result.length);
    assertEquals("a.class", result[0]);
}
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) BuildException(org.apache.tools.ant.BuildException) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper) Test(org.junit.Test)

Example 2 with GlobPatternMapper

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

the class Zip method getResourcesToAdd.

/**
 * Collect the resources that are newer than the corresponding
 * entries (or missing) in the original archive.
 *
 * <p>If we are going to recreate the archive instead of updating
 * it, all resources should be considered as new, if a single one
 * is.  Because of this, subclasses overriding this method must
 * call <code>super.getResourcesToAdd</code> and indicate with the
 * third arg if they already know that the archive is
 * out-of-date.</p>
 *
 * @param filesets The filesets to grab resources from
 * @param zipFile intended archive file (may or may not exist)
 * @param needsUpdate whether we already know that the archive is
 * out-of-date.  Subclasses overriding this method are supposed to
 * set this value correctly in their call to
 * <code>super.getResourcesToAdd</code>.
 * @return an array of resources to add for each fileset passed in as well
 *         as a flag that indicates whether the archive is uptodate.
 *
 * @exception BuildException if it likes
 */
protected ArchiveState getResourcesToAdd(final FileSet[] filesets, final File zipFile, boolean needsUpdate) throws BuildException {
    final Resource[][] initialResources = grabResources(filesets);
    if (isEmpty(initialResources)) {
        if (Boolean.FALSE.equals(HAVE_NON_FILE_SET_RESOURCES_TO_ADD.get())) {
            if (needsUpdate && doUpdate) {
                /*
                     * This is a rather hairy case.
                     *
                     * One of our subclasses knows that we need to
                     * update the archive, but at the same time, there
                     * are no resources known to us that would need to
                     * be added.  Only the subclass seems to know
                     * what's going on.
                     *
                     * This happens if <jar> detects that the manifest
                     * has changed, for example.  The manifest is not
                     * part of any resources because of our support
                     * for inline <manifest>s.
                     *
                     * If we invoke createEmptyZip like Ant 1.5.2 did,
                     * we'll loose all stuff that has been in the
                     * original archive (bugzilla report 17780).
                     */
                return new ArchiveState(true, initialResources);
            }
            if ("skip".equals(emptyBehavior)) {
                if (doUpdate) {
                    logWhenWriting(archiveType + " archive " + zipFile + " not updated because no new files were" + " included.", Project.MSG_VERBOSE);
                } else {
                    logWhenWriting("Warning: skipping " + archiveType + " archive " + zipFile + " because no files were included.", Project.MSG_WARN);
                }
            } else if ("fail".equals(emptyBehavior)) {
                throw new BuildException("Cannot create " + archiveType + " archive " + zipFile + ": no files were included.", getLocation());
            } else {
                // Create.
                if (!zipFile.exists()) {
                    needsUpdate = true;
                }
            }
        }
        // (re-)create the archive anyway
        return new ArchiveState(needsUpdate, initialResources);
    }
    if (!zipFile.exists()) {
        return new ArchiveState(true, initialResources);
    }
    if (needsUpdate && !doUpdate) {
        // we are recreating the archive, need all resources
        return new ArchiveState(true, initialResources);
    }
    final Resource[][] newerResources = new Resource[filesets.length][];
    for (int i = 0; i < filesets.length; i++) {
        if (!(fileset instanceof ZipFileSet) || ((ZipFileSet) fileset).getSrc(getProject()) == null) {
            final File base = filesets[i].getDir(getProject());
            for (int j = 0; j < initialResources[i].length; j++) {
                final File resourceAsFile = FILE_UTILS.resolveFile(base, initialResources[i][j].getName());
                if (resourceAsFile.equals(zipFile)) {
                    throw new BuildException("A zip file cannot include " + "itself", getLocation());
                }
            }
        }
    }
    for (int i = 0; i < filesets.length; i++) {
        if (initialResources[i].length == 0) {
            newerResources[i] = new Resource[] {};
            continue;
        }
        FileNameMapper myMapper = new IdentityMapper();
        if (filesets[i] instanceof ZipFileSet) {
            final ZipFileSet zfs = (ZipFileSet) filesets[i];
            if (zfs.getFullpath(getProject()) != null && !zfs.getFullpath(getProject()).equals("")) {
                // in this case all files from origin map to
                // the fullPath attribute of the zipfileset at
                // destination
                final MergingMapper fm = new MergingMapper();
                fm.setTo(zfs.getFullpath(getProject()));
                myMapper = fm;
            } else if (zfs.getPrefix(getProject()) != null && !zfs.getPrefix(getProject()).equals("")) {
                final GlobPatternMapper gm = new GlobPatternMapper();
                gm.setFrom("*");
                String prefix = zfs.getPrefix(getProject());
                if (!prefix.endsWith("/") && !prefix.endsWith("\\")) {
                    prefix += "/";
                }
                gm.setTo(prefix + "*");
                myMapper = gm;
            }
        }
        newerResources[i] = selectOutOfDateResources(initialResources[i], myMapper);
        needsUpdate = needsUpdate || (newerResources[i].length > 0);
        if (needsUpdate && !doUpdate) {
            // to scan further.
            break;
        }
    }
    if (needsUpdate && !doUpdate) {
        // we are recreating the archive, need all resources
        return new ArchiveState(true, initialResources);
    }
    return new ArchiveState(needsUpdate, newerResources);
}
Also used : IdentityMapper(org.apache.tools.ant.util.IdentityMapper) GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) ZipResource(org.apache.tools.ant.types.resources.ZipResource) BuildException(org.apache.tools.ant.BuildException) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) MergingMapper(org.apache.tools.ant.util.MergingMapper)

Example 3 with GlobPatternMapper

use of org.apache.tools.ant.util.GlobPatternMapper in project jangaroo-tools by CoreMedia.

the class PropcTask method scanDir.

/**
 * Scans the directory looking for source files to be compiled.
 * The results are returned in the class variable compileList
 *
 * @param srcDir  the source directory
 * @param destDir the destination directory
 * @param files   the files to scan
 */
protected void scanDir(File srcDir, File destDir, String[] files) {
    GlobPatternMapper m = new GlobPatternMapper();
    m.setFrom("*.properties");
    m.setTo("*.as");
    SourceFileScanner sfs = new SourceFileScanner(this);
    File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
    if (newFiles.length > 0) {
        File[] newCompileList = new File[compileList.length + newFiles.length];
        System.arraycopy(compileList, 0, newCompileList, 0, compileList.length);
        System.arraycopy(newFiles, 0, newCompileList, compileList.length, newFiles.length);
        compileList = newCompileList;
    }
}
Also used : GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) SourceFileScanner(org.apache.tools.ant.util.SourceFileScanner) File(java.io.File)

Example 4 with GlobPatternMapper

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

the class Javac method scanDir.

/**
 * Scans the directory looking for source files to be compiled.
 * The results are returned in the class variable compileList
 *
 * @param srcDir   The source directory
 * @param destDir  The destination directory
 * @param files    An array of filenames
 */
protected void scanDir(final File srcDir, final File destDir, final String[] files) {
    final GlobPatternMapper m = new GlobPatternMapper();
    for (String extension : findSupportedFileExtensions()) {
        m.setFrom(extension);
        m.setTo("*.class");
        final SourceFileScanner sfs = new SourceFileScanner(this);
        final File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
        if (newFiles.length > 0) {
            lookForPackageInfos(srcDir, newFiles);
            final File[] newCompileList = new File[compileList.length + newFiles.length];
            System.arraycopy(compileList, 0, newCompileList, 0, compileList.length);
            System.arraycopy(newFiles, 0, newCompileList, compileList.length, newFiles.length);
            compileList = newCompileList;
        }
    }
}
Also used : GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) SourceFileScanner(org.apache.tools.ant.util.SourceFileScanner) File(java.io.File)

Example 5 with GlobPatternMapper

use of org.apache.tools.ant.util.GlobPatternMapper 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)

Aggregations

GlobPatternMapper (org.apache.tools.ant.util.GlobPatternMapper)11 File (java.io.File)9 SourceFileScanner (org.apache.tools.ant.util.SourceFileScanner)8 BuildException (org.apache.tools.ant.BuildException)4 FileNameMapper (org.apache.tools.ant.util.FileNameMapper)3 MergingMapper (org.apache.tools.ant.util.MergingMapper)3 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)2 Path (org.apache.tools.ant.types.Path)2 ChainedMapper (org.apache.tools.ant.util.ChainedMapper)2 FlatFileNameMapper (org.apache.tools.ant.util.FlatFileNameMapper)2 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)2 Test (org.junit.Test)2 List (java.util.List)1 Resource (org.apache.tools.ant.types.Resource)1 ZipFileSet (org.apache.tools.ant.types.ZipFileSet)1 ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)1 FileResource (org.apache.tools.ant.types.resources.FileResource)1 ZipResource (org.apache.tools.ant.types.resources.ZipResource)1 IdentityMapper (org.apache.tools.ant.util.IdentityMapper)1