Apply patch for bug 55864 - XSSFImportFromXML.importFromXML() does not support optional elements

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1633404 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2014-10-21 15:56:45 +00:00
parent 3c37d8f3cd
commit 2e283f2ebf
3 changed files with 157 additions and 108 deletions

View File

@ -97,6 +97,8 @@ public class XSSFImportFromXML {
String xpathString = singleXmlCell.getXpath();
Node result = (Node) xpath.evaluate(xpathString, doc, XPathConstants.NODE);
// result can be null if value is optional (xsd:minOccurs=0), see bugzilla 55864
if (result != null) {
String textContent = result.getTextContent();
logger.log(POILogger.DEBUG, "Extracting with xpath " + xpathString + " : value is '" + textContent + "'");
XSSFCell cell = singleXmlCell.getReferencedCell();
@ -104,6 +106,7 @@ public class XSSFImportFromXML {
+ cell.getSheet().getSheetName());
cell.setCellValue(textContent);
}
}
for (XSSFTable table : tables) {
@ -160,6 +163,7 @@ public class XSSFImportFromXML {
_docElem = doc.getDocumentElement();
}
@Override
public String getNamespaceURI(String prefix) {
return getNamespaceForPrefix(prefix);
}
@ -214,11 +218,13 @@ public class XSSFImportFromXML {
}
// Dummy implementation - not used!
@Override
public Iterator getPrefixes(String val) {
return null;
}
// Dummy implementation - not used!
@Override
public String getPrefix(String uri) {
return null;
}

View File

@ -37,7 +37,7 @@ public class TestXSSFImportFromXML extends TestCase {
public void testImportFromXML() throws Exception{
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
try {
String name = "name";
String teacher = "teacher";
String tutor = "tutor";
@ -75,16 +75,17 @@ public class TestXSSFImportFromXML extends TestCase {
assertTrue(row.getCell(5).getStringCellValue().equals(topic));
assertTrue(row.getCell(6).getStringCellValue().equals(project));
assertTrue(row.getCell(7).getStringCellValue().equals(credits));
} finally {
wb.close();
}
}
public void testMultiTable() throws Exception{
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");
try {
String cellC6 = "c6";
String cellC7 = "c7";
String cellC8 = "c8";
@ -119,16 +120,15 @@ public class TestXSSFImportFromXML extends TestCase {
assertEquals(cellC7,sheet.getRow(6).getCell(2).getStringCellValue());
assertEquals(cellC8,sheet.getRow(7).getCell(2).getStringCellValue());
assertEquals(cellC9,sheet.getRow(8).getCell(2).getStringCellValue());
} finally {
wb.close();
}
}
public void testSingleAttributeCellWithNamespace() throws Exception{
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx");
try {
String id = "a";
String displayName = "dispName";
String ref="19";
@ -150,6 +150,49 @@ public class TestXSSFImportFromXML extends TestCase {
assertEquals(displayName,sheet.getRow(11).getCell(5).getStringCellValue());
assertEquals(ref,sheet.getRow(14).getCell(7).getStringCellValue());
assertEquals(count,sheet.getRow(18).getCell(3).getStringCellValue());
} finally {
wb.close();
}
}
public void testOptionalFields_Bugzilla_55864() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55864.xlsx");
try {
String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<PersonInfoRoot>" +
"<PersonData>" +
"<FirstName>Albert</FirstName>" +
"<LastName>Einstein</LastName>" +
"<BirthDate>1879-03-14</BirthDate>" +
"</PersonData>" +
"</PersonInfoRoot>";
XSSFMap map = wb.getMapInfo().getXSSFMapByName("PersonInfoRoot_Map");
assertNotNull(map);
XSSFImportFromXML importer = new XSSFImportFromXML(map);
importer.importFromXML(testXML);
XSSFSheet sheet=wb.getSheetAt(0);
XSSFRow rowHeadings = sheet.getRow(0);
XSSFRow rowData = sheet.getRow(1);
assertEquals("FirstName", rowHeadings.getCell(0).getStringCellValue());
assertEquals("Albert", rowData.getCell(0).getStringCellValue());
assertEquals("LastName", rowHeadings.getCell(1).getStringCellValue());
assertEquals("Einstein", rowData.getCell(1).getStringCellValue());
assertEquals("BirthDate", rowHeadings.getCell(2).getStringCellValue());
assertEquals("1879-03-14", rowData.getCell(2).getStringCellValue());
// Value for OptionalRating is declared optional (minOccurs=0) in 55864.xlsx
assertEquals("OptionalRating", rowHeadings.getCell(3).getStringCellValue());
assertNull("", rowData.getCell(3));
} finally {
wb.close();
}
}
}

Binary file not shown.