]> source.dussan.org Git - poi.git/commitdiff
fix 47287 - StringIndexOutOfBoundsException in CharacterRun.replaceText()
authorSergey Vladimirov <sergey@apache.org>
Thu, 7 Jul 2011 12:11:53 +0000 (12:11 +0000)
committerSergey Vladimirov <sergey@apache.org>
Thu, 7 Jul 2011 12:11:53 +0000 (12:11 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1143786 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/scratchpad/src/org/apache/poi/hwpf/model/TextPiece.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java

index c919ea0762d3c21bef06cad573c716e28b61062d..b87e29f6f69b754e93e68d349e8780b0cd40a51c 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta4" date="2011-??-??">
+           <action dev="poi-developers" type="fix">47287 - StringIndexOutOfBoundsException in CharacterRun.replaceText()</action>
            <action dev="poi-developers" type="fix">46817 - Regression: Text from some table cells missing</action>
            <action dev="poi-developers" type="add">Add getOverallRange() method to HWPFDocumentCore</action>
            <action dev="poi-developers" type="fix">PAPX referenced outside of TextPiecesTable are ignored now and not loaded</action>
index 50e1127b5d75fcb09fddeac56018a968605c844b..ce448911abb290edb80d414826d8f837c38f4717 100644 (file)
@@ -141,7 +141,10 @@ public final class TextPiece extends PropertyNode<TextPiece>
                   /* find where the deleted area overlaps with this text piece */
                   int overlapStart = Math.max(myStart, start);
                   int overlapEnd = Math.min(myEnd, end);
-                  ((StringBuffer)_buf).delete(overlapStart, overlapEnd);
+
+                  int bufStart = overlapStart - myStart;
+                  int bufEnd = overlapEnd - myStart;
+                  ((StringBuffer)_buf).delete(bufStart, bufEnd);
           }
 
           // We need to invoke this even if text from this piece is not being
index 2d3a3ff012100bbc8a7b6d29eac33d6047b51245..6a2fce237c28d621d5c3bd0136528d99a79bfbcf 100644 (file)
@@ -525,75 +525,79 @@ public final class TestProblems extends HWPFTestCase {
     }
 
     /**
-     * [FAILING] Bug 47287 - StringIndexOutOfBoundsException in CharacterRun.replaceText()
+     * [RESOLVED FIXED] Bug 47287 - StringIndexOutOfBoundsException in
+     * CharacterRun.replaceText()
      */
-    public void test47287() {
-        HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug47287.doc");
-        String[] values = {
-                "1-1",
-                "1-2",
-                "1-3",
-                "1-4",
-                "1-5",
-                "1-6",
-                "1-7",
-                "1-8",
-                "1-9",
-                "1-10",
-                "1-11",
-                "1-12",
-                "1-13",
-                "1-14",
-                "1-15",
-        };
+    public void test47287()
+    {
+        HWPFDocument doc = HWPFTestDataSamples.openSampleFile( "Bug47287.doc" );
+        String[] values = { "1-1", "1-2", "1-3", "1-4", "1-5", "1-6", "1-7",
+                "1-8", "1-9", "1-10", "1-11", "1-12", "1-13", "1-14", "1-15", };
         int usedVal = 0;
-        try {
-            String PLACEHOLDER = "\u2002\u2002\u2002\u2002\u2002";
-            Range r = doc.getRange();
-            for (int x = 0; x < r.numSections(); x++) {
-                Section s = r.getSection(x);
-                for (int y = 0; y < s.numParagraphs(); y++) {
-                    Paragraph p = s.getParagraph(y);
-
-                    for (int z = 0; z < p.numCharacterRuns(); z++) {
-                        boolean isFound = false;
-
-                        //character run
-                        CharacterRun run = p.getCharacterRun(z);
-                        //character run text
-                        String text = run.text();
-                        String oldText = text;
-                        int c = text.indexOf("FORMTEXT ");
-                        if (c < 0) {
-                            int k = text.indexOf(PLACEHOLDER);
-                            if (k >= 0) {
-                                text = text.substring(0, k) + values[usedVal] + text.substring(k + PLACEHOLDER.length());
+        String PLACEHOLDER = "\u2002\u2002\u2002\u2002\u2002";
+        Range r = doc.getRange();
+        for ( int x = 0; x < r.numSections(); x++ )
+        {
+            Section s = r.getSection( x );
+            for ( int y = 0; y < s.numParagraphs(); y++ )
+            {
+                Paragraph p = s.getParagraph( y );
+
+                for ( int z = 0; z < p.numCharacterRuns(); z++ )
+                {
+                    boolean isFound = false;
+
+                    // character run
+                    CharacterRun run = p.getCharacterRun( z );
+                    // character run text
+                    String text = run.text();
+                    String oldText = text;
+                    int c = text.indexOf( "FORMTEXT " );
+                    if ( c < 0 )
+                    {
+                        int k = text.indexOf( PLACEHOLDER );
+                        if ( k >= 0 )
+                        {
+                            text = text.substring( 0, k ) + values[usedVal]
+                                    + text.substring( k + PLACEHOLDER.length() );
+                            usedVal++;
+                            isFound = true;
+                        }
+                    }
+                    else
+                    {
+                        for ( ; c >= 0; c = text.indexOf( "FORMTEXT ", c
+                                + "FORMTEXT ".length() ) )
+                        {
+                            int k = text.indexOf( PLACEHOLDER, c );
+                            if ( k >= 0 )
+                            {
+                                text = text.substring( 0, k )
+                                        + values[usedVal]
+                                        + text.substring( k
+                                                + PLACEHOLDER.length() );
                                 usedVal++;
                                 isFound = true;
                             }
-                        } else {
-                            for (; c >= 0; c = text.indexOf("FORMTEXT ", c + "FORMTEXT ".length())) {
-                                int k = text.indexOf(PLACEHOLDER, c);
-                                if (k >= 0) {
-                                    text = text.substring(0, k) + values[usedVal] + text.substring(k + PLACEHOLDER.length());
-                                    usedVal++;
-                                    isFound = true;
-                                }
-                            }
                         }
-                        if (isFound) {
-                            run.replaceText(oldText, text, 0);
-                        }
-
                     }
+                    if ( isFound )
+                    {
+                        run.replaceText( oldText, text, 0 );
+                    }
+
                 }
             }
-            fixed("47287");
-        } catch (StringIndexOutOfBoundsException e) {
-            // expected exception
         }
-    }
 
+        String docText = r.text();
+
+        assertTrue( docText.contains( "1-1" ) );
+        assertTrue( docText.contains( "1-12" ) );
+
+        assertFalse( docText.contains( "1-13" ) );
+        assertFalse( docText.contains( "1-15" ) );
+    }
 
     private static void insertTable(int rows, int columns) {
         // POI apparently can't create a document from scratch,