/**
* Replace (one instance 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")
* @param pOffset
* The offset or index where the text to be replaced begins
* (relative to/within this <code>Range</code>)
*/
public void replaceText(String pPlaceHolder, String pValue, int pOffset) {
int absPlaceHolderIndex = getStartOffset() + pOffset;
Range subRange = new Range(absPlaceHolderIndex, (absPlaceHolderIndex + pPlaceHolder
.length()), getDocument());
// this Range isn't a proper parent of the subRange() so we'll have to
// keep
// track of an updated endOffset on our own
int previousEndOffset = subRange.getEndOffset();
subRange.insertBefore(pValue);
if (subRange.getEndOffset() != previousEndOffset) {
adjustForInsert(subRange.getEndOffset() - previousEndOffset);
}
// re-create the sub-range so we can delete it
subRange = new Range((absPlaceHolderIndex + pValue.length()), (absPlaceHolderIndex
+ pPlaceHolder.length() + pValue.length()), getDocument());
// deletes are automagically propagated
subRange.delete();
}
/**
* 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) {
boolean keepLooking = true;
while (keepLooking) {
String text = text();
int offset = text.indexOf(pPlaceHolder);
if (offset >= 0)
replaceText(pPlaceHolder, pValue, offset);
else
keepLooking = false;
}
}