/** * Copy fromFile to toFile. * @param fromFile File * @param toFile File * @param filtering boolean * @param overwrite boolean */ private void copyFile(File fromFile, File toFile, boolean filtering, boolean overwrite) { try { log("Copying " + fromFile + " to " + toFile, verbosity); FilterSetCollection executionFilters = new FilterSetCollection(); if (filtering) { executionFilters.addFilterSet(getProject().getGlobalFilterSet()); } getFilterSets().forEach(executionFilters::addFilterSet); getFileUtils().copyFile(fromFile, toFile, executionFilters, getFilterChains(), forceOverwrite, getPreserveLastModified(), /* append: */ false, getEncoding(), getOutputEncoding(), getProject(), getForce()); } catch (IOException ioe) { throw new BuildException("Failed to copy " + fromFile + " to " + toFile + " due to " + ioe.getMessage(), ioe, getLocation()); } }
protected static void copyStream(InputStream in, OutputStream out, FilterSetCollection filterSets, String encoding) throws IOException { byte[] b = new byte[1024]; if (filterSets.hasFilters()) { InputStreamReader reader = new InputStreamReader(in, encoding); OutputStreamWriter writer = new OutputStreamWriter(out, encoding); LineTokenizer tok = new LineTokenizer(); tok.setIncludeDelims(true); for (String l = tok.getToken(reader); l != null; l = tok.getToken(reader)) { writer.write(filterSets.replaceTokens(l)); } writer.close(); reader.close(); } else { while (in.available() > 0) { int cnt = in.read(b, 0, b.length); if (cnt > -1) { out.write(b, 0, cnt); } } } }
/** * Move the generated source file(s) to the base directory * * @throws org.apache.tools.ant.BuildException When error * copying/removing files. */ private void moveGeneratedFile(File baseDir, File sourceBaseFile, String classname, RmicAdapter adapter) throws BuildException { String classFileName = classname.replace('.', File.separatorChar) + ".class"; String[] generatedFiles = adapter.getMapper().mapFileName(classFileName); for (String generatedFile : generatedFiles) { if (!generatedFile.endsWith(".class")) { // don't know how to handle that - a IDL file doesn't // have a corresponding Java source for example. continue; } String sourceFileName = StringUtils.removeSuffix(generatedFile, ".class") + ".java"; File oldFile = new File(baseDir, sourceFileName); if (!oldFile.exists()) { // no source file generated, nothing to move continue; } File newFile = new File(sourceBaseFile, sourceFileName); try { if (filtering) { FILE_UTILS.copyFile(oldFile, newFile, new FilterSetCollection(getProject() .getGlobalFilterSet())); } else { FILE_UTILS.copyFile(oldFile, newFile); } oldFile.delete(); } catch (IOException ioe) { throw new BuildException("Failed to copy " + oldFile + " to " + newFile + " due to " + ioe.getMessage(), ioe, getLocation()); } } }
private static void copyWithFilterSets(final Resource source, final Resource dest, final FilterSetCollection filters, final Vector<FilterChain> filterChains, final boolean append, final String inputEncoding, final String outputEncoding, final Project project) throws IOException { if (areSame(source, dest)) { // copying the "same" file to itself will corrupt the file, so we skip it log(project, "Skipping (self) copy of " + source + " to " + dest); return; } try (Reader in = filterWith(project, inputEncoding, filterChains, source.getInputStream()); BufferedWriter out = new BufferedWriter(new OutputStreamWriter( getOutputStream(dest, append, project), charsetFor(outputEncoding)))) { final LineTokenizer lineTokenizer = new LineTokenizer(); lineTokenizer.setIncludeDelims(true); String newline = null; String line = lineTokenizer.getToken(in); while (line != null) { if (line.length() == 0) { // this should not happen, because the lines are // returned with the end of line delimiter out.newLine(); } else { newline = filters.replaceTokens(line); out.write(newline); } line = lineTokenizer.getToken(in); } } }
@Override protected void doFileOperations() { if (fileCopyMap.size() > 0) { log("Copying " + fileCopyMap.size() + " file" + (fileCopyMap.size() == 1 ? "" : "s") + " to " + destDir.getAbsolutePath()); Enumeration<String> e = fileCopyMap.keys(); while (e.hasMoreElements()) { String fromFile = e.nextElement(); String[] toFiles = (String[]) fileCopyMap.get(fromFile); for (String toFile : toFiles) { 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 = getFilterSets().elements(); filterEnum.hasMoreElements();) { executionFilters .addFilterSet((FilterSet) filterEnum.nextElement()); } File srcFile = new File(fromFile); File dstFile = new File(toFile); preprocess(srcFile, dstFile); } catch (Exception ioe) { // ioe.printStackTrace(); String msg = "Failed to copy " + fromFile + " to " + toFile + " due to " + ioe.getMessage(); File targetFile = new File(toFile); if (targetFile.exists() && !targetFile.delete()) { msg += " and I couldn't delete the corrupt " + toFile; } throw new BuildException(msg, ioe, getLocation()); } } } } }
/** * Actually does the file (and possibly empty directory) copies. * This is a good method for subclasses to override. */ protected void doFileOperations() { if (!fileCopyMap.isEmpty()) { log("Copying " + fileCopyMap.size() + " file" + (fileCopyMap.size() == 1 ? "" : "s") + " to " + destDir.getAbsolutePath()); for (final Map.Entry<String, String[]> e : fileCopyMap.entrySet()) { final String fromFile = e.getKey(); final String[] toFiles = e.getValue(); for (final String toFile : toFiles) { if (fromFile.equals(toFile)) { log("Skipping self-copy of " + fromFile, verbosity); continue; } try { log("Copying " + fromFile + " to " + toFile, verbosity); final FilterSetCollection executionFilters = new FilterSetCollection(); if (filtering) { executionFilters .addFilterSet(getProject().getGlobalFilterSet()); } for (final FilterSet filterSet : filterSets) { executionFilters.addFilterSet(filterSet); } fileUtils.copyFile(new File(fromFile), new File(toFile), executionFilters, filterChains, forceOverwrite, preserveLastModified, /* append: */ false, inputEncoding, outputEncoding, getProject(), getForce()); } catch (final IOException ioe) { String msg = "Failed to copy " + fromFile + " to " + toFile + " due to " + getDueTo(ioe); final File targetFile = new File(toFile); if (!(ioe instanceof ResourceUtils.ReadOnlyTargetFileException) && 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) { int createCount = 0; for (final String[] dirs : dirCopyMap.values()) { for (String dir : dirs) { final File d = new File(dir); if (!d.exists()) { if (!(d.mkdirs() || d.isDirectory())) { 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(final Map<Resource, String[]> map) { if (!map.isEmpty()) { log("Copying " + map.size() + " resource" + (map.size() == 1 ? "" : "s") + " to " + destDir.getAbsolutePath()); for (final Map.Entry<Resource, String[]> e : map.entrySet()) { final Resource fromResource = e.getKey(); for (final String toFile : e.getValue()) { try { log("Copying " + fromResource + " to " + toFile, verbosity); final FilterSetCollection executionFilters = new FilterSetCollection(); if (filtering) { executionFilters .addFilterSet(getProject().getGlobalFilterSet()); } for (final FilterSet filterSet : filterSets) { executionFilters.addFilterSet(filterSet); } ResourceUtils.copyResource(fromResource, new FileResource(destDir, toFile), executionFilters, filterChains, forceOverwrite, preserveLastModified, /* append: */ false, inputEncoding, outputEncoding, getProject(), getForce()); } catch (final IOException ioe) { String msg = "Failed to copy " + fromResource + " to " + toFile + " due to " + getDueTo(ioe); final File targetFile = new File(toFile); if (!(ioe instanceof ResourceUtils.ReadOnlyTargetFileException) && 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); } } } } }
/** * Convenience method to copy content from one Resource to another * specifying whether token filtering must be used, whether filter chains * must be used, whether newer destination files may be overwritten and * whether the last modified time of <code>dest</code> file should be made * equal to the last modified time of <code>source</code>. * * @param source the Resource to copy from. * Must not be <code>null</code>. * @param dest the Resource to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param filterChains filterChains to apply during the copy. * @param overwrite Whether or not the destination Resource should be * overwritten if it already exists. * @param preserveLastModified Whether or not the last modified time of * the destination Resource should be set to that * of the source. * @param inputEncoding the encoding used to read the files. * @param outputEncoding the encoding used to write the files. * @param project the project instance. * * @throws IOException if the copying fails. * * @since Ant 1.7 */ public static void copyResource(final Resource source, final Resource dest, final FilterSetCollection filters, final Vector<FilterChain> filterChains, final boolean overwrite, final boolean preserveLastModified, final String inputEncoding, final String outputEncoding, final Project project) throws IOException { copyResource(source, dest, filters, filterChains, overwrite, preserveLastModified, false, inputEncoding, outputEncoding, project); }
/** * Convenience method to copy content from one Resource to another * specifying whether token filtering must be used, whether filter chains * must be used, whether newer destination files may be overwritten and * whether the last modified time of <code>dest</code> file should be made * equal to the last modified time of <code>source</code>. * * @param source the Resource to copy from. * Must not be <code>null</code>. * @param dest the Resource to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param filterChains filterChains to apply during the copy. * @param overwrite Whether or not the destination Resource should be * overwritten if it already exists. * @param preserveLastModified Whether or not the last modified time of * the destination Resource should be set to that * of the source. * @param append Whether to append to an Appendable Resource. * @param inputEncoding the encoding used to read the files. * @param outputEncoding the encoding used to write the files. * @param project the project instance. * * @throws IOException if the copying fails. * * @since Ant 1.8 */ public static void copyResource(final Resource source, final Resource dest, final FilterSetCollection filters, final Vector<FilterChain> filterChains, final boolean overwrite, final boolean preserveLastModified, final boolean append, final String inputEncoding, final String outputEncoding, final Project project) throws IOException { copyResource(source, dest, filters, filterChains, overwrite, preserveLastModified, append, inputEncoding, outputEncoding, project, /* force: */ false); }
/** * Convenience method to copy a file from a source to a destination * specifying if token * filtering must be used, if source files may overwrite newer destination * files and the last * modified time of <code>destFile</code> file should be made equal to * the last modified time * of <code>sourceFile</code>. * * @param sourceFile Name of file to copy from. Must not be <code>null</code>. * @param destFile Name of file to copy to. Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param overwrite Whether or not the destination file should be * overwritten if it already exists. * @param preserveLastModified Whether or not the last modified time of * the resulting file * should be set to that of the source file. * * @throws IOException if the copying fails. */ public void copyFile(String sourceFile, String destFile, FilterSetCollection filters, boolean overwrite, boolean preserveLastModified) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, overwrite, preserveLastModified); }
/** * Convenience method to copy a file from a source to a destination specifying if token * filtering must be used, if source files may overwrite newer destination files and the last * modified time of <code>destFile</code> file should be made equal to the last modified time * of <code>sourceFile</code>. * * @param sourceFile Name of file to copy from. Must not be <code>null</code>. * @param destFile Name of file to copy to. Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param overwrite Whether or not the destination file should be overwritten if it already * exists. * @param preserveLastModified Whether or not the last modified time of the resulting file * should be set to that of the source file. * @param encoding the encoding used to read and write the files. * * @throws IOException if the copying fails. * * @since Ant 1.5 */ public void copyFile(String sourceFile, String destFile, FilterSetCollection filters, boolean overwrite, boolean preserveLastModified, String encoding) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, overwrite, preserveLastModified, encoding); }
/** * Convenience method to copy a file from a source to a * destination specifying if token filtering must be used, if * filter chains must be used, if source files may overwrite * newer destination files and the last modified time of * <code>destFile</code> file should be made equal * to the last modified time of <code>sourceFile</code>. * * @param sourceFile Name of file to copy from. * Must not be <code>null</code>. * @param destFile Name of file to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param filterChains filterChains to apply during the copy. * @param overwrite Whether or not the destination file should be * overwritten if it already exists. * @param preserveLastModified Whether or not the last modified time of * the resulting file should be set to that * of the source file. * @param encoding the encoding used to read and write the files. * @param project the project instance. * * @throws IOException if the copying fails. * * @since Ant 1.5 */ public void copyFile(String sourceFile, String destFile, FilterSetCollection filters, Vector<FilterChain> filterChains, boolean overwrite, boolean preserveLastModified, String encoding, Project project) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, filterChains, overwrite, preserveLastModified, encoding, project); }
/** * Convenience method to copy a file from a source to a destination specifying if token * filtering must be used, if filter chains must be used, if source files may overwrite newer * destination files and the last modified time of <code>destFile</code> file should be made * equal to the last modified time of <code>sourceFile</code>. * * @param sourceFile Name of file to copy from. Must not be <code>null</code>. * @param destFile Name of file to copy to. Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param filterChains filterChains to apply during the copy. * @param overwrite Whether or not the destination file should be overwritten if it already * exists. * @param preserveLastModified Whether or not the last modified time of the resulting file * should be set to that of the source file. * @param inputEncoding the encoding used to read the files. * @param outputEncoding the encoding used to write the files. * @param project the project instance. * * @throws IOException if the copying fails. * * @since Ant 1.6 */ public void copyFile(String sourceFile, String destFile, FilterSetCollection filters, Vector<FilterChain> filterChains, boolean overwrite, boolean preserveLastModified, String inputEncoding, String outputEncoding, Project project) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, filterChains, overwrite, preserveLastModified, inputEncoding, outputEncoding, project); }
/** * Convenience method to copy a file from a source to a destination specifying if token * filtering must be used, if source files may overwrite newer destination files, the last * modified time of <code>destFile</code> file should be made equal to the last modified time * of <code>sourceFile</code> and which character encoding to assume. * * @param sourceFile the file to copy from. Must not be <code>null</code>. * @param destFile the file to copy to. Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param overwrite Whether or not the destination file should be overwritten if it already * exists. * @param preserveLastModified Whether or not the last modified time of the resulting file * should be set to that of the source file. * @param encoding the encoding used to read and write the files. * * @throws IOException if the copying fails. * * @since Ant 1.5 */ public void copyFile(File sourceFile, File destFile, FilterSetCollection filters, boolean overwrite, boolean preserveLastModified, String encoding) throws IOException { copyFile(sourceFile, destFile, filters, null, overwrite, preserveLastModified, encoding, null); }
/** * Convenience method to copy a file from a source to a * destination specifying if token filtering must be used, if * filter chains must be used, if source files may overwrite * newer destination files and the last modified time of * <code>destFile</code> file should be made equal * to the last modified time of <code>sourceFile</code>. * * @param sourceFile the file to copy from. * Must not be <code>null</code>. * @param destFile the file to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param filterChains filterChains to apply during the copy. * @param overwrite Whether or not the destination file should be * overwritten if it already exists. * @param preserveLastModified Whether or not the last modified time of * the resulting file should be set to that * of the source file. * @param encoding the encoding used to read and write the files. * @param project the project instance. * * @throws IOException if the copying fails. * * @since Ant 1.5 */ public void copyFile(File sourceFile, File destFile, FilterSetCollection filters, Vector<FilterChain> filterChains, boolean overwrite, boolean preserveLastModified, String encoding, Project project) throws IOException { copyFile(sourceFile, destFile, filters, filterChains, overwrite, preserveLastModified, encoding, encoding, project); }
/** * Convenience method to copy a file from a source to a * destination specifying if token filtering must be used, if * filter chains must be used, if source files may overwrite * newer destination files and the last modified time of * <code>destFile</code> file should be made equal * to the last modified time of <code>sourceFile</code>. * * @param sourceFile the file to copy from. * Must not be <code>null</code>. * @param destFile the file to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param filterChains filterChains to apply during the copy. * @param overwrite Whether or not the destination file should be * overwritten if it already exists. * @param preserveLastModified Whether or not the last modified time of * the resulting file should be set to that * of the source file. * @param inputEncoding the encoding used to read the files. * @param outputEncoding the encoding used to write the files. * @param project the project instance. * * * @throws IOException if the copying fails. * * @since Ant 1.6 */ public void copyFile(File sourceFile, File destFile, FilterSetCollection filters, Vector<FilterChain> filterChains, boolean overwrite, boolean preserveLastModified, String inputEncoding, String outputEncoding, Project project) throws IOException { copyFile(sourceFile, destFile, filters, filterChains, overwrite, preserveLastModified, false, inputEncoding, outputEncoding, project); }
/** * Convenience method to copy a file from a source to a * destination specifying if token filtering must be used, if * filter chains must be used, if source files may overwrite * newer destination files and the last modified time of * <code>destFile</code> file should be made equal * to the last modified time of <code>sourceFile</code>. * * @param sourceFile the file to copy from. * Must not be <code>null</code>. * @param destFile the file to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param filterChains filterChains to apply during the copy. * @param overwrite Whether or not the destination file should be * overwritten if it already exists. * @param preserveLastModified Whether or not the last modified time of * the resulting file should be set to that * of the source file. * @param append whether to append to the destination file. * @param inputEncoding the encoding used to read the files. * @param outputEncoding the encoding used to write the files. * @param project the project instance. * * * @throws IOException if the copying fails. * * @since Ant 1.8 */ public void copyFile(File sourceFile, File destFile, FilterSetCollection filters, Vector<FilterChain> filterChains, boolean overwrite, boolean preserveLastModified, boolean append, String inputEncoding, String outputEncoding, Project project) throws IOException { copyFile(sourceFile, destFile, filters, filterChains, overwrite, preserveLastModified, append, inputEncoding, outputEncoding, project, /* force: */ false); }
/** * Convenience method to copy a file from a source to a * destination specifying if token filtering must be used, if * filter chains must be used, if source files may overwrite * newer destination files and the last modified time of * <code>destFile</code> file should be made equal * to the last modified time of <code>sourceFile</code>. * * @param sourceFile the file to copy from. * Must not be <code>null</code>. * @param destFile the file to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param filterChains filterChains to apply during the copy. * @param overwrite Whether or not the destination file should be * overwritten if it already exists. * @param preserveLastModified Whether or not the last modified time of * the resulting file should be set to that * of the source file. * @param append whether to append to the destination file. * @param inputEncoding the encoding used to read the files. * @param outputEncoding the encoding used to write the files. * @param project the project instance. * @param force whether to overwrite read-only destination files. * * @throws IOException if the copying fails. * * @since Ant 1.8.2 */ public void copyFile(File sourceFile, File destFile, FilterSetCollection filters, Vector<FilterChain> filterChains, boolean overwrite, boolean preserveLastModified, boolean append, String inputEncoding, String outputEncoding, Project project, boolean force) throws IOException { ResourceUtils.copyResource(new FileResource(sourceFile), new FileResource(destFile), filters, filterChains, overwrite, preserveLastModified, append, inputEncoding, outputEncoding, project, force); }
/** * Convenience method to copy a file from a source to a destination * specifying if token filtering must be used. * * @param sourceFile Name of file to copy from. * Must not be <code>null</code>. * @param destFile Name of file to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * * @throws IOException if the copying fails. */ public void copyFile(String sourceFile, String destFile, FilterSetCollection filters) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, false, false); }
/** * Convenience method to copy a file from a source to a destination specifying if token * filtering must be used and if source files may overwrite newer destination files. * * @param sourceFile Name of file to copy from. Must not be <code>null</code>. * @param destFile Name of file to copy to. Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param overwrite Whether or not the destination file should be overwritten if it already * exists. * * @throws IOException if the copying fails. */ public void copyFile(String sourceFile, String destFile, FilterSetCollection filters, boolean overwrite) throws IOException { copyFile(new File(sourceFile), new File(destFile), filters, overwrite, false); }
/** * Convenience method to copy a file from a source to a destination * specifying if token filtering must be used. * * @param sourceFile the file to copy from. * Must not be <code>null</code>. * @param destFile the file to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * * @throws IOException if the copying fails. */ public void copyFile(File sourceFile, File destFile, FilterSetCollection filters) throws IOException { copyFile(sourceFile, destFile, filters, false, false); }
/** * Convenience method to copy a file from a source to a * destination specifying if token filtering must be used and if * source files may overwrite newer destination files. * * @param sourceFile the file to copy from. * Must not be <code>null</code>. * @param destFile the file to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param overwrite Whether or not the destination file should be * overwritten if it already exists. * * @throws IOException if the copying fails. */ public void copyFile(File sourceFile, File destFile, FilterSetCollection filters, boolean overwrite) throws IOException { copyFile(sourceFile, destFile, filters, overwrite, false); }
/** * Convenience method to copy a file from a source to a * destination specifying if token filtering must be used, if * source files may overwrite newer destination files and the * last modified time of <code>destFile</code> file should be made equal * to the last modified time of <code>sourceFile</code>. * * @param sourceFile the file to copy from. * Must not be <code>null</code>. * @param destFile the file to copy to. * Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param overwrite Whether or not the destination file should be * overwritten if it already exists. * @param preserveLastModified Whether or not the last modified time of * the resulting file should be set to that * of the source file. * * @throws IOException if the copying fails. */ public void copyFile(File sourceFile, File destFile, FilterSetCollection filters, boolean overwrite, boolean preserveLastModified) throws IOException { copyFile(sourceFile, destFile, filters, overwrite, preserveLastModified, null); }