]> source.dussan.org Git - poi.git/commitdiff
#56791 More updates from OPOIFS to NPOIFS
authorNick Burch <nick@apache.org>
Mon, 11 May 2015 18:04:30 +0000 (18:04 +0000)
committerNick Burch <nick@apache.org>
Mon, 11 May 2015 18:04:30 +0000 (18:04 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1678783 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/POIDocument.java
src/java/org/apache/poi/POIOLE2TextExtractor.java
src/java/org/apache/poi/hpsf/HPSFPropertiesOnlyDocument.java
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/java/org/apache/poi/poifs/filesystem/EntryUtils.java
src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java
src/scratchpad/src/org/apache/poi/hslf/record/CurrentUserAtom.java
src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
src/scratchpad/testcases/org/apache/poi/TestPOIDocumentScratchpad.java
src/testcases/org/apache/poi/TestPOIDocumentMain.java

index bcef6005492a381ec82bd44de7b83e39efbcbb5c..50c088ba7b6fa0cb1e2b7e662cbfcf92d22f7d42 100644 (file)
@@ -30,14 +30,10 @@ import org.apache.poi.hpsf.PropertySet;
 import org.apache.poi.hpsf.PropertySetFactory;
 import org.apache.poi.hpsf.SummaryInformation;
 import org.apache.poi.poifs.crypt.EncryptionInfo;
-import org.apache.poi.poifs.filesystem.DirectoryEntry;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.DocumentInputStream;
-import org.apache.poi.poifs.filesystem.Entry;
-import org.apache.poi.poifs.filesystem.EntryUtils;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 
@@ -45,8 +41,6 @@ import org.apache.poi.util.POILogger;
  * This holds the common functionality for all POI
  *  Document classes.
  * Currently, this relates to Document Information Properties 
- * 
- * @author Nick Burch
  */
 public abstract class POIDocument {
     /** Holds metadata on our document */
@@ -71,14 +65,6 @@ public abstract class POIDocument {
        this.directory = dir;
     }
 
-    /**
-     * @deprecated use {@link POIDocument#POIDocument(DirectoryNode)} instead 
-     */
-    @Deprecated
-    protected POIDocument(DirectoryNode dir, POIFSFileSystem fs) {
-       this.directory = dir;
-    }
-
     protected POIDocument(POIFSFileSystem fs) {
        this(fs.getRoot());
     }
@@ -179,12 +165,13 @@ public abstract class POIDocument {
     protected PropertySet getPropertySet(String setName, EncryptionInfo encryptionInfo) {
         DirectoryNode dirNode = directory;
         
+        NPOIFSFileSystem encPoifs = null;
         if (encryptionInfo != null) {
             try {
                 InputStream is = encryptionInfo.getDecryptor().getDataStream(directory);
-                NPOIFSFileSystem poifs = new NPOIFSFileSystem(is);
+                encPoifs = new NPOIFSFileSystem(is);
                 is.close();
-                dirNode = poifs.getRoot();
+                dirNode = encPoifs.getRoot();
             } catch (Exception e) {
                 logger.log(POILogger.ERROR, "Error getting encrypted property set with name " + setName, e);
                 return null;
@@ -208,6 +195,11 @@ public abstract class POIDocument {
         try {
             // Create the Property Set
             PropertySet set = PropertySetFactory.create(dis);
+            // Tidy up if needed
+            if (encPoifs != null) {
+                encPoifs.close();
+            }
+            // Return the properties
             return set;
         } catch(IOException ie) {
             // Must be corrupt or something like that
@@ -218,26 +210,39 @@ public abstract class POIDocument {
         }
         return null;
     }
+    
+    /**
+     * Writes out the updated standard Document Information Properties (HPSF)
+     *  into the currently open NPOIFSFileSystem
+     * TODO Implement in-place update
+     * 
+     * @throws IOException if an error when writing to the open
+     *      {@link NPOIFSFileSystem} occurs
+     * TODO throws exception if open from stream not file
+     */
+    protected void writeProperties() throws IOException {
+        throw new IllegalStateException("In-place write is not yet supported");
+    }
 
     /**
-     * Writes out the standard Documment Information Properties (HPSF)
+     * Writes out the standard Document Information Properties (HPSF)
      * @param outFS the POIFSFileSystem to write the properties into
      * 
      * @throws IOException if an error when writing to the 
-     *      {@link POIFSFileSystem} occurs
+     *      {@link NPOIFSFileSystem} occurs
      */
-    protected void writeProperties(POIFSFileSystem outFS) throws IOException {
+    protected void writeProperties(NPOIFSFileSystem outFS) throws IOException {
         writeProperties(outFS, null);
     }
     /**
-     * Writes out the standard Documment Information Properties (HPSF)
-     * @param outFS the POIFSFileSystem to write the properties into
+     * Writes out the standard Document Information Properties (HPSF)
+     * @param outFS the NPOIFSFileSystem to write the properties into
      * @param writtenEntries a list of POIFS entries to add the property names too
      * 
      * @throws IOException if an error when writing to the 
-     *      {@link POIFSFileSystem} occurs
+     *      {@link NPOIFSFileSystem} occurs
      */
-    protected void writeProperties(POIFSFileSystem outFS, List<String> writtenEntries) throws IOException {
+    protected void writeProperties(NPOIFSFileSystem outFS, List<String> writtenEntries) throws IOException {
         SummaryInformation si = getSummaryInformation();
         if (si != null) {
             writePropertySet(SummaryInformation.DEFAULT_STREAM_NAME, si, outFS);
@@ -258,12 +263,12 @@ public abstract class POIDocument {
      * Writes out a given ProperySet
      * @param name the (POIFS Level) name of the property to write
      * @param set the PropertySet to write out 
-     * @param outFS the POIFSFileSystem to write the property into
+     * @param outFS the NPOIFSFileSystem to write the property into
      * 
      * @throws IOException if an error when writing to the 
-     *      {@link POIFSFileSystem} occurs
+     *      {@link NPOIFSFileSystem} occurs
      */
-    protected void writePropertySet(String name, PropertySet set, POIFSFileSystem outFS) throws IOException {
+    protected void writePropertySet(String name, PropertySet set, NPOIFSFileSystem outFS) throws IOException {
         try {
             MutablePropertySet mSet = new MutablePropertySet(set);
             ByteArrayOutputStream bOut = new ByteArrayOutputStream();
index c0f4cbd1d741b9b755893c812dc90986f502a117..7679136c89701db0832d615a44d3508c08ccad8a 100644 (file)
@@ -20,7 +20,6 @@ import org.apache.poi.hpsf.DocumentSummaryInformation;
 import org.apache.poi.hpsf.SummaryInformation;
 import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor;
 import org.apache.poi.poifs.filesystem.DirectoryEntry;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 
 /**
  * Common Parent for OLE2 based Text Extractors
@@ -81,17 +80,4 @@ public abstract class POIOLE2TextExtractor extends POITextExtractor {
     {
         return document.directory;
     }
-
-    /**
-     * Return the underlying POIFS FileSystem of this document.
-     * 
-     * @return the POIFSFileSystem that is associated with the POIDocument of this extractor. 
-     *
-     * @deprecated Use {@link #getRoot()} instead
-     */
-    @Deprecated
-    public POIFSFileSystem getFileSystem()
-    {
-        return document.directory.getFileSystem();
-    }
 }
index f1b9ca96291fc1e14a2612dc4a535654fffcead8..ea6bc0b1bcdde50305ad2c3ec002e42e0cd0f75c 100644 (file)
@@ -44,7 +44,7 @@ public class HPSFPropertiesOnlyDocument extends POIDocument {
      * Write out, with any properties changes, but nothing else
      */
     public void write(OutputStream out) throws IOException {
-        POIFSFileSystem fs = new POIFSFileSystem();
+        NPOIFSFileSystem fs = new NPOIFSFileSystem();
 
         // For tracking what we've written out, so far
         List<String> excepts = new ArrayList<String>(1);
index aee2d5444afb7ef3cf397794e40576ac2661297e..be1c7ef9fb728a76f3f8ab2e9ea9f055fa5917e1 100644 (file)
@@ -1277,7 +1277,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
             throws IOException
     {
         byte[] bytes = getBytes();
-        POIFSFileSystem fs = new POIFSFileSystem();
+        NPOIFSFileSystem fs = new NPOIFSFileSystem();
 
         // For tracking what we've written out, used if we're
         //  going to be preserving nodes
index 4bce0641abb45e80372e980fad430dab0201ffa3..d8a55563a2f6b74461d6215357128cc8d4782d53 100644 (file)
@@ -124,8 +124,21 @@ public class EntryUtils
      * @param target
      *            is the target POIFS to copy to
      */
-    public static void copyNodes( POIFSFileSystem source,
-            POIFSFileSystem target ) throws IOException
+    public static void copyNodes( OPOIFSFileSystem source,
+            OPOIFSFileSystem target ) throws IOException
+    {
+        copyNodes( source.getRoot(), target.getRoot() );
+    }
+    /**
+     * Copies all nodes from one POIFS to the other
+     * 
+     * @param source
+     *            is the source POIFS to copy from
+     * @param target
+     *            is the target POIFS to copy to
+     */
+    public static void copyNodes( NPOIFSFileSystem source,
+            NPOIFSFileSystem target ) throws IOException
     {
         copyNodes( source.getRoot(), target.getRoot() );
     }
@@ -140,8 +153,26 @@ public class EntryUtils
      * @param target is the target POIFS to copy to
      * @param excepts is a list of Entry Names to be excluded from the copy
      */
-    public static void copyNodes( POIFSFileSystem source,
-            POIFSFileSystem target, List<String> excepts ) throws IOException
+    public static void copyNodes( OPOIFSFileSystem source,
+            OPOIFSFileSystem target, List<String> excepts ) throws IOException
+    {
+        copyNodes(
+              new FilteringDirectoryNode(source.getRoot(), excepts),
+              new FilteringDirectoryNode(target.getRoot(), excepts)
+        );
+    }
+    /**
+     * Copies nodes from one POIFS to the other, minus the excepts.
+     * This delegates the filtering work to {@link FilteringDirectoryNode},
+     *  so excepts can be of the form "NodeToExclude" or
+     *  "FilteringDirectory/ExcludedChildNode"
+     * 
+     * @param source is the source POIFS to copy from
+     * @param target is the target POIFS to copy to
+     * @param excepts is a list of Entry Names to be excluded from the copy
+     */
+    public static void copyNodes( NPOIFSFileSystem source,
+            NPOIFSFileSystem target, List<String> excepts ) throws IOException
     {
         copyNodes(
               new FilteringDirectoryNode(source.getRoot(), excepts),
index 55c3f6f876f5d06f822bfadc4e62daa4dc9b9063..5097ea39c0665c6d872834124f703be149ac6875 100644 (file)
@@ -52,6 +52,7 @@ import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.DocumentEntry;
 import org.apache.poi.poifs.filesystem.DocumentInputStream;
 import org.apache.poi.poifs.filesystem.EntryUtils;
+import org.apache.poi.poifs.filesystem.FilteringDirectoryNode;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.LittleEndian;
@@ -584,7 +585,7 @@ public final class HSLFSlideShow extends POIDocument {
         _records = encryptedSS.updateEncryptionRecord(_records);
 
         // Get a new Filesystem to write into
-        POIFSFileSystem outFS = new POIFSFileSystem();
+        NPOIFSFileSystem outFS = new NPOIFSFileSystem();
 
         // The list of entries we've written out
         List<String> writtenEntries = new ArrayList<String>(1);
@@ -628,7 +629,9 @@ public final class HSLFSlideShow extends POIDocument {
 
         // If requested, write out any other streams we spot
         if(preserveNodes) {
-            EntryUtils.copyNodes(directory.getFileSystem(), outFS, writtenEntries);
+            FilteringDirectoryNode sNode = new FilteringDirectoryNode(directory, writtenEntries);
+            FilteringDirectoryNode dNode = new FilteringDirectoryNode(outFS.getRoot(), writtenEntries);
+            EntryUtils.copyNodes(sNode, dNode);
         }
 
         // Send the POIFSFileSystem object out to the underlying stream
@@ -651,13 +654,14 @@ public final class HSLFSlideShow extends POIDocument {
 
     /**
      * Writes out the standard Documment Information Properties (HPSF)
-     * @param outFS the POIFSFileSystem to write the properties into
+     * @param outFS the NPOIFSFileSystem to write the properties into
      * @param writtenEntries a list of POIFS entries to add the property names too
      * 
      * @throws IOException if an error when writing to the 
      *      {@link POIFSFileSystem} occurs
      */
-    protected void writeProperties(POIFSFileSystem outFS, List<String> writtenEntries) throws IOException {
+    @Override
+    protected void writeProperties(NPOIFSFileSystem outFS, List<String> writtenEntries) throws IOException {
         super.writeProperties(outFS, writtenEntries);
         DocumentEncryptionAtom dea = getDocumentEncryptionAtom();
         if (dea != null) {
index 0ed4357eec53d98a75b4836d68e9e81314b9d405..e34103a8ce1856a1c102e2c5e5b375dde059c8ac 100644 (file)
@@ -30,21 +30,18 @@ import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException;
 import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.DocumentEntry;
+import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.LittleEndian;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.util.StringUtil;
 
-
 /**
- * This is a special kind of Atom, becauase it doesn't live inside the
- *  PowerPoint document. Instead, it lives in a seperate stream in the
- *  document. As such, it has to be treaded specially
- *
- * @author Nick Burch
+ * This is a special kind of Atom, because it doesn't live inside the
+ *  PowerPoint document. Instead, it lives in a separate stream in the
+ *  document. As such, it has to be treated specially
  */
-
 public class CurrentUserAtom
 {
        private static POILogger logger = POILogFactory.getLogger(CurrentUserAtom.class);
@@ -117,6 +114,7 @@ public class CurrentUserAtom
 
        /** 
         * Find the Current User in the filesystem, and create from that
+        * @deprecated Use {@link #CurrentUserAtom(DirectoryNode)} instead
         */
        public CurrentUserAtom(POIFSFileSystem fs) throws IOException {
                this(fs.getRoot());
@@ -278,7 +276,7 @@ public class CurrentUserAtom
        /**
         * Writes ourselves back out to a filesystem
         */
-       public void writeToFS(POIFSFileSystem fs) throws IOException {
+       public void writeToFS(NPOIFSFileSystem fs) throws IOException {
                // Grab contents
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                writeOut(baos);
index 7012887899828f5f9774ae42941deeb24125b717..8aef34b2143989b4a550e7a96362623a2d6f05cb 100644 (file)
@@ -67,10 +67,10 @@ import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.DocumentEntry;
 import org.apache.poi.poifs.filesystem.Entry;
 import org.apache.poi.poifs.filesystem.EntryUtils;
+import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.Internal;
 
-
 /**
  *
  * This class acts as the bucket that we throw all of the Word data structures
@@ -924,7 +924,7 @@ public final class HWPFDocument extends HWPFDocumentCore
     }
 
         // create new document preserving order of entries
-        POIFSFileSystem pfs = new POIFSFileSystem();
+        NPOIFSFileSystem pfs = new NPOIFSFileSystem();
         boolean docWritten = false;
         boolean dataWritten = false;
         boolean objectPoolWritten = false;
index b3f91236ec994854252e85703a3467fbd2b247e7..f09f39f3cdabf17a085df09a17c6180c7c5ae077 100644 (file)
@@ -74,7 +74,7 @@ public final class TestPOIDocumentScratchpad extends TestCase {
 
        public void testWriteProperties() throws Exception {
                // Just check we can write them back out into a filesystem
-               POIFSFileSystem outFS = new POIFSFileSystem();
+               NPOIFSFileSystem outFS = new NPOIFSFileSystem();
                doc.writeProperties(outFS);
 
                // Should now hold them
@@ -86,7 +86,7 @@ public final class TestPOIDocumentScratchpad extends TestCase {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
        // Write them out
-       POIFSFileSystem outFS = new POIFSFileSystem();
+       NPOIFSFileSystem outFS = new NPOIFSFileSystem();
        doc.writeProperties(outFS);
        outFS.writeFilesystem(baos);
 
index 67552588f110168a1d1ca4227ad3902ecef3d706..4ababd6f46018c60e3926ae5da753d2913466982 100644 (file)
@@ -26,6 +26,7 @@ import junit.framework.TestCase;
 
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 
 /**
@@ -72,7 +73,7 @@ public final class TestPOIDocumentMain extends TestCase {
 
        public void testWriteProperties() throws Exception {
                // Just check we can write them back out into a filesystem
-               POIFSFileSystem outFS = new POIFSFileSystem();
+               NPOIFSFileSystem outFS = new NPOIFSFileSystem();
                doc.readProperties();
                doc.writeProperties(outFS);
 
@@ -89,7 +90,7 @@ public final class TestPOIDocumentMain extends TestCase {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
                // Write them out
-               POIFSFileSystem outFS = new POIFSFileSystem();
+               NPOIFSFileSystem outFS = new NPOIFSFileSystem();
                doc.readProperties();
                doc.writeProperties(outFS);
                outFS.writeFilesystem(baos);