📄 copy.java
字号:
*/ protected void buildMap(File fromDir, File toDir, String[] names, FileNameMapper mapper, Hashtable map) { String[] toCopy = null; if (forceOverwrite) { Vector v = new Vector(); for (int i = 0; i < names.length; i++) { if (mapper.mapFileName(names[i]) != null) { v.addElement(names[i]); } } toCopy = new String[v.size()]; v.copyInto(toCopy); } else { SourceFileScanner ds = new SourceFileScanner(this); toCopy = ds.restrict(names, fromDir, toDir, mapper, granularity); } for (int i = 0; i < toCopy.length; i++) { File src = new File(fromDir, toCopy[i]); String[] mappedFiles = mapper.mapFileName(toCopy[i]); if (!enableMultipleMappings) { map.put(src.getAbsolutePath(), new String[] {new File(toDir, mappedFiles[0]).getAbsolutePath()}); } else { // reuse the array created by the mapper for (int k = 0; k < mappedFiles.length; k++) { mappedFiles[k] = new File(toDir, mappedFiles[k]).getAbsolutePath(); } map.put(src.getAbsolutePath(), mappedFiles); } } } /** * Create a map of resources to copy. * * @param fromResources The source resources. * @param toDir the destination directory. * @param mapper a <code>FileNameMapper</code> value. * @return a map of source resource to array of destination files. * @since Ant 1.7 */ protected Map buildMap(Resource[] fromResources, final File toDir, FileNameMapper mapper) { HashMap map = new HashMap(); Resource[] toCopy = null; if (forceOverwrite) { Vector v = new Vector(); for (int i = 0; i < fromResources.length; i++) { if (mapper.mapFileName(fromResources[i].getName()) != null) { v.addElement(fromResources[i]); } } toCopy = new Resource[v.size()]; v.copyInto(toCopy); } else { toCopy = ResourceUtils.selectOutOfDateSources(this, fromResources, mapper, new ResourceFactory() { public Resource getResource(String name) { return new FileResource(toDir, name); } }, granularity); } for (int i = 0; i < toCopy.length; i++) { String[] mappedFiles = mapper.mapFileName(toCopy[i].getName()); if (!enableMultipleMappings) { map.put(toCopy[i], new String[] {new File(toDir, mappedFiles[0]).getAbsolutePath()}); } else { // reuse the array created by the mapper for (int k = 0; k < mappedFiles.length; k++) { mappedFiles[k] = new File(toDir, mappedFiles[k]).getAbsolutePath(); } map.put(toCopy[i], mappedFiles); } } return map; } /** * Actually does the file (and possibly empty directory) copies. * This is a good method for subclasses to override. */ protected void doFileOperations() { if (fileCopyMap.size() > 0) { log("Copying " + fileCopyMap.size() + " file" + (fileCopyMap.size() == 1 ? "" : "s") + " to " + destDir.getAbsolutePath()); Enumeration e = fileCopyMap.keys(); while (e.hasMoreElements()) { String fromFile = (String) e.nextElement(); String[] toFiles = (String[]) fileCopyMap.get(fromFile); for (int i = 0; i < toFiles.length; i++) { String toFile = toFiles[i]; if (fromFile.equals(toFile)) { log("Skipping self-copy of " + fromFile, verbosity); continue; } try { log("Copying " + fromFile + " to " + toFile, verbosity); FilterSetCollection executionFilters = new FilterSetCollection(); if (filtering) { executionFilters .addFilterSet(getProject().getGlobalFilterSet()); } for (Enumeration filterEnum = filterSets.elements(); filterEnum.hasMoreElements();) { executionFilters .addFilterSet((FilterSet) filterEnum.nextElement()); } fileUtils.copyFile(fromFile, toFile, executionFilters, filterChains, forceOverwrite, preserveLastModified, inputEncoding, outputEncoding, getProject()); } catch (IOException ioe) { String msg = "Failed to copy " + fromFile + " to " + toFile + " due to " + getDueTo(ioe); File targetFile = new File(toFile); if (targetFile.exists() && !targetFile.delete()) { msg += " and I couldn't delete the corrupt " + toFile; } if (failonerror) { throw new BuildException(msg, ioe, getLocation()); } log(msg, Project.MSG_ERR); } } } } if (includeEmpty) { Enumeration e = dirCopyMap.elements(); int createCount = 0; while (e.hasMoreElements()) { String[] dirs = (String[]) e.nextElement(); for (int i = 0; i < dirs.length; i++) { File d = new File(dirs[i]); if (!d.exists()) { if (!d.mkdirs()) { log("Unable to create directory " + d.getAbsolutePath(), Project.MSG_ERR); } else { createCount++; } } } } if (createCount > 0) { log("Copied " + dirCopyMap.size() + " empty director" + (dirCopyMap.size() == 1 ? "y" : "ies") + " to " + createCount + " empty director" + (createCount == 1 ? "y" : "ies") + " under " + destDir.getAbsolutePath()); } } } /** * Actually does the resource copies. * This is a good method for subclasses to override. * @param map a map of source resource to array of destination files. * @since Ant 1.7 */ protected void doResourceOperations(Map map) { if (map.size() > 0) { log("Copying " + map.size() + " resource" + (map.size() == 1 ? "" : "s") + " to " + destDir.getAbsolutePath()); Iterator iter = map.keySet().iterator(); while (iter.hasNext()) { Resource fromResource = (Resource) iter.next(); String[] toFiles = (String[]) map.get(fromResource); for (int i = 0; i < toFiles.length; i++) { String toFile = toFiles[i]; try { log("Copying " + fromResource + " to " + toFile, verbosity); FilterSetCollection executionFilters = new FilterSetCollection(); if (filtering) { executionFilters .addFilterSet(getProject().getGlobalFilterSet()); } for (Enumeration filterEnum = filterSets.elements(); filterEnum.hasMoreElements();) { executionFilters .addFilterSet((FilterSet) filterEnum.nextElement()); } ResourceUtils.copyResource(fromResource, new FileResource(destDir, toFile), executionFilters, filterChains, forceOverwrite, preserveLastModified, inputEncoding, outputEncoding, getProject()); } catch (IOException ioe) { String msg = "Failed to copy " + fromResource + " to " + toFile + " due to " + getDueTo(ioe); File targetFile = new File(toFile); if (targetFile.exists() && !targetFile.delete()) { msg += " and I couldn't delete the corrupt " + toFile; } if (failonerror) { throw new BuildException(msg, ioe, getLocation()); } log(msg, Project.MSG_ERR); } } } } } /** * Whether this task can deal with non-file resources. * * <p><copy> can while <move> can't since we don't * know how to remove non-file resources.</p> * * <p>This implementation returns true only if this task is * <copy>. Any subclass of this class that also wants to * support non-file resources needs to override this method. We * need to do so for backwards compatibility reasons since we * can't expect subclasses to support resources.</p> * @return true if this task supports non file resources. * @since Ant 1.7 */ protected boolean supportsNonFileResources() { return getClass().equals(Copy.class); } /** * Adds the given strings to a list contained in the given map. * The file is the key into the map. */ private static void add(File baseDir, String[] names, Map m) { if (names != null) { baseDir = getKeyFile(baseDir); List l = (List) m.get(baseDir); if (l == null) { l = new ArrayList(names.length); m.put(baseDir, l); } l.addAll(java.util.Arrays.asList(names)); } } /** * Adds the given string to a list contained in the given map. * The file is the key into the map. */ private static void add(File baseDir, String name, Map m) { if (name != null) { add(baseDir, new String[] {name}, m); } } /** * Either returns its argument or a plaeholder if the argument is null. */ private static File getKeyFile(File f) { return f == null ? NULL_FILE_PLACEHOLDER : f; } /** * returns the mapper to use based on nested elements or the * flatten attribute. */ private FileNameMapper getMapper() { FileNameMapper mapper = null; if (mapperElement != null) { mapper = mapperElement.getImplementation(); } else if (flatten) { mapper = new FlatFileNameMapper(); } else { mapper = new IdentityMapper(); } return mapper; } /** * Handle getMessage() for exceptions. * @param ex the exception to handle * @return ex.getMessage() if ex.getMessage() is not null * otherwise return ex.toString() */ private String getMessage(Exception ex) { return ex.getMessage() == null ? ex.toString() : ex.getMessage(); } /** * Returns a reason for failure based on * the exception thrown. * If the exception is not IOException output the class name, * output the message * if the exception is MalformedInput add a little note. */ private String getDueTo(Exception ex) { boolean baseIOException = ex.getClass() == IOException.class; StringBuffer message = new StringBuffer(); if (!baseIOException || ex.getMessage() == null) { message.append(ex.getClass().getName()); } if (ex.getMessage() != null) { if (!baseIOException) { message.append(" "); } message.append(ex.getMessage()); } if (ex.getClass().getName().indexOf("MalformedInput") != -1) { message.append(LINE_SEPARATOR); message.append( "This is normally due to the input file containing invalid"); message.append(LINE_SEPARATOR); message.append("bytes for the character encoding used : "); message.append( (inputEncoding == null ? fileUtils.getDefaultEncoding() : inputEncoding)); message.append(LINE_SEPARATOR); } return message.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -