⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 delete.java

📁 java ant的源码!非常值得看的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**     * add a "None" selector entry on the selector list     * @param selector the selector to be added     */    public void addNone(NoneSelector selector) {        usedMatchingTask = true;        super.addNone(selector);    }    /**     * add a majority selector entry on the selector list     * @param selector the selector to be added     */    public void addMajority(MajoritySelector selector) {        usedMatchingTask = true;        super.addMajority(selector);    }    /**     * add a selector date entry on the selector list     * @param selector the selector to be added     */    public void addDate(DateSelector selector) {        usedMatchingTask = true;        super.addDate(selector);    }    /**     * add a selector size entry on the selector list     * @param selector the selector to be added     */    public void addSize(SizeSelector selector) {        usedMatchingTask = true;        super.addSize(selector);    }    /**     * add a selector filename entry on the selector list     * @param selector the selector to be added     */    public void addFilename(FilenameSelector selector) {        usedMatchingTask = true;        super.addFilename(selector);    }    /**     * add an extended selector entry on the selector list     * @param selector the selector to be added     */    public void addCustom(ExtendSelector selector) {        usedMatchingTask = true;        super.addCustom(selector);    }    /**     * add a contains selector entry on the selector list     * @param selector the selector to be added     */    public void addContains(ContainsSelector selector) {        usedMatchingTask = true;        super.addContains(selector);    }    /**     * add a present selector entry on the selector list     * @param selector the selector to be added     */    public void addPresent(PresentSelector selector) {        usedMatchingTask = true;        super.addPresent(selector);    }    /**     * add a depth selector entry on the selector list     * @param selector the selector to be added     */    public void addDepth(DepthSelector selector) {        usedMatchingTask = true;        super.addDepth(selector);    }    /**     * add a depends selector entry on the selector list     * @param selector the selector to be added     */    public void addDepend(DependSelector selector) {        usedMatchingTask = true;        super.addDepend(selector);    }    /**     * add a regular expression selector entry on the selector list     * @param selector the selector to be added     */    public void addContainsRegexp(ContainsRegexpSelector selector) {        usedMatchingTask = true;        super.addContainsRegexp(selector);    }    /**     * add the modified selector     * @param selector the selector to add     * @since ant 1.6     */    public void addModified(ModifiedSelector selector) {        usedMatchingTask = true;        super.addModified(selector);    }    /**     * add an arbitrary selector     * @param selector the selector to be added     * @since Ant 1.6     */    public void add(FileSelector selector) {        usedMatchingTask = true;        super.add(selector);    }    /**     * Delete the file(s).     * @exception BuildException if an error occurs     */    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.size() == 0 && 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 {                log("Could not find file " + file.getAbsolutePath()                    + " to delete.", quiet ? Project.MSG_VERBOSE : verbosity);            }        }        // delete the directory        if (dir != null && dir.exists() && dir.isDirectory()            && !usedMatchingTask) {            /*               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);        }        Resources resourcesToDelete = new Resources();        resourcesToDelete.setProject(getProject());        Resources filesetDirs = new Resources();        filesetDirs.setProject(getProject());        FileSet implicit = null;        if (usedMatchingTask && dir != null && dir.isDirectory()) {            //add the files from the default fileset:            implicit = getImplicitFileSet();            implicit.setProject(getProject());            filesets.add(implicit);        }        for (int i = 0, size = filesets.size(); i < size; i++) {            FileSet fs = (FileSet) filesets.get(i);            if (fs.getProject() == null) {                log("Deleting fileset with no project specified;"                    + " assuming executing project", Project.MSG_VERBOSE);                fs = (FileSet) fs.clone();                fs.setProject(getProject());            }            if (!fs.getDir().isDirectory()) {                handle("Directory does not exist:" + fs.getDir());            } else {                resourcesToDelete.add(fs);                if (includeEmpty) {                    filesetDirs.add(new ReverseDirs(fs.getDir(), fs                            .getDirectoryScanner().getIncludedDirectories()));                }            }        }        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 (Iterator iter = resourcesToDelete.iterator(); iter.hasNext();) {                    FileResource r = (FileResource) iter.next();                    // nonexistent resources could only occur if we already                    // deleted something from a fileset:                    if (!r.isExists()) {                        continue;                    }                    if (!(r.isDirectory()) || r.getFile().list().length == 0) {                        log("Deleting " + r, verbosity);                        if (!delete(r.getFile()) && failonerror) {                            handle("Unable to delete "                                + (r.isDirectory() ? "directory " : "file ") + r);                        }                    }                }            } else {                 handle(getTaskName() + " handles only filesystem resources");            }        } catch (Exception e) {            handle(e);        } finally {            if (implicit != null) {                filesets.remove(implicit);            }        }    }//************************************************************************//  protected and private methods//************************************************************************    private void handle(String msg) {        handle(new BuildException(msg));    }    private void handle(Exception e) {        if (failonerror) {            throw (e instanceof BuildException)                ? (BuildException) e : new BuildException(e);        }        log(e, quiet ? Project.MSG_VERBOSE : verbosity);    }    /**     * Accommodate Windows bug encountered in both Sun and IBM JDKs.     * Others possible. If the delete does not work, call System.gc(),     * wait a little and try again.     */    private boolean delete(File f) {        if (!f.delete()) {            if (Os.isFamily("windows")) {                System.gc();            }            try {                Thread.sleep(DELETE_RETRY_SLEEP_MILLIS);            } catch (InterruptedException ex) {                // Ignore Exception            }            if (!f.delete()) {                if (deleteOnExit) {                    int level = quiet ? Project.MSG_VERBOSE : Project.MSG_INFO;                    log("Failed to delete " + f + ", calling deleteOnExit."                        + " This attempts to delete the file when the Ant jvm"                        + " has exited and might not succeed.", level);                    f.deleteOnExit();                    return true;                }                return false;            }        }        return true;    }    /**     * Delete a directory     *     * @param d the directory to delete     */    protected void removeDir(File d) {        String[] list = d.list();        if (list == null) {            list = new String[0];        }        for (int i = 0; i < list.length; i++) {            String s = list[i];            File f = new File(d, s);            if (f.isDirectory()) {                removeDir(f);            } else {                log("Deleting " + f.getAbsolutePath(), quiet ? Project.MSG_VERBOSE : verbosity);                if (!delete(f)) {                    handle("Unable to delete file " + f.getAbsolutePath());                }            }        }        log("Deleting directory " + d.getAbsolutePath(), verbosity);        if (!delete(d)) {            handle("Unable to delete directory " + dir.getAbsolutePath());        }    }    /**     * remove an array of files in a directory, and a list of subdirectories     * which will only be deleted if 'includeEmpty' is true     * @param d directory to work from     * @param files array of files to delete; can be of zero length     * @param dirs array of directories to delete; can of zero length     */    protected void removeFiles(File d, String[] files, String[] dirs) {        if (files.length > 0) {            log("Deleting " + files.length + " files from "                + d.getAbsolutePath(), quiet ? Project.MSG_VERBOSE : verbosity);            for (int j = 0; j < files.length; j++) {                File f = new File(d, files[j]);                log("Deleting " + f.getAbsolutePath(),                        quiet ? Project.MSG_VERBOSE : verbosity);                if (!delete(f)) {                    handle("Unable to delete file " + f.getAbsolutePath());                }            }        }        if (dirs.length > 0 && includeEmpty) {            int dirCount = 0;            for (int j = dirs.length - 1; j >= 0; j--) {                File currDir = new File(d, dirs[j]);                String[] dirFiles = currDir.list();                if (dirFiles == null || dirFiles.length == 0) {                    log("Deleting " + currDir.getAbsolutePath(),                            quiet ? Project.MSG_VERBOSE : verbosity);                    if (!delete(currDir)) {                        handle("Unable to delete directory "                                + currDir.getAbsolutePath());                    } else {                        dirCount++;                    }                }            }            if (dirCount > 0) {                log("Deleted "                     + dirCount                     + " director" + (dirCount == 1 ? "y" : "ies")                     + " form " + d.getAbsolutePath(),                     quiet ? Project.MSG_VERBOSE : verbosity);            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -