From 397d0991a541a3086dfce8bf424e8efeec32f8ed Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Thu, 11 Aug 2011 13:55:35 +0000 Subject: [PATCH] fix document parts boundaries modifications on text changes (in FIB) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1156616 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hwpf/model/FileInformationBlock.java | 7 ++- .../org/apache/poi/hwpf/usermodel/Range.java | 12 +++-- .../apache/poi/hwpf/usermodel/TestBugs.java | 49 ++++++++++++++++++- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java b/src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java index ec9453dcec..e8f4062783 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java @@ -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 // } } - 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 c99c4eb28d..f368be1979 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java @@ -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; } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java index b93d952aea..c271cc66f0 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java @@ -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 ); + } } -- 2.39.5