import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.Iterator;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
-import org.apache.poi.hwpf.model.BookmarksTables;
-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.*;
import org.apache.poi.hwpf.model.io.HWPFFileSystem;
import org.apache.poi.hwpf.model.io.HWPFOutputStream;
import org.apache.poi.hwpf.usermodel.Bookmarks;
{
if ( !docWritten )
{
- pfs.createOrUpdateDocument( new ByteArrayInputStream( mainBuf ),
- STREAM_WORD_DOCUMENT );
+ write(pfs, mainBuf, STREAM_WORD_DOCUMENT);
docWritten = true;
}
}
{
if ( !tableWritten )
{
- pfs.createOrUpdateDocument( new ByteArrayInputStream( tableBuf ),
- STREAM_TABLE_1 );
+ write(pfs, tableBuf, STREAM_TABLE_1);
tableWritten = true;
}
}
{
if ( !dataWritten )
{
- pfs.createOrUpdateDocument( new ByteArrayInputStream( dataBuf ),
- STREAM_DATA );
+ write(pfs, dataBuf, STREAM_DATA);
dataWritten = true;
}
}
}
if ( !docWritten )
- pfs.createOrUpdateDocument( new ByteArrayInputStream( mainBuf ),
- STREAM_WORD_DOCUMENT );
+ write(pfs, mainBuf, STREAM_WORD_DOCUMENT);
if ( !tableWritten )
- pfs.createOrUpdateDocument( new ByteArrayInputStream( tableBuf ),
- STREAM_TABLE_1 );
+ write(pfs, tableBuf, STREAM_TABLE_1);
if ( !propertiesWritten )
writeProperties( pfs );
if ( !dataWritten )
- pfs.createOrUpdateDocument( new ByteArrayInputStream( dataBuf ),
- STREAM_DATA );
+ write(pfs, dataBuf, STREAM_DATA);
if ( !objectPoolWritten && copyOtherEntries )
_objectPool.writeTo( pfs.getRoot() );
this._tableStream = tableStream.toByteArray();
this._dataStream = dataBuf;
}
+ private static void write(NPOIFSFileSystem pfs, byte[] data, String name) throws IOException {
+ pfs.createOrUpdateDocument(new ByteArrayInputStream(data), name);
+ }
@Internal
public byte[] getDataStream()
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import org.apache.poi.POIDataSamples;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFTestCase;
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.util.IOUtils;
import org.apache.poi.util.TempFile;
/**
r = doc.getRange();
assertEquals("I am a test document\r", r.getParagraph(0).text());
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) {}
+ }
}