[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

@ -189,9 +189,11 @@ public class XSSFExportToXml implements Comparator<String>{
short startColumnIndex = table.getStartCellReference().getCol();
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(j-startColumnIndex);
XSSFXmlColumnPr pointer = tableColumns.get(tableColumnIndex);
String localXPath = pointer.getLocalXPath();
Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
@ -200,6 +202,7 @@ public class XSSFExportToXml implements Comparator<String>{
}
}
}
}
} /*else {
// TODO: implement filtering management in xpath
}*/

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,6 +103,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
public void testExportToXMLInverseOrder() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples
@ -141,6 +151,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
public void testXPathOrdering() {
XSSFWorkbook wb = XSSFTestDataSamples
@ -164,6 +175,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
public void testMultiTable() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples
@ -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");
// 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);
// }
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,6 +277,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
public void testFormulaCells_Bugzilla_55927() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx");
@ -297,6 +311,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
public void testFormulaCells_Bugzilla_55926() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx");
@ -334,6 +349,7 @@ public final class TestXSSFExportToXML extends TestCase {
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,6 +643,7 @@ public final class TestXSSFExportToXML extends TestCase {
assertTrue(found);
}
@Test
public void testBug59026() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx");
@ -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.