]> source.dussan.org Git - poi.git/commitdiff
fix document parts boundaries modifications on text changes (in FIB)
authorSergey Vladimirov <sergey@apache.org>
Thu, 11 Aug 2011 13:55:35 +0000 (13:55 +0000)
committerSergey Vladimirov <sergey@apache.org>
Thu, 11 Aug 2011 13:55:35 +0000 (13:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1156616 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java
src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java

index ec9453dcecf33bc08c7dff230b23a44875a07cee..e8f406278393b58cd9931e7a3d84076eb6012567 100644 (file)
@@ -544,6 +544,12 @@ public final class FileInformationBlock extends FIBAbstractType
 
     public void setSubdocumentTextStreamLength( SubdocumentType type, int length )
     {
+        if ( length < 0 )
+            throw new IllegalArgumentException(
+                    "Subdocument length can't be less than 0 (passed value is "
+                            + length + "). " + "If there is no subdocument "
+                            + "length must be set to zero." );
+
         _longHandler.setLong( type.getFibLongFieldIndex(), length );
     }
 
@@ -977,4 +983,3 @@ public final class FileInformationBlock extends FIBAbstractType
 //    }
 
 }
-
index c99c4eb28d78a35e8488a37d632d6478b76c0ce2..f368be1979a48804102e773683522eb314aba1ba 100644 (file)
@@ -1078,11 +1078,13 @@ public class Range { // TODO -instantiable superclass
             currentEnd += currentLength;
 
             // do we need to shift this part?
-            if ( _start < currentEnd )
-            {
-                fib.setSubdocumentTextStreamLength( type, currentLength
-                        + adjustment );
-            }
+            if ( _start > currentEnd )
+                continue;
+
+            fib.setSubdocumentTextStreamLength( type, currentLength
+                    + adjustment );
+
+            break;
         }
     }
 
index b93d952aea7d610ab24dcb0eaf526757ef0c8349..c271cc66f0e9cf60a4dc28771df2760b66132bdb 100644 (file)
@@ -26,6 +26,11 @@ import org.apache.poi.util.LittleEndian;
 
 import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
+
+import org.apache.poi.hwpf.model.SubdocumentType;
+
+import org.apache.poi.hwpf.model.FileInformationBlock;
+
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.hwpf.HWPFDocument;
@@ -513,8 +518,8 @@ public class TestBugs extends TestCase
     }
 
     /**
-     * Bug 51604 - replace text fails for doc ( poi 3.8 beta release from
-     * download site )
+     * [RESOLVED FIXED] Bug 51604 - replace text fails for doc ( poi 3.8 beta
+     * release from download site )
      */
     public void test51604()
     {
@@ -541,4 +546,44 @@ public class TestBugs extends TestCase
         assertEquals( "+1+2+3+4+5+6+7+8+9+10+11+12", text );
     }
 
+    /**
+     * [RESOLVED FIXED] Bug 51604 - replace text fails for doc ( poi 3.8 beta
+     * release from download site )
+     */
+    public void test51604p2()
+    {
+        HWPFDocument doc = HWPFTestDataSamples
+                .openSampleFile( "Bug51604.doc" );
+
+        Range range = doc.getRange();
+        int numParagraph = range.numParagraphs();
+        for ( int i = 0; i < numParagraph; i++ )
+        {
+            Paragraph paragraph = range.getParagraph( i );
+            int numCharRuns = paragraph.numCharacterRuns();
+            for ( int j = 0; j < numCharRuns; j++ )
+            {
+                CharacterRun charRun = paragraph.getCharacterRun( j );
+                String text = charRun.text();
+                if ( text.contains( "Header" ) )
+                    charRun.replaceText( text, "added" );
+            }
+        }
+
+        doc = HWPFTestDataSamples.writeOutAndReadBack( doc );
+        final FileInformationBlock fileInformationBlock = doc
+                .getFileInformationBlock();
+
+        int totalLength = 0;
+        for ( SubdocumentType type : SubdocumentType.values() )
+        {
+            final int partLength = fileInformationBlock
+                    .getSubdocumentTextStreamLength( type );
+            assert ( partLength >= 0 );
+
+            totalLength += partLength;
+        }
+
+        assertEquals( doc.getText().length(), totalLength );
+    }
 }