PR: 18217 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274328 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -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 | |||
| =================================== | |||
| @@ -104,6 +104,19 @@ operation as <a href="../CoreTypes/filterset.html">filtersets</a> | |||
| <td valign="top">Log the files that are being copied.</td> | |||
| <td valign="top" align="center">No; defaults to false.</td> | |||
| </tr> | |||
| <tr> | |||
| <td valign="top">encoding</td> | |||
| <td valign="top">The encoding to assume when filter-copying the | |||
| files. <em>since Ant 1.5</em>.</td> | |||
| <td align="center">No - defaults to default JVM encoding</td> | |||
| </tr> | |||
| <tr> | |||
| <td valign="top">outputencoding</td> | |||
| <td valign="top">The encoding to use when writing the files. | |||
| <em>since Ant 1.6</em>.</td> | |||
| <td align="center">No - defaults to the value of the encoding | |||
| attribute if given or the default JVM encoding otherwise.</td> | |||
| </tr> | |||
| </table> | |||
| <h3>Parameters specified as nested elements</h3> | |||
| @@ -89,6 +89,19 @@ to move to the <var>todir</var> directory.</p> | |||
| <td valign="top">Log the files that are being moved.</td> | |||
| <td valign="top" align="center">No; defaults to false.</td> | |||
| </tr> | |||
| <tr> | |||
| <td valign="top">encoding</td> | |||
| <td valign="top">The encoding to assume when filter-copying the | |||
| files. <em>since Ant 1.5</em>.</td> | |||
| <td align="center">No - defaults to default JVM encoding</td> | |||
| </tr> | |||
| <tr> | |||
| <td valign="top">outputencoding</td> | |||
| <td valign="top">The encoding to use when writing the files. | |||
| <em>since Ant 1.6</em>.</td> | |||
| <td align="center">No - defaults to the value of the encoding | |||
| attribute if given or the default JVM encoding otherwise.</td> | |||
| </tr> | |||
| </table> | |||
| <h3>Parameters specified as nested elements</h3> | |||
| <h4>mapper</h4> | |||
| @@ -62,6 +62,11 @@ | |||
| </copy> | |||
| </target> | |||
| <target name="testTranscoding"> | |||
| <copy file="copy/input/iso8859-1" tofile="copytest1.tmp" | |||
| encoding="ISO8859_1" outputencoding="UTF8"/> | |||
| </target> | |||
| <target name="cleanup"> | |||
| <delete file="copytest1.tmp"/> | |||
| <delete file="copytest3.tmp"/> | |||
| @@ -0,0 +1 @@ | |||
| äöüÄÖÜß | |||
| @@ -0,0 +1 @@ | |||
| 蔕�ヨワ゚ | |||
| @@ -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, | |||
| * <code>null</code> 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(); | |||
| @@ -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()) { | |||
| @@ -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. | |||
| @@ -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 | |||
| * <code>destFile</code> file should be made equal | |||
| * to the last modified time of <code>sourceFile</code>. | |||
| * | |||
| * @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 | |||
| * <code>destFile</code> file should be made equal | |||
| * to the last modified time of <code>sourceFile</code>. | |||
| * | |||
| * @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) { | |||
| @@ -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)); | |||
| } | |||
| } | |||