From 8e37dd0ba24131a542e2b7d4c7e1983c69ca188b Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 13 Nov 2001 13:46:04 +0000 Subject: [PATCH] Add encoding attribute to PR: 961 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269904 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/fixcrlf.html | 5 ++ src/etc/testcases/taskdefs/fixcrlf/build.xml | 11 +++- .../taskdefs/fixcrlf/expected/input.lf.utf16 | Bin 0 -> 26 bytes .../taskdefs/fixcrlf/input/input.crlf.utf16 | Bin 0 -> 30 bytes .../apache/tools/ant/taskdefs/FixCRLF.java | 49 +++++++++++++++--- .../tools/ant/taskdefs/FixCrLfTest.java | 6 +++ 6 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 src/etc/testcases/taskdefs/fixcrlf/expected/input.lf.utf16 create mode 100644 src/etc/testcases/taskdefs/fixcrlf/input/input.crlf.utf16 diff --git a/docs/manual/CoreTasks/fixcrlf.html b/docs/manual/CoreTasks/fixcrlf.html index 76173eb12..318a4faac 100644 --- a/docs/manual/CoreTasks/fixcrlf.html +++ b/docs/manual/CoreTasks/fixcrlf.html @@ -211,6 +211,11 @@ supports all attributes of <fileset> No + + encoding + The encoding of the files + No - defaults to default JVM encoding +

Examples

  <fixcrlf srcdir="${src}"
diff --git a/src/etc/testcases/taskdefs/fixcrlf/build.xml b/src/etc/testcases/taskdefs/fixcrlf/build.xml
index 66a3369b5..ab9943e7a 100644
--- a/src/etc/testcases/taskdefs/fixcrlf/build.xml
+++ b/src/etc/testcases/taskdefs/fixcrlf/build.xml
@@ -5,7 +5,7 @@
   
 
   
-    
+
   
 
   
@@ -97,4 +97,13 @@
              />
   
 
+  
+    
+  
+
 
diff --git a/src/etc/testcases/taskdefs/fixcrlf/expected/input.lf.utf16 b/src/etc/testcases/taskdefs/fixcrlf/expected/input.lf.utf16
new file mode 100644
index 0000000000000000000000000000000000000000..8ffb4e46bcc789b3da28082db8da436f1d657bee
GIT binary patch
literal 26
acmezOpTUPAlOc~GmBEmK3(huT-~s?)4h4k(

literal 0
HcmV?d00001

diff --git a/src/etc/testcases/taskdefs/fixcrlf/input/input.crlf.utf16 b/src/etc/testcases/taskdefs/fixcrlf/input/input.crlf.utf16
new file mode 100644
index 0000000000000000000000000000000000000000..81ad5814ed95b0da31439fa04e2a7c353eb25642
GIT binary patch
literal 30
bcmezOpTUPAlOc~GmBEmKmw^k;GXn7dban-w

literal 0
HcmV?d00001

diff --git a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
index 53522c5ef..784f6e862 100644
--- a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
+++ b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
@@ -60,12 +60,18 @@ import org.apache.tools.ant.Project;
 import org.apache.tools.ant.types.EnumeratedAttribute;
 import org.apache.tools.ant.util.FileUtils;
 
-import java.io.File;
 import java.io.BufferedReader;
-import java.io.FileReader;
-import java.io.IOException;
 import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
 import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
 import java.util.Enumeration;
 import java.util.NoSuchElementException;
 
@@ -84,6 +90,7 @@ import java.util.NoSuchElementException;
  * 
  • eol *
  • tab *
  • eof + *
  • encoding * * Of these arguments, only sourcedir is required. *

    @@ -150,6 +157,11 @@ public class FixCRLF extends MatchingTask { private FileUtils fileUtils = FileUtils.newFileUtils(); + /** + * Encoding to assume for the files + */ + private String encoding = null; + /** * Defaults the properties based on the system type. *

    • Unix: eol="LF" tab="asis" eof="remove" @@ -316,6 +328,15 @@ public class FixCRLF extends MatchingTask { ctrlz = ADD; } } + + /** + * Specifies the encoding Ant expects the files to be in - + * defaults to the platforms default encoding. + */ + public void setEncoding(String encoding) { + this.encoding = encoding; + } + /** * Executes the task. */ @@ -346,7 +367,8 @@ public class FixCRLF extends MatchingTask { (eol==ASIS ? "asis" : eol==CR ? "cr" : eol==LF ? "lf" : "crlf") + " tab=" + (tabs==TABS ? "add" : tabs==ASIS ? "asis" : "remove") + " eof=" + (ctrlz==ADD ? "add" : ctrlz==ASIS ? "asis" : "remove") + - " tablength=" + tablength, + " tablength=" + tablength + + " encoding=" + (encoding == null ? "default" : encoding), Project.MSG_VERBOSE); DirectoryScanner ds = super.getDirectoryScanner(srcDir); @@ -357,6 +379,16 @@ public class FixCRLF extends MatchingTask { } } + /** + * Creates a Reader reading from a given file an taking the user + * defined encoding into account. + */ + private Reader getReader(File f) throws IOException { + return (encoding == null) ? new FileReader(f) + : new InputStreamReader(new FileInputStream(f), encoding); + } + + /** * Checks for the inequality of two files */ @@ -373,9 +405,9 @@ public class FixCRLF extends MatchingTask { try { reader1 = new BufferedReader - (new FileReader(file1), INBUFLEN); + (getReader(file1), INBUFLEN); reader2 = new BufferedReader - (new FileReader(file2), INBUFLEN); + (getReader(file2), INBUFLEN); while ((buflen = reader1.read(buf1, 0, INBUFLEN)) != -1 ) { reader2.read(buf2, 0, INBUFLEN); // Compare the contents of the buffers @@ -414,7 +446,8 @@ public class FixCRLF extends MatchingTask { // Set up the output Writer try { tmpFile = fileUtils.createTempFile("fixcrlf", "", destD); - FileWriter writer = new FileWriter(tmpFile); + Writer writer = (encoding == null) ? new FileWriter(tmpFile) + : new OutputStreamWriter(new FileOutputStream(tmpFile), encoding); outWriter = new BufferedWriter(writer); } catch (IOException e) { throw new BuildException(e); @@ -820,7 +853,7 @@ public class FixCRLF extends MatchingTask { { try { reader = new BufferedReader - (new FileReader(srcFile), INBUFLEN); + (getReader(srcFile), INBUFLEN); nextLine(); } catch (IOException e) { throw new BuildException(e); diff --git a/src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java b/src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java index 5dcc43b99..51088e371 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java @@ -159,6 +159,12 @@ public class FixCrLfTest extends TaskdefsTest { assertEquals(modTime, result.lastModified()); } + public void testEncoding() throws IOException { + executeTarget("testEncoding"); + assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/input.lf.utf16"), + new File("src/etc/testcases/taskdefs/fixcrlf/result/input.crlf.utf16")); + } + public void assertEqualContent(File expect, File result) throws AssertionFailedError, IOException { if (!result.exists()) {