Browse Source

Add encoding to <replace> task so that replace can work on double byte

characters


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269721 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 24 years ago
parent
commit
304895ce62
2 changed files with 26 additions and 4 deletions
  1. +5
    -0
      docs/manual/CoreTasks/replace.html
  2. +21
    -4
      src/main/org/apache/tools/ant/taskdefs/Replace.java

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

@@ -30,6 +30,11 @@ must use a nested <code>&lt;replacetoken&gt;</code> element.</p>
<td valign="top">The base directory to use when replacing a token in
multiple files.</td>
</tr>
<tr>
<td valign="top">encoding</td>
<td valign="top">The encoding of the files upon which replace operates.</td>
<td align="center">No - defaults to default JVM encoding</td>
</tr>
<tr>
<td valign="top">token</td>
<td valign="top">the token which must be replaced.</td>


+ 21
- 4
src/main/org/apache/tools/ant/taskdefs/Replace.java View File

@@ -83,6 +83,9 @@ public class Replace extends MatchingTask {
private int replaceCount;
private boolean summary = false;
/** The encoding used to read and write files - if null, uses default */
private String encoding = null;
//Inner class
public class NestedString {

@@ -291,8 +294,13 @@ public class Replace extends MatchingTask {
}

try {
BufferedReader br = new BufferedReader(new FileReader(src));
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
Reader fileReader = encoding == null ? new FileReader(src)
: new InputStreamReader(new FileInputStream(src), encoding);
Writer fileWriter = encoding == null ? new FileWriter(src)
: new OutputStreamWriter(new FileOutputStream(src), encoding);
BufferedReader br = new BufferedReader(fileReader);
BufferedWriter bw = new BufferedWriter(fileWriter);

// read the entire file into a StringBuffer
// size of work buffer may be bigger than needed
@@ -354,8 +362,8 @@ public class Replace extends MatchingTask {
temp.delete();
}
} catch (IOException ioe) {
ioe.printStackTrace();
throw new BuildException(ioe, location);
throw new BuildException("IOException in " + src + " - " +
ioe.getClass().getName() + ":" + ioe.getMessage(), ioe, location);
}
}

@@ -412,6 +420,15 @@ public class Replace extends MatchingTask {
createReplaceValue().addText(value);
}

/**
* Set the file encoding to use on the files read and written by replace
*
* @param encoding the encoding to use on the files
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
}
/**
* Nested <replacetoken> element.
*/


Loading…
Cancel
Save