poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1754108 - in /poi/trunk/src/scratchpad: src/org/apache/poi/hwpf/HWPFDocument.java testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java
Date Tue, 26 Jul 2016 10:58:09 GMT
Author: nick
Date: Tue Jul 26 10:58:08 2016
New Revision: 1754108

URL: http://svn.apache.org/viewvc?rev=1754108&view=rev
Log:
HWPFDocument.write(File) support and tests #57919

Added:
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java  
(with props)
Modified:
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.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=1754108&r1=1754107&r2=1754108&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 Tue Jul 26 10:58:08
2016
@@ -571,20 +571,39 @@ public final class HWPFDocument extends
         return _fields;
     }
 
+    /**
+     * Warning - not currently implemented for HWPF!
+     */
     @Override
     public void write() throws IOException {
+        // TODO Implement
         throw new IllegalStateException("Coming soon!");
     }
+    
+    /**
+     * Writes out the word file that is represented by an instance of this class.
+     * 
+     * If the {@link File} exists, it will be replaced, otherwise a new one 
+     * will be created
+     *
+     * @param newFile The File to write to.
+     * @throws IOException If there is an unexpected IOException from writing
+     *         to the File.
+     *         
+     * @since 3.15 beta 3
+     */
     @Override
     public void write(File newFile) throws IOException {
-        throw new IllegalStateException("Coming soon!");
+        NPOIFSFileSystem pfs = POIFSFileSystem.create(newFile);
+        write(pfs, true);
+        pfs.writeFilesystem();
     }
 
     /**
      * Writes out the word file that is represented by an instance of this class.
      * 
-     * If {@code stream} is a {@link java.io.FileOutputStream} on a networked drive
-     * or has a high cost/latency associated with each written byte,
+     * For better performance when writing to files, use {@link #write(File)}.
+     * If {@code stream} has a high cost/latency associated with each written byte,
      * consider wrapping the OutputStream in a {@link java.io.BufferedOutputStream}
      * to improve write performance.
      *
@@ -592,9 +611,12 @@ public final class HWPFDocument extends
      * @throws IOException If there is an unexpected IOException from the passed
      *         in OutputStream.
      */
-    public void write(OutputStream out)
-            throws IOException
-            {
+    public void write(OutputStream out) throws IOException {
+        NPOIFSFileSystem pfs = new NPOIFSFileSystem();
+        write(pfs, true);
+        pfs.writeFilesystem( out );
+    }
+    private void write(NPOIFSFileSystem pfs, boolean copyOtherEntries) throws IOException
{
         // initialize our streams for writing.
         HWPFFileSystem docSys = new HWPFFileSystem();
         HWPFOutputStream wordDocumentStream = docSys.getStream(STREAM_WORD_DOCUMENT);
@@ -891,7 +913,8 @@ public final class HWPFDocument extends
         }
 
         // create new document preserving order of entries
-        NPOIFSFileSystem pfs = new NPOIFSFileSystem();
+        // TODO Check "copyOtherEntries" and tweak behaviour based on that
+        // TODO That's needed for in-place write
         boolean docWritten = false;
         boolean dataWritten = false;
         boolean objectPoolWritten = false;
@@ -967,7 +990,6 @@ public final class HWPFDocument extends
         if ( !objectPoolWritten )
             _objectPool.writeTo( pfs.getRoot() );
 
-        pfs.writeFilesystem( out );
         this.directory = pfs.getRoot();
 
         /*

Added: 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=1754108&view=auto
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java (added)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java Tue
Jul 26 10:58:08 2016
@@ -0,0 +1,81 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hwpf.usermodel;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+
+import org.apache.poi.hwpf.HWPFDocument;
+import org.apache.poi.hwpf.HWPFTestCase;
+import org.apache.poi.hwpf.HWPFTestDataSamples;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.util.TempFile;
+
+/**
+ * Test various write situations
+ */
+public final class TestHWPFWrite extends HWPFTestCase {
+   /**
+    * Write to a stream
+    */
+   public void testWriteStream() throws Exception {
+      HWPFDocument doc = HWPFTestDataSamples.openSampleFile("SampleDoc.doc");
+
+      Range r = doc.getRange();
+      assertEquals("I am a test document\r", r.getParagraph(0).text());
+      
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      doc.write(baos);
+      doc.close();
+      ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+      
+      doc = new HWPFDocument(bais);
+      r = doc.getRange();
+      assertEquals("I am a test document\r", r.getParagraph(0).text());
+      doc.close();
+   }
+   
+   /**
+    * Write to a new file
+    */
+   public void testWriteNewFile() throws Exception {
+       HWPFDocument doc = HWPFTestDataSamples.openSampleFile("SampleDoc.doc");
+
+       Range r = doc.getRange();
+       assertEquals("I am a test document\r", r.getParagraph(0).text());
+       
+       File file = TempFile.createTempFile("TestDocument", ".doc");
+       doc.write(file);
+       doc.close();
+
+       // Check reading from File and Stream
+       doc = new HWPFDocument(new FileInputStream(file));
+       r = doc.getRange();
+       assertEquals("I am a test document\r", r.getParagraph(0).text());
+       doc.close();
+       
+       doc = new HWPFDocument(new POIFSFileSystem(file));
+       r = doc.getRange();
+       assertEquals("I am a test document\r", r.getParagraph(0).text());
+       doc.close();
+    }
+   
+   // TODO In-place write positive and negative checks
+}

Propchange: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java
------------------------------------------------------------------------------
    svn:eol-style = native



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


Mime
View raw message