]> source.dussan.org Git - poi.git/commitdiff
Apply with tweaks the patch from bug #45269 - improve replaceText on HWPF ranges
authorNick Burch <nick@apache.org>
Fri, 4 Jun 2010 17:19:31 +0000 (17:19 +0000)
committerNick Burch <nick@apache.org>
Fri, 4 Jun 2010 17:19:31 +0000 (17:19 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@951498 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java
test-data/document/Bug45269.doc [new file with mode: 0644]

index 20794454f7f78788b72acf8d40ff0dca4a7ea585..45793d79616f9025397ecbdf99273fdb411fa22e 100644 (file)
@@ -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>
index ff6f2162e2fa1d4311b225406645376c0bfb4c1e..1cac46d098a6b965713514dd16f8b75e7ae53c97 100644 (file)
@@ -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
index dbb02d4a0d6d38520220e55cfdb9be34986fc76d..94b66f89456d61f75e5a3b0f60a7dafd76a42d5e 100644 (file)
@@ -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));
+      }
+    }
+       }
 }
index 8303224bd921c5c754f47621f9763684b4e6a4b1..9798a756d60a3eb1024c69fead98d8d572df11c6 100644 (file)
@@ -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 (file)
index 0000000..aea4ff9
Binary files /dev/null and b/test-data/document/Bug45269.doc differ