BUG-60285 avoid NPE if missing relationship id

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1765861 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim Allison 2016-10-20 18:45:03 +00:00
parent 9fd46e3174
commit 9c455cb5c9
3 changed files with 30 additions and 1 deletions

View File

@ -47,6 +47,7 @@ import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument; import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
/** /**
@ -226,7 +227,16 @@ public class XSSFReader {
//step 2. Read array of CTSheet elements, wrap it in a ArayList and construct an iterator //step 2. Read array of CTSheet elements, wrap it in a ArayList and construct an iterator
//Note, using XMLBeans might be expensive, consider refactoring to use SAX or a plain regexp search //Note, using XMLBeans might be expensive, consider refactoring to use SAX or a plain regexp search
CTWorkbook wbBean = WorkbookDocument.Factory.parse(wb.getInputStream(), DEFAULT_XML_OPTIONS).getWorkbook(); CTWorkbook wbBean = WorkbookDocument.Factory.parse(wb.getInputStream(), DEFAULT_XML_OPTIONS).getWorkbook();
sheetIterator = wbBean.getSheets().getSheetList().iterator(); List<CTSheet> validSheets = new ArrayList<CTSheet>();
for (CTSheet ctSheet : wbBean.getSheets().getSheetList()) {
//if there's no relationship id, silently skip the sheet
if ("".equals(ctSheet.getId())) {
//skip it
} else {
validSheets.add(ctSheet);
}
}
sheetIterator = validSheets.iterator();
} catch (InvalidFormatException e){ } catch (InvalidFormatException e){
throw new POIXMLException(e); throw new POIXMLException(e);
} catch (XmlException e){ } catch (XmlException e){

View File

@ -244,6 +244,25 @@ public final class TestXSSFReader extends TestCase {
assertEquals(false, iter.hasNext()); assertEquals(false, iter.hasNext());
assertEquals("Orders", iter.getSheetName()); assertEquals("Orders", iter.getSheetName());
pkg.close();
}
/**
* NPE when sheet has no relationship id in the workbook
* 60825
*/
public void testSheetWithNoRelationshipId() throws Exception {
OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("60825.xlsx");
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);
assertNotNull(strings);
XSSFReader reader = new XSSFReader(pkg);
StylesTable styles = reader.getStylesTable();
assertNotNull(styles);
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) reader.getSheetsData();
assertNotNull(iter.next());
assertFalse(iter.hasNext());
pkg.close(); pkg.close();
} }
} }

Binary file not shown.