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

📄 copy.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @since 1.32, Ant 1.5     */    public String getEncoding() {        return inputEncoding;    }    /**     * Set the character encoding for output files.     * @param encoding the output character encoding.     * @since Ant 1.6     */    public void setOutputEncoding(String encoding) {        this.outputEncoding = encoding;    }    /**     * Get the character encoding for output files.     * @return the character encoding for output files,     * <code>null</code> if not set.     *     * @since Ant 1.6     */    public String getOutputEncoding() {        return outputEncoding;    }    /**     * Set the number of milliseconds leeway to give before deciding a     * target is out of date.     *     * <p>Default is 1 second, or 2 seconds on DOS systems.</p>     * @param granularity the granularity used to decide if a target is out of     *                    date.     * @since Ant 1.6.2     */    public void setGranularity(long granularity) {        this.granularity = granularity;    }    /**     * Perform the copy operation.     * @exception BuildException if an error occurs.     */    public void execute() throws BuildException {        File savedFile = file; // may be altered in validateAttributes        File savedDestFile = destFile;        File savedDestDir = destDir;        ResourceCollection savedRc = null;        if (file == null && destFile != null && rcs.size() == 1) {            // will be removed in validateAttributes            savedRc = (ResourceCollection) rcs.elementAt(0);        }        // make sure we don't have an illegal set of options        validateAttributes();        try {            // deal with the single file            copySingleFile();            // deal with the ResourceCollections            /* for historical and performance reasons we have to do               things in a rather complex way.               (1) Move is optimized to move directories if a fileset               has been included completely, therefore FileSets need a               special treatment.  This is also required to support               the failOnError semantice (skip filesets with broken               basedir but handle the remaining collections).               (2) We carry around a few protected methods that work               on basedirs and arrays of names.  To optimize stuff, all               resources with the same basedir get collected in               separate lists and then each list is handled in one go.            */            HashMap filesByBasedir = new HashMap();            HashMap dirsByBasedir = new HashMap();            HashSet baseDirs = new HashSet();            ArrayList nonFileResources = new ArrayList();            for (int i = 0; i < rcs.size(); i++) {                ResourceCollection rc = (ResourceCollection) rcs.elementAt(i);                // Step (1) - beware of the ZipFileSet                if (rc instanceof FileSet && rc.isFilesystemOnly()) {                    FileSet fs = (FileSet) rc;                    DirectoryScanner ds = null;                    try {                        ds = fs.getDirectoryScanner(getProject());                    } catch (BuildException e) {                        if (failonerror                            || !getMessage(e).endsWith(" not found.")) {                            throw e;                        } else {                            log("Warning: " + getMessage(e), Project.MSG_ERR);                            continue;                        }                    }                    File fromDir = fs.getDir(getProject());                    String[] srcFiles = ds.getIncludedFiles();                    String[] srcDirs = ds.getIncludedDirectories();                    if (!flatten && mapperElement == null                        && ds.isEverythingIncluded() && !fs.hasPatterns()) {                        completeDirMap.put(fromDir, destDir);                    }                    add(fromDir, srcFiles, filesByBasedir);                    add(fromDir, srcDirs, dirsByBasedir);                    baseDirs.add(fromDir);                } else { // not a fileset or contains non-file resources                    if (!rc.isFilesystemOnly() && !supportsNonFileResources()) {                        throw new BuildException(                                   "Only FileSystem resources are supported.");                    }                    Iterator resources = rc.iterator();                    while (resources.hasNext()) {                        Resource r = (Resource) resources.next();                        if (!r.isExists()) {                            continue;                        }                        File baseDir = NULL_FILE_PLACEHOLDER;                        String name = r.getName();                        if (r instanceof FileResource) {                            FileResource fr = (FileResource) r;                            baseDir = getKeyFile(fr.getBaseDir());                            if (fr.getBaseDir() == null) {                                name = fr.getFile().getAbsolutePath();                            }                        }                        // copying of dirs is trivial and can be done                        // for non-file resources as well as for real                        // files.                        if (r.isDirectory() || r instanceof FileResource) {                            add(baseDir, name,                                r.isDirectory() ? dirsByBasedir                                                : filesByBasedir);                            baseDirs.add(baseDir);                        } else { // a not-directory file resource                            // needs special treatment                            nonFileResources.add(r);                        }                    }                }            }            iterateOverBaseDirs(baseDirs, dirsByBasedir, filesByBasedir);            // do all the copy operations now...            try {                doFileOperations();            } catch (BuildException e) {                if (!failonerror) {                    log("Warning: " + getMessage(e), Project.MSG_ERR);                } else {                    throw e;                }            }            if (nonFileResources.size() > 0) {                Resource[] nonFiles =                    (Resource[]) nonFileResources.toArray(new Resource[nonFileResources.size()]);                // restrict to out-of-date resources                Map map = scan(nonFiles, destDir);                try {                    doResourceOperations(map);                } catch (BuildException e) {                    if (!failonerror) {                        log("Warning: " + getMessage(e), Project.MSG_ERR);                    } else {                        throw e;                    }                }            }        } finally {            // clean up again, so this instance can be used a second            // time            file = savedFile;            destFile = savedDestFile;            destDir = savedDestDir;            if (savedRc != null) {                rcs.insertElementAt(savedRc, 0);            }            fileCopyMap.clear();            dirCopyMap.clear();            completeDirMap.clear();        }    }    /************************************************************************     **  protected and private methods     ************************************************************************/    private void copySingleFile() {        // deal with the single file        if (file != null) {            if (file.exists()) {                if (destFile == null) {                    destFile = new File(destDir, file.getName());                }                if (forceOverwrite || !destFile.exists()                    || (file.lastModified() - granularity                        > destFile.lastModified())) {                    fileCopyMap.put(file.getAbsolutePath(),                                    new String[] {destFile.getAbsolutePath()});                } else {                    log(file + " omitted as " + destFile                        + " is up to date.", Project.MSG_VERBOSE);                }            } else {                String message = "Warning: Could not find file "                    + file.getAbsolutePath() + " to copy.";                if (!failonerror) {                    log(message, Project.MSG_ERR);                } else {                    throw new BuildException(message);                }            }        }    }    private void iterateOverBaseDirs(        HashSet baseDirs, HashMap dirsByBasedir, HashMap filesByBasedir) {        Iterator iter = baseDirs.iterator();        while (iter.hasNext()) {            File f = (File) iter.next();            List files = (List) filesByBasedir.get(f);            List dirs = (List) dirsByBasedir.get(f);            String[] srcFiles = new String[0];            if (files != null) {                srcFiles = (String[]) files.toArray(srcFiles);            }            String[] srcDirs = new String[0];            if (dirs != null) {                srcDirs = (String[]) dirs.toArray(srcDirs);            }            scan(f == NULL_FILE_PLACEHOLDER ? null : f, destDir, srcFiles,                 srcDirs);        }    }    /**     * Ensure we have a consistent and legal set of attributes, and set     * any internal flags necessary based on different combinations     * of attributes.     * @exception BuildException if an error occurs.     */    protected void validateAttributes() throws BuildException {        if (file == null && rcs.size() == 0) {            throw new BuildException(                "Specify at least one source--a file or a resource collection.");        }        if (destFile != null && destDir != null) {            throw new BuildException(                "Only one of tofile and todir may be set.");        }        if (destFile == null && destDir == null) {            throw new BuildException("One of tofile or todir must be set.");        }        if (file != null && file.isDirectory()) {            throw new BuildException("Use a resource collection to copy directories.");        }        if (destFile != null && rcs.size() > 0) {            if (rcs.size() > 1) {                throw new BuildException(                    "Cannot concatenate multiple files into a single file.");            } else {                ResourceCollection rc = (ResourceCollection) rcs.elementAt(0);                if (!rc.isFilesystemOnly()) {                    throw new BuildException("Only FileSystem resources are"                                             + " supported when concatenating"                                             + " files.");                }                if (rc.size() == 0) {                    throw new BuildException(                        "Cannot perform operation from directory to file.");                } else if (rc.size() == 1) {                    FileResource r = (FileResource) rc.iterator().next();                    if (file == null) {                        file = r.getFile();                        rcs.removeElementAt(0);                    } else {                        throw new BuildException(                            "Cannot concatenate multiple files into a single file.");                    }                } else {                    throw new BuildException(                        "Cannot concatenate multiple files into a single file.");                }            }        }        if (destFile != null) {            destDir = destFile.getParentFile();        }    }    /**     * Compares source files to destination files to see if they should be     * copied.     *     * @param fromDir  The source directory.     * @param toDir    The destination directory.     * @param files    A list of files to copy.     * @param dirs     A list of directories to copy.     */    protected void scan(File fromDir, File toDir, String[] files,                        String[] dirs) {        FileNameMapper mapper = getMapper();        buildMap(fromDir, toDir, files, mapper, fileCopyMap);        if (includeEmpty) {            buildMap(fromDir, toDir, dirs, mapper, dirCopyMap);        }    }    /**     * Compares source resources to destination files to see if they     * should be copied.     *     * @param fromResources  The source resources.     * @param toDir          The destination directory.     *     * @return a Map with the out-of-date resources as keys and an     * array of target file names as values.     *     * @since Ant 1.7     */    protected Map scan(Resource[] fromResources, File toDir) {        return buildMap(fromResources, toDir, getMapper());    }    /**     * Add to a map of files/directories to copy.     *     * @param fromDir the source directory.     * @param toDir   the destination directory.     * @param names   a list of filenames.     * @param mapper  a <code>FileNameMapper</code> value.     * @param map     a map of source file to array of destination files.

⌨️ 快捷键说明

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