Patch from N.Hira from bug #45252 - Improvement for HWPF Range.replaceText()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@672570 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-06-28 18:58:23 +00:00
parent 24053679a8
commit 6399ea1768
4 changed files with 70 additions and 9 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! --> <!-- Don't forget to update status.xml too! -->
<release version="3.1.1-alpha1" date="2008-??-??"> <release version="3.1.1-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">45252 - Improvement for HWPF Range.replaceText()</action>
<action dev="POI-DEVELOPERS" type="fix">45001 - Further fix for HWPF Range.delete() and unicode characters</action> <action dev="POI-DEVELOPERS" type="fix">45001 - Further fix for HWPF Range.delete() and unicode characters</action>
<action dev="POI-DEVELOPERS" type="add">45175 - Support for variable length operands in org.apache.poi.hwpf.sprm.SprmOperation</action> <action dev="POI-DEVELOPERS" type="add">45175 - Support for variable length operands in org.apache.poi.hwpf.sprm.SprmOperation</action>
<action dev="POI-DEVELOPERS" type="fix">Avoid spurious missing lines with the MissingRecordAware event code, and odd files that contain RowRecords in the middle of the cell Records.</action> <action dev="POI-DEVELOPERS" type="fix">Avoid spurious missing lines with the MissingRecordAware event code, and odd files that contain RowRecords in the middle of the cell Records.</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! --> <!-- Don't forget to update changes.xml too! -->
<changes> <changes>
<release version="3.1.1-alpha1" date="2008-??-??"> <release version="3.1.1-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">45252 - Improvement for HWPF Range.replaceText()</action>
<action dev="POI-DEVELOPERS" type="fix">45001 - Further fix for HWPF Range.delete() and unicode characters</action> <action dev="POI-DEVELOPERS" type="fix">45001 - Further fix for HWPF Range.delete() and unicode characters</action>
<action dev="POI-DEVELOPERS" type="add">45175 - Support for variable length operands in org.apache.poi.hwpf.sprm.SprmOperation</action> <action dev="POI-DEVELOPERS" type="add">45175 - Support for variable length operands in org.apache.poi.hwpf.sprm.SprmOperation</action>
<action dev="POI-DEVELOPERS" type="fix">Avoid spurious missing lines with the MissingRecordAware event code, and odd files that contain RowRecords in the middle of the cell Records.</action> <action dev="POI-DEVELOPERS" type="fix">Avoid spurious missing lines with the MissingRecordAware event code, and odd files that contain RowRecords in the middle of the cell Records.</action>

View File

