You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2016/08/11 15:27:33 UTC

svn commit: r1756003 - in /poi/trunk/src/scratchpad: src/org/apache/poi/hwpf/HWPFDocument.java testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java

Author: nick
Date: Thu Aug 11 15:27:33 2016
New Revision: 1756003

URL: http://svn.apache.org/viewvc?rev=1756003&view=rev
Log:
Slightly simplify HWPF writing code, and add in-place write tests

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

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java?rev=1756003&r1=1756002&r2=1756003&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java Thu Aug 11 15:27:33 2016
@@ -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
             {
                 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
             {
                 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
             {
                 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
         }
 
         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
         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()

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java?rev=1756003&r1=1756002&r2=1756003&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java (original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java Thu Aug 11 15:27:33 2016
@@ -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
        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) {}
+   }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org