]> source.dussan.org Git - poi.git/commitdiff
backlink used doc's stringbuilder and singlenton TextPiece from TextPieceTable
authorSergey Vladimirov <sergey@apache.org>
Tue, 9 Aug 2011 05:17:35 +0000 (05:17 +0000)
committerSergey Vladimirov <sergey@apache.org>
Tue, 9 Aug 2011 05:17:35 +0000 (05:17 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1155210 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
src/scratchpad/src/org/apache/poi/hwpf/model/SinglentonTextPiece.java [new file with mode: 0644]
src/scratchpad/src/org/apache/poi/hwpf/model/TextPiece.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java

index fa8a47649cc8cd5cbbe39eb3bb81d5a3f4292391..b9327f48f1a8747eef3b42dcf806ddc93391d466 100644 (file)
@@ -39,11 +39,11 @@ import org.apache.poi.hwpf.model.NoteType;
 import org.apache.poi.hwpf.model.NotesTables;
 import org.apache.poi.hwpf.model.PAPBinTable;
 import org.apache.poi.hwpf.model.PicturesTable;
-import org.apache.poi.hwpf.model.PieceDescriptor;
 import org.apache.poi.hwpf.model.RevisionMarkAuthorTable;
 import org.apache.poi.hwpf.model.SavedByTable;
 import org.apache.poi.hwpf.model.SectionTable;
 import org.apache.poi.hwpf.model.ShapesTable;
+import org.apache.poi.hwpf.model.SinglentonTextPiece;
 import org.apache.poi.hwpf.model.StyleSheet;
 import org.apache.poi.hwpf.model.SubdocumentType;
 import org.apache.poi.hwpf.model.TextPiece;
@@ -94,7 +94,8 @@ public final class HWPFDocument extends HWPFDocumentCore
   * structure*/
   protected ComplexFileTable _cft;
 
-  protected final StringBuilder _text;
+  /** Contains text buffer linked directly to single-piece document text piece */
+  protected StringBuilder _text;
 
   /** Holds the save history for this document. */
   protected SavedByTable _sbt;
@@ -284,9 +285,9 @@ public final class HWPFDocument extends HWPFDocumentCore
         {
             _cft = new ComplexFileTable();
             _tpt = _cft.getTextPieceTable();
-            _tpt.add( new TextPiece( 0, _text.length(), _text.toString()
-                    .getBytes( "UTF-16LE" ), new PieceDescriptor( new byte[8],
-                    0 ) ) );
+            final TextPiece textPiece = new SinglentonTextPiece( _text );
+            _tpt.add( textPiece );
+            _text = textPiece.getStringBuilder();
         }
 
         // Read FSPA and Escher information
diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/SinglentonTextPiece.java b/src/scratchpad/src/org/apache/poi/hwpf/model/SinglentonTextPiece.java
new file mode 100644 (file)
index 0000000..28a055e
--- /dev/null
@@ -0,0 +1,51 @@
+package org.apache.poi.hwpf.model;
+
+import java.io.IOException;
+
+import org.apache.poi.util.Internal;
+
+@Internal
+public class SinglentonTextPiece extends TextPiece
+{
+
+    public SinglentonTextPiece( StringBuilder buffer ) throws IOException
+    {
+        super( 0, buffer.length(), buffer.toString().getBytes( "UTF-16LE" ),
+                new PieceDescriptor( new byte[8], 0 ) );
+    }
+
+    @Override
+    public int bytesLength()
+    {
+        return getStringBuilder().length() * 2;
+    }
+
+    @Override
+    public int characterLength()
+    {
+        return getStringBuilder().length();
+    }
+
+    @Override
+    public int getCP()
+    {
+        return 0;
+    }
+
+    @Override
+    public int getEnd()
+    {
+        return characterLength();
+    }
+
+    @Override
+    public int getStart()
+    {
+        return 0;
+    }
+
+    public String toString()
+    {
+        return "SinglentonTextPiece (" + characterLength() + " chars)";
+    }
+}
index c6acd4371c568ce1a44628d4c59f700ca6928263..42e4b3793d7bf562cc6a36a4dec69b50569f0c4a 100644 (file)
@@ -31,7 +31,7 @@ import org.apache.poi.util.Internal;
  * @author Ryan Ackley
  */
 @Internal
-public final class TextPiece extends PropertyNode<TextPiece>
+public class TextPiece extends PropertyNode<TextPiece>
 {
   private boolean _usesUnicode;
 
@@ -134,6 +134,7 @@ public final class TextPiece extends PropertyNode<TextPiece>
     * @param start Local start position, in characters
     * @param end Local end position, in characters
     */
+   @Deprecated
    public String substring(int start, int end)
    {
        StringBuilder buf = (StringBuilder)_buf;
@@ -157,6 +158,7 @@ public final class TextPiece extends PropertyNode<TextPiece>
     * @param start The start position for the delete, in characters
     * @param length The number of characters to delete
     */
+   @Deprecated
    public void adjustForDelete(int start, int length) {
           int numChars = length;
 
@@ -187,6 +189,7 @@ public final class TextPiece extends PropertyNode<TextPiece>
    /**
     * Returns the length, in characters
     */
+   @Deprecated
    public int characterLength()
    {
      return (getEnd() - getStart());
index d524a17728d49d182a2bf6d8177a309178d8f4a9..36a38ac259379fb548a109ebe186b7a31cf18927 100644 (file)
@@ -16,6 +16,8 @@
 ==================================================================== */
 package org.apache.poi.hwpf.usermodel;
 
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
@@ -473,4 +475,33 @@ public class TestBugs extends TestCase
         HWPFTestDataSamples.openSampleFileFromArchive( "Bug51524.zip" );
     }
 
+    /**
+     * Bug 51604 - replace text fails for doc ( poi 3.8 beta release from
+     * download site )
+     */
+    public void test51604()
+    {
+        HWPFDocument document = HWPFTestDataSamples
+                .openSampleFile( "Bug51604.doc" );
+
+        Range range = document.getRange();
+        int numParagraph = range.numParagraphs();
+        int counter = 0;
+        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();
+                charRun.replaceText( text, "+" + ( ++counter ) );
+            }
+        }
+
+        document = HWPFTestDataSamples.writeOutAndReadBack( document );
+        String text = document.getDocumentText();
+        assertEquals( "+1+2+3+4+5+6+7+8+9+10+11+12", text );
+    }
+
 }