]> source.dussan.org Git - poi.git/commitdiff
BUG-60285 avoid NPE if missing relationship id
authorTim Allison <tallison@apache.org>
Thu, 20 Oct 2016 18:45:03 +0000 (18:45 +0000)
committerTim Allison <tallison@apache.org>
Thu, 20 Oct 2016 18:45:03 +0000 (18:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1765861 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java
src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java
test-data/spreadsheet/60825.xlsx [new file with mode: 0644]

index 8d50bc62cdcedefeb79b234180035fdd78ed194e..2accb026571507f3fcf66e0edb529f56b0f5d869 100644 (file)
@@ -47,6 +47,7 @@ import org.apache.poi.xssf.usermodel.XSSFShape;
 import org.apache.xmlbeans.XmlException;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState;
 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
                 //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();
-                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){
                 throw new POIXMLException(e);
             } catch (XmlException e){
index e31254b641e3be41b24da8488315cdd77226a1e3..49742dc9d92b117ad8424e810c85f184a5ce238d 100644 (file)
@@ -246,4 +246,23 @@ public final class TestXSSFReader extends TestCase {
        
        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();
+    }
 }
diff --git a/test-data/spreadsheet/60825.xlsx b/test-data/spreadsheet/60825.xlsx
new file mode 100644 (file)
index 0000000..ffcfe08
Binary files /dev/null and b/test-data/spreadsheet/60825.xlsx differ