Bug 17372 - Enhancement of replace Task
Summary: Enhancement of replace Task
Status: NEW
Alias: None
Product: Ant
Classification: Unclassified
Component: Core tasks (show other bugs)
Version: 1.5.1
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---
Assignee: Ant Notifications List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-02-25 14:04 UTC by Vidhya
Modified: 2008-11-24 03:57 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vidhya 2003-02-25 14:04:25 UTC
Hi,

Herewith I am submitting the enhanced code of  Replace task .The existing
replace
task  has the functionality to replace all occurrences of the string specified
in
the 'token' attribute .
 The enhanced code adds the following functionality's. 
 1.'firstoccurence' can be true/false ,default value is false. Only the first
occurrence of the string gets replaced . 
 2. 'lastoccurence' can be true /false  ,default value is false. Only the
lastoccurence of the string gets replaced . 
3.'nthoccurence' attribute takes an integer  value  If the int value is 4 , the
fourth occurrence of the  string gets replaced 
4. 'fromline ' and ' toline' attributes applies the replace task functionality
to a specified portion of the file. 
   e.g.; if fromline ='4' and toline='7' , the replacement occurs only between
line 4 and 7 . 

Please find the enhanced code given below.  

---------------------------
Code ...

private boolean firstoccurence = false;
private boolean lastoccurence = false;
private int nthoccurence=0;
private int fromline= 0;
private int toline = 0;


private void processFile(File src) throws BuildException {

	.
	.
	.
	//reads a part of the file specified by
	// fromline and toline into foundpart
	StringBuffer part1=new StringBuffer();
        StringBuffer foundpart=new StringBuffer();
        StringBuffer part2 = new StringBuffer();
        StringBuffer fullbuf= new StringBuffer();
        String tmpstr= new String();
        String nstring= new String();

        /* if from and to lines are specified*/
        if(fromline!=0 && toline!=0){
            if(i!=fromline){
            	for(i=1; i<fromline;i++){
            	    tmpstr=(String)br.readLine();
              	    part1.append(tmpstr).append("\n");
              	}
            }
            for(i=fromline;i<=toline;i++){
            	tmpstr=(String)br.readLine();
            	if(tmpstr==null)break;
            	foundpart.append(tmpstr).append("\n");
            }
           /*contains the string between the lines from and to*/

       	nstring = foundpart.toString();
       	while(true){
	    tmpstr=br.readLine();
            if(tmpstr==null) break;
            part2.append(tmpstr).append("\n");
        }

    }

	if(fromline!=0 && toline!=0)
            nstring=stringReplace(nstring,tok,val);
        else
            newString = stringReplace(newString, tok, val);

        if (replacefilters.size() > 0) {
            if (fromline !=0 && toline!=0)
               nstring=processReplacefilters(nstring,src.getPath());
            else
               newString = processReplacefilters(newString, src.getPath());
         }



         if(fromline!=0 && toline!=0){
   	     if(part1!=null) newString=part1.toString()+nstring.toString();
             else newString=nstring.toString();
             if(part2!=null) newString+=part2.toString();
         }

	.
	.
	.
}



public void setFirstOccurence(boolean firstoccurence){
    this.firstoccurence= firstoccurence;
}

public void setLastOccurence(boolean lastoccurence){
   this.lastoccurence=lastoccurence;
}

public void setNthoccurence(int nthoccurence){
    this.nthoccurence= nthoccurence;
}

public void setFromLine(int fromline){
    this.fromline = fromline;
}

public void setToLine(int toline){
    this.toline= toline;
}

private String stringReplace(String str, String str1, String str2) {

    StringBuffer ret = new StringBuffer();
    int start = 0;
    int found = str.indexOf(str1);
    int count=1;
    int lfound=str.lastIndexOf(str1);

    while (found >= 0) {
	appendString(ret,str.substring(start,found));
        //replaces the first occurence if firstoccurence is true and count
        //is equal to one.
        if(firstoccurence && count == 1) {
            appendString(ret,str2);
            //breaks the loop only if firstoccurence attribute is specified
            //as true and nthoccurence is zero or not specified and also
            //if lastoccurence is false
            if(firstoccurence && !lastoccurence && nthoccurence == 0) {
                start = found + str1.length();
                break;
            }
        }else if(nthoccurence == count) {
            //replaces the nth occurence if nth occurence and count
           //is equal.

	    appendString(ret,str2);
            if(!firstoccurence && !lastoccurence && nthoccurence != 0) {
	        start = found + str1.length();
                break;
            }

	}else if(!firstoccurence && !lastoccurence && nthoccurence == 0) {
            //if no attributes firstoccurence, lastoccurence &
            //nth occurence is specified then it replaces all token with the
            //specified value.
           appendString(ret,str2);
	}else {
            //breaks the loop only if lastoccurence attribute is specified
            //as true and nthoccurence is zero or not specified and also
            //if firstoccurence is false
            if(!firstoccurence && lastoccurence && nthoccurence == 0) {
                appendString(ret,str.substring(found,lfound));
                appendString(ret,str2);
                start = lfound + str1.length();
                break;
            }else if(lfound == found && lastoccurence) {
                //replaces the last occurence
                appendString(ret,str2);
            }else {
                appendString(ret,str1);
            }
        }

        // search again
        start = found + str1.length();
        found = str.indexOf(str1, start);
        ++replaceCount;
        count++;
    }

    if(str.length() > start) {
        appendString(ret,str.substring(start, str.length()));
    }

    return ret.toString();

}

//newly added
private void appendString(StringBuffer sb, String str) {
    if(str != null) {
        sb.append(str);
    }

}

-------------------------------------------------------------


Thanks and regards 
C.S.Vidhya 
SIP Technologies and Exports Ltd. Chennai, India