poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1767057 - in /poi/trunk: src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java test-data/spreadsheet/59026.xlsx
Date Fri, 28 Oct 2016 18:24:04 GMT
Author: centic
Date: Fri Oct 28 18:24:04 2016
New Revision: 1767057

URL: http://svn.apache.org/viewvc?rev=1767057&view=rev
Log:
Bug 59026: Add two null-checks to make exporting XLSX to XML work

Added:
    poi/trunk/test-data/spreadsheet/59026.xlsx
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java?rev=1767057&r1=1767056&r2=1767057&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java Fri Oct 28
18:24:04 2016
@@ -29,7 +29,6 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Vector;
 
-import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Source;
 import javax.xml.transform.Transformer;
@@ -98,12 +97,11 @@ public class XSSFExportToXml implements
      * Exports the data in an XML stream
      *
      * @param os OutputStream in which will contain the output XML
-     * @param validate if true, validates the XML againts the XML Schema
-     * @throws SAXException
-     * @throws TransformerException  
-     * @throws ParserConfigurationException 
+     * @param validate if true, validates the XML against the XML Schema
+     * @throws SAXException If validating the document fails
+     * @throws TransformerException If transforming the document fails
      */
-    public void exportToXML(OutputStream os, boolean validate) throws SAXException, ParserConfigurationException,
TransformerException {
+    public void exportToXML(OutputStream os, boolean validate) throws SAXException, TransformerException
{
         exportToXML(os, "UTF-8", validate);
     }
 
@@ -112,12 +110,11 @@ public class XSSFExportToXml implements
      *
      * @param os OutputStream in which will contain the output XML
      * @param encoding the output charset encoding
-     * @param validate if true, validates the XML againts the XML Schema
-     * @throws SAXException
-     * @throws ParserConfigurationException 
-     * @throws TransformerException
+     * @param validate if true, validates the XML against the XML Schema
+     * @throws SAXException If validating the document fails
+     * @throws TransformerException If transforming the document fails
      */
-    public void exportToXML(OutputStream os, String encoding, boolean validate) throws SAXException,
ParserConfigurationException, TransformerException{
+    public void exportToXML(OutputStream os, String encoding, boolean validate) throws SAXException,
TransformerException{
         List<XSSFSingleXmlCell> singleXMLCells = map.getRelatedSingleXMLCell();
         List<XSSFTable> tables = map.getRelatedTables();
 
@@ -240,6 +237,7 @@ public class XSSFExportToXml implements
      *
      * @param xml the XML to validate
      * @return true, if document is valid
+     * @throws SAXException If validating the document fails
      */
     private boolean isValid(Document xml) throws SAXException{
         try{
@@ -397,11 +395,11 @@ public class XSSFExportToXml implements
         String[] leftTokens = leftXpath.split("/");
         String[] rightTokens = rightXpath.split("/");
 
-        int minLenght = leftTokens.length< rightTokens.length? leftTokens.length : rightTokens.length;
+        int minLength = leftTokens.length< rightTokens.length? leftTokens.length : rightTokens.length;
 
         Node localComplexTypeRootNode = xmlSchema;
 
-        for(int i =1;i <minLenght; i++) {
+        for(int i =1;i <minLength; i++) {
 
             String leftElementName = leftTokens[i];
             String rightElementName = rightTokens[i];
@@ -427,6 +425,9 @@ public class XSSFExportToXml implements
     }
 
     private int indexOfElementInComplexType(String elementName,Node complexType) {
+        if(complexType == null) {
+            return -1;
+        }
 
         NodeList list  = complexType.getChildNodes();
         int indexOf = -1;
@@ -472,6 +473,10 @@ public class XSSFExportToXml implements
 
     private String getComplexTypeNameFromChildren(Node localComplexTypeRootNode,
             String elementNameWithoutNamespace) {
+        if(localComplexTypeRootNode == null) {
+            return "";
+        }
+
         NodeList  list  = localComplexTypeRootNode.getChildNodes();
         String complexTypeName = "";
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java?rev=1767057&r1=1767056&r2=1767057&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java Fri
Oct 28 18:24:04 2016
@@ -17,17 +17,7 @@
 
 package org.apache.poi.xssf.extractor;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.util.Date;
-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;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellType;
@@ -38,12 +28,20 @@ import org.apache.poi.xssf.XSSFTestDataS
 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;
 
-import junit.framework.TestCase;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * @author Roberto Manicardi
@@ -101,15 +99,13 @@ public final class TestXSSFExportToXML e
 		XSSFWorkbook wb = XSSFTestDataSamples
 				.openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
 
-		MapInfo mapInfo = null;
-
         boolean found = false;
 		for (POIXMLDocumentPart p : wb.getRelations()) {
 
 			if (!(p instanceof MapInfo)) {
 				continue;
 			}
-			mapInfo = (MapInfo) p;
+            MapInfo mapInfo = (MapInfo) p;
 
 			XSSFMap map = mapInfo.getXSSFMapById(1);
 			XSSFExportToXml exporter = new XSSFExportToXml(map);
@@ -150,13 +146,11 @@ public final class TestXSSFExportToXML e
 		XSSFWorkbook wb = XSSFTestDataSamples
 				.openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
 
-		MapInfo mapInfo = null;
-
         boolean found = false;
 		for (POIXMLDocumentPart p : wb.getRelations()) {
 
 			if (p instanceof MapInfo) {
-				mapInfo = (MapInfo) p;
+                MapInfo mapInfo = (MapInfo) p;
 
 				XSSFMap map = mapInfo.getXSSFMapById(1);
 				XSSFExportToXml exporter = new XSSFExportToXml(map);
@@ -340,7 +334,6 @@ public final class TestXSSFExportToXML e
        assertTrue(found);
    }
    
-   @Test
    public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception {
 
        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx");
@@ -627,4 +620,18 @@ public final class TestXSSFExportToXML e
        }
        assertTrue(found);
    }
+
+    public void testBug59026() throws Exception {
+        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx");
+
+        Collection<XSSFMap> mappings = wb.getCustomXMLMappings();
+        assertTrue(mappings.size() > 0);
+        for (XSSFMap map : mappings) {
+            XSSFExportToXml exporter = new XSSFExportToXml(map);
+
+            ByteArrayOutputStream os = new ByteArrayOutputStream();
+            exporter.exportToXML(os, false);
+            assertNotNull(os.toString("UTF-8"));
+        }
+    }
 }

Added: poi/trunk/test-data/spreadsheet/59026.xlsx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/59026.xlsx?rev=1767057&view=auto
==============================================================================
Binary files poi/trunk/test-data/spreadsheet/59026.xlsx (added) and poi/trunk/test-data/spreadsheet/59026.xlsx
Fri Oct 28 18:24:04 2016 differ



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message