mirror of https://github.com/apache/poi.git
Slightly simplify HWPF writing code, and add in-place write tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1756003 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2bb05f3071
commit
e726429d5f
|
@ -22,32 +22,10 @@ import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
import org.apache.poi.hpsf.DocumentSummaryInformation;
|
import org.apache.poi.hpsf.DocumentSummaryInformation;
|
||||||
import org.apache.poi.hpsf.SummaryInformation;
|
import org.apache.poi.hpsf.SummaryInformation;
|
||||||
import org.apache.poi.hwpf.model.BookmarksTables;
|
import org.apache.poi.hwpf.model.*;
|
||||||
import org.apache.poi.hwpf.model.CHPBinTable;
|
|
||||||
import org.apache.poi.hwpf.model.ComplexFileTable;
|
|
||||||
import org.apache.poi.hwpf.model.DocumentProperties;
|
|
||||||
import org.apache.poi.hwpf.model.EscherRecordHolder;
|
|
||||||
import org.apache.poi.hwpf.model.FSPADocumentPart;
|
|
||||||
import org.apache.poi.hwpf.model.FSPATable;
|
|
||||||
import org.apache.poi.hwpf.model.FieldsTables;
|
|
||||||
import org.apache.poi.hwpf.model.FontTable;
|
|
||||||
import org.apache.poi.hwpf.model.ListTables;
|
|
||||||
import org.apache.poi.hwpf.model.NoteType;
|
|
||||||
import org.apache.poi.hwpf.model.NotesTables;
|
|
||||||
import org.apache.poi.hwpf.model.PAPBinTable;
|
|
||||||
import org.apache.poi.hwpf.model.PicturesTable;
|
|
||||||
import org.apache.poi.hwpf.model.RevisionMarkAuthorTable;
|
|
||||||
import org.apache.poi.hwpf.model.SavedByTable;
|
|
||||||
import org.apache.poi.hwpf.model.SectionTable;
|
|
||||||
import org.apache.poi.hwpf.model.SinglentonTextPiece;
|
|
||||||
import org.apache.poi.hwpf.model.StyleSheet;
|
|
||||||
import org.apache.poi.hwpf.model.SubdocumentType;
|
|
||||||
import org.apache.poi.hwpf.model.TextPiece;
|
|
||||||
import org.apache.poi.hwpf.model.TextPieceTable;
|
|
||||||
import org.apache.poi.hwpf.model.io.HWPFFileSystem;
|
import org.apache.poi.hwpf.model.io.HWPFFileSystem;
|
||||||
import org.apache.poi.hwpf.model.io.HWPFOutputStream;
|
import org.apache.poi.hwpf.model.io.HWPFOutputStream;
|
||||||
import org.apache.poi.hwpf.usermodel.Bookmarks;
|
import org.apache.poi.hwpf.usermodel.Bookmarks;
|
||||||
|
@ -938,8 +916,7 @@ public final class HWPFDocument extends HWPFDocumentCore {
|
||||||
{
|
{
|
||||||
if ( !docWritten )
|
if ( !docWritten )
|
||||||
{
|
{
|
||||||
pfs.createOrUpdateDocument( new ByteArrayInputStream( mainBuf ),
|
write(pfs, mainBuf, STREAM_WORD_DOCUMENT);
|
||||||
STREAM_WORD_DOCUMENT );
|
|
||||||
docWritten = true;
|
docWritten = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -960,8 +937,7 @@ public final class HWPFDocument extends HWPFDocumentCore {
|
||||||
{
|
{
|
||||||
if ( !tableWritten )
|
if ( !tableWritten )
|
||||||
{
|
{
|
||||||
pfs.createOrUpdateDocument( new ByteArrayInputStream( tableBuf ),
|
write(pfs, tableBuf, STREAM_TABLE_1);
|
||||||
STREAM_TABLE_1 );
|
|
||||||
tableWritten = true;
|
tableWritten = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -980,8 +956,7 @@ public final class HWPFDocument extends HWPFDocumentCore {
|
||||||
{
|
{
|
||||||
if ( !dataWritten )
|
if ( !dataWritten )
|
||||||
{
|
{
|
||||||
pfs.createOrUpdateDocument( new ByteArrayInputStream( dataBuf ),
|
write(pfs, dataBuf, STREAM_DATA);
|
||||||
STREAM_DATA );
|
|
||||||
dataWritten = true;
|
dataWritten = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -992,16 +967,13 @@ public final class HWPFDocument extends HWPFDocumentCore {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !docWritten )
|
if ( !docWritten )
|
||||||
pfs.createOrUpdateDocument( new ByteArrayInputStream( mainBuf ),
|
write(pfs, mainBuf, STREAM_WORD_DOCUMENT);
|
||||||
STREAM_WORD_DOCUMENT );
|
|
||||||
if ( !tableWritten )
|
if ( !tableWritten )
|
||||||
pfs.createOrUpdateDocument( new ByteArrayInputStream( tableBuf ),
|
write(pfs, tableBuf, STREAM_TABLE_1);
|
||||||
STREAM_TABLE_1 );
|
|
||||||
if ( !propertiesWritten )
|
if ( !propertiesWritten )
|
||||||
writeProperties( pfs );
|
writeProperties( pfs );
|
||||||
if ( !dataWritten )
|
if ( !dataWritten )
|
||||||
pfs.createOrUpdateDocument( new ByteArrayInputStream( dataBuf ),
|
write(pfs, dataBuf, STREAM_DATA);
|
||||||
STREAM_DATA );
|
|
||||||
if ( !objectPoolWritten && copyOtherEntries )
|
if ( !objectPoolWritten && copyOtherEntries )
|
||||||
_objectPool.writeTo( pfs.getRoot() );
|
_objectPool.writeTo( pfs.getRoot() );
|
||||||
|
|
||||||
|
@ -1015,6 +987,9 @@ public final class HWPFDocument extends HWPFDocumentCore {
|
||||||
this._tableStream = tableStream.toByteArray();
|
this._tableStream = tableStream.toByteArray();
|
||||||
this._dataStream = dataBuf;
|
this._dataStream = dataBuf;
|
||||||
}
|
}
|
||||||
|
private static void write(NPOIFSFileSystem pfs, byte[] data, String name) throws IOException {
|
||||||
|
pfs.createOrUpdateDocument(new ByteArrayInputStream(data), name);
|
||||||
|
}
|
||||||
|
|
||||||
@Internal
|
@Internal
|
||||||
public byte[] getDataStream()
|
public byte[] getDataStream()
|
||||||
|
|
|
@ -21,11 +21,16 @@ import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
|
||||||
|
import org.apache.poi.POIDataSamples;
|
||||||
import org.apache.poi.hwpf.HWPFDocument;
|
import org.apache.poi.hwpf.HWPFDocument;
|
||||||
import org.apache.poi.hwpf.HWPFTestCase;
|
import org.apache.poi.hwpf.HWPFTestCase;
|
||||||
import org.apache.poi.hwpf.HWPFTestDataSamples;
|
import org.apache.poi.hwpf.HWPFTestDataSamples;
|
||||||
|
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
|
||||||
|
import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
|
||||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||||
|
import org.apache.poi.util.IOUtils;
|
||||||
import org.apache.poi.util.TempFile;
|
import org.apache.poi.util.TempFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -75,7 +80,65 @@ public final class TestHWPFWrite extends HWPFTestCase {
|
||||||
r = doc.getRange();
|
r = doc.getRange();
|
||||||
assertEquals("I am a test document\r", r.getParagraph(0).text());
|
assertEquals("I am a test document\r", r.getParagraph(0).text());
|
||||||
doc.close();
|
doc.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO In-place write positive and negative checks
|
/**
|
||||||
|
* Writing to the file we opened from - note, uses a temp file to
|
||||||
|
* avoid changing our test files!
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
public void testInPlaceWrite() throws Exception {
|
||||||
|
// Setup as a copy of a known-good file
|
||||||
|
final File file = TempFile.createTempFile("TestDocument", ".doc");
|
||||||
|
IOUtils.copy(
|
||||||
|
POIDataSamples.getDocumentInstance().openResourceAsStream("SampleDoc.doc"),
|
||||||
|
new FileOutputStream(file)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Open from the temp file in read-write mode
|
||||||
|
HWPFDocument doc = new HWPFDocument(new NPOIFSFileSystem(file, false).getRoot());
|
||||||
|
Range r = doc.getRange();
|
||||||
|
assertEquals("I am a test document\r", r.getParagraph(0).text());
|
||||||
|
|
||||||
|
// Change
|
||||||
|
r.replaceText("X XX a test document\r", false);
|
||||||
|
|
||||||
|
// Save in-place, close, re-open and check
|
||||||
|
doc.write();
|
||||||
|
doc.close();
|
||||||
|
|
||||||
|
doc = new HWPFDocument(new NPOIFSFileSystem(file).getRoot());
|
||||||
|
assertEquals("X XX a test document\r", r.getParagraph(0).text());
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
public void testInvalidInPlaceWrite() throws Exception {
|
||||||
|
HWPFDocument doc;
|
||||||
|
|
||||||
|
// Can't work for InputStream opened files
|
||||||
|
doc = new HWPFDocument(
|
||||||
|
POIDataSamples.getDocumentInstance().openResourceAsStream("SampleDoc.doc"));
|
||||||
|
try {
|
||||||
|
doc.write();
|
||||||
|
fail("Shouldn't work for InputStream");
|
||||||
|
} catch (IllegalStateException e) {}
|
||||||
|
|
||||||
|
// Can't work for OPOIFS
|
||||||
|
OPOIFSFileSystem ofs = new OPOIFSFileSystem(
|
||||||
|
POIDataSamples.getDocumentInstance().openResourceAsStream("SampleDoc.doc"));
|
||||||
|
doc = new HWPFDocument(ofs.getRoot());
|
||||||
|
try {
|
||||||
|
doc.write();
|
||||||
|
fail("Shouldn't work for OPOIFSFileSystem");
|
||||||
|
} catch (IllegalStateException e) {}
|
||||||
|
|
||||||
|
// Can't work for Read-Only files
|
||||||
|
NPOIFSFileSystem fs = new NPOIFSFileSystem(
|
||||||
|
POIDataSamples.getDocumentInstance().getFile("SampleDoc.doc"), true);
|
||||||
|
doc = new HWPFDocument(fs.getRoot());
|
||||||
|
try {
|
||||||
|
doc.write();
|
||||||
|
fail("Shouldn't work for Read Only");
|
||||||
|
} catch (IllegalStateException e) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue