]> source.dussan.org Git - poi.git/commitdiff
Bug 55924: Avoid errors during exporting XML if there are numeric cells without value...
authorDominik Stadler <centic@apache.org>
Mon, 3 Feb 2014 20:17:59 +0000 (20:17 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 3 Feb 2014 20:17:59 +0000 (20:17 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1564045 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java
test-data/spreadsheet/55924.xlsx [new file with mode: 0644]

index 49b095eded2378d80f134793cfd3f900d0fc70d1..543b243c5e9790d1244d88e0820e9e777fdab751 100644 (file)
@@ -174,6 +174,11 @@ public class XSSFExportToXml implements Comparator<String>{
                         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);
+                        }
                     }
                 }
 
index fbbf1e0eb5df29bef14bbe5be0959704c8b9e0ec..0bdef92863a6eea9a23fabcca07e7f95e5ddbe88 100644 (file)
 
 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("<DATE>")[1].split("</DATE>")[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("<A>")[1].split("</A>")[0].trim();
+           String euro = a.split("<EUR>")[1].split("</EUR>")[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 (file)
index 0000000..2ce091a
Binary files /dev/null and b/test-data/spreadsheet/55924.xlsx differ