}
// pack.originalPackagePath = file.getAbsolutePath();
return pack;
- } catch (InvalidFormatException e) {
- IOUtils.closeQuietly(pack);
- throw e;
- } catch (RuntimeException e) {
+ } catch (InvalidFormatException | RuntimeException e) {
IOUtils.closeQuietly(pack);
throw e;
}
}
pack.originalPackagePath = file.getAbsolutePath();
return pack;
- } catch (InvalidFormatException e) {
- IOUtils.closeQuietly(pack);
- throw e;
- } catch (RuntimeException e) {
+ } catch (InvalidFormatException | RuntimeException e) {
IOUtils.closeQuietly(pack);
throw e;
}
if (pack.partList == null) {
pack.getParts();
}
- } catch (InvalidFormatException e) {
- IOUtils.closeQuietly(pack);
- throw e;
- } catch (RuntimeException e) {
+ } catch (InvalidFormatException | RuntimeException e) {
IOUtils.closeQuietly(pack);
throw e;
}
}
String name = path.substring(path.lastIndexOf(File.separatorChar) + 1);
- FileInputStream is = new FileInputStream(path);
- try {
- addThumbnail(name, is);
- } finally {
- is.close();
- }
+ try (FileInputStream is = new FileInputStream(path)) {
+ addThumbnail(name, is);
+ }
}
/**
* Add a thumbnail to the package. This method is provided to make easier
String contentType = element.getAttribute(CONTENT_TYPE_ATTRIBUTE_NAME);
addOverrideContentType(partName, contentType);
}
- } catch (URISyntaxException urie) {
- throw new InvalidFormatException(urie.getMessage());
- } catch (SAXException e) {
- throw new InvalidFormatException(e.getMessage());
- } catch (IOException e) {
- throw new InvalidFormatException(e.getMessage());
+ } catch (URISyntaxException | IOException | SAXException e) {
+ throw new InvalidFormatException(e.getMessage());
}
- }
+ }
/**
* Save the contents type part.
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException;
-import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.apache.poi.ss.usermodel.Workbook;
private void assertEntityLimitReached(Exception e) throws UnsupportedEncodingException {
ByteArrayOutputStream str = new ByteArrayOutputStream();
- PrintWriter writer = new PrintWriter(new OutputStreamWriter(str, "UTF-8"));
- try {
+ try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(str, "UTF-8"))) {
e.printStackTrace(writer);
- } finally {
- writer.close();
}
String string = new String(str.toByteArray(), "UTF-8");
assertTrue("Had: " + string, string.contains("The parser has encountered more than"));
}
try {
- POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-xmlbomb.xlsx"));
- try {
+ try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-xmlbomb.xlsx"))) {
assertNotNull(extractor);
-
+
try {
extractor.getText();
} catch (IllegalStateException e) {
// expected due to shared strings expansion
}
- } finally {
- extractor.close();
}
} catch (POIXMLException e) {
assertEntityLimitReached(e);
public void testZipEntityExpansionSharedStringTable() throws Exception {
Workbook wb = WorkbookFactory.create(XSSFTestDataSamples.openSamplePackage("poc-shared-strings.xlsx"));
wb.close();
-
- POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"));
- try {
+
+ try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"))) {
assertNotNull(extractor);
try {
} catch (IllegalStateException e) {
// expected due to shared strings expansion
}
- } finally {
- extractor.close();
}
}
boolean before = ExtractorFactory.getThreadPrefersEventExtractors();
ExtractorFactory.setThreadPrefersEventExtractors(true);
try {
- POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"));
- try {
+ try (POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"))) {
assertNotNull(extractor);
-
+
try {
extractor.getText();
} catch (IllegalStateException e) {
// expected due to shared strings expansion
}
- } finally {
- extractor.close();
}
} catch (XmlException e) {
assertEntityLimitReached(e);
try {
pkg.getParts();
fail("Shouldn't work");
- } catch (ODFNotOfficeXmlFileException e) {
- } catch (NotOfficeXmlFileException ne) {}
+ } catch (NotOfficeXmlFileException e) {
+ // expected here
+ }
pkg.close();
assertNotNull(pkg.getZipArchive());
try {
pkg.getParts();
fail("Shouldn't work");
- } catch (ODFNotOfficeXmlFileException e) {
- } catch (NotOfficeXmlFileException ne) {}
+ } catch (NotOfficeXmlFileException e) {
+ // expected here
+ }
pkg.close();
assertNotNull(pkg.getZipArchive());
@Test
public void office2007prettyPrintedRels() throws Exception {
- OPCPackage pkg = OPCPackage.open(testdata.getFile("office2007prettyPrintedRels.docx"), PackageAccess.READ);
- try {
+ try (OPCPackage pkg = OPCPackage.open(testdata.getFile("office2007prettyPrintedRels.docx"), PackageAccess.READ)) {
SignatureConfig sic = new SignatureConfig();
sic.setOpcPackage(pkg);
SignatureInfo si = new SignatureInfo();
si.setSignatureConfig(sic);
boolean isValid = si.verifySignature();
assertTrue(isValid);
- } finally {
- pkg.close();
}
}
};
for (String testFile : testFiles) {
- OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ);
- try {
+ try (OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ)) {
SignatureConfig sic = new SignatureConfig();
sic.setOpcPackage(pkg);
SignatureInfo si = new SignatureInfo();
result.add(sp.getSigner());
}
}
-
+
assertNotNull(result);
- assertEquals("test-file: "+testFile, 1, result.size());
+ assertEquals("test-file: " + testFile, 1, result.size());
X509Certificate signer = result.get(0);
LOG.log(POILogger.DEBUG, "signer: " + signer.getSubjectX500Principal());
-
+
boolean b = si.verifySignature();
- assertTrue("test-file: "+testFile, b);
+ assertTrue("test-file: " + testFile, b);
pkg.revert();
- } finally {
- pkg.close();
}
}
}
@Test
public void getMultiSigners() throws Exception {
String testFile = "hello-world-signed-twice.docx";
- OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ);
- try {
+ try (OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ)) {
SignatureConfig sic = new SignatureConfig();
sic.setOpcPackage(pkg);
SignatureInfo si = new SignatureInfo();
result.add(sp.getSigner());
}
}
-
+
assertNotNull(result);
- assertEquals("test-file: "+testFile, 2, result.size());
+ assertEquals("test-file: " + testFile, 2, result.size());
X509Certificate signer1 = result.get(0);
X509Certificate signer2 = result.get(1);
LOG.log(POILogger.DEBUG, "signer 1: " + signer1.getSubjectX500Principal());
LOG.log(POILogger.DEBUG, "signer 2: " + signer2.getSubjectX500Principal());
-
+
boolean b = si.verifySignature();
- assertTrue("test-file: "+testFile, b);
+ assertTrue("test-file: " + testFile, b);
pkg.revert();
- } finally {
- pkg.close();
}
}
//X509Certificate x509B = x509;
File tpl = copy(testdata.getFile("bug58630.xlsx"));
- OPCPackage pkg = OPCPackage.open(tpl);
- try {
+ try (OPCPackage pkg = OPCPackage.open(tpl)) {
//SignatureConfig signatureConfig = new SignatureConfig();
assertNotNull(pkg);
- } finally {
- pkg.close();
}
}
}
File tmpFile = new File(buildDir, "sigtest"+extension);
- OutputStream fos = new FileOutputStream(tmpFile);
- try {
- InputStream fis = new FileInputStream(input);
- try {
+ try (OutputStream fos = new FileOutputStream(tmpFile)) {
+ try (InputStream fis = new FileInputStream(input)) {
IOUtils.copy(fis, fos);
- } finally {
- fis.close();
}
- } finally {
- fos.close();
}
return tmpFile;
assertEquals(1, tempFiles.size());
File tempFile = tempFiles.get(0);
assertTrue("tempFile exists?", tempFile.exists());
- InputStream stream = new FileInputStream(tempFile);
- try {
+ try (InputStream stream = new FileInputStream(tempFile)) {
byte[] data = IOUtils.toByteArray(stream);
String text = new String(data, UTF_8);
assertFalse(text.contains(sheetName));
assertFalse(text.contains(cellValue));
- } finally {
- stream.close();
}
workbook.dispose();
assertFalse("tempFile deleted after dispose?", tempFile.exists());
Workbook wb1 = _testDataProvider.createWorkbook();
Sheet s = wb1.createSheet("testSheet1");
- Row r = null;
- Cell c = null;
+ Row r;
+ Cell c;
CellStyle cs = wb1.createCellStyle();
Font f = wb1.createFont();
f.setFontHeightInPoints((short) 20);
/**
* bug 58452: Copy cell formulas containing unregistered function names
* Make sure that formulas with unknown/unregistered UDFs can be written to and read back from a file.
- *
- * @throws IOException
*/
@Test
public void testFormulaWithUnknownUDF() throws IOException {
/**
* Cell with the formula that returns error must return error code(There was
* an problem that cell could not return error value form formula cell).
- * @throws IOException
*/
@Test
public void testGetErrorCellValueFromFormulaCell() throws IOException {
- Workbook wb = _testDataProvider.createWorkbook();
- try {
+ try (Workbook wb = _testDataProvider.createWorkbook()) {
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellFormula("SQRT(-1)");
wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(cell);
assertEquals(36, cell.getErrorCellValue());
- } finally {
- wb.close();
}
}
/**
* The maximum length of cell contents (text) is 32,767 characters.
- * @throws IOException
*/
@Test
public void testMaxTextLength() throws IOException{
: SpreadsheetVersion.EXCEL2007.getMaxTextLength();
assertEquals(32767, maxlen);
- StringBuffer b = new StringBuffer() ;
+ StringBuilder b = new StringBuilder() ;
// 32767 is okay
for( int i = 0 ; i < maxlen ; i++ )