Search in sources :

Example 1 with IdentityMapper

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

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

the class Zip method getNonFileSetResourcesToAdd.

/**
 * 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 rcs 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 getNonFileSetResourcesToAdd(final ResourceCollection[] rcs, final File zipFile, boolean needsUpdate) throws BuildException {
    /*
         * Backwards compatibility forces us to repeat the logic of
         * getResourcesToAdd(FileSet[], ...) here once again.
         */
    final Resource[][] initialResources = grabNonFileSetResources(rcs);
    final boolean empty = isEmpty(initialResources);
    HAVE_NON_FILE_SET_RESOURCES_TO_ADD.set(!empty);
    if (empty) {
        // will take care of it.
        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[rcs.length][];
    for (int i = 0; i < rcs.length; i++) {
        if (initialResources[i].length == 0) {
            newerResources[i] = new Resource[] {};
            continue;
        }
        for (int j = 0; j < initialResources[i].length; j++) {
            final FileProvider fp = initialResources[i][j].as(FileProvider.class);
            if (fp != null && zipFile.equals(fp.getFile())) {
                throw new BuildException("A zip file cannot include itself", getLocation());
            }
        }
        newerResources[i] = selectOutOfDateResources(initialResources[i], new IdentityMapper());
        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) FileProvider(org.apache.tools.ant.types.resources.FileProvider) 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)

Example 3 with IdentityMapper

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

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

Example 5 with IdentityMapper

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

the class SignJar method execute.

/**
 * sign the jar(s)
 *
 * @throws BuildException on errors
 */
@Override
public void execute() throws BuildException {
    // validation logic
    final boolean hasJar = jar != null;
    final boolean hasSignedJar = signedjar != null;
    final boolean hasDestDir = destDir != null;
    final boolean hasMapper = mapper != null;
    if (!hasJar && !hasResources()) {
        throw new BuildException(ERROR_NO_SOURCE);
    }
    if (null == alias) {
        throw new BuildException(ERROR_NO_ALIAS);
    }
    if (null == storepass) {
        throw new BuildException(ERROR_NO_STOREPASS);
    }
    if (hasDestDir && hasSignedJar) {
        throw new BuildException(ERROR_TODIR_AND_SIGNEDJAR);
    }
    if (hasResources() && hasSignedJar) {
        throw new BuildException(ERROR_SIGNEDJAR_AND_PATHS);
    }
    // we can change implementation details later
    if (!hasDestDir && hasMapper) {
        throw new BuildException(ERROR_MAPPER_WITHOUT_DEST);
    }
    beginExecution();
    try {
        // special case single jar handling with signedjar attribute set
        if (hasJar && hasSignedJar) {
            // single jar processing
            signOneJar(jar, signedjar);
            // return here.
            return;
        }
        // the rest of the method treats single jar like
        // a nested path with one file
        Path sources = createUnifiedSourcePath();
        // set up our mapping policy
        FileNameMapper destMapper = hasMapper ? mapper : new IdentityMapper();
        // deal with the paths
        for (Resource r : sources) {
            FileResource fr = ResourceUtils.asFileResource(r.as(FileProvider.class));
            // calculate our destination directory; it is either the destDir
            // attribute, or the base dir of the fileset (for in situ updates)
            File toDir = hasDestDir ? destDir : fr.getBaseDir();
            // determine the destination filename via the mapper
            String[] destFilenames = destMapper.mapFileName(fr.getName());
            if (destFilenames == null || destFilenames.length != 1) {
                // we only like simple mappers.
                throw new BuildException(ERROR_BAD_MAP + fr.getFile());
            }
            File destFile = new File(toDir, destFilenames[0]);
            signOneJar(fr.getFile(), destFile);
        }
    } finally {
        endExecution();
    }
}
Also used : Path(org.apache.tools.ant.types.Path) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) FileProvider(org.apache.tools.ant.types.resources.FileProvider) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) FileResource(org.apache.tools.ant.types.resources.FileResource) BuildException(org.apache.tools.ant.BuildException) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) File(java.io.File)

Aggregations

BuildException (org.apache.tools.ant.BuildException)7 IdentityMapper (org.apache.tools.ant.util.IdentityMapper)7 FileNameMapper (org.apache.tools.ant.util.FileNameMapper)6 Resource (org.apache.tools.ant.types.Resource)5 File (java.io.File)4 FileResource (org.apache.tools.ant.types.resources.FileResource)3 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)2 ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)2 FileProvider (org.apache.tools.ant.types.resources.FileProvider)2 ZipResource (org.apache.tools.ant.types.resources.ZipResource)2 MergingMapper (org.apache.tools.ant.util.MergingMapper)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Objects (java.util.Objects)1 Stack (java.util.Stack)1 StringTokenizer (java.util.StringTokenizer)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1