Browse Source

Bug 55923: Fix compare/sorting of nodes in exported XML

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1564050 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_3_11_BETA1
Dominik Stadler 10 years ago
parent
commit
43b4e3f378

+ 3
- 9
src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java View File

*/ */
@Override @Override
public int compare(String leftXpath, String rightXpath) { public int compare(String leftXpath, String rightXpath) {

int result = 0;
Node xmlSchema = map.getSchema(); Node xmlSchema = map.getSchema();



String[] leftTokens = leftXpath.split("/"); String[] leftTokens = leftXpath.split("/");
String[] rightTokens = rightXpath.split("/"); String[] rightTokens = rightXpath.split("/");




Node localComplexTypeRootNode = xmlSchema; Node localComplexTypeRootNode = xmlSchema;



for(int i =1;i <minLenght; i++) { for(int i =1;i <minLenght; i++) {


String leftElementName =leftTokens[i]; String leftElementName =leftTokens[i];
String rightElementName = rightTokens[i]; String rightElementName = rightTokens[i];


if (leftElementName.equals(rightElementName)) { if (leftElementName.equals(rightElementName)) {


Node complexType = getComplexTypeForElement(leftElementName, xmlSchema,localComplexTypeRootNode); Node complexType = getComplexTypeForElement(leftElementName, xmlSchema,localComplexTypeRootNode);
localComplexTypeRootNode = complexType; localComplexTypeRootNode = complexType;
} else { } else {
int rightIndex = indexOfElementInComplexType(rightElementName,localComplexTypeRootNode); int rightIndex = indexOfElementInComplexType(rightElementName,localComplexTypeRootNode);
if (leftIndex!=-1 && rightIndex!=-1) { if (leftIndex!=-1 && rightIndex!=-1) {
if ( leftIndex < rightIndex) { if ( leftIndex < rightIndex) {
result = -1;
return -1;
}if ( leftIndex > rightIndex) { }if ( leftIndex > rightIndex) {
result = 1;
return 1;
} }
} else { } else {
// NOTE: the xpath doesn't match correctly in the schema // NOTE: the xpath doesn't match correctly in the schema
} }
} }


return result;
return 0;
} }


private int indexOfElementInComplexType(String elementName,Node complexType) { private int indexOfElementInComplexType(String elementName,Node complexType) {

+ 207
- 4
src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java View File

import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
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 junit.framework.TestCase; import junit.framework.TestCase;


import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.MapInfo; import org.apache.poi.xssf.model.MapInfo;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFMap; import org.apache.poi.xssf.usermodel.XSSFMap;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test; import org.junit.Test;


XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");


boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) { for (POIXMLDocumentPart p : wb.getRelations()) {


if (!(p instanceof MapInfo)) { if (!(p instanceof MapInfo)) {
assertEquals("aa", crediti); assertEquals("aa", crediti);
parseXML(xml); parseXML(xml);
}
found = true;
}
assertTrue(found);
} }


public void testExportToXMLInverseOrder() throws Exception { public void testExportToXMLInverseOrder() throws Exception {


MapInfo mapInfo = null; MapInfo mapInfo = null;


boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) { for (POIXMLDocumentPart p : wb.getRelations()) {


if (!(p instanceof MapInfo)) { if (!(p instanceof MapInfo)) {
assertEquals("ro", crediti); assertEquals("ro", crediti);
parseXML(xml); parseXML(xml);
}
found = true;
}
assertTrue(found);
} }


public void testXPathOrdering() { public void testXPathOrdering() {


MapInfo mapInfo = null; MapInfo mapInfo = null;


boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) { for (POIXMLDocumentPart p : wb.getRelations()) {


if (p instanceof MapInfo) { if (p instanceof MapInfo) {
assertEquals(1, exporter.compare("/CORSO/DOCENTE", "/CORSO/NOME")); assertEquals(1, exporter.compare("/CORSO/DOCENTE", "/CORSO/NOME"));
assertEquals(-1, exporter.compare("/CORSO/NOME", "/CORSO/DOCENTE")); assertEquals(-1, exporter.compare("/CORSO/NOME", "/CORSO/DOCENTE"));
} }
}
found = true;
}
assertTrue(found);
} }


public void testMultiTable() throws Exception { public void testMultiTable() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples XSSFWorkbook wb = XSSFTestDataSamples
.openSampleWorkbook("CustomXMLMappings-complex-type.xlsx"); .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");


boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) { for (POIXMLDocumentPart p : wb.getRelations()) {


if (p instanceof MapInfo) { if (p instanceof MapInfo) {
assertTrue(matcher.find()); assertTrue(matcher.find());
} }
} }
}
found = true;
}
assertTrue(found);
} }


public void test55850ComplexXmlExport() throws Exception { public void test55850ComplexXmlExport() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples XSSFWorkbook wb = XSSFTestDataSamples
.openSampleWorkbook("55850.xlsx"); .openSampleWorkbook("55850.xlsx");


boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) { for (POIXMLDocumentPart p : wb.getRelations()) {


if (!(p instanceof MapInfo)) { if (!(p instanceof MapInfo)) {
assertEquals("19", chf); assertEquals("19", chf);
parseXML(xmlData); parseXML(xmlData);
found = true;
} }
assertTrue(found);
} }


