diff options
author | Dominik Stadler <centic@apache.org> | 2015-05-26 19:30:04 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2015-05-26 19:30:04 +0000 |
commit | e7957fc6b8417b9b6a094c164c24f69b67d0ce2b (patch) | |
tree | ff264378f5bbdd9ff66bb2f30074cef4fdb67245 /src | |
parent | 2d71e80930e4b3effc33580c8aff5734c5eab181 (diff) | |
download | poi-e7957fc6b8417b9b6a094c164c24f69b67d0ce2b.tar.gz poi-e7957fc6b8417b9b6a094c164c24f69b67d0ce2b.zip |
Make POIXMLDocument implement Closeable as it holds an OCPPackage with open resources and thus should be closed after usage. Until now only XSSFWorkbook did this, but it makes sense for all derived classes.
Also make close() in POIXMLDocument public to not have to re-implement it in all sub-classes.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1681822 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
3 files changed, 12 insertions, 18 deletions
diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocument.java b/src/ooxml/java/org/apache/poi/POIXMLDocument.java index bf241f897b..0e18ee1121 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocument.java +++ b/src/ooxml/java/org/apache/poi/POIXMLDocument.java @@ -16,6 +16,7 @@ ==================================================================== */ package org.apache.poi; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -37,7 +38,7 @@ import org.apache.poi.poifs.common.POIFSConstants; import org.apache.poi.util.IOUtils; import org.apache.xmlbeans.impl.common.SystemCache; -public abstract class POIXMLDocument extends POIXMLDocumentPart{ +public abstract class POIXMLDocument extends POIXMLDocumentPart implements Closeable { public static final String DOCUMENT_CREATOR = "Apache POI"; // OLE embeddings relation name @@ -171,7 +172,7 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart{ * Closes the underlying {@link OPCPackage} from which this * document was read, if there is one */ - protected void close() throws IOException { + public void close() throws IOException { if (pkg != null) { if (pkg.getPackageAccess() == PackageAccess.READ) { pkg.revert(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 9230797e41..9406fb17c7 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -1549,16 +1549,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X } /** - * Closes the underlying {@link OPCPackage} from which - * the Workbook was read, if any. Has no effect on newly - * created Workbooks. - */ - @Override - public void close() throws IOException { - super.close(); - } - - /** * Returns SharedStringsTable - tha cache of string for this workbook * * @return the shared string table diff --git a/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java b/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java index f4e39ab658..55f4093fb6 100644 --- a/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java +++ b/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java @@ -155,13 +155,16 @@ public final class TestPOIXMLDocument extends TestCase { public void testRelationOrder() throws Exception { OPCPackage pkg = PackageHelper.open(POIDataSamples.getDocumentInstance().openResourceAsStream("WordWithAttachments.docx")); OPCParser doc = new OPCParser(pkg); - doc.parse(new TestFactory()); - - for(POIXMLDocumentPart rel : doc.getRelations()){ - //TODO finish me - assertNotNull(rel); + try { + doc.parse(new TestFactory()); + + for(POIXMLDocumentPart rel : doc.getRelations()){ + //TODO finish me + assertNotNull(rel); + } + } finally { + doc.close(); } - } public void testCommitNullPart() throws IOException, InvalidFormatException { |