@ -333,7 +333,7 @@ public class Range
_doc.getCharacterTable().adjustForInsert(_charStart, adjustedLength); _doc.getCharacterTable().adjustForInsert(_charStart, adjustedLength);
_doc.getParagraphTable().adjustForInsert(_parStart, adjustedLength); _doc.getParagraphTable().adjustForInsert(_parStart, adjustedLength);
_doc.getSectionTable().adjustForInsert(_sectionStart, adjustedLength); _doc.getSectionTable().adjustForInsert(_sectionStart, adjustedLength);
adjustForInsert(text.length()); adjustForInsert(adjustedLength);
// update the FIB.CCPText field // update the FIB.CCPText field
adjustFIB(text.length()); adjustFIB(text.length());
@ -656,8 +656,15 @@ public class Range
); );
} }
// 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); subRange.insertBefore(pValue);
if (subRange.getEndOffset() != previousEndOffset)
_end += (subRange.getEndOffset() - previousEndOffset);
// re-create the sub-range so we can delete it // re-create the sub-range so we can delete it
subRange = new Range( subRange = new Range(
(absPlaceHolderIndex + pValue.length()), (absPlaceHolderIndex + pValue.length()),
@ -671,9 +678,30 @@ public class Range
(pValue.length() * 2)), getDocument() (pValue.length() * 2)), getDocument()
); );
// deletes are automagically propagated
subRange.delete(); 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;
}
}
/** /**
* Gets the character run at index. The index is relative to this range. * Gets the character run at index. The index is relative to this range.
* *
@ -915,7 +943,7 @@ public class Range
/** /**
* adjust this range after an insert happens. * adjust this range after an insert happens.
* @param length the length to adjust for * @param length the length to adjust for (expected to be a count of code-points, not necessarily chars)
*/ */
private void adjustForInsert(int length) private void adjustForInsert(int length)
{ {

View File

@ -39,8 +39,9 @@ public class TestRangeReplacement extends TestCase {
"It is used to confirm that text replacement works even if Unicode characters (such as \u201c\u2014\u201d (U+2014), \u201c\u2e8e\u201d (U+2E8E), or \u201c\u2714\u201d (U+2714)) are present. Everybody should be thankful to the ${organization} and all the POI contributors for their assistance in this matter.\r"; "It is used to confirm that text replacement works even if Unicode characters (such as \u201c\u2014\u201d (U+2014), \u201c\u2e8e\u201d (U+2E8E), or \u201c\u2714\u201d (U+2714)) are present. Everybody should be thankful to the ${organization} and all the POI contributors for their assistance in this matter.\r";
private String searchText = "${organization}"; private String searchText = "${organization}";
private String replacementText = "Apache Software Foundation"; private String replacementText = "Apache Software Foundation";
private String expectedText = private String expectedText2 =
"It is used to confirm that text replacement works even if Unicode characters (such as \u201c\u2014\u201d (U+2014), \u201c\u2e8e\u201d (U+2E8E), or \u201c\u2714\u201d (U+2714)) are present. Everybody should be thankful to the Apache Software Foundation and all the POI contributors for their assistance in this matter.\r"; "It is used to confirm that text replacement works even if Unicode characters (such as \u201c\u2014\u201d (U+2014), \u201c\u2e8e\u201d (U+2E8E), or \u201c\u2714\u201d (U+2714)) are present. Everybody should be thankful to the Apache Software Foundation and all the POI contributors for their assistance in this matter.\r";
private String expectedText3 = "Thank you, Apache Software Foundation!\r";
private String illustrativeDocFile; private String illustrativeDocFile;
@ -84,7 +85,7 @@ public class TestRangeReplacement extends TestCase {
/** /**
* Test that we can replace text in our Range with Unicode text. * Test that we can replace text in our Range with Unicode text.
*/ */
public void testRangeReplacement() throws Exception { public void testRangeReplacementOne() throws Exception {
HWPFDocument daDoc = new HWPFDocument(new FileInputStream(illustrativeDocFile)); HWPFDocument daDoc = new HWPFDocument(new FileInputStream(illustrativeDocFile));
@ -104,16 +105,46 @@ public class TestRangeReplacement extends TestCase {
para.replaceText(searchText, replacementText, offset); para.replaceText(searchText, replacementText, offset);
// we need to let the model re-calculate the Range before we evaluate it
range = daDoc.getRange();
assertEquals(1, range.numSections()); assertEquals(1, range.numSections());
section = range.getSection(0); section = range.getSection(0);
assertEquals(5, section.numParagraphs()); assertEquals(4, section.numParagraphs());
para = section.getParagraph(2); para = section.getParagraph(2);
text = para.text(); text = para.text();
assertEquals(expectedText, text); assertEquals(expectedText2, text);
}
/**
* Test that we can replace text in our Range with Unicode text.
*/
public void testRangeReplacementAll() throws Exception {
HWPFDocument daDoc = new HWPFDocument(new FileInputStream(illustrativeDocFile));
Range range = daDoc.getRange();
assertEquals(1, range.numSections());
Section section = range.getSection(0);
assertEquals(5, section.numParagraphs());
Paragraph para = section.getParagraph(2);
String text = para.text();
assertEquals(originalText, text);
range.replaceText(searchText, replacementText);
assertEquals(1, range.numSections());
section = range.getSection(0);
assertEquals(5, section.numParagraphs());
para = section.getParagraph(2);
text = para.text();
assertEquals(expectedText2, text);
para = section.getParagraph(3);
text = para.text();
assertEquals(expectedText3, text);
} }
} }