Bug 57603: Apply suggested patch

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923052 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2025-01-11 09:23:35 +00:00
parent c1f52674fd
commit ae9355dcf4
4 changed files with 59 additions and 53 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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);
}
}