You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSSFExportToXml.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.extractor;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.text.DateFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Collections;
  21. import java.util.Comparator;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Vector;
  26. import javax.xml.parsers.DocumentBuilder;
  27. import javax.xml.parsers.DocumentBuilderFactory;
  28. import javax.xml.parsers.ParserConfigurationException;
  29. import javax.xml.transform.OutputKeys;
  30. import javax.xml.transform.Source;
  31. import javax.xml.transform.Transformer;
  32. import javax.xml.transform.TransformerException;
  33. import javax.xml.transform.TransformerFactory;
  34. import javax.xml.transform.dom.DOMSource;
  35. import javax.xml.transform.stream.StreamResult;
  36. import javax.xml.validation.Schema;
  37. import javax.xml.validation.SchemaFactory;
  38. import javax.xml.validation.Validator;
  39. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  40. import org.apache.poi.ss.usermodel.Cell;
  41. import org.apache.poi.ss.usermodel.DateUtil;
  42. import org.apache.poi.util.XMLHelper;
  43. import org.apache.poi.xssf.usermodel.XSSFCell;
  44. import org.apache.poi.xssf.usermodel.XSSFMap;
  45. import org.apache.poi.xssf.usermodel.XSSFRow;
  46. import org.apache.poi.xssf.usermodel.XSSFSheet;
  47. import org.apache.poi.xssf.usermodel.XSSFTable;
  48. import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell;
  49. import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
  50. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXmlDataType;
  51. import org.w3c.dom.Document;
  52. import org.w3c.dom.Element;
  53. import org.w3c.dom.NamedNodeMap;
  54. import org.w3c.dom.Node;
  55. import org.w3c.dom.NodeList;
  56. import org.xml.sax.SAXException;
  57. /**
  58. *
  59. * Maps an XLSX to an XML according to one of the mapping defined.
  60. *
  61. *
  62. * The output XML Schema must respect this limitations:
  63. *
  64. * <ul>
  65. * <li> all mandatory elements and attributes must be mapped (enable validation to check this)</li>
  66. *
  67. * <li> no &lt;any&gt; in complex type/element declaration </li>
  68. * <li> no &lt;anyAttribute&gt; attributes declaration </li>
  69. * <li> no recursive structures: recursive structures can't be nested more than one level </li>
  70. * <li> no abstract elements: abstract complex types can be declared but must not be used in elements. </li>
  71. * <li> no mixed content: an element can't contain simple text and child element(s) together </li>
  72. * <li> no &lt;substitutionGroup&gt; in complex type/element declaration </li>
  73. * </ul>
  74. */
  75. public class XSSFExportToXml implements Comparator<String>{
  76. private XSSFMap map;
  77. /**
  78. * Creates a new exporter and sets the mapping to be used when generating the XML output document
  79. *
  80. * @param map the mapping rule to be used
  81. */
  82. public XSSFExportToXml(XSSFMap map) {
  83. this.map = map;
  84. }
  85. /**
  86. *
  87. * Exports the data in an XML stream
  88. *
  89. * @param os OutputStream in which will contain the output XML
  90. * @param validate if true, validates the XML againts the XML Schema
  91. * @throws SAXException
  92. * @throws TransformerException
  93. * @throws ParserConfigurationException
  94. */
  95. public void exportToXML(OutputStream os, boolean validate) throws SAXException, ParserConfigurationException, TransformerException {
  96. exportToXML(os, "UTF-8", validate);
  97. }
  98. private Document getEmptyDocument() throws ParserConfigurationException{
  99. DocumentBuilderFactory dbfac = XMLHelper.getDocumentBuilderFactory();
  100. DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
  101. Document doc = docBuilder.newDocument();
  102. return doc;
  103. }
  104. /**
  105. * Exports the data in an XML stream
  106. *
  107. * @param os OutputStream in which will contain the output XML
  108. * @param encoding the output charset encoding
  109. * @param validate if true, validates the XML againts the XML Schema
  110. * @throws SAXException
  111. * @throws ParserConfigurationException
  112. * @throws TransformerException
  113. * @throws InvalidFormatException
  114. */
  115. public void exportToXML(OutputStream os, String encoding, boolean validate) throws SAXException, ParserConfigurationException, TransformerException{
  116. List<XSSFSingleXmlCell> singleXMLCells = map.getRelatedSingleXMLCell();
  117. List<XSSFTable> tables = map.getRelatedTables();
  118. String rootElement = map.getCtMap().getRootElement();
  119. Document doc = getEmptyDocument();
  120. Element root = null;
  121. if (isNamespaceDeclared()) {
  122. root=doc.createElementNS(getNamespace(),rootElement);
  123. } else {
  124. root = doc.createElementNS("", rootElement);
  125. }
  126. doc.appendChild(root);
  127. List<String> xpaths = new Vector<String>();
  128. Map<String,XSSFSingleXmlCell> singleXmlCellsMappings = new HashMap<String,XSSFSingleXmlCell>();
  129. Map<String,XSSFTable> tableMappings = new HashMap<String,XSSFTable>();
  130. for(XSSFSingleXmlCell simpleXmlCell : singleXMLCells) {
  131. xpaths.add(simpleXmlCell.getXpath());
  132. singleXmlCellsMappings.put(simpleXmlCell.getXpath(), simpleXmlCell);
  133. }
  134. for(XSSFTable table : tables) {
  135. String commonXPath = table.getCommonXpath();
  136. xpaths.add(commonXPath);
  137. tableMappings.put(commonXPath, table);
  138. }
  139. Collections.sort(xpaths,this);
  140. for(String xpath : xpaths) {
  141. XSSFSingleXmlCell simpleXmlCell = singleXmlCellsMappings.get(xpath);
  142. XSSFTable table = tableMappings.get(xpath);
  143. if (!xpath.matches(".*\\[.*")) {
  144. // Exports elements and attributes mapped with simpleXmlCell
  145. if (simpleXmlCell!=null) {
  146. XSSFCell cell = simpleXmlCell.getReferencedCell();
  147. if (cell!=null) {
  148. Node currentNode = getNodeByXPath(xpath,doc.getFirstChild(),doc,false);
  149. STXmlDataType.Enum dataType = simpleXmlCell.getXmlDataType();
  150. mapCellOnNode(cell,currentNode,dataType);
  151. //remove nodes which are empty in order to keep the output xml valid
  152. if("".equals(currentNode.getTextContent()) && currentNode.getParentNode() != null) {
  153. currentNode.getParentNode().removeChild(currentNode);
  154. }
  155. }
  156. }
  157. // Exports elements and attributes mapped with tables
  158. if (table!=null) {
  159. List<XSSFXmlColumnPr> tableColumns = table.getXmlColumnPrs();
  160. XSSFSheet sheet = table.getXSSFSheet();
  161. int startRow = table.getStartCellReference().getRow();
  162. // In mappings created with Microsoft Excel the first row contains the table header and must be skipped
  163. startRow +=1;
  164. int endRow = table.getEndCellReference().getRow();
  165. for(int i = startRow; i<= endRow; i++) {
  166. XSSFRow row = sheet.getRow(i);
  167. Node tableRootNode = getNodeByXPath(table.getCommonXpath(),doc.getFirstChild(),doc,true);
  168. short startColumnIndex = table.getStartCellReference().getCol();
  169. for(int j = startColumnIndex; j<= table.getEndCellReference().getCol();j++) {
  170. XSSFCell cell = row.getCell(j);
  171. if (cell!=null) {
  172. XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex);
  173. String localXPath = pointer.getLocalXPath();
  174. Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
  175. STXmlDataType.Enum dataType = pointer.getXmlDataType();
  176. mapCellOnNode(cell,currentNode,dataType);
  177. }
  178. }
  179. }
  180. }
  181. } else {
  182. // TODO: implement filtering management in xpath
  183. }
  184. }
  185. boolean isValid = true;
  186. if (validate) {
  187. isValid =isValid(doc);
  188. }
  189. if (isValid) {
  190. /////////////////
  191. //Output the XML
  192. //set up a transformer
  193. TransformerFactory transfac = TransformerFactory.newInstance();
  194. Transformer trans = transfac.newTransformer();
  195. trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  196. trans.setOutputProperty(OutputKeys.INDENT, "yes");
  197. trans.setOutputProperty(OutputKeys.ENCODING, encoding);
  198. //create string from xml tree
  199. StreamResult result = new StreamResult(os);
  200. DOMSource source = new DOMSource(doc);
  201. trans.transform(source, result);
  202. }
  203. }
  204. /**
  205. * Validate the generated XML against the XML Schema associated with the XSSFMap
  206. *
  207. * @param xml the XML to validate
  208. * @return true, if document is valid
  209. */
  210. private boolean isValid(Document xml) throws SAXException{
  211. try{
  212. String language = "http://www.w3.org/2001/XMLSchema";
  213. SchemaFactory factory = SchemaFactory.newInstance(language);
  214. Source source = new DOMSource(map.getSchema());
  215. Schema schema = factory.newSchema(source);
  216. Validator validator = schema.newValidator();
  217. validator.validate(new DOMSource(xml));
  218. //if no exceptions where raised, the document is valid
  219. return true;
  220. } catch(IOException e) {
  221. e.printStackTrace();
  222. }
  223. return false;
  224. }
  225. private void mapCellOnNode(XSSFCell cell, Node node, STXmlDataType.Enum outputDataType) {
  226. String value ="";
  227. switch (cell.getCellType()) {
  228. case XSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break;
  229. case XSSFCell.CELL_TYPE_BOOLEAN: value += cell.getBooleanCellValue(); break;
  230. case XSSFCell.CELL_TYPE_ERROR: value = cell.getErrorCellString(); break;
  231. case XSSFCell.CELL_TYPE_FORMULA:
  232. if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) {
  233. value = cell.getStringCellValue();
  234. } else {
  235. if (DateUtil.isCellDateFormatted(cell)) {
  236. value = getFormattedDate(cell);
  237. } else {
  238. value += cell.getNumericCellValue();
  239. }
  240. }
  241. break;
  242. case XSSFCell.CELL_TYPE_NUMERIC:
  243. if (DateUtil.isCellDateFormatted(cell)) {
  244. value = getFormattedDate(cell);
  245. } else {
  246. value += cell.getRawValue();
  247. }
  248. break;
  249. default: ;
  250. }
  251. if (node instanceof Element) {
  252. Element currentElement = (Element) node;
  253. currentElement.setTextContent(value);
  254. } else {
  255. node.setNodeValue(value);
  256. }
  257. }
  258. private String removeNamespace(String elementName) {
  259. return elementName.matches(".*:.*")?elementName.split(":")[1]:elementName;
  260. }
  261. private String getFormattedDate(XSSFCell cell) {
  262. DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  263. return sdf.format(cell.getDateCellValue());
  264. }
  265. private Node getNodeByXPath(String xpath,Node rootNode,Document doc,boolean createMultipleInstances) {
  266. String[] xpathTokens = xpath.split("/");
  267. Node currentNode =rootNode;
  268. // The first token is empty, the second is the root node
  269. for(int i =2; i<xpathTokens.length;i++) {
  270. String axisName = removeNamespace(xpathTokens[i]);
  271. if (!axisName.startsWith("@")) {
  272. NodeList list =currentNode.getChildNodes();
  273. Node selectedNode = null;
  274. if (!(createMultipleInstances && i==xpathTokens.length-1) ) {
  275. // select the last child node only if we need to map to a single cell
  276. selectedNode = selectNode(axisName, list);
  277. }
  278. if (selectedNode==null) {
  279. selectedNode = createElement(doc, currentNode, axisName);
  280. }
  281. currentNode = selectedNode;
  282. } else {
  283. Node attribute = createAttribute(doc, currentNode, axisName);
  284. currentNode = attribute;
  285. }
  286. }
  287. return currentNode;
  288. }
  289. private Node createAttribute(Document doc, Node currentNode, String axisName) {
  290. String attributeName = axisName.substring(1);
  291. NamedNodeMap attributesMap = currentNode.getAttributes();
  292. Node attribute = attributesMap.getNamedItem(attributeName);
  293. if (attribute==null) {
  294. attribute = doc.createAttributeNS("", attributeName);
  295. attributesMap.setNamedItem(attribute);
  296. }
  297. return attribute;
  298. }
  299. private Node createElement(Document doc, Node currentNode, String axisName) {
  300. Node selectedNode;
  301. if (isNamespaceDeclared()) {
  302. selectedNode =doc.createElementNS(getNamespace(),axisName);
  303. } else {
  304. selectedNode = doc.createElementNS("", axisName);
  305. }
  306. currentNode.appendChild(selectedNode);
  307. return selectedNode;
  308. }
  309. private Node selectNode(String axisName, NodeList list) {
  310. Node selectedNode = null;
  311. for(int j=0;j<list.getLength();j++) {
  312. Node node = list.item(j);
  313. if (node.getNodeName().equals(axisName)) {
  314. selectedNode=node;
  315. break;
  316. }
  317. }
  318. return selectedNode;
  319. }
  320. private boolean isNamespaceDeclared() {
  321. String schemaNamespace = getNamespace();
  322. return schemaNamespace!=null && !schemaNamespace.equals("");
  323. }
  324. private String getNamespace() {
  325. return map.getCTSchema().getNamespace();
  326. }
  327. /**
  328. * Compares two xpaths to define an ordering according to the XML Schema
  329. *
  330. */
  331. @Override
  332. public int compare(String leftXpath, String rightXpath) {
  333. Node xmlSchema = map.getSchema();
  334. String[] leftTokens = leftXpath.split("/");
  335. String[] rightTokens = rightXpath.split("/");
  336. int minLenght = leftTokens.length< rightTokens.length? leftTokens.length : rightTokens.length;
  337. Node localComplexTypeRootNode = xmlSchema;
  338. for(int i =1;i <minLenght; i++) {
  339. String leftElementName =leftTokens[i];
  340. String rightElementName = rightTokens[i];
  341. if (leftElementName.equals(rightElementName)) {
  342. Node complexType = getComplexTypeForElement(leftElementName, xmlSchema,localComplexTypeRootNode);
  343. localComplexTypeRootNode = complexType;
  344. } else {
  345. int leftIndex = indexOfElementInComplexType(leftElementName,localComplexTypeRootNode);
  346. int rightIndex = indexOfElementInComplexType(rightElementName,localComplexTypeRootNode);
  347. if (leftIndex!=-1 && rightIndex!=-1) {
  348. if ( leftIndex < rightIndex) {
  349. return -1;
  350. }if ( leftIndex > rightIndex) {
  351. return 1;
  352. }
  353. } else {
  354. // NOTE: the xpath doesn't match correctly in the schema
  355. }
  356. }
  357. }
  358. return 0;
  359. }
  360. private int indexOfElementInComplexType(String elementName,Node complexType) {
  361. NodeList list = complexType.getChildNodes();
  362. int indexOf = -1;
  363. for(int i=0; i< list.getLength();i++) {
  364. Node node = list.item(i);
  365. if (node instanceof Element) {
  366. if (node.getLocalName().equals("element")) {
  367. Node nameAttribute = node.getAttributes().getNamedItem("name");
  368. if (nameAttribute.getNodeValue().equals(removeNamespace(elementName))) {
  369. indexOf = i;
  370. break;
  371. }
  372. }
  373. }
  374. }
  375. return indexOf;
  376. }
  377. private Node getComplexTypeForElement(String elementName,Node xmlSchema,Node localComplexTypeRootNode) {
  378. String elementNameWithoutNamespace = removeNamespace(elementName);
  379. String complexTypeName = getComplexTypeNameFromChildren(localComplexTypeRootNode, elementNameWithoutNamespace);
  380. // Note: we expect that all the complex types are defined at root level
  381. Node complexTypeNode = null;
  382. if (!"".equals(complexTypeName)) {
  383. complexTypeNode = getComplexTypeNodeFromSchemaChildren(xmlSchema, complexTypeNode, complexTypeName);
  384. }
  385. return complexTypeNode;
  386. }
  387. private String getComplexTypeNameFromChildren(Node localComplexTypeRootNode,
  388. String elementNameWithoutNamespace) {
  389. NodeList list = localComplexTypeRootNode.getChildNodes();
  390. String complexTypeName = "";
  391. for(int i=0; i< list.getLength();i++) {
  392. Node node = list.item(i);
  393. if ( node instanceof Element) {
  394. if (node.getLocalName().equals("element")) {
  395. Node nameAttribute = node.getAttributes().getNamedItem("name");
  396. if (nameAttribute.getNodeValue().equals(elementNameWithoutNamespace)) {
  397. Node complexTypeAttribute = node.getAttributes().getNamedItem("type");
  398. if (complexTypeAttribute!=null) {
  399. complexTypeName = complexTypeAttribute.getNodeValue();
  400. break;
  401. }
  402. }
  403. }
  404. }
  405. }
  406. return complexTypeName;
  407. }
  408. private Node getComplexTypeNodeFromSchemaChildren(Node xmlSchema, Node complexTypeNode,
  409. String complexTypeName) {
  410. NodeList complexTypeList = xmlSchema.getChildNodes();
  411. for(int i=0; i< complexTypeList.getLength();i++) {
  412. Node node = complexTypeList.item(i);
  413. if ( node instanceof Element) {
  414. if (node.getLocalName().equals("complexType")) {
  415. Node nameAttribute = node.getAttributes().getNamedItem("name");
  416. if (nameAttribute.getNodeValue().equals(complexTypeName)) {
  417. NodeList complexTypeChildList =node.getChildNodes();
  418. for(int j=0; j<complexTypeChildList.getLength();j++) {
  419. Node sequence = complexTypeChildList.item(j);
  420. if ( sequence instanceof Element) {
  421. if (sequence.getLocalName().equals("sequence") || sequence.getLocalName().equals("all")) {
  422. complexTypeNode = sequence;
  423. break;
  424. }
  425. }
  426. }
  427. if (complexTypeNode!=null) {
  428. break;
  429. }
  430. }
  431. }
  432. }
  433. }
  434. return complexTypeNode;
  435. }
  436. }