ASF Bugzilla – Attachment 22157 Details for
Bug 45252
Improvement for Range.replaceText()
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch to Range.replaceText() to allow replace-all and updated unit test
rangeReplacement.patch (text/plain), 4.91 KB, created by
N. Hira
on 2008-06-22 23:13:42 UTC
(
hide
)
Description:
Patch to Range.replaceText() to allow replace-all and updated unit test
Filename:
MIME Type:
Creator:
N. Hira
Created:
2008-06-22 23:13:42 UTC
Size:
4.91 KB
patch
obsolete
>--- ./src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java 2008-06-23 01:05:17.000000000 -0500 >+++ /Users/nhira/Desktop/Range.java 2008-06-23 00:52:10.000000000 -0500 >@@ -333,7 +335,7 @@ > _doc.getCharacterTable().adjustForInsert(_charStart, adjustedLength); > _doc.getParagraphTable().adjustForInsert(_parStart, adjustedLength); > _doc.getSectionTable().adjustForInsert(_sectionStart, adjustedLength); >- adjustForInsert(text.length()); >+ adjustForInsert(adjustedLength); > > // update the FIB.CCPText field > adjustFIB(text.length()); >@@ -656,8 +658,15 @@ > ); > } > >+ // 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) >+ _end += (subRange.getEndOffset() - previousEndOffset); >+ > // re-create the sub-range so we can delete it > subRange = new Range( > (absPlaceHolderIndex + pValue.length()), >@@ -671,10 +680,31 @@ > (pValue.length() * 2)), 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; >+ } >+ } >+ >+ /** > * Gets the character run at index. The index is relative to this range. > * > * @param index The index of the character run to get. >@@ -915,7 +945,7 @@ > > /** > * 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) > { >--- ./src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java 2008-06-23 01:05:18.000000000 -0500 >+++ /Users/nhira/Desktop/TestRangeReplacement.java 2008-06-23 00:55:14.000000000 -0500 >@@ -39,8 +39,9 @@ > "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 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"; >+ private String expectedText3 = "Thank you, Apache Software Foundation!\r"; > > private String illustrativeDocFile; > >@@ -84,7 +85,7 @@ > /** > * 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)); > >@@ -104,16 +105,46 @@ > > 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()); > section = range.getSection(0); > >+ assertEquals(4, section.numParagraphs()); >+ para = section.getParagraph(2); >+ >+ text = para.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(expectedText, text); >+ assertEquals(expectedText3, text); > } > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 45252
: 22157