From de7cedad2be446d6ef235ea25d1a0b0734704569 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 28 Aug 2014 21:41:30 +0000 Subject: [PATCH] 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 --- .../poi/xssf/extractor/XSSFExportToXml.java | 17 +++++++-- .../xssf/extractor/TestXSSFExportToXML.java | 36 +++++++++++++++++- test-data/spreadsheet/56730.xlsx | Bin 0 -> 9808 bytes 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 test-data/spreadsheet/56730.xlsx 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{ 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{ 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{ 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{ 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("")[1].split("")[0].trim()); + assertEquals("12.5", xmlData.split("")[1].split("")[0].trim()); + + parseXML(xmlData); + + found = true; + } + assertTrue(found); + } } diff --git a/test-data/spreadsheet/56730.xlsx b/test-data/spreadsheet/56730.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..9883de4f6671df377b4d959fd0a9ee71fdcf95e3 GIT binary patch literal 9808 zcmeHtWmFv7()QpI++BhP8!SME;0^;JxCIXo90my*ToYt)%@8z!AW0y2a0%`bT!XvI zJIT4{%Q@WKcfG&9Klk)n(|h&muIkyf>#2HnRXXaZu@sZRP8 zIA=@(pn2BWj>c0?QWO^mFehv2RB0#U-JR7CTqfCJ&9# zrJ^)f9mKrS$0@qKqAVrf8I*wN48aws&K0QU&5c`NzjC)17@Cyby2Jrq3sjMq&-_zd zjn{HM)|2=@xrzd*b+=-9lngze3l>07mX``_M7^?PZIujNvXIFVR1~o?@3exBJ1#2= z*}@ ztWG$^kR()aXd(=fM5Z#wzz*mbwE(bD-6&no_=)eLO|;P1_Wru^n7jsb@4QztVgpV$ zat{!%ZoVuY?6ZdhS-1erdPq;Irm)vAR_O9ty3J1MD*J(m#e4e|e(zi8uX!{-YMMDv z$2E8e`7wi#tlz53iO&TF)CGSmUdW8C{qEg_#Qmb}im`*lo@Ux@7aIV$xj_Ov`Wse1 zX#yFJ;0~w^hvFSLR*jr3Y+bmyew_co=zlRV|1$NcxIUF;AXeLse67^d#k##bIxM$F zS-yr|gXRn4HtnaEZy3c-j)RZ_6wRgvj#vA{^k=qLtTkwt63$=_)NXVo_Q2! zY2m&$y=!Sr)K2k=%0zp5#@5IqCqB(|?4qDNGLoV?{*u9zpIVjZ&fcbTZY)r(%fxB` zN?u^`Eq?i7NCV9yPLYwuXXt2NXm3T&Mb64A`N0kz+$%n&Nh(p|nS`LPZp%_n?-ce; z6{JdjRR|?#dDZF2P?m%QV!bN;z!=Ee5s_?X~S7aeS zXdY~SYSP$>t6>D6_-{}`@ni@9&uAS;000F59l^tv`wyVFJ2=~zIyl(;$Z3Cq1_7Sf z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