Browse Source

Add encoding attribute to <fixcrlf>

PR: 961


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269904 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
8e37dd0ba2
6 changed files with 62 additions and 9 deletions
  1. +5
    -0
      docs/manual/CoreTasks/fixcrlf.html
  2. +10
    -1
      src/etc/testcases/taskdefs/fixcrlf/build.xml
  3. BIN
      src/etc/testcases/taskdefs/fixcrlf/expected/input.lf.utf16
  4. BIN
      src/etc/testcases/taskdefs/fixcrlf/input/input.crlf.utf16
  5. +41
    -8
      src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
  6. +6
    -0
      src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java

+ 5
- 0
docs/manual/CoreTasks/fixcrlf.html View File

@@ -211,6 +211,11 @@ supports all attributes of <code>&lt;fileset&gt;</code>
</td> </td>
<td valign="top" align="center">No</td> <td valign="top" align="center">No</td>
</tr> </tr>
<tr>
<td valign="top">encoding</td>
<td valign="top">The encoding of the files</td>
<td align="center">No - defaults to default JVM encoding</td>
</tr>
</table> </table>
<h3>Examples</h3> <h3>Examples</h3>
<pre> &lt;fixcrlf srcdir=&quot;${src}&quot; <pre> &lt;fixcrlf srcdir=&quot;${src}&quot;


+ 10
- 1
src/etc/testcases/taskdefs/fixcrlf/build.xml View File

@@ -5,7 +5,7 @@
</target> </target>


<target name="cleanup"> <target name="cleanup">
<delete dir="result" />
</target> </target>


<target name="test1" depends="init"> <target name="test1" depends="init">
@@ -97,4 +97,13 @@
/> />
</target> </target>


<target name="testEncoding" depends="init">
<fixcrlf srcdir="input" destdir="result"
includes="input.crlf.utf16"
javafiles="false"
cr="remove"
encoding="UTF16"
/>
</target>

</project> </project>

BIN
src/etc/testcases/taskdefs/fixcrlf/expected/input.lf.utf16 View File


BIN
src/etc/testcases/taskdefs/fixcrlf/input/input.crlf.utf16 View File


+ 41
- 8
src/main/org/apache/tools/ant/taskdefs/FixCRLF.java View File

@@ -60,12 +60,18 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;


import java.io.File;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedWriter; 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.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.Enumeration;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;


@@ -84,6 +90,7 @@ import java.util.NoSuchElementException;
* <li>eol * <li>eol
* <li>tab * <li>tab
* <li>eof * <li>eof
* <li>encoding
* </ul> * </ul>
* Of these arguments, only <b>sourcedir</b> is required. * Of these arguments, only <b>sourcedir</b> is required.
* <p> * <p>
@@ -150,6 +157,11 @@ public class FixCRLF extends MatchingTask {


private FileUtils fileUtils = FileUtils.newFileUtils(); private FileUtils fileUtils = FileUtils.newFileUtils();


/**
* Encoding to assume for the files
*/
private String encoding = null;

/** /**
* Defaults the properties based on the system type. * Defaults the properties based on the system type.
* <ul><li>Unix: eol="LF" tab="asis" eof="remove" * <ul><li>Unix: eol="LF" tab="asis" eof="remove"
@@ -316,6 +328,15 @@ public class FixCRLF extends MatchingTask {
ctrlz = ADD; 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. * Executes the task.
*/ */
@@ -346,7 +367,8 @@ public class FixCRLF extends MatchingTask {
(eol==ASIS ? "asis" : eol==CR ? "cr" : eol==LF ? "lf" : "crlf") + (eol==ASIS ? "asis" : eol==CR ? "cr" : eol==LF ? "lf" : "crlf") +
" tab=" + (tabs==TABS ? "add" : tabs==ASIS ? "asis" : "remove") + " tab=" + (tabs==TABS ? "add" : tabs==ASIS ? "asis" : "remove") +
" eof=" + (ctrlz==ADD ? "add" : ctrlz==ASIS ? "asis" : "remove") + " eof=" + (ctrlz==ADD ? "add" : ctrlz==ASIS ? "asis" : "remove") +
" tablength=" + tablength,
" tablength=" + tablength +
" encoding=" + (encoding == null ? "default" : encoding),
Project.MSG_VERBOSE); Project.MSG_VERBOSE);


DirectoryScanner ds = super.getDirectoryScanner(srcDir); 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 * Checks for the inequality of two files
*/ */
@@ -373,9 +405,9 @@ public class FixCRLF extends MatchingTask {
try { try {
reader1 = new BufferedReader reader1 = new BufferedReader
(new FileReader(file1), INBUFLEN);
(getReader(file1), INBUFLEN);
reader2 = new BufferedReader reader2 = new BufferedReader
(new FileReader(file2), INBUFLEN);
(getReader(file2), INBUFLEN);
while ((buflen = reader1.read(buf1, 0, INBUFLEN)) != -1 ) { while ((buflen = reader1.read(buf1, 0, INBUFLEN)) != -1 ) {
reader2.read(buf2, 0, INBUFLEN); reader2.read(buf2, 0, INBUFLEN);
// Compare the contents of the buffers // Compare the contents of the buffers
@@ -414,7 +446,8 @@ public class FixCRLF extends MatchingTask {
// Set up the output Writer // Set up the output Writer
try { try {
tmpFile = fileUtils.createTempFile("fixcrlf", "", destD); 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); outWriter = new BufferedWriter(writer);
} catch (IOException e) { } catch (IOException e) {
throw new BuildException(e); throw new BuildException(e);
@@ -820,7 +853,7 @@ public class FixCRLF extends MatchingTask {
{ {
try { try {
reader = new BufferedReader reader = new BufferedReader
(new FileReader(srcFile), INBUFLEN);
(getReader(srcFile), INBUFLEN);
nextLine(); nextLine();
} catch (IOException e) { } catch (IOException e) {
throw new BuildException(e); throw new BuildException(e);


+ 6
- 0
src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java View File

@@ -159,6 +159,12 @@ public class FixCrLfTest extends TaskdefsTest {
assertEquals(modTime, result.lastModified()); 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) public void assertEqualContent(File expect, File result)
throws AssertionFailedError, IOException { throws AssertionFailedError, IOException {
if (!result.exists()) { if (!result.exists()) {


Loading…
Cancel
Save