Search in sources :

Example 1 with Resources

use of org.apache.tools.ant.types.resources.Resources in project ant by apache.

the class Delete method add.

/**
 * Add an arbitrary ResourceCollection to be deleted.
 * @param rc the filesystem-only ResourceCollection.
 */
public void add(ResourceCollection rc) {
    if (rc == null) {
        return;
    }
    if (rcs == null) {
        rcs = new Resources();
        rcs.setCache(true);
    }
    rcs.add(rc);
}
Also used : Resources(org.apache.tools.ant.types.resources.Resources)

Example 2 with Resources

use of org.apache.tools.ant.types.resources.Resources 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 3 with Resources

use of org.apache.tools.ant.types.resources.Resources in project ant by apache.

the class Delete method execute.

/**
 * Delete the file(s).
 *
 * @exception BuildException if an error occurs
 */
@Override
public void execute() throws BuildException {
    if (usedMatchingTask) {
        log("DEPRECATED - Use of the implicit FileSet is deprecated.  Use a nested fileset element instead.", quiet ? Project.MSG_VERBOSE : verbosity);
    }
    if (file == null && dir == null && filesets.isEmpty() && rcs == null) {
        throw new BuildException("At least one of the file or dir attributes, or a nested resource collection, must be set.");
    }
    if (quiet && failonerror) {
        throw new BuildException("quiet and failonerror cannot both be set to true", getLocation());
    }
    // delete the single file
    if (file != null) {
        if (file.exists()) {
            if (file.isDirectory()) {
                log("Directory " + file.getAbsolutePath() + " cannot be removed using the file attribute.  Use dir instead.", quiet ? Project.MSG_VERBOSE : verbosity);
            } else {
                log("Deleting: " + file.getAbsolutePath());
                if (!delete(file)) {
                    handle("Unable to delete file " + file.getAbsolutePath());
                }
            }
        } else if (isDanglingSymlink(file)) {
            log("Trying to delete file " + file.getAbsolutePath() + " which looks like a broken symlink.", quiet ? Project.MSG_VERBOSE : verbosity);
            if (!delete(file)) {
                handle("Unable to delete file " + file.getAbsolutePath());
            }
        } else {
            log("Could not find file " + file.getAbsolutePath() + " to delete.", quiet ? Project.MSG_VERBOSE : verbosity);
        }
    }
    // delete the directory
    if (dir != null && !usedMatchingTask) {
        if (dir.exists() && dir.isDirectory()) {
            /*
                  If verbosity is MSG_VERBOSE, that mean we are doing
                  regular logging (backwards as that sounds).  In that
                  case, we want to print one message about deleting the
                  top of the directory tree.  Otherwise, the removeDir
                  method will handle messages for _all_ directories.
                */
            if (verbosity == Project.MSG_VERBOSE) {
                log("Deleting directory " + dir.getAbsolutePath());
            }
            removeDir(dir);
        } else if (isDanglingSymlink(dir)) {
            log("Trying to delete directory " + dir.getAbsolutePath() + " which looks like a broken symlink.", quiet ? Project.MSG_VERBOSE : verbosity);
            if (!delete(dir)) {
                handle("Unable to delete directory " + dir.getAbsolutePath());
            }
        }
    }
    Resources resourcesToDelete = new Resources();
    resourcesToDelete.setProject(getProject());
    resourcesToDelete.setCache(true);
    Resources filesetDirs = new Resources();
    filesetDirs.setProject(getProject());
    filesetDirs.setCache(true);
    FileSet implicit = null;
    if (usedMatchingTask && dir != null && dir.isDirectory()) {
        // add the files from the default fileset:
        implicit = getImplicitFileSet();
        implicit.setProject(getProject());
        filesets.add(implicit);
    }
    final int size = filesets.size();
    for (FileSet fs : filesets) {
        if (fs.getProject() == null) {
            log("Deleting fileset with no project specified; assuming executing project", Project.MSG_VERBOSE);
            fs = (FileSet) fs.clone();
            fs.setProject(getProject());
        }
        final File fsDir = fs.getDir();
        if (!fs.getErrorOnMissingDir() && (fsDir == null || !fsDir.exists())) {
            continue;
        }
        if (fsDir == null) {
            throw new BuildException("File or Resource without directory or file specified");
        } else if (!fsDir.isDirectory()) {
            handle("Directory does not exist: " + fsDir);
        } else {
            DirectoryScanner ds = fs.getDirectoryScanner();
            // the previous line has already scanned the
            // filesystem, in order to avoid a rescan when later
            // iterating, capture the results now and store them
            final String[] files = ds.getIncludedFiles();
            resourcesToDelete.add(new ResourceCollection() {

                @Override
                public boolean isFilesystemOnly() {
                    return true;
                }

                @Override
                public int size() {
                    return files.length;
                }

                @Override
                public Iterator<Resource> iterator() {
                    return new FileResourceIterator(getProject(), fsDir, files);
                }
            });
            if (includeEmpty) {
                filesetDirs.add(new ReverseDirs(getProject(), fsDir, ds.getIncludedDirectories()));
            }
            if (removeNotFollowedSymlinks) {
                String[] n = ds.getNotFollowedSymlinks();
                if (n.length > 0) {
                    String[] links = new String[n.length];
                    System.arraycopy(n, 0, links, 0, n.length);
                    Arrays.sort(links, Comparator.reverseOrder());
                    for (String link : links) {
                        final Path filePath = Paths.get(link);
                        if (!Files.isSymbolicLink(filePath)) {
                            // it's not a symbolic link, so move on
                            continue;
                        }
                        // it's a symbolic link, so delete it
                        final boolean deleted = filePath.toFile().delete();
                        if (!deleted) {
                            handle("Could not delete symbolic link at " + filePath);
                        }
                    }
                }
            }
        }
    }
    resourcesToDelete.add(filesetDirs);
    if (rcs != null) {
        // sort first to files, then dirs
        Restrict exists = new Restrict();
        exists.add(EXISTS);
        exists.add(rcs);
        Sort s = new Sort();
        s.add(REVERSE_FILESYSTEM);
        s.add(exists);
        resourcesToDelete.add(s);
    }
    try {
        if (resourcesToDelete.isFilesystemOnly()) {
            for (Resource r : resourcesToDelete) {
                // nonexistent resources could only occur if we already
                // deleted something from a fileset:
                File f = r.as(FileProvider.class).getFile();
                if (!f.exists()) {
                    continue;
                }
                if (!f.isDirectory() || f.list().length == 0) {
                    log("Deleting " + f, verbosity);
                    if (!delete(f) && failonerror) {
                        handle("Unable to delete " + (f.isDirectory() ? "directory " : "file ") + f);
                    }
                }
            }
        } else {
            handle(getTaskName() + " handles only filesystem resources");
        }
    } catch (Exception e) {
        handle(e);
    } finally {
        if (implicit != null) {
            filesets.remove(implicit);
        }
    }
}
Also used : Path(java.nio.file.Path) FileSet(org.apache.tools.ant.types.FileSet) Resource(org.apache.tools.ant.types.Resource) FileResourceIterator(org.apache.tools.ant.types.resources.FileResourceIterator) BuildException(org.apache.tools.ant.BuildException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) Restrict(org.apache.tools.ant.types.resources.Restrict) FileProvider(org.apache.tools.ant.types.resources.FileProvider) Sort(org.apache.tools.ant.types.resources.Sort) BuildException(org.apache.tools.ant.BuildException) Resources(org.apache.tools.ant.types.resources.Resources) File(java.io.File) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 4 with Resources

