[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);
short startColumnIndex = table.getStartCellReference().getCol();
for(int j = startColumnIndex; j<= table.getEndCellReference().getCol();j++) {
XSSFCell cell = row.getCell(j);
if (cell!=null) {
XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex);
String localXPath = pointer.getLocalXPath();
Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
mapCellOnNode(cell,currentNode);
for(int j = startColumnIndex; j<= table.getEndCellReference().getCol(); j++) {
int tableColumnIndex = j - startColumnIndex;
if (tableColumnIndex < tableColumns.size()) {
XSSFCell cell = row.getCell(j);
if (cell != null) {
XSSFXmlColumnPr pointer = tableColumns.get(tableColumnIndex);
String localXPath = pointer.getLocalXPath();
Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
mapCellOnNode(cell,currentNode);
}
}
}
}

View File

@ -17,24 +17,11 @@
package org.apache.poi.xssf.extractor;
import junit.framework.TestCase;
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.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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
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;
@ -43,11 +30,33 @@ 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 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
*/
public final class TestXSSFExportToXML extends TestCase {
public final class TestXSSFExportToXML {
@Test
public void testExportToXML() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
@ -94,7 +103,8 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
public void testExportToXMLInverseOrder() throws Exception {
@Test
public void testExportToXMLInverseOrder() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples
.openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
@ -141,7 +151,8 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
public void testXPathOrdering() {
@Test
public void testXPathOrdering() {
XSSFWorkbook wb = XSSFTestDataSamples
.openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
@ -164,7 +175,8 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
public void testMultiTable() throws Exception {
@Test
public void testMultiTable() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples
.openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");
@ -207,19 +219,20 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
@Ignore(value="Fails, but I don't know if it is ok or not...")
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");
//
// for (XSSFMap map : wb.getCustomXMLMappings()) {
// XSSFExportToXml exporter = new XSSFExportToXml(map);
//
// ByteArrayOutputStream os = new ByteArrayOutputStream();
// exporter.exportToXML(os, true);
// }
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx");
for (XSSFMap map : wb.getCustomXMLMappings()) {
XSSFExportToXml exporter = new XSSFExportToXml(map);
ByteArrayOutputStream os = new ByteArrayOutputStream();
exporter.exportToXML(os, true);
}
}
@Test
public void test55850ComplexXmlExport() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples
@ -264,76 +277,79 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
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);
}
@Test
public void testFormulaCells_Bugzilla_55927() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx");
public void testFormulaCells_Bugzilla_55926() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx");
boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) {
boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) {
if (!(p instanceof MapInfo)) {
continue;
}
MapInfo mapInfo = (MapInfo) p;
if (!(p instanceof MapInfo)) {
continue;
}
MapInfo mapInfo = (MapInfo) p;
XSSFMap map = mapInfo.getXSSFMapById(1);
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);
ByteArrayOutputStream os = new ByteArrayOutputStream();
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
assertNotNull(xmlData);
assertFalse(xmlData.equals(""));
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);
}
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);
}
@Test
public void testFormulaCells_Bugzilla_55926() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.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(""));
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 {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx");
@ -369,6 +385,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
public void testXmlExportSchemaWithXSAllTag_Bugzilla_56169() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56169.xlsx");
@ -403,6 +420,7 @@ public final class TestXSSFExportToXML extends TestCase {
}
}
@Test
public void testXmlExportCompare_Bug_55923() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
@ -432,6 +450,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
public void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
@ -498,6 +517,7 @@ public final class TestXSSFExportToXML extends TestCase {
}
}
@Test
public void testExportDataTypes() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
@ -558,6 +578,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
public void testValidateFalse() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
@ -588,6 +609,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
public void testRefElementsInXmlSchema_Bugzilla_56730() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.xlsx");
@ -621,7 +643,8 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
public void testBug59026() throws Exception {
@Test
public void testBug59026() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx");
Collection<XSSFMap> mappings = wb.getCustomXMLMappings();
@ -634,4 +657,15 @@ public final class TestXSSFExportToXML extends TestCase {
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.