]> source.dussan.org Git - poi.git/commitdiff
Slightly simplify HWPF writing code, and add in-place write tests
authorNick Burch <nick@apache.org>
Thu, 11 Aug 2016 15:27:33 +0000 (15:27 +0000)
committerNick Burch <nick@apache.org>
Thu, 11 Aug 2016 15:27:33 +0000 (15:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1756003 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java

index 0d653891767096d52d63e190d40c42b43cbb3611..ce9d58f038251ed5c3ed389692a692f0a13a94d1 100644 (file)
@@ -22,32 +22,10 @@ import java.io.File;
 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;
@@ -938,8 +916,7 @@ public final class HWPFDocument extends HWPFDocumentCore {
             {
                 if ( !docWritten )
                 {
-                    pfs.createOrUpdateDocument( new ByteArrayInputStream( mainBuf ),
-                            STREAM_WORD_DOCUMENT );
+                    write(pfs, mainBuf, STREAM_WORD_DOCUMENT);
                     docWritten = true;
                 }
             }
@@ -960,8 +937,7 @@ public final class HWPFDocument extends HWPFDocumentCore {
             {
                 if ( !tableWritten )
                 {
-                    pfs.createOrUpdateDocument( new ByteArrayInputStream( tableBuf ),
-                            STREAM_TABLE_1 );
+                    write(pfs, tableBuf, STREAM_TABLE_1);
                     tableWritten = true;
                 }
             }
@@ -980,8 +956,7 @@ public final class HWPFDocument extends HWPFDocumentCore {
             {
                 if ( !dataWritten )
                 {
-                    pfs.createOrUpdateDocument( new ByteArrayInputStream( dataBuf ),
-                            STREAM_DATA );
+                    write(pfs, dataBuf, STREAM_DATA);
                     dataWritten = true;
                 }
             }
@@ -992,16 +967,13 @@ public final class HWPFDocument extends HWPFDocumentCore {
         }
 
         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() );
 
@@ -1015,6 +987,9 @@ public final class HWPFDocument extends HWPFDocumentCore {
         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()
index 95d7919e33f239ad66920fc17502ae455d5753a1..a45b031da2507d235775e931415fba85b8d88f1a 100644 (file)
@@ -21,11 +21,16 @@ import java.io.ByteArrayInputStream;
 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;
 
 /**
@@ -75,7 +80,65 @@ public final class TestHWPFWrite extends HWPFTestCase {
        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) {}
+   }
 }