aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2011-09-29 19:39:51 +0000
committerNick Burch <nick@apache.org>2011-09-29 19:39:51 +0000
commitca6ff46842d21a6051ea29f1b7d6142aa83ad9ac (patch)
treefc53154e8873717f05d6677ee3edc30addcd8e7e /src
parent0e75f256c361402a486c79e57a0323d6c7f92433 (diff)
downloadpoi-ca6ff46842d21a6051ea29f1b7d6142aa83ad9ac.tar.gz
poi-ca6ff46842d21a6051ea29f1b7d6142aa83ad9ac.zip
Allow the passing of a File object to WorkbookFactory.create, which permits lower memory processing than the InputStream version
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1177409 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r--src/documentation/content/xdocs/status.xml1
-rw-r--r--src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java21
-rw-r--r--src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java14
-rw-r--r--src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java2
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java4
5 files changed, 39 insertions, 3 deletions
diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml
index e0ba0b061a..e976ca7f1f 100644
--- a/src/documentation/content/xdocs/status.xml
+++ b/src/documentation/content/xdocs/status.xml
@@ -34,6 +34,7 @@
<changes>
<release version="3.8-beta5" date="2011-??-??">
+ <action dev="poi-developers" type="add">Allow the passing of a File object to WorkbookFactory.create, which permits lower memory processing than the InputStream version</action>
<action dev="poi-developers" type="fix">51873 - update HSMF to ignore Outlook 2002 Olk10SideProp entries, which don't behave like normal chunks</action>
<action dev="poi-developers" type="fix">51850 - support creating comments in XSSF on an earlier slide when later ones already have them</action>
<action dev="poi-developers" type="add">51804 - optionally include Master Slide text in XSLF text extraction, as HSLF already offers</action>
diff --git a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
index a3ce259e54..6b86e4de7d 100644
--- a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
+++ b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
@@ -16,6 +16,8 @@
==================================================================== */
package org.apache.poi.ss.usermodel;
+import java.io.File;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
@@ -24,6 +26,8 @@ import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
+import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@@ -64,4 +68,21 @@ public class WorkbookFactory {
}
throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}
+ /**
+ * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
+ * the given File, which must exist and be readable.
+ */
+ public static Workbook create(File file) throws IOException, InvalidFormatException {
+ if(! file.exists()) {
+ throw new FileNotFoundException(file.toString());
+ }
+
+ try {
+ NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
+ return new HSSFWorkbook(fs.getRoot(), true);
+ } catch(OfficeXmlFileException e) {
+ OPCPackage pkg = OPCPackage.openOrCreate(file);
+ return new XSSFWorkbook(pkg);
+ }
+ }
}
diff --git a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java
index 6858e93acb..9dee1a5c01 100644
--- a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java
+++ b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java
@@ -77,7 +77,21 @@ public final class TestWorkbookFactory extends TestCase {
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
+
+ // File -> either
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xls)
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof HSSFWorkbook);
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xlsx)
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof XSSFWorkbook);
+
+ // Invalid type -> exception
try {
wb = WorkbookFactory.create(
HSSFTestDataSamples.openSampleFileStream(txt)
diff --git a/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java b/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java
index 46e4ec55ef..f684676e91 100644
--- a/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java
+++ b/src/testcases/org/apache/poi/hssf/HSSFTestDataSamples.java
@@ -38,7 +38,7 @@ public final class HSSFTestDataSamples {
public static InputStream openSampleFileStream(String sampleFileName) {
return _inst.openResourceAsStream(sampleFileName);
}
- public static File getSampeFile(String sampleFileName) {
+ public static File getSampleFile(String sampleFileName) {
return _inst.getFile(sampleFileName);
}
public static byte[] getTestDataFileContent(String fileName) {
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
index 68ec3b2260..68462a2e2f 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
@@ -540,7 +540,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
// Open the two filesystems
DirectoryNode[] files = new DirectoryNode[2];
files[0] = (new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("Simple.xls"))).getRoot();
- files[1] = (new NPOIFSFileSystem(HSSFTestDataSamples.getSampeFile("Simple.xls"))).getRoot();
+ files[1] = (new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("Simple.xls"))).getRoot();
// Open without preserving nodes
for(DirectoryNode dir : files) {
@@ -563,7 +563,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
// Open the two filesystems
DirectoryNode[] files = new DirectoryNode[2];
files[0] = (new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"))).getRoot();
- files[1] = (new NPOIFSFileSystem(HSSFTestDataSamples.getSampeFile("WithEmbeddedObjects.xls"))).getRoot();
+ files[1] = (new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("WithEmbeddedObjects.xls"))).getRoot();
// Check the embedded parts
for(DirectoryNode root : files) {