diff --git a/WHATSNEW b/WHATSNEW index 98e8e0fd5..e372ffb69 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -184,6 +184,9 @@ Other changes: different separator. This is useful if you want to run certain ported Unix tools. +* Copy has a new outputencoding attribute that can be used to change + the encoding while copying files. Bugzilla Report 18217. + Changes from Ant 1.5.2 to Ant 1.5.3 =================================== diff --git a/docs/manual/CoreTasks/copy.html b/docs/manual/CoreTasks/copy.html index 9bc156fd9..0f79edee9 100644 --- a/docs/manual/CoreTasks/copy.html +++ b/docs/manual/CoreTasks/copy.html @@ -104,6 +104,19 @@ operation as filtersets Log the files that are being copied. No; defaults to false. + + encoding + The encoding to assume when filter-copying the + files. since Ant 1.5. + No - defaults to default JVM encoding + + + outputencoding + The encoding to use when writing the files. + since Ant 1.6. + No - defaults to the value of the encoding + attribute if given or the default JVM encoding otherwise. +

Parameters specified as nested elements

diff --git a/docs/manual/CoreTasks/move.html b/docs/manual/CoreTasks/move.html index fd8052c97..c2e1982ec 100644 --- a/docs/manual/CoreTasks/move.html +++ b/docs/manual/CoreTasks/move.html @@ -89,6 +89,19 @@ to move to the todir directory.

Log the files that are being moved. No; defaults to false. + + encoding + The encoding to assume when filter-copying the + files. since Ant 1.5. + No - defaults to default JVM encoding + + + outputencoding + The encoding to use when writing the files. + since Ant 1.6. + No - defaults to the value of the encoding + attribute if given or the default JVM encoding otherwise. +

Parameters specified as nested elements

mapper

