diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 20794454f7..45793d7961 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 45269 - improve replaceText on HWPF ranges 47815 - correct documentation on what happens when you request a String from a non-string Formula cell 49386 - avoid NPE when extracting OOXML file properties which are dates 49377 - only call DecimalFormat.setRoundingMode on Java 1.6 - it's needed to match excel's rendering of numbers diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java index ff6f2162e2..1cac46d098 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java @@ -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 diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java index dbb02d4a0d..94b66f8945 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java @@ -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)); + } + } + } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java index 8303224bd9..9798a756d6 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java @@ -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); } diff --git a/test-data/document/Bug45269.doc b/test-data/document/Bug45269.doc new file mode 100644 index 0000000000..aea4ff93d6 Binary files /dev/null and b/test-data/document/Bug45269.doc differ