aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2015-04-29 20:19:38 +0000
committerNick Burch <nick@apache.org>2015-04-29 20:19:38 +0000
commit37f045dc022338766686bc14d6fe4f467225de36 (patch)
treec4fe13c6a4c3ddf47af57e0b92d759254477bd80
parent5cb5ab6807e8f1d5e0e1427fe8af6add70e7e13d (diff)
downloadpoi-37f045dc022338766686bc14d6fe4f467225de36.tar.gz
poi-37f045dc022338766686bc14d6fe4f467225de36.zip
#57593 Complete create overloading in WorkbookFactory to take passwords
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1676847 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java23
-rw-r--r--src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java60
2 files changed, 77 insertions, 6 deletions
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 b4f341533d..b6ec1cf8ab 100644
--- a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
+++ b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
@@ -121,7 +121,9 @@ public class WorkbookFactory {
* using an {@link InputStream} has a higher memory footprint
* than using a {@link File}.</p>
* <p>Note that in order to properly release resources the
- * Workbook should be closed after use.
+ * Workbook should be closed after use. Note also that loading
+ * from an InputStream requires more memory than loading
+ * from a File, so prefer {@link #create(File)} where possible.
* @throws EncryptedDocumentException If the workbook given is password protected
*/
public static Workbook create(InputStream inp) throws IOException, InvalidFormatException, EncryptedDocumentException {
@@ -136,7 +138,9 @@ public class WorkbookFactory {
* using an {@link InputStream} has a higher memory footprint
* than using a {@link File}.</p>
* <p>Note that in order to properly release resources the
- * Workbook should be closed after use.
+ * Workbook should be closed after use. Note also that loading
+ * from an InputStream requires more memory than loading
+ * from a File, so prefer {@link #create(File)} where possible.
* @throws EncryptedDocumentException If the wrong password is given for a protected file
*/
public static Workbook create(InputStream inp, String password) throws IOException, InvalidFormatException, EncryptedDocumentException {
@@ -155,7 +159,6 @@ public class WorkbookFactory {
throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}
- // TODO file+password
/**
* Creates the appropriate HSSFWorkbook / XSSFWorkbook from
* the given File, which must exist and be readable.
@@ -164,14 +167,24 @@ public class WorkbookFactory {
* @throws EncryptedDocumentException If the workbook given is password protected
*/
public static Workbook create(File file) throws IOException, InvalidFormatException, EncryptedDocumentException {
+ return create(file, null);
+ }
+ /**
+ * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
+ * the given File, which must exist and be readable, and
+ * may be password protected
+ * <p>Note that in order to properly release resources the
+ * Workbook should be closed after use.
+ * @throws EncryptedDocumentException If the wrong password is given for a protected file
+ */
+ public static Workbook create(File file, String password) throws IOException, InvalidFormatException, EncryptedDocumentException {
if (! file.exists()) {
throw new FileNotFoundException(file.toString());
}
try {
- @SuppressWarnings("resource")
NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
- return new HSSFWorkbook(fs.getRoot(), true);
+ return create(fs, password);
} catch(OfficeXmlFileException e) {
// opening as .xls failed => try opening as .xlsx
OPCPackage pkg = OPCPackage.open(file);
diff --git a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java
index 068a70910b..582c8c3646 100644
--- a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java
+++ b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java
@@ -194,6 +194,64 @@ public final class TestWorkbookFactory extends TestCase {
public void testCreateWithPasswordFromFile() throws Exception {
Workbook wb;
- // TODO
+ // Unprotected, no password given, opens normally
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xls), null
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof HSSFWorkbook);
+ wb.close();
+
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xlsx), null
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof XSSFWorkbook);
+
+
+ // Unprotected, wrong password, opens normally
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xls), "wrong"
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof HSSFWorkbook);
+ wb.close();
+
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xlsx), "wrong"
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof XSSFWorkbook);
+
+
+ // Protected, correct password, opens fine
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xls_prot[0]), xls_prot[1]
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof HSSFWorkbook);
+ wb.close();
+
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), xlsx_prot[1]
+ );
+ assertNotNull(wb);
+ assertTrue(wb instanceof XSSFWorkbook);
+
+
+ // Protected, wrong password, throws Exception
+ try {
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xls_prot[0]), "wrong"
+ );
+ fail("Shouldn't be able to open with the wrong password");
+ } catch (EncryptedDocumentException e) {}
+
+ try {
+ wb = WorkbookFactory.create(
+ HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), "wrong"
+ );
+ fail("Shouldn't be able to open with the wrong password");
+ } catch (EncryptedDocumentException e) {}
}
}