ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gattaz, Olivier" <ogat...@adonix.fr>
Subject ReplaceRegExp bugs
Date Fri, 10 May 2002 12:18:34 GMT

Bonjour "developers",

I'am a fan of ANT and I use the optional task ReplaceRegExp (v1.5) to set
buildnum constants in my sources.

I discover two bugs in the "execute()" method of the class "ReplaceRegExp".
 1) the variable "files[j]" contains only the file name, to use the base
directory path of the fileset we must build a full path name
 2) the variable "file" is not initialised in the context "!f.exists()"

See behind the rectified and documented source code of the method.

I can't access to the ANT's CVS base, our FireWall blocks my WinCvs client
....

You do a very good job.

Best regard.

A plus, Olivier.

//--------------------------------------------------------------------
/**
* Apply the Regexp on one file or on one or more fileset(s)
*/
public void execute() throws BuildException{
	//memo start tickcount
	long wStartTime = System.currentTimeMillis();
	int wNbAllFiles = 0;
	this.nbReplace = 0;//raz nb replace(s) counter
	if (regex == null) {
	  throw new BuildException("No expression to match.");
	}
	if (subs == null) {
	  throw new BuildException("Nothing to replace expression with.");
	}
	if (file != null && filesets.size() > 0) {
	  throw new BuildException("You cannot supply the 'file' attribute
and filesets at the same time.");
	}
	int options = 0;
	if (flags.indexOf('g') != -1) {
	  options |= Regexp.REPLACE_ALL;
	}
	if (flags.indexOf('i') != -1) {
	  options |= Regexp.MATCH_CASE_INSENSITIVE;
	}
	if (flags.indexOf('m') != -1) {
	  options |= Regexp.MATCH_MULTILINE;
	}
	if (flags.indexOf('s') != -1) {
	  options |= Regexp.MATCH_SINGLELINE;
	}
	
	//if the regexp must be applied to one file
	if (file != null) {
	  if (!file.exists()){
	    log("The following file is missing: '" + file.getAbsolutePath()
+"'",Project.MSG_ERR);
	  }
	  else{
	    try{
	      doReplace(file, options);
	      wNbAllFiles++;//add one processed file
	    }
	    catch (IOException e){
	      log("An error occurred processing file: '" +
	file.getAbsolutePath() + "': " + e.toString(),Project.MSG_ERR);
	    }
	  }
	}
	//else, apply to one or more fileset(s)
	else{
	  //for each fileset(s) if there is one or more
	  int sz = filesets.size();
	  if (verbose) log("Nb Fileset(s)='" + sz+ "'",Project.MSG_WARN);
	  if (sz>0){
	    //loop on the filset(s)
	    for(int i=0;i<sz;i++){
	      FileSet fs = (FileSet)(filesets.elementAt(i));
	      DirectoryScanner ds = fs.getDirectoryScanner(getProject());
	      //get the base directory path of the fileset
	      String wBaseDirPath = ds.getBasedir().getAbsolutePath();
	      if (verbose) log("Basedir='" + wBaseDirPath +
"'",Project.MSG_WARN);
	      //for each file(s) if there is one or more
	      String files[] = ds.getIncludedFiles();
	      int wNbFiles = files.length;
	      if (verbose) log("Nb File(s)='" + wNbFiles+
"'",Project.MSG_WARN);
	      StringBuffer wFullFilePath;
	      if (wNbFiles>0){
	        //declaration before the loop on the file(s) of a fileset
	        File f;
	        for (int j=0;j<wNbFiles;j++){
	          if (verbose) log("FilePath='" + files[j]+
"'",Project.MSG_WARN);	
	          //Error: files[j] contains only the file name ! to use the
base directory path of the fileset we must build a full path name
	          //File f = new File(files[j]);
	          //build the full file path : necessary if the current
directory is not the basedir of the ant project !
	          wFullFilePath = new
StringBuffer(128).append(wBaseDirPath);
	          if (wBaseDirPath.charAt(wBaseDirPath.length()-1)!=
File.separatorChar) wFullFilePath.append(File.separator);
	          wFullFilePath.append(files[j]);
	          //build the File instance
	          f = new File(wFullFilePath.toString());
	          //replace if the file exists
	          if (f.exists()){
	            try{
	              //apply the regexp
	              doReplace(f, options);
	              wNbAllFiles++;//add one processed file
	            }
	            catch (Exception e){
	              log("An error occurred processing file: '" +
f.getAbsolutePath() + "': " + e.toString(),Project.MSG_ERR);
	            }
	          }
	          else {
	            //Error: file is not initialised in this context
	            //log("The following file is missing: '" +
file.getAbsolutePath() + "'",Project.MSG_ERR);
	            log("The following file does not exist: '" +
f.getAbsolutePath() + "'",Project.MSG_ERR);
	          }
	        }//for file(s)
	      }
	    }//for fileset(s)
	  }
	}
	//log at the end of execute
	StringBuffer wMess = new StringBuffer(128);
	wMess.append(" Number of scanned file(s)
	'").append(wNbAllFiles).append('\'');
	wMess.append(" Number of replace(s)
	'").append(this.nbReplace).append('\'');
	wMess.append(" Duration
	'").append(System.currentTimeMillis()-wStartTime).append('\'');
	wMess.append('.');
	log(wMess.toString(),Project.MSG_WARN);
}










--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message