Skip to content

Commit

Permalink
Bug 57603: Apply suggested patch
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923052 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
centic9 committed Jan 11, 2025
1 parent c1f5267 commit ae9355d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,8 @@ private void write(POIFSFileSystem pfs, boolean copyOtherEntries) throws IOExcep

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public ArrayList<PAPX> getParagraphs()
}

public void writeTo( ByteArrayOutputStream wordDocumentStream,
ByteArrayOutputStream tableStream, CharIndexTranslator translator )
ByteArrayOutputStream tableStream, ByteArrayOutputStream dataStream, CharIndexTranslator translator )
throws IOException
{

Expand Down Expand Up @@ -401,7 +401,7 @@ public void writeTo( ByteArrayOutputStream wordDocumentStream,
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ void testReadWrite() throws IOException {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand Down Expand Up @@ -314,64 +313,66 @@ void test47286() throws IOException {
* 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");
}
}

/**
Expand Down Expand Up @@ -718,10 +719,13 @@ private void assertSection2Margin(Section section) {
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);
}
}

Expand Down

0 comments on commit ae9355d

Please sign in to comment.