From ca4d710776c766f9765d28fd7b7ccc3b5a88bd0e Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 4 Jun 2010 17:19:31 +0000 Subject: [PATCH] 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 --- src/documentation/content/xdocs/status.xml | 1 + .../org/apache/poi/hwpf/usermodel/Range.java | 5 ++- .../poi/hwpf/usermodel/TestProblems.java | 37 ++++++++++++++++++ .../hwpf/usermodel/TestRangeReplacement.java | 8 +++- test-data/document/Bug45269.doc | Bin 0 -> 64512 bytes 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 test-data/document/Bug45269.doc 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 0000000000000000000000000000000000000000..aea4ff93d61f671be442041017164c65bee2e1ca GIT binary patch literal 64512 zcmeI5U2Igx702hUz0R8OVH@fYpkaY65IzhxA2AJWZQ~H*K+DN;q#m%cShm9`H}(^gHQsC}TSs)~L{3F4u&#qR&i?8o&U zJ6`+D78}kD|9fZ7opWa9H*@FC-5~LscdM^n`q7db{ zHbeDL1GEL&3T=b7Lk~fX&<^Oc(8JJ9=yTBLp)Wv9P&3p5?SfjNN1)x%qfi^P2Wp2N zgZ4uEpvR#Os1teu+7BIo4nj{tPeF&E!_XI@r=cUzQRo=-4D>AY9F&BPLtW4bs2l2m zo`-s&KIjGLMW`PdfCixybP_rRrJ*5c7@~1JgX@=|v(QV>If(3E$y_r-ZZG>0DFx3s zdOnD2+Ipa1kw-0NOgLUKZsGN!nDUdA(facQCSyhS!~BCxo^0bi|CP9ZJD-1Q*PZbE zQ$OhWr|V2Q0<@?M159nIsEnY6#=L3IW;dc!Io-Gjq+ zPV75Cw$@aaeG^c+4jFsDuF>4s@dhnUlN^|TuF^@$O?0~kZ<9ILg&e^fDH{muXw}D~ z5xbsNV3kfHl{SC2Yi2b9l_g9vVd@iRT_R{s(7U5$o#|-DVh-(UUu*{2YRiY9$JzqZ z*6$Vog1Vx7Gn%lUQL-;GD*J z2rGUsQhiwM=}sEw@X=yMmR}$JSQM+x#s~s>k1fL+=@8m6y;il`NYw%O;0-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