mirror of https://github.com/apache/poi.git
Apply with tweaks the patch from bug #45269 - improve replaceText on HWPF ranges
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@951498 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
439bd8de3e
commit
ca4d710776
|
@ -34,6 +34,7 @@
|
|||
|
||||
<changes>
|
||||
<release version="3.7-SNAPSHOT" date="2010-??-??">
|
||||
<action dev="POI-DEVELOPERS" type="fix">45269 - improve replaceText on HWPF ranges</action>
|
||||
<action dev="POI-DEVELOPERS" type="fix">47815 - correct documentation on what happens when you request a String from a non-string Formula cell</action>
|
||||
<action dev="POI-DEVELOPERS" type="fix">49386 - avoid NPE when extracting OOXML file properties which are dates</action>
|
||||
<action dev="POI-DEVELOPERS" type="fix">49377 - only call DecimalFormat.setRoundingMode on Java 1.6 - it's needed to match excel's rendering of numbers</action>
|
||||
|
|
|
@ -745,8 +745,9 @@ public class Range { // TODO -instantiable superclass
|
|||
|
||||
subRange.insertBefore(pValue);
|
||||
|
||||
if (subRange.getEndOffset() != previousEndOffset)
|
||||
_end += (subRange.getEndOffset() - previousEndOffset);
|
||||
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
|
||||
|
|
|
@ -161,4 +161,41 @@ public final class TestProblems extends HWPFTestCase {
|
|||
HWPFDocument doc2 = writeOutAndRead(doc);
|
||||
assertEquals("Nick Burch", doc2.getSummaryInformation().getAuthor());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for reading paragraphs from Range after replacing some
|
||||
* text in this Range.
|
||||
* Bug #45269
|
||||
*/
|
||||
public void testReadParagraphsAfterReplaceText()throws Exception{
|
||||
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug45269.doc");
|
||||
Range range = doc.getRange();
|
||||
|
||||
String toFind = "campo1";
|
||||
String longer = " foi porraaaaa ";
|
||||
String shorter = " foi ";
|
||||
|
||||
//check replace with longer text
|
||||
for (int x = 0; x < range.numParagraphs(); x++) {
|
||||
Paragraph para = range.getParagraph(x);
|
||||
int offset = para.text().indexOf(toFind);
|
||||
if (offset >= 0) {
|
||||
para.replaceText(toFind, longer, offset);
|
||||
assertEquals(offset, para.text().indexOf(longer));
|
||||
}
|
||||
}
|
||||
|
||||
doc = HWPFTestDataSamples.openSampleFile("Bug45269.doc");
|
||||
range = doc.getRange();
|
||||
|
||||
//check replace with shorter text
|
||||
for (int x = 0; x < range.numParagraphs(); x++) {
|
||||
Paragraph para = range.getParagraph(x);
|
||||
int offset = para.text().indexOf(toFind);
|
||||
if (offset >= 0) {
|
||||
para.replaceText(toFind, shorter, offset);
|
||||
assertEquals(offset, para.text().indexOf(shorter));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,12 +85,16 @@ public final class TestRangeReplacement extends TestCase {
|
|||
|
||||
HWPFDocument daDoc = HWPFTestDataSamples.openSampleFile(illustrativeDocFile);
|
||||
|
||||
// Has one section
|
||||
Range range = daDoc.getRange();
|
||||
assertEquals(1, range.numSections());
|
||||
|
||||
// The first section has 5 paragraphs
|
||||
Section section = range.getSection(0);
|
||||
assertEquals(5, section.numParagraphs());
|
||||
|
||||
|
||||
// Change some text
|
||||
Paragraph para = section.getParagraph(2);
|
||||
|
||||
String text = para.text();
|
||||
|
@ -101,12 +105,14 @@ public final class TestRangeReplacement extends TestCase {
|
|||
|
||||
para.replaceText(searchText, replacementText, offset);
|
||||
|
||||
// Ensure we still have one section, 5 paragraphs
|
||||
assertEquals(1, range.numSections());
|
||||
section = range.getSection(0);
|
||||
|
||||
assertEquals(4, section.numParagraphs());
|
||||
assertEquals(5, section.numParagraphs());
|
||||
para = section.getParagraph(2);
|
||||
|
||||
// Ensure the text is what we should now have
|
||||
text = para.text();
|
||||
assertEquals(expectedText2, text);
|
||||
}
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue