Search in sources :

Example 6 with FileNameMapper

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

the class Mapper method add.

/**
 * Add a nested <code>FileNameMapper</code>.
 * @param fileNameMapper   the <code>FileNameMapper</code> to add.
 */
public void add(FileNameMapper fileNameMapper) {
    if (isReference()) {
        throw noChildrenAllowed();
    }
    if (container == null) {
        if (type == null && classname == null) {
            container = new CompositeMapper();
        } else {
            FileNameMapper m = getImplementation();
            if (m instanceof ContainerMapper) {
                container = (ContainerMapper) m;
            } else {
                throw new BuildException(String.valueOf(m) + " mapper implementation does not support nested mappers!");
            }
        }
    }
    container.add(fileNameMapper);
    setChecked(false);
}
Also used : CompositeMapper(org.apache.tools.ant.util.CompositeMapper) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) BuildException(org.apache.tools.ant.BuildException) ContainerMapper(org.apache.tools.ant.util.ContainerMapper)

Example 7 with FileNameMapper

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

the class Expand method expandFile.

/**
 * This method is to be overridden by extending unarchival tasks.
 *
 * @param fileUtils the fileUtils
 * @param srcF      the source file
 * @param dir       the destination directory
 */
protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
    log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
    FileNameMapper mapper = getMapper();
    if (!srcF.exists()) {
        throw new BuildException("Unable to expand " + srcF + " as the file does not exist", getLocation());
    }
    try (ZipFile zf = new ZipFile(srcF, encoding, scanForUnicodeExtraFields)) {
        boolean empty = true;
        Enumeration<ZipEntry> e = zf.getEntries();
        while (e.hasMoreElements()) {
            empty = false;
            ZipEntry ze = e.nextElement();
            InputStream is = null;
            log("extracting " + ze.getName(), Project.MSG_DEBUG);
            try {
                extractFile(fileUtils, srcF, dir, // NOSONAR
                is = zf.getInputStream(ze), ze.getName(), new Date(ze.getTime()), ze.isDirectory(), mapper);
            } finally {
                FileUtils.close(is);
            }
        }
        if (empty && getFailOnEmptyArchive()) {
            throw new BuildException("archive '%s' is empty", srcF);
        }
        log("expand complete", Project.MSG_VERBOSE);
    } catch (IOException ioe) {
        throw new BuildException("Error while expanding " + srcF.getPath() + "\n" + ioe.toString(), ioe);
    }
}
Also used : ZipFile(org.apache.tools.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(org.apache.tools.zip.ZipEntry) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) Date(java.util.Date)

Example 8 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper 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 9 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper 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 10 with FileNameMapper

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

the class Native2Ascii method execute.

/**
 * Execute the task
 *
 * @throws BuildException is there is a problem in the task execution.
 */
@Override
public void execute() throws BuildException {
    // Scanner to find our inputs
    DirectoryScanner scanner = null;
    // list of files to process
    String[] files;
    // default srcDir to basedir
    if (srcDir == null) {
        srcDir = getProject().resolveFile(".");
    }
    // Require destDir
    if (destDir == null) {
        throw new BuildException("The dest attribute must be set.");
    }
    // include a file with the same extension, but ....
    if (srcDir.equals(destDir) && extension == null && mapper == null) {
        throw new BuildException("The ext attribute or a mapper must be set if src and dest dirs are the same.");
    }
    FileNameMapper m;
    if (mapper == null) {
        if (extension == null) {
            m = new IdentityMapper();
        } else {
            m = new ExtMapper();
        }
    } else {
        m = mapper.getImplementation();
    }
    scanner = getDirectoryScanner(srcDir);
    files = scanner.getIncludedFiles();
    SourceFileScanner sfs = new SourceFileScanner(this);
    files = sfs.restrict(files, srcDir, destDir, m);
    int count = files.length;
    if (count == 0) {
        return;
    }
    String message = "Converting " + count + " file" + (count != 1 ? "s" : "") + " from ";
    log(message + srcDir + " to " + destDir);
    for (String file : files) {
        String[] dest = m.mapFileName(file);
        if (dest != null && dest.length > 0) {
            convert(file, dest[0]);
        }
    }
}
Also used : IdentityMapper(org.apache.tools.ant.util.IdentityMapper) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) BuildException(org.apache.tools.ant.BuildException) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) SourceFileScanner(org.apache.tools.ant.util.SourceFileScanner)

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