From 6e99aad0bf5e475f82ca850e63aa29d1584fca46 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 31 Jan 2003 12:15:18 +0000 Subject: [PATCH] Add encoding attribute to git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273939 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 +++ docs/manual/OptionalTasks/replaceregexp.html | 5 ++++ .../ant/taskdefs/optional/ReplaceRegExp.java | 27 ++++++++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index a46f4656e..e9de573ea 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -196,6 +196,10 @@ Other changes: You can put in it all the strings described by p4 help usage. Refer to the docs for more information. +* now has an optional encoding attribute to support + replacing in files that are in a different encoding than the + platform's default. + Changes from Ant 1.5.1Beta1 to 1.5.1 ==================================== diff --git a/docs/manual/OptionalTasks/replaceregexp.html b/docs/manual/OptionalTasks/replaceregexp.html index 2053aaf33..64ff60d28 100644 --- a/docs/manual/OptionalTasks/replaceregexp.html +++ b/docs/manual/OptionalTasks/replaceregexp.html @@ -81,6 +81,11 @@ We strongly recommend that you use Jakarta Oro. Defaults to false. No + + encoding + The encoding of the file. since Ant 1.6 + No - defaults to default JVM encoding +

Examples

  <replaceregexp file="${src}/build.properties"
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
index 3eb2836f1..703c95714 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
@@ -1,7 +1,7 @@
 /*
  * The Apache Software License, Version 1.1
  *
- * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
+ * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
  * reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -56,10 +56,13 @@ package org.apache.tools.ant.taskdefs.optional;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.FileWriter;
+import java.io.InputStreamReader;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.io.Reader;
 import java.util.Vector;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.DirectoryScanner;
@@ -153,6 +156,10 @@ public class ReplaceRegExp extends Task {
 
     private FileUtils fileUtils = FileUtils.newFileUtils();
 
+    /**
+     * Encoding to assume for the files
+     */
+    private String encoding = null;
 
     /** Default Constructor  */
     public ReplaceRegExp() {
@@ -240,6 +247,16 @@ public class ReplaceRegExp extends Task {
     }
 
 
+    /**
+     * Specifies the encoding Ant expects the files to be in -
+     * defaults to the platforms default encoding.
+     *
+     * @since Ant 1.6
+     */
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
+    }
+
     /**
      * list files to apply the replacement to
      */
@@ -300,11 +317,15 @@ public class ReplaceRegExp extends Task {
         File parentDir = fileUtils.getParentFile(f);
         File temp = fileUtils.createTempFile("replace", ".txt", parentDir);
 
-        FileReader r = null;
+        Reader r = null;
         FileWriter w = null;
 
         try {
-            r = new FileReader(f);
+            if (encoding == null) {
+                r = new FileReader(f);
+            } else {
+                r = new InputStreamReader(new FileInputStream(f), encoding);
+            }
             w = new FileWriter(temp);
 
             BufferedReader br = new BufferedReader(r);