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 | different separator. This is useful if you want to run certain | ||||
| ported Unix tools. | 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 | 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">Log the files that are being copied.</td> | ||||
| <td valign="top" align="center">No; defaults to false.</td> | <td valign="top" align="center">No; defaults to false.</td> | ||||
| </tr> | </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> | </table> | ||||
| <h3>Parameters specified as nested elements</h3> | <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">Log the files that are being moved.</td> | ||||
| <td valign="top" align="center">No; defaults to false.</td> | <td valign="top" align="center">No; defaults to false.</td> | ||||
| </tr> | </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> | </table> | ||||
| <h3>Parameters specified as nested elements</h3> | <h3>Parameters specified as nested elements</h3> | ||||
| <h4>mapper</h4> | <h4>mapper</h4> | ||||
| @@ -62,6 +62,11 @@ | |||||
| </copy> | </copy> | ||||
| </target> | </target> | ||||
| <target name="testTranscoding"> | |||||
| <copy file="copy/input/iso8859-1" tofile="copytest1.tmp" | |||||
| encoding="ISO8859_1" outputencoding="UTF8"/> | |||||
| </target> | |||||
| <target name="cleanup"> | <target name="cleanup"> | ||||
| <delete file="copytest1.tmp"/> | <delete file="copytest1.tmp"/> | ||||
| <delete file="copytest3.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 filterChains = new Vector(); | ||||
| private Vector filterSets = new Vector(); | private Vector filterSets = new Vector(); | ||||
| private FileUtils fileUtils; | private FileUtils fileUtils; | ||||
| private String encoding = null; | |||||
| private String inputEncoding = null; | |||||
| private String outputEncoding = null; | |||||
| /** | /** | ||||
| * Copy task constructor. | * Copy task constructor. | ||||
| @@ -291,7 +292,10 @@ public class Copy extends Task { | |||||
| * @since 1.32, Ant 1.5 | * @since 1.32, Ant 1.5 | ||||
| */ | */ | ||||
| public void setEncoding (String encoding) { | 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 | * @since 1.32, Ant 1.5 | ||||
| */ | */ | ||||
| public String getEncoding() { | 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, | fileUtils.copyFile(fromFile, toFile, executionFilters, | ||||
| filterChains, forceOverwrite, | filterChains, forceOverwrite, | ||||
| preserveLastModified, encoding, | |||||
| getProject()); | |||||
| preserveLastModified, inputEncoding, | |||||
| outputEncoding, getProject()); | |||||
| } catch (IOException ioe) { | } catch (IOException ioe) { | ||||
| String msg = "Failed to copy " + fromFile + " to " + toFile | String msg = "Failed to copy " + fromFile + " to " + toFile | ||||
| + " due to " + ioe.getMessage(); | + " due to " + ioe.getMessage(); | ||||
| @@ -1,7 +1,7 @@ | |||||
| /* | /* | ||||
| * The Apache Software License, Version 1.1 | * 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. | * reserved. | ||||
| * | * | ||||
| * Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
| @@ -172,7 +172,9 @@ public class Move extends Copy { | |||||
| getFilterChains(), | getFilterChains(), | ||||
| forceOverwrite, | forceOverwrite, | ||||
| getPreserveLastModified(), | getPreserveLastModified(), | ||||
| getEncoding(), getProject()); | |||||
| getEncoding(), | |||||
| getOutputEncoding(), | |||||
| getProject()); | |||||
| f = new File(fromFile); | f = new File(fromFile); | ||||
| if (!f.delete()) { | if (!f.delete()) { | ||||
| @@ -330,6 +330,13 @@ public class Sync extends Task { | |||||
| _copy.setEncoding(encoding); | _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. | * Subclass Copy in order to access it's file/dir maps. | ||||
| @@ -248,6 +248,29 @@ public class FileUtils { | |||||
| encoding, project); | 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. | * Convienence method to copy a file from a source to a destination. | ||||
| * No filtering is performed. | * No filtering is performed. | ||||
| @@ -334,6 +357,28 @@ public class FileUtils { | |||||
| boolean overwrite, boolean preserveLastModified, | boolean overwrite, boolean preserveLastModified, | ||||
| String encoding, Project project) | String encoding, Project project) | ||||
| throws IOException { | 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() || | if (overwrite || !destFile.exists() || | ||||
| destFile.lastModified() < sourceFile.lastModified()) { | destFile.lastModified() < sourceFile.lastModified()) { | ||||
| @@ -354,23 +399,30 @@ public class FileUtils { | |||||
| final boolean filterChainsAvailable = (filterChains != null | final boolean filterChainsAvailable = (filterChains != null | ||||
| && filterChains.size() > 0); | && filterChains.size() > 0); | ||||
| if (filterSetsAvailable || filterChainsAvailable) { | |||||
| if (filterSetsAvailable || filterChainsAvailable | |||||
| || (inputEncoding != null | |||||
| && !inputEncoding.equals(outputEncoding)) | |||||
| || (inputEncoding == null && outputEncoding != null)) { | |||||
| BufferedReader in = null; | BufferedReader in = null; | ||||
| BufferedWriter out = null; | BufferedWriter out = null; | ||||
| try { | try { | ||||
| if (encoding == null) { | |||||
| if (inputEncoding == null) { | |||||
| in = new BufferedReader(new FileReader(sourceFile)); | in = new BufferedReader(new FileReader(sourceFile)); | ||||
| out = new BufferedWriter(new FileWriter(destFile)); | |||||
| } else { | } else { | ||||
| in = | in = | ||||
| new BufferedReader(new InputStreamReader( | new BufferedReader(new InputStreamReader( | ||||
| new FileInputStream(sourceFile), | new FileInputStream(sourceFile), | ||||
| encoding)); | |||||
| inputEncoding)); | |||||
| } | |||||
| if (outputEncoding == null) { | |||||
| out = new BufferedWriter(new FileWriter(destFile)); | |||||
| } else { | |||||
| out = | out = | ||||
| new BufferedWriter(new OutputStreamWriter( | new BufferedWriter(new OutputStreamWriter( | ||||
| new FileOutputStream(destFile), | new FileOutputStream(destFile), | ||||
| encoding)); | |||||
| outputEncoding)); | |||||
| } | } | ||||
| if (filterChainsAvailable) { | if (filterChainsAvailable) { | ||||
| @@ -1,7 +1,7 @@ | |||||
| /* | /* | ||||
| * The Apache Software License, Version 1.1 | * 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. | * reserved. | ||||
| * | * | ||||
| * Redistribution and use in source and binary forms, with or without | * 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"); | "copytest_single_file_fileset.tmp"); | ||||
| assertTrue(file.exists()); | 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)); | |||||
| } | |||||
| } | } | ||||