[Bug-61281] guard against index out of bounds in XSSFExportToXml column mapping

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1801634 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2017-07-11 16:38:49 +00:00
parent f63c846c03
commit 6de159d232
3 changed files with 139 additions and 102 deletions

View File

@ -188,14 +188,17 @@ public class XSSFExportToXml implements Comparator<String>{
Node tableRootNode = getNodeByXPath(table.getCommonXpath(),doc.getFirstChild(),doc,true); Node tableRootNode = getNodeByXPath(table.getCommonXpath(),doc.getFirstChild(),doc,true);
short startColumnIndex = table.getStartCellReference().getCol(); short startColumnIndex = table.getStartCellReference().getCol();
for(int j = startColumnIndex; j<= table.getEndCellReference().getCol();j++) { for(int j = startColumnIndex; j<= table.getEndCellReference().getCol(); j++) {
XSSFCell cell = row.getCell(j); int tableColumnIndex = j - startColumnIndex;
if (cell!=null) { if (tableColumnIndex < tableColumns.size()) {
XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex); XSSFCell cell = row.getCell(j);
String localXPath = pointer.getLocalXPath(); if (cell != null) {
Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false); XSSFXmlColumnPr pointer = tableColumns.get(tableColumnIndex);
String localXPath = pointer.getLocalXPath();
mapCellOnNode(cell,currentNode); Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
mapCellOnNode(cell,currentNode);
}
} }
} }
} }

View File

