<include name="xmlsec-2.0.1.jar"/>
<include name="xmlsec-2.0.5.jar"/>
<include name="bcprov-ext-jdk15on-1.51.jar"/>
+ <include name="bcprov-ext-jdk15on-1.53.jar"/>
<include name="bcpkix-jdk15on-1.51.jar"/>
+ <include name="bcpkix-jdk15on-1.53.jar"/>
<include name="slf4j-api-1.7.7.jar"/>
</fileset>
</delete>
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.NotOLE2FileException;
import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.util.IOUtils;
/**
* A text extractor for old Excel files, which are too old for
*/
public class OldExcelExtractor implements Closeable {
private RecordInputStream ris;
+
+ // sometimes we hold the stream here and thus need to ensure it is closed at some point
+ private Closeable toClose;
+
private int biffVersion;
private int fileType;
try {
poifs = new NPOIFSFileSystem(f);
open(poifs);
+ toClose = poifs;
return;
} catch (OldExcelFormatException e) {
// will be handled by workaround below
// is thrown while opening
biffStream.close();
throw e;
+ } catch (RuntimeException e) {
+ // ensure that the stream is properly closed here if an Exception
+ // is thrown while opening
+ biffStream.close();
+ throw e;
}
}
}
} else {
ris = new RecordInputStream(bis);
+ toClose = bis;
prepare();
}
}
@Override
public void close() {
- // not necessary any more ...
+ // some cases require this close here
+ if(toClose != null) {
+ IOUtils.closeQuietly(toClose);
+ toClose = null;
+ }
}
-
+
protected void handleNumericCell(StringBuffer text, double value) {
// TODO Need to fetch / use format strings
text.append(value);
}
OPCPackage pack = new ZipPackage(file, access);
- if (pack.partList == null && access != PackageAccess.WRITE) {
- pack.getParts();
- }
- pack.originalPackagePath = file.getAbsolutePath();
- return pack;
+ try {
+ if (pack.partList == null && access != PackageAccess.WRITE) {
+ pack.getParts();
+ }
+ pack.originalPackagePath = file.getAbsolutePath();
+ return pack;
+ } catch (InvalidFormatException e) {
+ try {
+ pack.close();
+ } catch (IOException e1) {
+ throw new IllegalStateException(e);
+ }
+ throw e;
+ } catch (RuntimeException e) {
+ try {
+ pack.close();
+ } catch (IOException e1) {
+ throw new IllegalStateException(e);
+ }
+ throw e;
+ }
}
/**
// OLE2 - Stream
try {
- OPCPackage.open(files.openResourceAsStream("SampleSS.xls"));
+ InputStream stream = files.openResourceAsStream("SampleSS.xls");
+ try {
+ OPCPackage.open(stream);
+ } finally {
+ stream.close();
+ }
fail("Shouldn't be able to open OLE2");
} catch (OLE2NotOfficeXmlFileException e) {
assertTrue(e.getMessage().contains("The supplied data appears to be in the OLE2 Format"));
// Raw XML - Stream
try {
- OPCPackage.open(files.openResourceAsStream("SampleSS.xml"));
+ InputStream stream = files.openResourceAsStream("SampleSS.xml");
+ try {
+ OPCPackage.open(stream);
+ } finally {
+ stream.close();
+ }
fail("Shouldn't be able to open XML");
} catch (NotOfficeXmlFileException e) {
assertTrue(e.getMessage().contains("The supplied data appears to be a raw XML file"));
// ODF / ODS - Stream
try {
- OPCPackage.open(files.openResourceAsStream("SampleSS.ods"));
+ InputStream stream = files.openResourceAsStream("SampleSS.ods");
+ try {
+ OPCPackage.open(stream);
+ } finally {
+ stream.close();
+ }
fail("Shouldn't be able to open ODS");
} catch (ODFNotOfficeXmlFileException e) {
assertTrue(e.toString().contains("The supplied data appears to be in ODF"));
// Plain Text - Stream
try {
- OPCPackage.open(files.openResourceAsStream("SampleSS.txt"));
+ InputStream stream = files.openResourceAsStream("SampleSS.txt");
+ try {
+ OPCPackage.open(stream);
+ } finally {
+ stream.close();
+ }
fail("Shouldn't be able to open Plain Text");
} catch (NotOfficeXmlFileException e) {
assertTrue(e.getMessage().contains("No valid entries or contents found"));