Browse Source

Make <replace> a matching task

This is based on the concept in the patch submitted by Charles Tewksbury
although the implementation details are a little different.

Submitted by:	Charles Tewksbury <tewks@adhesive.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267699 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 25 years ago
parent
commit
003b013e23
1 changed files with 51 additions and 14 deletions
  1. +51
    -14
      src/main/org/apache/tools/ant/taskdefs/Replace.java

+ 51
- 14
src/main/org/apache/tools/ant/taskdefs/Replace.java View File

@@ -60,36 +60,65 @@ import java.util.*;


/** /**
* Replaces all the occurrences of the given string token with the given * Replaces all the occurrences of the given string token with the given
* string value of the indicated file.
* string value of the indicated files.
* *
* @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a> * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
*/ */
public class Replace extends Task {
public class Replace extends MatchingTask {
private File src = null; private File src = null;
private File dest = null;
private String token = null; private String token = null;
private String value = ""; private String value = "";

private File dir = null;
/** /**
* Do the execution. * Do the execution.
*/ */
public void execute() throws BuildException { public void execute() throws BuildException {
project.log("Replacing " + token + " --> " + value);
if (token == null) {
throw new BuildException("replace token must not be null");
}

if (src == null && dir == null) {
throw new BuildException("Either the file or the dir attribute must be specified");
}
if (src == null || token == null ) {
project.log("File and token must not be null");
return;
project.log("Replacing " + token + " --> " + value);

if (src != null) {
processFile(src);
} }
if (dest == null) {
throw new BuildException("Error creating temp file.");
if (dir != null) {
DirectoryScanner ds = super.getDirectoryScanner(dir);
String[] srcs = ds.getIncludedFiles();

for(int i=0; i<srcs.length; i++) {
File file = new File(dir,srcs[i]);
processFile(file);
}
} }
}

/**
* Perform the replacement on the given file.
*
* The replacement is performed on a temporary file which then replaces the original file.
*
* @param src the source file
*/
private void processFile(File src) throws BuildException {
File temp = new File(src.getPath() + ".temp");

if (temp.exists()) {
throw new BuildException("Replace: temporary file " + temp.getPath() + " already exists");
}

try { try {
BufferedReader br = new BufferedReader(new FileReader(src)); BufferedReader br = new BufferedReader(new FileReader(src));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest));
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));


String line; String line;
@@ -105,18 +134,26 @@ public class Replace extends Task {
br.close(); br.close();
src.delete(); src.delete();
dest.renameTo(src);
temp.renameTo(src);
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
throw new BuildException(ioe);
} }
} }


/** /**
* Set the source file. * Set the source file.
*/ */
public void setFile(String file) { public void setFile(String file) {
this.src = project.resolveFile(file); this.src = project.resolveFile(file);
this.dest = project.resolveFile(file + ".temp");
}

/**
* Set the source files path when using matching tasks.
*/
public void setDir(String dirName) {
dir = project.resolveFile(dirName);
} }


/** /**


Loading…
Cancel
Save