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.
*
* @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);
}
// 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;
if ( !objectPoolWritten )
_objectPool.writeTo( pfs.getRoot() );
- pfs.writeFilesystem( out );
this.directory = pfs.getRoot();
/*
--- /dev/null
+/* ====================================================================
+ 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
+}