diff --git a/src/etc/testcases/taskdefs/copy.xml b/src/etc/testcases/taskdefs/copy.xml index 73014a75a..7a6643523 100644 --- a/src/etc/testcases/taskdefs/copy.xml +++ b/src/etc/testcases/taskdefs/copy.xml @@ -62,6 +62,11 @@ + + + + diff --git a/src/etc/testcases/taskdefs/copy/expected/utf-8 b/src/etc/testcases/taskdefs/copy/expected/utf-8 new file mode 100644 index 000000000..c1949bc19 --- /dev/null +++ b/src/etc/testcases/taskdefs/copy/expected/utf-8 @@ -0,0 +1 @@ +äöüÄÖÜß diff --git a/src/etc/testcases/taskdefs/copy/input/iso8859-1 b/src/etc/testcases/taskdefs/copy/input/iso8859-1 new file mode 100644 index 000000000..09044014b --- /dev/null +++ b/src/etc/testcases/taskdefs/copy/input/iso8859-1 @@ -0,0 +1 @@ +äöüÄÖÜß diff --git a/src/main/org/apache/tools/ant/taskdefs/Copy.java b/src/main/org/apache/tools/ant/taskdefs/Copy.java index 1f2eb8e3a..87e181b48 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Copy.java +++ b/src/main/org/apache/tools/ant/taskdefs/Copy.java @@ -118,7 +118,8 @@ public class Copy extends Task { private Vector filterChains = new Vector(); private Vector filterSets = new Vector(); private FileUtils fileUtils; - private String encoding = null; + private String inputEncoding = null; + private String outputEncoding = null; /** * Copy task constructor. @@ -291,7 +292,10 @@ public class Copy extends Task { * @since 1.32, Ant 1.5 */ public void setEncoding (String encoding) { - this.encoding = encoding; + this.inputEncoding = encoding; + if (outputEncoding == null) { + outputEncoding = encoding; + } } /** @@ -300,7 +304,26 @@ public class Copy extends Task { * @since 1.32, Ant 1.5 */ public String getEncoding() { - return encoding; + return inputEncoding; + } + + /** + * Sets the character encoding for output files. + * + * @since Ant 1.6 + */ + public void setOutputEncoding(String encoding) { + this.outputEncoding = encoding; + } + + /** + * @return the character encoding for output files, + * null if not set. + * + * @since Ant 1.6 + */ + public String getOutputEncoding() { + return outputEncoding; } /** @@ -526,8 +549,8 @@ public class Copy extends Task { } fileUtils.copyFile(fromFile, toFile, executionFilters, filterChains, forceOverwrite, - preserveLastModified, encoding, - getProject()); + preserveLastModified, inputEncoding, + outputEncoding, getProject()); } catch (IOException ioe) { String msg = "Failed to copy " + fromFile + " to " + toFile + " due to " + ioe.getMessage(); diff --git a/src/main/org/apache/tools/ant/taskdefs/Move.java b/src/main/org/apache/tools/ant/taskdefs/Move.java index 7dca8834c..4249aa93b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Move.java +++ b/src/main/org/apache/tools/ant/taskdefs/Move.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2000-2002 The Apache Software Foundation. All rights + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -172,7 +172,9 @@ public class Move extends Copy { getFilterChains(), forceOverwrite, getPreserveLastModified(), - getEncoding(), getProject()); + getEncoding(), + getOutputEncoding(), + getProject()); f = new File(fromFile); if (!f.delete()) { diff --git a/src/main/org/apache/tools/ant/taskdefs/Sync.java b/src/main/org/apache/tools/ant/taskdefs/Sync.java index 187696962..04457b14c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Sync.java +++ b/src/main/org/apache/tools/ant/taskdefs/Sync.java @@ -330,6 +330,13 @@ public class Sync extends Task { _copy.setEncoding(encoding); } + /** + * Sets the character encoding for output files. + */ + public void setOutputEncoding(String encoding) { + _copy.setOutputEncoding(encoding); + } + /** * Subclass Copy in order to access it's file/dir maps. diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index ec1ff948b..30d264187 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -248,6 +248,29 @@ public class FileUtils { encoding, project); } + /** + * Convienence 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 + * destFile file should be made equal + * to the last modified time of sourceFile. + * + * @throws IOException + * + * @since Ant 1.6 + */ + public void copyFile(String sourceFile, String destFile, + FilterSetCollection filters, Vector 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); + } + /** * Convienence method to copy a file from a source to a destination. * No filtering is performed. @@ -334,6 +357,28 @@ public class FileUtils { boolean overwrite, boolean preserveLastModified, String encoding, Project project) throws IOException { + copyFile(sourceFile, destFile, filters, filterChains, + overwrite, preserveLastModified, encoding, encoding, project); + } + + /** + * Convienence 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 + * destFile file should be made equal + * to the last modified time of sourceFile. + * + * @throws IOException + * + * @since 1.15, Ant 1.6 + */ + public void copyFile(File sourceFile, File destFile, + FilterSetCollection filters, Vector filterChains, + boolean overwrite, boolean preserveLastModified, + String inputEncoding, String outputEncoding, + Project project) + throws IOException { if (overwrite || !destFile.exists() || destFile.lastModified() < sourceFile.lastModified()) { @@ -354,23 +399,30 @@ public class FileUtils { final boolean filterChainsAvailable = (filterChains != null && filterChains.size() > 0); - if (filterSetsAvailable || filterChainsAvailable) { + if (filterSetsAvailable || filterChainsAvailable + || (inputEncoding != null + && !inputEncoding.equals(outputEncoding)) + || (inputEncoding == null && outputEncoding != null)) { BufferedReader in = null; BufferedWriter out = null; try { - if (encoding == null) { + if (inputEncoding == null) { in = new BufferedReader(new FileReader(sourceFile)); - out = new BufferedWriter(new FileWriter(destFile)); } else { in = new BufferedReader(new InputStreamReader( new FileInputStream(sourceFile), - encoding)); + inputEncoding)); + } + + if (outputEncoding == null) { + out = new BufferedWriter(new FileWriter(destFile)); + } else { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(destFile), - encoding)); + outputEncoding)); } if (filterChainsAvailable) { diff --git a/src/testcases/org/apache/tools/ant/taskdefs/CopyTest.java b/src/testcases/org/apache/tools/ant/taskdefs/CopyTest.java index e94e87147..1689395b9 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/CopyTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/CopyTest.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2000-2002 The Apache Software Foundation. All rights + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -150,4 +150,12 @@ public class CopyTest extends BuildFileTest { "copytest_single_file_fileset.tmp"); assertTrue(file.exists()); } + + public void testTranscoding() throws IOException { + executeTarget("testTranscoding"); + FileUtils fileUtils = FileUtils.newFileUtils(); + File f1 = getProject().resolveFile("copy/expected/utf-8"); + File f2 = getProject().resolveFile("copytest1.tmp"); + assertTrue(fileUtils.contentEquals(f1, f2)); + } }