public void testFormulaCells_Bugzilla_55927() throws Exception { public void testFormulaCells_Bugzilla_55927() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx");
boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) { for (POIXMLDocumentPart p : wb.getRelations()) {
if (!(p instanceof MapInfo)) { if (!(p instanceof MapInfo)) {
assertEquals("2012-01-13", date); assertEquals("2012-01-13", date);
parseXML(xmlData); parseXML(xmlData);
found = true;
} }
assertTrue(found);
} }


public void testFormulaCells_Bugzilla_55926() throws Exception { public void testFormulaCells_Bugzilla_55926() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx");


boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) { for (POIXMLDocumentPart p : wb.getRelations()) {


if (!(p instanceof MapInfo)) { if (!(p instanceof MapInfo)) {
assertEquals("5.1", doubleValue); assertEquals("5.1", doubleValue);
parseXML(xmlData); parseXML(xmlData);
found = true;
} }
assertTrue(found);
} }
@Test @Test


XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx");


boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) { for (POIXMLDocumentPart p : wb.getRelations()) {


if (!(p instanceof MapInfo)) { if (!(p instanceof MapInfo)) {
assertEquals("1",euro); assertEquals("1",euro);
parseXML(xmlData); parseXML(xmlData);
found = true;
} }
assertTrue(found);
} }


public void testXmlExportCompare_Bug_55923() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");

boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) {

if (!(p instanceof MapInfo)) {
continue;
}
MapInfo mapInfo = (MapInfo) p;

XSSFMap map = mapInfo.getXSSFMapById(4);

assertNotNull("XSSFMap is null", map);

XSSFExportToXml exporter = new XSSFExportToXml(map);
assertEquals(0, exporter.compare("", ""));
assertEquals(0, exporter.compare("/", "/"));
assertEquals(0, exporter.compare("//", "//"));
assertEquals(0, exporter.compare("/a/", "/b/"));
assertEquals(-1, exporter.compare("/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:EUR",
"/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:CHF"));
found = true;
}
assertTrue(found);
}
public void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");

boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) {

if (!(p instanceof MapInfo)) {
continue;
}
MapInfo mapInfo = (MapInfo) p;

XSSFMap map = mapInfo.getXSSFMapById(4);

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 a_b = a.split("<B>")[1].split("</B>")[0].trim();
String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
assertEquals("1",a_b_c_e_euro);
assertEquals("2",a_b_c_e_chf);
String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
assertEquals("3",a_b_d_e_euro);
assertEquals("4",a_b_d_e_chf);
found = true;
}
assertTrue(found);
}
private void parseXML(String xmlData) throws IOException, SAXException, ParserConfigurationException { private void parseXML(String xmlData) throws IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true); docBuilderFactory.setNamespaceAware(true);
return null; return null;
} }
} }
public void testExportDataTypes() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cString = row.createCell(0);
cString.setCellValue("somestring");
cString.setCellType(XSSFCell.CELL_TYPE_STRING);
Cell cBoolean = row.createCell(1);
cBoolean.setCellValue(true);
cBoolean.setCellType(XSSFCell.CELL_TYPE_BOOLEAN);
Cell cError = row.createCell(2);
cError.setCellType(XSSFCell.CELL_TYPE_ERROR);
Cell cFormulaString = row.createCell(3);
cFormulaString.setCellFormula("A1");
cFormulaString.setCellType(XSSFCell.CELL_TYPE_FORMULA);
Cell cFormulaNumeric = row.createCell(4);
cFormulaNumeric.setCellFormula("F1");
cFormulaNumeric.setCellType(XSSFCell.CELL_TYPE_FORMULA);
Cell cNumeric = row.createCell(5);
cNumeric.setCellValue(1.2);
cNumeric.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
Cell cDate = row.createCell(6);
cDate.setCellValue(new Date());
cDate.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) {

if (!(p instanceof MapInfo)) {
continue;
}
MapInfo mapInfo = (MapInfo) p;

XSSFMap map = mapInfo.getXSSFMapById(4);

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(""));
parseXML(xmlData);
found = true;
}
assertTrue(found);
}

public void testValidateFalse() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");

boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) {

if (!(p instanceof MapInfo)) {
continue;
}
MapInfo mapInfo = (MapInfo) p;

XSSFMap map = mapInfo.getXSSFMapById(4);

assertNotNull("XSSFMap is null", map);

XSSFExportToXml exporter = new XSSFExportToXml(map);
ByteArrayOutputStream os = new ByteArrayOutputStream();
exporter.exportToXML(os, false);
String xmlData = os.toString("UTF-8");

assertNotNull(xmlData);
assertFalse(xmlData.equals(""));
parseXML(xmlData);
found = true;
}
assertTrue(found);
}
} }

BIN
test-data/spreadsheet/55923.xlsx View File


Loading…
Cancel
Save