From ae9355dcf422240ea47c092f8aff186455637236 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sat, 11 Jan 2025 09:23:35 +0000 Subject: [PATCH] Bug 57603: Apply suggested patch git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923052 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hwpf/HWPFDocument.java | 3 +- .../apache/poi/hwpf/model/PAPBinTable.java | 4 +- .../poi/hwpf/model/TestPAPBinTable.java | 3 +- .../apache/poi/hwpf/usermodel/TestBugs.java | 102 +++++++++--------- 4 files changed, 59 insertions(+), 53 deletions(-) diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java index 3c4275b2a9..a4fd555a91 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/HWPFDocument.java @@ -765,7 +765,8 @@ public final class HWPFDocument extends HWPFDocumentCore { // 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(); diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java index 32e9ca9a36..225fc97e32 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java @@ -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(); diff --git a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java index b65843ab63..6588162260 100644 --- a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java +++ b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java @@ -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(); diff --git a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestBugs.java b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestBugs.java index be98329282..bd4e0fb653 100644 --- a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestBugs.java +++ b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestBugs.java @@ -23,7 +23,6 @@ import static org.apache.poi.hwpf.HWPFTestDataSamples.openSampleFile; 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); + 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; + 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); + // 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(); + + assertContains(docText, "1-1"); + assertContains(docText, "1-12"); + + assertNotContained(docText, "1-13"); + assertNotContained(docText, "1-15"); } - - String docText = r.text(); - - assertContains(docText, "1-1"); - assertContains(docText, "1-12"); - - 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); } }