bodewig 01/11/13 05:46:04
Modified: docs/manual/CoreTasks fixcrlf.html
src/etc/testcases/taskdefs/fixcrlf build.xml
src/main/org/apache/tools/ant/taskdefs FixCRLF.java
src/testcases/org/apache/tools/ant/taskdefs FixCrLfTest.java
Added: src/etc/testcases/taskdefs/fixcrlf/expected input.lf.utf16
src/etc/testcases/taskdefs/fixcrlf/input input.crlf.utf16
Log:
Add encoding attribute to <fixcrlf>
PR: 961
Revision Changes Path
1.7 +5 -0 jakarta-ant/docs/manual/CoreTasks/fixcrlf.html
Index: fixcrlf.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/fixcrlf.html,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- fixcrlf.html 2001/10/30 10:05:34 1.6
+++ fixcrlf.html 2001/11/13 13:46:03 1.7
@@ -211,6 +211,11 @@
</td>
<td valign="top" align="center">No</td>
</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>
<h3>Examples</h3>
<pre> <fixcrlf srcdir="${src}"
1.2 +10 -1 jakarta-ant/src/etc/testcases/taskdefs/fixcrlf/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-ant/src/etc/testcases/taskdefs/fixcrlf/build.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- build.xml 2001/08/03 11:22:22 1.1
+++ build.xml 2001/11/13 13:46:03 1.2
@@ -5,7 +5,7 @@
</target>
<target name="cleanup">
- <delete dir="result" />
+
</target>
<target name="test1" depends="init">
@@ -94,6 +94,15 @@
tab="remove"
cr="remove"
eof="remove"
+ />
+ </target>
+
+ <target name="testEncoding" depends="init">
+ <fixcrlf srcdir="input" destdir="result"
+ includes="input.crlf.utf16"
+ javafiles="false"
+ cr="remove"
+ encoding="UTF16"
/>
</target>
1.1 jakarta-ant/src/etc/testcases/taskdefs/fixcrlf/expected/input.lf.utf16
<<Binary file>>
1.1 jakarta-ant/src/etc/testcases/taskdefs/fixcrlf/input/input.crlf.utf16
<<Binary file>>
1.21 +42 -9 jakarta-ant/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
Index: FixCRLF.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- FixCRLF.java 2001/11/02 16:36:49 1.20
+++ FixCRLF.java 2001/11/13 13:46:04 1.21
@@ -60,12 +60,18 @@
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 @@
* <li>eol
* <li>tab
* <li>eof
+ * <li>encoding
* </ul>
* Of these arguments, only <b>sourcedir</b> is required.
* <p>
@@ -108,7 +115,7 @@
*
* @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
* @author <a href="mailto:pbwest@powerup.com.au">Peter B. West</a>
- * @version $Revision: 1.20 $ $Name: $
+ * @version $Revision: 1.21 $ $Name: $
*/
public class FixCRLF extends MatchingTask {
@@ -151,6 +158,11 @@
private FileUtils fileUtils = FileUtils.newFileUtils();
/**
+ * Encoding to assume for the files
+ */
+ private String encoding = null;
+
+ /**
* Defaults the properties based on the system type.
* <ul><li>Unix: eol="LF" tab="asis" eof="remove"
* <li>Mac: eol="CR" tab="asis" eof="remove"
@@ -316,7 +328,16 @@
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.
*/
public void execute() throws BuildException {
@@ -346,7 +367,8 @@
(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);
@@ -358,6 +380,16 @@
}
/**
+ * 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
*/
private boolean filesEqual(File file1, File file2) {
@@ -373,9 +405,9 @@
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 @@
// 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 @@
{
try {
reader = new BufferedReader
- (new FileReader(srcFile), INBUFLEN);
+ (getReader(srcFile), INBUFLEN);
nextLine();
} catch (IOException e) {
throw new BuildException(e);
1.3 +6 -0 jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java
Index: FixCrLfTest.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FixCrLfTest.java 2001/08/06 12:25:56 1.2
+++ FixCrLfTest.java 2001/11/13 13:46:04 1.3
@@ -159,6 +159,12 @@
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()) {
--
To unsubscribe, e-mail: <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>
|