Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid NPE on spreadsheetDrawing row tags #65

Merged
merged 1 commit into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ private void handleEvent(XMLEvent event) throws SAXException {
if(event.getEventType() == XMLStreamConstants.CHARACTERS) {
Characters c = event.asCharacters();
lastContents += c.getData();
} else if(event.getEventType() == XMLStreamConstants.START_ELEMENT) {
} else if(event.getEventType() == XMLStreamConstants.START_ELEMENT
&& isSpreadsheetTag(event.asStartElement().getName())) {
StartElement startElement = event.asStartElement();
String tagLocalName = startElement.getName().getLocalPart();

Expand Down Expand Up @@ -152,7 +153,8 @@ private void handleEvent(XMLEvent event) throws SAXException {

// Clear contents cache
lastContents = "";
} else if(event.getEventType() == XMLStreamConstants.END_ELEMENT) {
} else if(event.getEventType() == XMLStreamConstants.END_ELEMENT
&& isSpreadsheetTag(event.asEndElement().getName())) {
EndElement endElement = event.asEndElement();
String tagLocalName = endElement.getName().getLocalPart();

Expand All @@ -170,6 +172,21 @@ private void handleEvent(XMLEvent event) throws SAXException {
}
}

/**
* Returns true if a tag is part of the main namespace for SpreadsheetML:
* <ul>
* <li>http://schemas.openxmlformats.org/spreadsheetml/2006/main
* <li>http://purl.oclc.org/ooxml/spreadsheetml/main
* </ul>
* As opposed to http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing, etc.
* @param name
* @return
*/
private boolean isSpreadsheetTag(QName name) {
return (name.getNamespaceURI() != null
&& name.getNamespaceURI().endsWith("/main"));
}

/**
* Get the hidden state for a given column
*
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,17 @@ public void testClosingFiles() throws Exception {
OPCPackage o = OPCPackage.open(new File("src/test/resources/blank_cell_StringCellValue.xlsx"), PackageAccess.READ);
o.close();
}

@Test
public void shouldIgnoreSpreadsheetDrawingRows() throws Exception {
try(
InputStream is = new FileInputStream(new File("src/test/resources/has_spreadsheetdrawing.xlsx"));
StreamingReader reader = StreamingReader.builder().read(is);
) {
Iterator<Row> iterator = reader.iterator();
while(iterator.hasNext()) {
iterator.next();
}
}
}
}
Binary file added src/test/resources/has_spreadsheetdrawing.xlsx
Binary file not shown.