aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2014-08-28 21:41:30 +0000
committerDominik Stadler <centic@apache.org>2014-08-28 21:41:30 +0000
commitde7cedad2be446d6ef235ea25d1a0b0734704569 (patch)
treeefeadf6d69f5d1b24cdb156f842b28682aa23515 /src/ooxml
parentc20a2b97e8d175fa5efa5cc6c4c6ad5b17bfc8b2 (diff)
downloadpoi-de7cedad2be446d6ef235ea25d1a0b0734704569.tar.gz
poi-de7cedad2be446d6ef235ea25d1a0b0734704569.zip
Bug 56730: Fix exporting XML if schema contains ref-elements
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1621209 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml')
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java17
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java36
2 files changed, 48 insertions, 5 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
index 90b89e36bf..d608f96157 100644
--- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
+++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
@@ -448,8 +448,8 @@ public class XSSFExportToXml implements Comparator<String>{
Node node = list.item(i);
if (node instanceof Element) {
if (node.getLocalName().equals("element")) {
- Node nameAttribute = node.getAttributes().getNamedItem("name");
- if (nameAttribute.getNodeValue().equals(removeNamespace(elementName))) {
+ Node element = getNameOrRefElement(node);
+ if (element.getNodeValue().equals(removeNamespace(elementName))) {
indexOf = i;
break;
}
@@ -460,6 +460,15 @@ public class XSSFExportToXml implements Comparator<String>{
return indexOf;
}
+ private Node getNameOrRefElement(Node node) {
+ Node returnNode = node.getAttributes().getNamedItem("name");
+ if(returnNode != null) {
+ return returnNode;
+ }
+
+ return node.getAttributes().getNamedItem("ref");
+ }
+
private Node getComplexTypeForElement(String elementName,Node xmlSchema,Node localComplexTypeRootNode) {
String elementNameWithoutNamespace = removeNamespace(elementName);
@@ -483,7 +492,7 @@ public class XSSFExportToXml implements Comparator<String>{
Node node = list.item(i);
if ( node instanceof Element) {
if (node.getLocalName().equals("element")) {
- Node nameAttribute = node.getAttributes().getNamedItem("name");
+ Node nameAttribute = getNameOrRefElement(node);
if (nameAttribute.getNodeValue().equals(elementNameWithoutNamespace)) {
Node complexTypeAttribute = node.getAttributes().getNamedItem("type");
if (complexTypeAttribute!=null) {
@@ -504,7 +513,7 @@ public class XSSFExportToXml implements Comparator<String>{
Node node = complexTypeList.item(i);
if ( node instanceof Element) {
if (node.getLocalName().equals("complexType")) {
- Node nameAttribute = node.getAttributes().getNamedItem("name");
+ Node nameAttribute = getNameOrRefElement(node);
if (nameAttribute.getNodeValue().equals(complexTypeName)) {
NodeList complexTypeChildList =node.getChildNodes();
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java
index 9621b91eb4..d0e118ae20 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java
@@ -49,7 +49,8 @@ import org.xml.sax.SAXException;
* @author Roberto Manicardi
*/
public final class TestXSSFExportToXML extends TestCase {
- public void testExportToXML() throws Exception {
+
+ public void testExportToXML() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
@@ -580,4 +581,37 @@ public final class TestXSSFExportToXML extends TestCase {
}
assertTrue(found);
}
+
+ public void testRefElementsInXmlSchema_Bugzilla_56730() throws Exception {
+ XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.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("2014-12-31", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
+ assertEquals("12.5", xmlData.split("<REFELEMENT>")[1].split("</REFELEMENT>")[0].trim());
+
+ parseXML(xmlData);
+
+ found = true;
+ }
+ assertTrue(found);
+ }
}