]> source.dussan.org Git - poi.git/commitdiff
Add File based constructor to OPCPackage, alongside existing String one (which constr...
authorNick Burch <nick@apache.org>
Tue, 19 Jun 2012 22:50:14 +0000 (22:50 +0000)
committerNick Burch <nick@apache.org>
Tue, 19 Jun 2012 22:50:14 +0000 (22:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1351894 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java
src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java
src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java

index e605f9ee6c2132ba5397f3d7890d4a24b3fec878..7ef684155fd25e1352ceaf511ca7851349abbb8b 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.9-beta1" date="2012-??-??">
+          <action dev="poi-developers" type="add">Add File based constructor to OPCPackage, alongside existing String one (which constructed a File from the string internally)</action>
           <action dev="poi-developers" type="fix">53389 - Handle formatting General and @ formats even if a locale is prefixed to them</action>
           <action dev="poi-developers" type="fix">53271 - Removed unconditional asserts in SXSSF</action>
           <action dev="poi-developers" type="add">53025 - Updatad documentation and example on using Data Validations  </action>
index 7a0da815b7c5cfd7eb87936d8df0714402b7cf0a..26c17c54049c034e20247f90bc4f1abdf6861a8b 100644 (file)
@@ -186,6 +186,20 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
                return open(path, defaultPackageAccess);
        }
 
+   /**
+    * Open a package with read/write permission.
+    *
+    * @param file
+    *            The file to open.
+    * @return A Package object, else <b>null</b>.
+    * @throws InvalidFormatException
+    *             If the specified file doesn't exist, and a parsing error
+    *             occur.
+    */
+   public static OPCPackage open(File file) throws InvalidFormatException {
+      return open(file, defaultPackageAccess);
+   }
+
        /**
         * Open a package.
         *
@@ -212,6 +226,31 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
                return pack;
        }
 
+   /**
+    * Open a package.
+    *
+    * @param file
+    *            The file to open.
+    * @param access
+    *            PackageBase access.
+    * @return A PackageBase object, else <b>null</b>.
+    * @throws InvalidFormatException
+    *             If the specified file doesn't exist, and a parsing error
+    *             occur.
+    */
+   public static OPCPackage open(File file, PackageAccess access)
+         throws InvalidFormatException {
+      if (file == null|| (file.exists() && file.isDirectory()))
+         throw new IllegalArgumentException("file");
+
+      OPCPackage pack = new ZipPackage(file, access);
+      if (pack.partList == null && access != PackageAccess.WRITE) {
+         pack.getParts();
+      }
+      pack.originalPackagePath = file.getAbsolutePath();
+      return pack;
+   }
+
        /**
         * Open a package.
         *
index 5ac16d3a0b4fa14188561ada04bae6fd695ee1aa..bf98cdd29b32ca678e109f7bed286b080ddcd46c 100644 (file)
@@ -85,30 +85,55 @@ public final class ZipPackage extends Package {
                );
        }
 
-       /**
-        * Constructor. Opens a Zip based Open XML document.
-        *
-        * @param path
-        *            The path of the file to open or create.
-        * @param access
-        *            The package access mode.
-        * @throws InvalidFormatException
-        *             If the content type part parsing encounters an error.
-        */
-       ZipPackage(String path, PackageAccess access) {
-               super(access);
-
-        ZipFile zipFile = null;
-
-        try {
-            zipFile = ZipHelper.openZipFile(path);
-        } catch (IOException e) {
-            throw new InvalidOperationException(
-                                               "Can't open the specified file: '" + path + "'", e);
-        }
-
-               this.zipArchive = new ZipFileZipEntrySource(zipFile);
-       }
+   /**
+    * Constructor. Opens a Zip based Open XML document.
+    *
+    * @param path
+    *            The path of the file to open or create.
+    * @param access
+    *            The package access mode.
+    * @throws InvalidFormatException
+    *             If the content type part parsing encounters an error.
+    */
+   ZipPackage(String path, PackageAccess access) {
+      super(access);
+
+      ZipFile zipFile = null;
+
+      try {
+         zipFile = ZipHelper.openZipFile(path);
+      } catch (IOException e) {
+         throw new InvalidOperationException(
+               "Can't open the specified file: '" + path + "'", e);
+      }
+
+      this.zipArchive = new ZipFileZipEntrySource(zipFile);
+   }
+
+   /**
+    * Constructor. Opens a Zip based Open XML document.
+    *
+    * @param file
+    *            The file to open or create.
+    * @param access
+    *            The package access mode.
+    * @throws InvalidFormatException
+    *             If the content type part parsing encounters an error.
+    */
+   ZipPackage(File file, PackageAccess access) {
+      super(access);
+
+      ZipFile zipFile = null;
+
+      try {
+         zipFile = ZipHelper.openZipFile(file);
+      } catch (IOException e) {
+         throw new InvalidOperationException(
+               "Can't open the specified file: '" + file + "'", e);
+      }
+
+      this.zipArchive = new ZipFileZipEntrySource(zipFile);
+   }
 
        /**
         * Retrieves the parts from this package. We assume that the package has not
index e808dc61e9f2dcbf6fe651c6b0adb8f0e57e6b46..9598b05cbec3a30c416a3bcae62a5d30814d899b 100644 (file)
@@ -72,11 +72,12 @@ public final class ZipHelper {
         * Retrieve the Zip entry of the content types part.
         */
        public static ZipEntry getContentTypeZipEntry(ZipPackage pkg) {
-               Enumeration entries = pkg.getZipArchive().getEntries();
+               Enumeration<? extends ZipEntry> entries = pkg.getZipArchive().getEntries();
+               
                // Enumerate through the Zip entries until we find the one named
                // '[Content_Types].xml'.
                while (entries.hasMoreElements()) {
-                       ZipEntry entry = (ZipEntry) entries.nextElement();
+                       ZipEntry entry = entries.nextElement();
                        if (entry.getName().equals(
                                        ContentTypeManager.CONTENT_TYPES_PART_NAME))
                                return entry;
@@ -141,6 +142,21 @@ public final class ZipHelper {
                }
        }
 
+   /**
+    * Opens the specified file as a zip, or returns null if no such file exists
+    *
+    * @param file
+    *            The file to open.
+    * @return The zip archive freshly open.
+    */
+   public static ZipFile openZipFile(File file) throws IOException {
+      if (!file.exists()) {
+         return null;
+      }
+
+      return new ZipFile(file);
+   }
+
        /**
         * Retrieve and open a zip file with the specified path.
         *
index 41f70d92bbe0078271823e7746562a5fd5438ebe..56a9f890283ef939a29b69d404161d2887e4b215 100644 (file)
@@ -87,7 +87,7 @@ public class WorkbookFactory {
              NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
              return new HSSFWorkbook(fs.getRoot(), true);
           } catch(OfficeXmlFileException e) {
-             OPCPackage pkg = OPCPackage.openOrCreate(file);
+             OPCPackage pkg = OPCPackage.open(file);
              return new XSSFWorkbook(pkg);
           }
        }