https://issues.apache.org/bugzilla/show_bug.cgi?id=51604

sanmoy <[email protected]> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |

--- Comment #14 from sanmoy <[email protected]> 2011-09-04 16:49:18 UTC ---
/**
* Replace (all instances of) a piece of text with another...
*
* @param pPlaceHolder
*            The text to be replaced (e.g., "${organization}")
* @param pValue
*            The replacement text (e.g., "Apache Software Foundation")
*/
public void replaceText(String pPlaceHolder, String pValue) 

The replaceText API will not work if the String pValue contains the String
pPlaceHolder

For example if pPlaceHolder="abcd" and pValue="abcd" or "abcdef" or "12abcdef"
this code will go to a infinite loop

Modify the original testcode charRun.replaceText(text, text); that is, try to
replace the original value with itself, it will not work, it will fall into a
infinite loop. For your convenience, I am copying the original code again.
Please test it with the attached files

FileInputStream fileInputStream = new FileInputStream(new File("C:\\in.doc")); 
FileOutputStream fileOutputStream = new FileOutputStream(new
File("C:\\out.doc")); 
HWPFDocument hwpfDocument = new HWPFDocument(fileInputStream); 
Range range = hwpfDocument.getRange(); 
int numParagraph = range.numParagraphs(); 
for (int i = 0; i < numParagraph; i++) 
{ 
Paragraph paragraph = range.getParagraph(i); 
int numCharRuns = paragraph.numCharacterRuns(); 
for (int j = 0; j < numCharRuns; j++) 
{ 
CharacterRun charRun = paragraph.getCharacterRun(j); 
String text = charRun.text(); 
charRun.replaceText(text, text); 
} 
} 
hwpfDocument.write(fileOutputStream); 
fileOutputStream.close();

I have tested with the latest nightly buid 3.8-beta5-20110904

I have debugged the poi code and found the problem in this following logic 
String text = text();
int offset = text.indexOf(pPlaceHolder);
text is returning the replaced value and if the replaced value contains the
original String, offset will always be >=0 and it will keep on increasing

public void replaceText(String pPlaceHolder, String pValue)  {
    boolean keepLooking = true;
    while (keepLooking){

    String text = text();
    int offset = text.indexOf(pPlaceHolder);
    if (offset >= 0)
        replaceText(pPlaceHolder, pValue, offset);
    else
        keepLooking = false;
        }
    }

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to