From: Dominik Stadler Date: Mon, 3 Feb 2014 20:17:59 +0000 (+0000) Subject: Bug 55924: Avoid errors during exporting XML if there are numeric cells without value... X-Git-Tag: REL_3_11_BETA1~278 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=77bcfcb41c2f60010e479bf9e9dd084db9a0eb2e;p=poi.git Bug 55924: Avoid errors during exporting XML if there are numeric cells without value, also verify in tests that XML can always be parsed git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1564045 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java index 49b095eded..543b243c5e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java @@ -174,6 +174,11 @@ public class XSSFExportToXml implements Comparator{ Node currentNode = getNodeByXPath(xpath,doc.getFirstChild(),doc,false); STXmlDataType.Enum dataType = simpleXmlCell.getXmlDataType(); mapCellOnNode(cell,currentNode,dataType); + + //remove nodes which are empty in order to keep the output xml valid + if("".equals(currentNode.getTextContent()) && currentNode.getParentNode() != null) { + currentNode.getParentNode().removeChild(currentNode); + } } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java index fbbf1e0eb5..0bdef92863 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java @@ -17,10 +17,16 @@ package org.apache.poi.xssf.extractor; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + import junit.framework.TestCase; import org.apache.poi.POIXMLDocumentPart; @@ -28,12 +34,15 @@ import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.model.MapInfo; import org.apache.poi.xssf.usermodel.XSSFMap; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Test; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; /** * @author Roberto Manicardi */ public final class TestXSSFExportToXML extends TestCase { - public void testExportToXML() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx"); @@ -71,6 +80,8 @@ public final class TestXSSFExportToXML extends TestCase { assertEquals("gvvv", argomento); assertEquals("aaaa", progetto); assertEquals("aa", crediti); + + parseXML(xml); } } @@ -114,6 +125,8 @@ public final class TestXSSFExportToXML extends TestCase { assertEquals("ds", argomento); assertEquals("ro", progetto); assertEquals("ro", crediti); + + parseXML(xml); } } @@ -212,6 +225,8 @@ public final class TestXSSFExportToXML extends TestCase { assertEquals("15", euro); assertEquals("19", chf); + + parseXML(xmlData); } } @@ -239,6 +254,8 @@ public final class TestXSSFExportToXML extends TestCase { String date = xmlData.split("")[1].split("")[0].trim(); assertEquals("2012-01-13", date); + + parseXML(xmlData); } } @@ -270,6 +287,59 @@ public final class TestXSSFExportToXML extends TestCase { assertEquals("Hello World", stringValue); assertEquals("5.1", doubleValue); + + parseXML(xmlData); + } + } + + @Test + public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception { + + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx"); + + for (POIXMLDocumentPart p : wb.getRelations()) { + + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; + + XSSFMap map = mapInfo.getXSSFMapById(1); + + assertNotNull("XSSFMap is null", map); + + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); + + assertNotNull(xmlData); + assertFalse(xmlData.equals("")); + + String a = xmlData.split("")[1].split("")[0].trim(); + String euro = a.split("")[1].split("")[0].trim(); + assertEquals("1",euro); + + parseXML(xmlData); + } + } + + private void parseXML(String xmlData) throws IOException, SAXException, ParserConfigurationException { + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + docBuilderFactory.setNamespaceAware(true); + docBuilderFactory.setValidating(false); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + docBuilder.setEntityResolver(new DummyEntityResolver()); + + docBuilder.parse(new ByteArrayInputStream(xmlData.getBytes("UTF-8"))); + } + + private static class DummyEntityResolver implements EntityResolver + { + @Override + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException + { + return null; } } } diff --git a/test-data/spreadsheet/55924.xlsx b/test-data/spreadsheet/55924.xlsx new file mode 100644 index 0000000000..2ce091a7c3 Binary files /dev/null and b/test-data/spreadsheet/55924.xlsx differ