From 4c7c869e593f900297a4b2f6649fb8b6d4e398f0 Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Fri, 8 Jul 2011 06:53:14 +0000 Subject: [PATCH] fix TestPAPBinTable git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1144177 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/org/apache/poi/hwpf/HWPFDocument.java | 2 +- .../apache/poi/hwpf/model/PAPBinTable.java | 25 +++++++++++++------ .../poi/hwpf/model/TestPAPBinTable.java | 14 +++++------ .../poi/hwpf/usermodel/TestProblems.java | 24 ++++++++++++++++++ 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java index 847a15abc0..575a8f8f49 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java @@ -223,7 +223,7 @@ public final class HWPFDocument extends HWPFDocumentCore // Now load the rest of the properties, which need to be adjusted // for where text really begin _cbt = new CHPBinTable(_mainStream, _tableStream, _fib.getFcPlcfbteChpx(), _fib.getLcbPlcfbteChpx(), cpMin, _tpt); - _pbt = new PAPBinTable(_mainStream, _tableStream, _dataStream, _fib.getFcPlcfbtePapx(), _fib.getLcbPlcfbtePapx(), cpMin, _tpt); + _pbt = new PAPBinTable(_mainStream, _tableStream, _dataStream, _fib.getFcPlcfbtePapx(), _fib.getLcbPlcfbtePapx(), cpMin, _tpt, true); // Read FSPA and Escher information _fspa = new FSPATable(_tableStream, _fib.getFcPlcspaMom(), _fib.getLcbPlcspaMom(), getTextTable().getTextPieces()); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/PAPBinTable.java b/src/scratchpad/src/org/apache/poi/hwpf/model/PAPBinTable.java index 4ca1087cca..129cbcc937 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/PAPBinTable.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/PAPBinTable.java @@ -46,9 +46,23 @@ public class PAPBinTable { } - public PAPBinTable(byte[] documentStream, byte[] tableStream, byte[] dataStream, int offset, - int size, int fcMin, TextPieceTable tpt) - { + /** + * @deprecated Use + * {@link #PAPBinTable(byte[],byte[],byte[],int,int,int,TextPieceTable,boolean)} + * instead + */ + public PAPBinTable( byte[] documentStream, byte[] tableStream, + byte[] dataStream, int offset, int size, int fcMin, + TextPieceTable tpt ) + { + this( documentStream, tableStream, dataStream, offset, size, fcMin, + tpt, true ); + } + + public PAPBinTable( byte[] documentStream, byte[] tableStream, + byte[] dataStream, int offset, int size, int fcMin, + TextPieceTable tpt, boolean ignorePapxWithoutTextPieces ) + { PlexOfCps binTable = new PlexOfCps(tableStream, offset, size, 4); this.tpt = tpt; @@ -70,7 +84,7 @@ public class PAPBinTable PAPX papx = pfkp.getPAPX(y); //we don't need PAPX if they are references nowhere - if (tpt.isIndexInTable( papx.getStartBytes(), papx.getEndBytes() )) + if (!ignorePapxWithoutTextPieces || tpt.isIndexInTable( papx.getStartBytes(), papx.getEndBytes() )) _paragraphs.add(papx); } } @@ -241,7 +255,4 @@ public class PAPBinTable while (overflow != null); tableStream.write(binTable.toByteArray()); } - - } - diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestPAPBinTable.java b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestPAPBinTable.java index 17e694afc3..20b2680335 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestPAPBinTable.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestPAPBinTable.java @@ -17,12 +17,12 @@ package org.apache.poi.hwpf.model; -import junit.framework.*; -import org.apache.poi.hwpf.*; -import org.apache.poi.hwpf.model.io.*; +import java.io.ByteArrayOutputStream; +import java.util.ArrayList; -import java.io.*; -import java.util.*; +import junit.framework.TestCase; +import org.apache.poi.hwpf.HWPFDocFixture; +import org.apache.poi.hwpf.model.io.HWPFFileSystem; public final class TestPAPBinTable extends TestCase @@ -40,7 +40,7 @@ public final class TestPAPBinTable byte[] tableStream = _hWPFDocFixture._tableStream; int fcMin = fib.getFcMin(); - _pAPBinTable = new PAPBinTable(mainStream, tableStream, null, fib.getFcPlcfbtePapx(), fib.getLcbPlcfbtePapx(), fcMin, fakeTPT); + _pAPBinTable = new PAPBinTable(mainStream, tableStream, null, fib.getFcPlcfbtePapx(), fib.getLcbPlcfbtePapx(), fcMin, fakeTPT, false); HWPFFileSystem fileSys = new HWPFFileSystem(); @@ -51,7 +51,7 @@ public final class TestPAPBinTable byte[] newTableStream = tableOut.toByteArray(); byte[] newMainStream = mainOut.toByteArray(); - PAPBinTable newBinTable = new PAPBinTable(newMainStream, newTableStream, null,0, newTableStream.length, 0, fakeTPT); + PAPBinTable newBinTable = new PAPBinTable(newMainStream, newTableStream, null,0, newTableStream.length, 0, fakeTPT, false); ArrayList oldTextRuns = _pAPBinTable.getParagraphs(); ArrayList newTextRuns = newBinTable.getParagraphs(); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java index 38fba59fd7..8eae187478 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java @@ -17,6 +17,9 @@ 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.List; @@ -524,6 +527,27 @@ public final class TestProblems extends HWPFTestCase { assertEquals(text1.replaceAll("\n", ""), text2.replaceAll("\n", "")); } + /** + * Bug 47286 - Word documents saves in wrong format if source contains form elements + * @throws IOException + */ + public void test46286() throws IOException { + HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug46286.doc"); + String text1 = new WordExtractor(doc1).getText().trim(); + + { + FileOutputStream fileOutputStream = new FileOutputStream( new File("test.doc") ); + doc1.write( fileOutputStream ); + fileOutputStream.close(); + } + + HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1); + String text2 = new WordExtractor(doc2).getText().trim(); + + // the text in the saved document has some differences in line separators but we tolerate that + assertEquals(text1.replaceAll("\n", ""), text2.replaceAll("\n", "")); + } + /** * [RESOLVED FIXED] Bug 47287 - StringIndexOutOfBoundsException in * CharacterRun.replaceText()