@ -17,24 +17,11 @@
package org.apache.poi.xssf.extractor; package org.apache.poi.xssf.extractor;
import junit.framework.TestCase; import static org.junit.Assert.assertEquals;
import org.apache.poi.POIXMLDocumentPart; import static org.junit.Assert.assertFalse;
import org.apache.poi.ss.usermodel.Cell; import static org.junit.Assert.assertNotNull;
import org.apache.poi.ss.usermodel.CellType; import static org.junit.Assert.assertTrue;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.util.XMLHelper;
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.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -43,11 +30,33 @@ import java.util.Date;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.util.DocumentHelper;
import org.apache.poi.util.XMLHelper;
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.Ignore;
import org.junit.Test;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** /**
* @author Roberto Manicardi * @author Roberto Manicardi
*/ */
public final class TestXSSFExportToXML extends TestCase { public final class TestXSSFExportToXML {
@Test
public void testExportToXML() throws Exception { public void testExportToXML() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
@ -94,7 +103,8 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found); assertTrue(found);
} }
public void testExportToXMLInverseOrder() throws Exception { @Test
public void testExportToXMLInverseOrder() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples XSSFWorkbook wb = XSSFTestDataSamples
.openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx"); .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
@ -141,7 +151,8 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found); assertTrue(found);
} }
public void testXPathOrdering() { @Test
public void testXPathOrdering() {
XSSFWorkbook wb = XSSFTestDataSamples XSSFWorkbook wb = XSSFTestDataSamples
.openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx"); .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
@ -164,7 +175,8 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found); assertTrue(found);
} }
public void testMultiTable() throws Exception { @Test
public void testMultiTable() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples XSSFWorkbook wb = XSSFTestDataSamples
.openSampleWorkbook("CustomXMLMappings-complex-type.xlsx"); .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");
@ -207,19 +219,20 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found); assertTrue(found);
} }
@Test
@Ignore(value="Fails, but I don't know if it is ok or not...")
public void testExportToXMLSingleAttributeNamespace() throws Exception { public void testExportToXMLSingleAttributeNamespace() throws Exception {
// TODO: Fails, but I don't know if it is ok or not... XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx");
// XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx"); for (XSSFMap map : wb.getCustomXMLMappings()) {
// XSSFExportToXml exporter = new XSSFExportToXml(map);
// for (XSSFMap map : wb.getCustomXMLMappings()) {
// XSSFExportToXml exporter = new XSSFExportToXml(map); ByteArrayOutputStream os = new ByteArrayOutputStream();
// exporter.exportToXML(os, true);
// ByteArrayOutputStream os = new ByteArrayOutputStream(); }
// exporter.exportToXML(os, true);
// }
} }
@Test
public void test55850ComplexXmlExport() throws Exception { public void test55850ComplexXmlExport() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples XSSFWorkbook wb = XSSFTestDataSamples
@ -264,76 +277,79 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found); assertTrue(found);
} }
public void testFormulaCells_Bugzilla_55927() throws Exception { @Test
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx"); public void testFormulaCells_Bugzilla_55927() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx");
boolean found = false;
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(""));
assertEquals("2012-01-13", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
assertEquals("2012-02-16", xmlData.split("<FORMULA_DATE>")[1].split("</FORMULA_DATE>")[0].trim());
parseXML(xmlData);
found = true;
}
assertTrue(found);
}
public void testFormulaCells_Bugzilla_55926() throws Exception { boolean found = false;
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx"); for (POIXMLDocumentPart p : wb.getRelations()) {
boolean found = false; if (!(p instanceof MapInfo)) {
for (POIXMLDocumentPart p : wb.getRelations()) { continue;
}
MapInfo mapInfo = (MapInfo) p;
if (!(p instanceof MapInfo)) { XSSFMap map = mapInfo.getXSSFMapById(1);
continue;
}
MapInfo mapInfo = (MapInfo) p;
XSSFMap map = mapInfo.getXSSFMapById(1); assertNotNull("XSSFMap is null", map);
assertNotNull("XSSFMap is null", map); XSSFExportToXml exporter = new XSSFExportToXml(map);
ByteArrayOutputStream os = new ByteArrayOutputStream();
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
XSSFExportToXml exporter = new XSSFExportToXml(map); assertNotNull(xmlData);
ByteArrayOutputStream os = new ByteArrayOutputStream(); assertFalse(xmlData.equals(""));
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
assertNotNull(xmlData); assertEquals("2012-01-13", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
assertFalse(xmlData.equals("")); assertEquals("2012-02-16", xmlData.split("<FORMULA_DATE>")[1].split("</FORMULA_DATE>")[0].trim());
String a = xmlData.split("<A>")[1].split("</A>")[0].trim(); parseXML(xmlData);
String doubleValue = a.split("<DOUBLE>")[1].split("</DOUBLE>")[0].trim();
String stringValue = a.split("<STRING>")[1].split("</STRING>")[0].trim(); found = true;
}
assertEquals("Hello World", stringValue); assertTrue(found);
assertEquals("5.1", doubleValue); }
parseXML(xmlData); @Test
public void testFormulaCells_Bugzilla_55926() throws Exception {
found = true; XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx");
}
assertTrue(found); boolean found = false;
} 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 doubleValue = a.split("<DOUBLE>")[1].split("</DOUBLE>")[0].trim();
String stringValue = a.split("<STRING>")[1].split("</STRING>")[0].trim();
assertEquals("Hello World", stringValue);
assertEquals("5.1", doubleValue);
parseXML(xmlData);
found = true;
}
assertTrue(found);
}
@Test
public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception { public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx");
@ -369,6 +385,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found); assertTrue(found);
} }
@Test
public void testXmlExportSchemaWithXSAllTag_Bugzilla_56169() throws Exception { public void testXmlExportSchemaWithXSAllTag_Bugzilla_56169() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56169.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56169.xlsx");
@ -403,6 +420,7 @@ public final class TestXSSFExportToXML extends TestCase {
} }
} }
@Test
public void testXmlExportCompare_Bug_55923() throws Exception { public void testXmlExportCompare_Bug_55923() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
@ -432,6 +450,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found); assertTrue(found);
} }
@Test
public void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception { public void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
@ -498,6 +517,7 @@ public final class TestXSSFExportToXML extends TestCase {
} }
} }
@Test
public void testExportDataTypes() throws Exception { public void testExportDataTypes() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
@ -558,6 +578,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found); assertTrue(found);
} }
@Test
public void testValidateFalse() throws Exception { public void testValidateFalse() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
@ -588,6 +609,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found); assertTrue(found);
} }
@Test
public void testRefElementsInXmlSchema_Bugzilla_56730() throws Exception { public void testRefElementsInXmlSchema_Bugzilla_56730() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.xlsx");
@ -621,7 +643,8 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found); assertTrue(found);
} }
public void testBug59026() throws Exception { @Test
public void testBug59026() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx");
Collection<XSSFMap> mappings = wb.getCustomXMLMappings(); Collection<XSSFMap> mappings = wb.getCustomXMLMappings();
@ -634,4 +657,15 @@ public final class TestXSSFExportToXML extends TestCase {
assertNotNull(os.toString("UTF-8")); assertNotNull(os.toString("UTF-8"));
} }
} }
@Test
public void testExportTableWithNonMappedColumn_Bugzilla_61281() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61281.xlsx");
for (XSSFMap map : wb.getCustomXMLMappings()) {
XSSFExportToXml exporter = new XSSFExportToXml(map);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
exporter.exportToXML(bos, true);
assertNotNull(DocumentHelper.readDocument(new ByteArrayInputStream(bos.toByteArray())));
}
}
} }

Binary file not shown.