package org.apache.poi.hpsf;
+import java.io.IOException;
import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.poi.POIDocument;
+import org.apache.poi.poifs.filesystem.EntryUtils;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
super(fs);
}
- public void write(OutputStream out) {
- throw new IllegalStateException("Unable to write, only for properties!");
+ /**
+ * Write out, with any properties changes, but nothing else
+ */
+ public void write(OutputStream out) throws IOException {
+ POIFSFileSystem fs = new POIFSFileSystem();
+
+ // For tracking what we've written out, so far
+ List<String> excepts = new ArrayList<String>(1);
+
+ // Write out our HPFS properties, with any changes
+ writeProperties(fs, excepts);
+
+ // Copy over everything else unchanged
+ EntryUtils.copyNodes(directory, fs.getRoot(), excepts);
+
+ // Save the resultant POIFSFileSystem to the output stream
+ fs.writeFilesystem(out);
}
}
\ No newline at end of file
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
+import org.apache.poi.POIDocument;
import org.apache.poi.hpsf.DocumentSummaryInformation;
+import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
// Write out and read back, should still be valid
- // TODO
+ POIDocument doc = new HPSFPropertiesOnlyDocument(fs);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ doc.write(baos);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ doc = new HPSFPropertiesOnlyDocument(new POIFSFileSystem(bais));
+
+ // Check properties are still there
+ assertEquals("Microsoft Word 10.0", si.getApplicationName());
+ assertEquals("", si.getTitle());
+ assertEquals("", si.getAuthor());
+ assertEquals("Cour de Justice", dsi.getCompany());
}
}