public static class NullFileHandler implements FileHandler {
@Override
- public void handleFile(InputStream stream, String path) throws Exception {
+ public void handleFile(InputStream stream, String path) {
}
@Override
- public void handleExtracting(File file) throws Exception {
+ public void handleExtracting(File file) {
}
@Override
- public void handleAdditional(File file) throws Exception {
+ public void handleAdditional(File file) {
}
}
}
short sid;
try {
sid = recCls.getField("RECORD_ID").getShort(null);
- } catch (IllegalArgumentException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- } catch (NoSuchFieldException e) {
+ } catch (IllegalArgumentException | NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
Constructor<? extends EscherRecord> constructor;
@Override
public String getText() {
- StringBuffer text = new StringBuffer();
+ StringBuilder text = new StringBuilder();
// We don't care about the difference between
// null (missing) and blank cells
// Replace any newlines with spaces, otherwise it
// breaks the output
String commentText = comment.getString().getString().replace('\n', ' ');
- text.append(" Comment by "+comment.getAuthor()+": "+commentText);
+ text.append(" Comment by ").append(comment.getAuthor()).append(": ").append(commentText);
}
}
}
public static String _extractHeaderFooter(HeaderFooter hf) {
- StringBuffer text = new StringBuffer();
+ StringBuilder text = new StringBuilder();
if(hf.getLeft() != null) {
text.append(hf.getLeft());
Object[] args = { in, };
try {
return _c.newInstance(args);
- } catch (IllegalArgumentException e) {
- throw new RuntimeException(e);
- } catch (InstantiationException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
+ } catch (IllegalArgumentException | IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
Object[] args = { in, };
try {
return (Record) _m.invoke(null, args);
- } catch (IllegalArgumentException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
+ } catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new org.apache.poi.util.RecordFormatException("Unable to construct record instance" , e.getTargetException());
if(isExternalReferences()) {
sb.append("External References]\n");
- sb.append(" .url = ").append(field_2_encoded_url).append("\n");
+ sb.append(" .url = ").append(getURL()).append("\n");
sb.append(" .nSheets = ").append(field_1_number_of_sheets).append("\n");
for (String sheetname : field_3_sheet_names) {
sb.append(" .name = ").append(sheetname).append("\n");
package org.apache.poi.hssf.record.aggregates;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
}
public final String toString() {
- StringBuffer sb = new StringBuffer(64);
- sb.append(getClass().getName()).append(" [");
- sb.append(_sfr.getRange());
- sb.append("]");
- return sb.toString();
+ return getClass().getName() + " [" +
+ _sfr.getRange() +
+ "]";
}
}
*/
private static <Z> List<Z> toList(Z[] zz) {
List<Z> result = new ArrayList<>(zz.length);
- for (int i = 0; i < zz.length; i++) {
- result.add(zz[i]);
- }
+ Collections.addAll(result, zz);
return result;
}
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
private static boolean isDash(String codes) {
if(codes.length() == 1) {
- switch (codes.charAt(0)) {
- case '-':
- return true;
+ if (codes.charAt(0) == '-') {
+ return true;
}
}
return false;
}
public final String toString() {
- StringBuilder sb = new StringBuilder(64);
- sb.append(getClass().getName()).append(" [");
- sb.append(lookupName(_functionIndex));
- sb.append(" nArgs=").append(_numberOfArgs);
- sb.append("]");
- return sb.toString();
+ return getClass().getName() + " [" + lookupName(_functionIndex) + " nArgs=" + _numberOfArgs + "]";
}
public final short getFunctionIndex() {
XSSFSheet sheet = null;
try {
sheet = getWorkbook().getSheetAt(0);
- } catch (InvalidFormatException ife) {
- } catch (IOException ioe) {
+ } catch (InvalidFormatException | IOException ife) {
}
return sheet;
}
chartCopy.importContent(srcChart);
chartCopy.setWorkbook(srcChart.getWorkbook());
c.setAttributeText(idQualifiedName, slide.getRelationId(chartCopy));
- } catch (InvalidFormatException e) {
- throw new POIXMLException(e);
- } catch (IOException e) {
+ } catch (InvalidFormatException | IOException e) {
throw new POIXMLException(e);
}
c.dispose();
XMLReader xmlReader;
try {
xmlReader = SAXHelper.newXMLReader();
- } catch (ParserConfigurationException e) {
- throw new POIXMLException(e);
- } catch (SAXException e) {
+ } catch (ParserConfigurationException | SAXException e) {
throw new POIXMLException(e);
}
xmlReader.setContentHandler(xmlSheetRefReader);
docPr.setName("chart " + id);
return inline;
- } catch (XmlException e) {
- throw new IllegalStateException(e);
- } catch (SAXException e) {
+ } catch (XmlException | SAXException e) {
throw new IllegalStateException(e);
}
}
* fixed, so that they are then run automatically.
*/
public final class TestUnfixedBugs {
- @Test
- public void testBug54084Unicode() throws IOException {
- // sample XLSX with the same text-contents as the text-file above
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("54084 - Greek - beyond BMP.xlsx");
-
- verifyBug54084Unicode(wb);
-
- //XSSFTestDataSamples.writeOut(wb, "bug 54084 for manual review");
-
- // now write the file and read it back in
- XSSFWorkbook wbWritten = XSSFTestDataSamples.writeOutAndReadBack(wb);
- verifyBug54084Unicode(wbWritten);
-
- // finally also write it out via the streaming interface and verify that we still can read it back in
- SXSSFWorkbook swb = new SXSSFWorkbook(wb);
- Workbook wbStreamingWritten = SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
- verifyBug54084Unicode(wbStreamingWritten);
-
- wbWritten.close();
- swb.close();
- wbStreamingWritten.close();
- wb.close();
- }
-
- private void verifyBug54084Unicode(Workbook wb) {
- // expected data is stored in UTF-8 in a text-file
- byte[] data = HSSFTestDataSamples.getTestDataFileContent("54084 - Greek - beyond BMP.txt");
- String testData = new String(data, StandardCharsets.UTF_8).trim();
-
- Sheet sheet = wb.getSheetAt(0);
- Row row = sheet.getRow(0);
- Cell cell = row.getCell(0);
-
- String value = cell.getStringCellValue();
- //System.out.println(value);
-
- assertEquals("The data in the text-file should exactly match the data that we read from the workbook", testData, value);
- }
-
@Test
public void test54071() throws Exception {
Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("54071.xlsx");
@Test
public void testBug55752() throws IOException {
- Workbook wb = new XSSFWorkbook();
- try {
+ try (Workbook wb = new XSSFWorkbook()) {
Sheet sheet = wb.createSheet("test");
for (int i = 0; i < 4; i++) {
// write to file for manual inspection
XSSFTestDataSamples.writeOut(wb, "bug 55752 for review");
- } finally {
- wb.close();
}
}
}
// verify that the resulting XML has the rows in correct order as required by Excel
- String xml = new String(stream.toByteArray(), "UTF-8");
+ String xml = new String(stream.toByteArray(), StandardCharsets.UTF_8);
int posR12 = xml.indexOf("<row r=\"12\"");
int posR13 = xml.indexOf("<row r=\"13\"");
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.NullOutputStream;
import org.apache.poi.util.TempFile;
+import org.apache.poi.xssf.SXSSFITestDataProvider;
import org.apache.poi.xssf.XLSBUnsupportedException;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
assertEquals("E11", sheet.getActiveCell().formatAsString());
wbBack.close();
}
+
+ @Test
+ public void testBug54084Unicode() throws IOException {
+ // sample XLSX with the same text-contents as the text-file above
+ XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("54084 - Greek - beyond BMP.xlsx");
+
+ verifyBug54084Unicode(wb);
+
+ //XSSFTestDataSamples.writeOut(wb, "bug 54084 for manual review");
+
+ // now write the file and read it back in
+ XSSFWorkbook wbWritten = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ verifyBug54084Unicode(wbWritten);
+
+ // finally also write it out via the streaming interface and verify that we still can read it back in
+ SXSSFWorkbook swb = new SXSSFWorkbook(wb);
+ Workbook wbStreamingWritten = SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
+ verifyBug54084Unicode(wbStreamingWritten);
+
+ wbWritten.close();
+ swb.close();
+ wbStreamingWritten.close();
+ wb.close();
+ }
+
+ private void verifyBug54084Unicode(Workbook wb) {
+ // expected data is stored in UTF-8 in a text-file
+ byte[] data = HSSFTestDataSamples.getTestDataFileContent("54084 - Greek - beyond BMP.txt");
+ String testData = new String(data, StandardCharsets.UTF_8).trim();
+
+ Sheet sheet = wb.getSheetAt(0);
+ Row row = sheet.getRow(0);
+ Cell cell = row.getCell(0);
+
+ String value = cell.getStringCellValue();
+ //System.out.println(value);
+
+ assertEquals("The data in the text-file should exactly match the data that we read from the workbook", testData, value);
+ }
+
}
Object o;
try {
o = PropertySetFactory.create(in);
- } catch (NoPropertySetStreamException ex) {
- o = ex;
- } catch (MarkUnsupportedException ex) {
+ } catch (NoPropertySetStreamException | MarkUnsupportedException ex) {
o = ex;
}
in.close();
Object o;
try {
o = PropertySetFactory.create(in);
- } catch (NoPropertySetStreamException ex) {
- o = ex;
- } catch (MarkUnsupportedException ex) {
+ } catch (NoPropertySetStreamException | MarkUnsupportedException ex) {
o = ex;
}
in.close();
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.util.RecordFormatException;
import org.junit.Test;
-import junit.framework.AssertionFailedError;
-
/**
* @author aviks
*
* fixed, so that they are then run automatically.
*/
public final class TestUnfixedBugs {
-
- @Test
- public void test43493() {
- // Has crazy corrupt sub-records on
- // a EmbeddedObjectRefSubRecord
- try {
- HSSFTestDataSamples.openSampleWorkbook("43493.xls");
- } catch (RecordFormatException e) {
- if (e.getCause().getCause() instanceof ArrayIndexOutOfBoundsException) {
- throw new AssertionFailedError("Identified bug 43493");
- }
- throw e;
- }
- }
-
- /**
- * Note - some parts of this bug have been fixed, and have been
- * transfered over to {@link TestBugs#bug49612_part()}
- */
- @Test
- public void test49612() throws IOException {
- HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49612.xls");
- HSSFSheet sh = wb.getSheetAt(0);
- HSSFRow row = sh.getRow(0);
- HSSFCell c1 = row.getCell(2);
- HSSFCell d1 = row.getCell(3);
- HSSFCell e1 = row.getCell(2);
-
- assertEquals("SUM(BOB+JIM)", c1.getCellFormula());
-
- // Problem 1: Filename missing, see bug #56742
- assertEquals("SUM('49612.xls'!BOB+'49612.xls'!JIM)", d1.getCellFormula());
-
- //Problem 2: Filename missing, see bug #56742
- //junit.framework.ComparisonFailure:
- //Expected :SUM('49612.xls'!BOB+'49612.xls'!JIM)
- //Actual :SUM(BOB+JIM)
- assertEquals("SUM('49612.xls'!BOB+'49612.xls'!JIM)", e1.getCellFormula());
-
- // Problem 3 - fixed and transfered
- wb.close();
- }
-
@Test
public void testFormulaRecordAggregate_1() throws Exception {
// fails at formula "=MEHRFACH.OPERATIONEN(E$3;$B$5;$D4)"
- Workbook wb = HSSFTestDataSamples.openSampleWorkbook("44958_1.xls");
- try {
- for(int i = 0;i < wb.getNumberOfSheets();i++) {
+ try (Workbook wb = HSSFTestDataSamples.openSampleWorkbook("44958_1.xls")) {
+ for (int i = 0; i < wb.getNumberOfSheets(); i++) {
Sheet sheet = wb.getSheetAt(i);
assertNotNull(wb.getSheet(sheet.getSheetName()));
sheet.groupColumn((short) 4, (short) 5);
sheet.setColumnGroupCollapsed(4, true);
sheet.setColumnGroupCollapsed(4, false);
-
- for(Row row : sheet) {
- for(Cell cell : row) {
+
+ for (Row row : sheet) {
+ for (Cell cell : row) {
try {
- cell.toString();
+ assertNotNull(cell.toString());
} catch (Exception e) {
throw new Exception("While handling: " + sheet.getSheetName() + "/" + row.getRowNum() + "/" + cell.getColumnIndex(), e);
}
}
}
}
- } finally {
- wb.close();
}
}
@Test
public void testFormulaRecordAggregate() throws Exception {
// fails at formula "=MEHRFACH.OPERATIONEN(E$3;$B$5;$D4)"
- Workbook wb = HSSFTestDataSamples.openSampleWorkbook("44958.xls");
- try {
- for(int i = 0;i < wb.getNumberOfSheets();i++) {
+ try (Workbook wb = HSSFTestDataSamples.openSampleWorkbook("44958.xls")) {
+ for (int i = 0; i < wb.getNumberOfSheets(); i++) {
Sheet sheet = wb.getSheetAt(i);
assertNotNull(wb.getSheet(sheet.getSheetName()));
sheet.groupColumn((short) 4, (short) 5);
sheet.setColumnGroupCollapsed(4, true);
sheet.setColumnGroupCollapsed(4, false);
-
- for(Row row : sheet) {
- for(Cell cell : row) {
+
+ for (Row row : sheet) {
+ for (Cell cell : row) {
try {
- cell.toString();
+ assertNotNull(cell.toString());
} catch (Exception e) {
throw new Exception("While handling: " + sheet.getSheetName() + "/" + row.getRowNum() + "/" + cell.getColumnIndex(), e);
}
}
}
- }
- } finally {
- wb.close();
+ }
}
}
try {
xr.parse(inSrc);
is.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- } catch (SAXException e) {
+ } catch (IOException | SAXException e) {
throw new RuntimeException(e);
}
}
InputStream is = zf.getInputStream(zf.getEntry("content.xml"));
extractFunctionData(new FunctionDataCollector(ps), is);
zf.close();
- } catch (ZipException e) {
- throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}