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 2014/04/28 16:19:13 UTC

svn commit: r1590650 - in /poi/trunk/src: examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java java/org/apache/poi/hpsf/PropertySetFactory.java

Author: nick
Date: Mon Apr 28 14:19:13 2014
New Revision: 1590650

URL: http://svn.apache.org/r1590650
Log:
Provide a convenience method for creating a PropertySet from a Directory + Entry

Modified:
    poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java
    poi/trunk/src/java/org/apache/poi/hpsf/PropertySetFactory.java

Modified: poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java?rev=1590650&r1=1590649&r2=1590650&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java Mon Apr 28 14:19:13 2014
@@ -26,14 +26,11 @@ import org.apache.poi.hpsf.CustomPropert
 import org.apache.poi.hpsf.DocumentSummaryInformation;
 import org.apache.poi.hpsf.MarkUnsupportedException;
 import org.apache.poi.hpsf.NoPropertySetStreamException;
-import org.apache.poi.hpsf.PropertySet;
 import org.apache.poi.hpsf.PropertySetFactory;
 import org.apache.poi.hpsf.SummaryInformation;
 import org.apache.poi.hpsf.UnexpectedPropertySetTypeException;
 import org.apache.poi.hpsf.WritingNotSupportedException;
 import org.apache.poi.poifs.filesystem.DirectoryEntry;
-import org.apache.poi.poifs.filesystem.DocumentEntry;
-import org.apache.poi.poifs.filesystem.DocumentInputStream;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 
 /**
@@ -105,17 +102,12 @@ public class ModifyDocumentSummaryInform
         SummaryInformation si;
         try
         {
-            DocumentEntry siEntry = (DocumentEntry)
-                dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
-            DocumentInputStream dis = new DocumentInputStream(siEntry);
-            PropertySet ps = new PropertySet(dis);
-            dis.close();
-            si = new SummaryInformation(ps);
+            si = (SummaryInformation)PropertySetFactory.create(
+                    dir, SummaryInformation.DEFAULT_STREAM_NAME);
         }
         catch (FileNotFoundException ex)
         {
-            /* There is no summary information yet. We have to create a new
-             * one. */
+            // There is no summary information yet. We have to create a new one
             si = PropertySetFactory.newSummaryInformation();
         }
 
@@ -133,12 +125,8 @@ public class ModifyDocumentSummaryInform
         DocumentSummaryInformation dsi;
         try
         {
-            DocumentEntry dsiEntry = (DocumentEntry)
-                dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
-            DocumentInputStream dis = new DocumentInputStream(dsiEntry);
-            PropertySet ps = new PropertySet(dis);
-            dis.close();
-            dsi = new DocumentSummaryInformation(ps);
+            dsi = (DocumentSummaryInformation)PropertySetFactory.create(
+                    dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
         }
         catch (FileNotFoundException ex)
         {

Modified: poi/trunk/src/java/org/apache/poi/hpsf/PropertySetFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hpsf/PropertySetFactory.java?rev=1590650&r1=1590649&r2=1590650&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hpsf/PropertySetFactory.java (original)
+++ poi/trunk/src/java/org/apache/poi/hpsf/PropertySetFactory.java Mon Apr 28 14:19:13 2014
@@ -17,21 +17,55 @@
 
 package org.apache.poi.hpsf;
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 
 import org.apache.poi.hpsf.wellknown.SectionIDMap;
+import org.apache.poi.poifs.filesystem.DirectoryEntry;
+import org.apache.poi.poifs.filesystem.DocumentEntry;
+import org.apache.poi.poifs.filesystem.DocumentInputStream;
 
 /**
  * <p>Factory class to create instances of {@link SummaryInformation},
  * {@link DocumentSummaryInformation} and {@link PropertySet}.</p>
- *
- * @author Rainer Klute <a
- * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  */
 public class PropertySetFactory
 {
+    /**
+     * <p>Creates the most specific {@link PropertySet} from an entry
+     *  in the specified POIFS Directory. This is preferrably a {@link
+     * DocumentSummaryInformation} or a {@link SummaryInformation}. If
+     * the specified entry does not contain a property set stream, an 
+     * exception is thrown. If no entry is found with the given name,
+     * an exception is thrown.</p>
+     *
+     * @param dir The directory to find the PropertySet in
+     * @param name The name of the entry containing the PropertySet
+     * @return The created {@link PropertySet}.
+     * @throws FileNotFoundException if there is no entry with that name
+     * @throws NoPropertySetStreamException if the stream does not
+     * contain a property set.
+     * @throws IOException if some I/O problem occurs.
+     * @exception UnsupportedEncodingException if the specified codepage is not
+     * supported.
+     */
+    public static PropertySet create(final DirectoryEntry dir, final String name)
+        throws FileNotFoundException, NoPropertySetStreamException,
+               IOException, UnsupportedEncodingException
+    {
+        InputStream inp = null;
+        try {
+            DocumentEntry entry = (DocumentEntry)dir.getEntry(name);
+            inp = new DocumentInputStream(entry);
+            try {
+                return create(inp);
+            } catch (MarkUnsupportedException e) { return null; }
+        } finally {
+            if (inp != null) inp.close();
+        }
+    }
 
     /**
      * <p>Creates the most specific {@link PropertySet} from an {@link
@@ -73,8 +107,6 @@ public class PropertySetFactory
         }
     }
 
-
-
     /**
      * <p>Creates a new summary information.</p>
      *
@@ -96,8 +128,6 @@ public class PropertySetFactory
         }
     }
 
-
-
     /**
      * <p>Creates a new document summary information.</p>
      *
@@ -118,5 +148,4 @@ public class PropertySetFactory
             throw new HPSFRuntimeException(ex);
         }
     }
-
-}
+}
\ No newline at end of file



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