Author: centic
Date: Sat Jan 11 09:23:35 2025
New Revision: 1923052

URL: http://svn.apache.org/viewvc?rev=1923052&view=rev
Log:
Bug 57603: Apply suggested patch

Modified:
    poi/trunk/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java
    
poi/trunk/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java
    
poi/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java
    
poi/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestBugs.java

Modified: 
poi/trunk/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java?rev=1923052&r1=1923051&r2=1923052&view=diff
==============================================================================
--- 
poi/trunk/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java 
(original)
+++ 
poi/trunk/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java 
Sat Jan 11 09:23:35 2025
@@ -765,7 +765,8 @@ public final class HWPFDocument extends
 
         // write out the PAPBinTable.
         _fib.setFcPlcfbtePapx(tableOffset);
-        _pbt.writeTo(wordDocumentStream, tableStream, 
_cft.getTextPieceTable());
+        // Right now we don't know how to save dataStream modifications, so we 
can just pipe them to a black hole.
+        _pbt.writeTo(wordDocumentStream, tableStream, new 
ByteArrayOutputStream(), _cft.getTextPieceTable());
         _fib.setLcbPlcfbtePapx(tableStream.size() - tableOffset);
         tableOffset = tableStream.size();
 

Modified: 
poi/trunk/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java?rev=1923052&r1=1923051&r2=1923052&view=diff
==============================================================================
--- 
poi/trunk/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java
 (original)
+++ 
poi/trunk/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java
 Sat Jan 11 09:23:35 2025
@@ -366,7 +366,7 @@ public class PAPBinTable
     }
 
     public void writeTo( ByteArrayOutputStream wordDocumentStream,
-            ByteArrayOutputStream tableStream, CharIndexTranslator translator )
+            ByteArrayOutputStream tableStream, ByteArrayOutputStream 
dataStream, CharIndexTranslator translator )
             throws IOException
     {
 
@@ -401,7 +401,7 @@ public class PAPBinTable
             PAPFormattedDiskPage pfkp = new PAPFormattedDiskPage();
             pfkp.fill(overflow);
 
-            byte[] bufFkp = pfkp.toByteArray(tableStream, translator);
+            byte[] bufFkp = pfkp.toByteArray(dataStream, translator);
             wordDocumentStream.write(bufFkp);
             overflow = pfkp.getOverflow();
 

Modified: 
poi/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java?rev=1923052&r1=1923051&r2=1923052&view=diff
==============================================================================
--- 
poi/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java
 (original)
+++ 
poi/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java
 Sat Jan 11 09:23:35 2025
@@ -59,7 +59,8 @@ public final class TestPAPBinTable {
         HWPFFileSystem fileSys = new HWPFFileSystem();
         ByteArrayOutputStream tableOut = fileSys.getStream( "1Table" );
         ByteArrayOutputStream mainOut = fileSys.getStream( "WordDocument" );
-        _pAPBinTable.writeTo( mainOut, tableOut, fakeTPT );
+        ByteArrayOutputStream dataOut = fileSys.getStream("Data");
+        _pAPBinTable.writeTo(mainOut, tableOut, dataOut, fakeTPT);
 
         byte[] newTableStream = tableOut.toByteArray();
         byte[] newMainStream = mainOut.toByteArray();

Modified: 
poi/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestBugs.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestBugs.java?rev=1923052&r1=1923051&r2=1923052&view=diff
==============================================================================
--- 
poi/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestBugs.java
 (original)
+++ 
poi/trunk/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestBugs.java
 Sat Jan 11 09:23:35 2025
@@ -23,7 +23,6 @@ import static org.apache.poi.hwpf.HWPFTe
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.ByteArrayOutputStream;
@@ -314,64 +313,66 @@ class TestBugs {
      * CharacterRun.replaceText()
      */
     @Test
-    void test47287() {
-        HWPFDocument doc = 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;
-        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);
+    void test47287() throws IOException {
+        try (HWPFDocument doc = 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;
+            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());
+                                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);
-                    }
 
+                    }
                 }
             }
-        }
 
-        String docText = r.text();
+            String docText = r.text();
 
-        assertContains(docText, "1-1");
-        assertContains(docText, "1-12");
+            assertContains(docText, "1-1");
+            assertContains(docText, "1-12");
 
-        assertNotContained(docText, "1-13");
-        assertNotContained(docText, "1-15");
+            assertNotContained(docText, "1-13");
+            assertNotContained(docText, "1-15");
+        }
     }
 
     /**
@@ -718,10 +719,13 @@ class TestBugs {
         assertEquals(section2NumColumns, section.getNumColumns());
     }
 
+    /**
+     * [RESOLVED FIXED] Bug 57603 - failed to create Word 2003 with seven or 
more columns
+     */
     @Test
     void test57603SevenRowTable() throws Exception {
         try (HWPFDocument hwpfDocument = 
openSampleFile("57603-seven_columns.doc")) {
-            assertThrows(ArrayIndexOutOfBoundsException.class, () -> 
HWPFTestDataSamples.writeOutAndReadBack(hwpfDocument));
+            HWPFTestDataSamples.writeOutAndReadBack(hwpfDocument);
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to