HWPF in-place write support

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1756001 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2016-08-11 15:14:04 +00:00
parent f743acc3e3
commit 8c7c45ecca

View File

@ -572,12 +572,27 @@ public final class HWPFDocument extends HWPFDocumentCore {
} }
/** /**
* Warning - not currently implemented for HWPF! * Write out the word file that is represented by this class, to the
* currently open {@link File}, via the writeable {@link POIFSFileSystem}
* it was opened as.
*
* <p>This will fail (with an {@link IllegalStateException} if the
* Document was opened read-only, opened from an {@link InputStream}
* instead of a File, or if this is not the root document. For those cases,
* you must use {@link #write(OutputStream)} or {@link #write(File)} to
* write to a brand new document.
*
* @since 3.15
*/ */
@Override @Override
public void write() throws IOException { public void write() throws IOException {
// TODO Implement validateInPlaceWritePossible();
throw new IllegalStateException("Coming soon!");
// Update the Document+Properties streams in the file
write(directory.getFileSystem(), false);
// Sync with the File on disk
directory.getFileSystem().writeFilesystem();
} }
/** /**
@ -912,22 +927,18 @@ public final class HWPFDocument extends HWPFDocumentCore {
dataBuf = tempBuf; dataBuf = tempBuf;
} }
// create new document preserving order of entries // Create a new document preserving order of entries / Update existing
// TODO Check "copyOtherEntries" and tweak behaviour based on that
// TODO That's needed for in-place write
boolean docWritten = false; boolean docWritten = false;
boolean dataWritten = false; boolean dataWritten = false;
boolean objectPoolWritten = false; boolean objectPoolWritten = false;
boolean tableWritten = false; boolean tableWritten = false;
boolean propertiesWritten = false; boolean propertiesWritten = false;
for ( Iterator<Entry> iter = directory.getEntries(); iter.hasNext(); ) for (Entry entry : directory) {
{
Entry entry = iter.next();
if ( entry.getName().equals( STREAM_WORD_DOCUMENT ) ) if ( entry.getName().equals( STREAM_WORD_DOCUMENT ) )
{ {
if ( !docWritten ) if ( !docWritten )
{ {
pfs.createDocument( new ByteArrayInputStream( mainBuf ), pfs.createOrUpdateDocument( new ByteArrayInputStream( mainBuf ),
STREAM_WORD_DOCUMENT ); STREAM_WORD_DOCUMENT );
docWritten = true; docWritten = true;
} }
@ -936,7 +947,11 @@ public final class HWPFDocument extends HWPFDocumentCore {
{ {
if ( !objectPoolWritten ) if ( !objectPoolWritten )
{ {
if ( copyOtherEntries ) {
_objectPool.writeTo( pfs.getRoot() ); _objectPool.writeTo( pfs.getRoot() );
} else {
// Object pool is already there, no need to change/copy
}
objectPoolWritten = true; objectPoolWritten = true;
} }
} }
@ -945,7 +960,7 @@ public final class HWPFDocument extends HWPFDocumentCore {
{ {
if ( !tableWritten ) if ( !tableWritten )
{ {
pfs.createDocument( new ByteArrayInputStream( tableBuf ), pfs.createOrUpdateDocument( new ByteArrayInputStream( tableBuf ),
STREAM_TABLE_1 ); STREAM_TABLE_1 );
tableWritten = true; tableWritten = true;
} }
@ -965,29 +980,29 @@ public final class HWPFDocument extends HWPFDocumentCore {
{ {
if ( !dataWritten ) if ( !dataWritten )
{ {
pfs.createDocument( new ByteArrayInputStream( dataBuf ), pfs.createOrUpdateDocument( new ByteArrayInputStream( dataBuf ),
STREAM_DATA ); STREAM_DATA );
dataWritten = true; dataWritten = true;
} }
} }
else else if ( copyOtherEntries )
{ {
EntryUtils.copyNodeRecursively( entry, pfs.getRoot() ); EntryUtils.copyNodeRecursively( entry, pfs.getRoot() );
} }
} }
if ( !docWritten ) if ( !docWritten )
pfs.createDocument( new ByteArrayInputStream( mainBuf ), pfs.createOrUpdateDocument( new ByteArrayInputStream( mainBuf ),
STREAM_WORD_DOCUMENT ); STREAM_WORD_DOCUMENT );
if ( !tableWritten ) if ( !tableWritten )
pfs.createDocument( new ByteArrayInputStream( tableBuf ), pfs.createOrUpdateDocument( new ByteArrayInputStream( tableBuf ),
STREAM_TABLE_1 ); STREAM_TABLE_1 );
if ( !propertiesWritten ) if ( !propertiesWritten )
writeProperties( pfs ); writeProperties( pfs );
if ( !dataWritten ) if ( !dataWritten )
pfs.createDocument( new ByteArrayInputStream( dataBuf ), pfs.createOrUpdateDocument( new ByteArrayInputStream( dataBuf ),
STREAM_DATA ); STREAM_DATA );
if ( !objectPoolWritten ) if ( !objectPoolWritten && copyOtherEntries )
_objectPool.writeTo( pfs.getRoot() ); _objectPool.writeTo( pfs.getRoot() );
this.directory = pfs.getRoot(); this.directory = pfs.getRoot();