ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From co...@locus.apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs Replace.java
Date Sun, 25 Jun 2000 11:45:20 GMT
conor       00/06/25 04:45:19

  Modified:    src/main/org/apache/tools/ant/taskdefs Replace.java
  Log:
  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>
  
  Revision  Changes    Path
  1.2       +51 -14    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Replace.java
  
  Index: Replace.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Replace.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Replace.java	2000/01/13 10:41:41	1.1
  +++ Replace.java	2000/06/25 11:45:19	1.2
  @@ -60,36 +60,65 @@
   
   /**
    * 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>
    */
  -public class Replace extends Task {
  +public class Replace extends MatchingTask {
       
       private File src = null;
  -    private File dest = null;
       private String token = null;
       private String value = "";
  +
  +    private File dir = null;
       
       /**
        * Do the execution.
        */
       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 {
               BufferedReader br = new BufferedReader(new FileReader(src));
  -            BufferedWriter bw = new BufferedWriter(new FileWriter(dest));
  +            BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
   
               String line;
               
  @@ -105,18 +134,26 @@
               br.close();
               
               src.delete();
  -            dest.renameTo(src);
  +            temp.renameTo(src);
           } catch (IOException ioe) {
               ioe.printStackTrace();
  +            throw new BuildException(ioe);
           }       
       }
  -    
  +
  +
       /**
        * Set the source file.
        */
       public void setFile(String 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);
       }
   
       /**
  
  
  

Mime
View raw message