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
This commit is contained in:
Dominik Stadler 2015-05-26 19:30:04 +00:00
parent 2f87b078e9
commit 9017803980
3 changed files with 12 additions and 18 deletions

View File

@ -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();

View File

@ -1548,16 +1548,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
out.close();
}
/**
* 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
*

View File

@ -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 {