use of org.apache.tools.ant.types.resources.Resources in project ant by apache.

the class Concat method add.

/**
 * Add an arbitrary ResourceCollection.
 * @param c the ResourceCollection to add.
 * @since Ant 1.7
 */
public void add(ResourceCollection c) {
    synchronized (this) {
        if (rc == null) {
            rc = new Resources();
            rc.setProject(getProject());
            rc.setCache(true);
        }
    }
    rc.add(c);
}
Also used : Resources(org.apache.tools.ant.types.resources.Resources)

Example 5 with Resources

use of org.apache.tools.ant.types.resources.Resources in project ant by apache.

the class PathConvert method getPath.

private synchronized Resources getPath() {
    if (path == null) {
        path = new Resources(getProject());
        path.setCache(true);
    }
    return path;
}
Also used : Resources(org.apache.tools.ant.types.resources.Resources)

Aggregations

Resources (org.apache.tools.ant.types.resources.Resources)6 BuildException (org.apache.tools.ant.BuildException)2 FileSet (org.apache.tools.ant.types.FileSet)2 Resource (org.apache.tools.ant.types.Resource)2 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)2 Restrict (org.apache.tools.ant.types.resources.Restrict)2 File (java.io.File)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 AbstractFileSet (org.apache.tools.ant.types.AbstractFileSet)1 FileProvider (org.apache.tools.ant.types.resources.FileProvider)1 FileResourceIterator (org.apache.tools.ant.types.resources.FileResourceIterator)1 Sort (org.apache.tools.ant.types.resources.Sort)1 Union (org.apache.tools.ant.types.resources.Union)1 Exists (org.apache.tools.ant.types.resources.selectors.Exists)1 FileNameMapper (org.apache.tools.ant.util.FileNameMapper)1 IdentityMapper (org.apache.tools.ant.util.IdentityMapper)1