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.

XSSFImportFromXML.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.StringReader;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import javax.xml.namespace.NamespaceContext;
  21. import javax.xml.parsers.DocumentBuilder;
  22. import javax.xml.parsers.ParserConfigurationException;
  23. import javax.xml.xpath.XPath;
  24. import javax.xml.xpath.XPathConstants;
  25. import javax.xml.xpath.XPathExpressionException;
  26. import javax.xml.xpath.XPathFactory;
  27. import org.apache.poi.util.DocumentHelper;
  28. import org.apache.poi.util.POILogFactory;
  29. import org.apache.poi.util.POILogger;
  30. import org.apache.poi.xssf.usermodel.XSSFCell;
  31. import org.apache.poi.xssf.usermodel.XSSFMap;
  32. import org.apache.poi.xssf.usermodel.XSSFRow;
  33. import org.apache.poi.xssf.usermodel.XSSFTable;
  34. import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell;
  35. import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
  36. import org.w3c.dom.Document;
  37. import org.w3c.dom.Element;
  38. import org.w3c.dom.NamedNodeMap;
  39. import org.w3c.dom.Node;
  40. import org.w3c.dom.NodeList;
  41. import org.xml.sax.InputSource;
  42. import org.xml.sax.SAXException;
  43. /**
  44. * Imports data from an external XML to an XLSX according to one of the mappings
  45. * defined.The output XML Schema must respect this limitations:
  46. * <ul>
  47. * <li>the input XML must be valid according to the XML Schema used in the mapping</li>
  48. * <li>denormalized table mapping is not supported (see OpenOffice part 4: chapter 3.5.1.7)</li>
  49. * <li>all the namespaces used in the document must be declared in the root node</li>
  50. * </ul>
  51. */
  52. public class XSSFImportFromXML {
  53. private final XSSFMap _map;
  54. private static POILogger logger = POILogFactory.getLogger(XSSFImportFromXML.class);
  55. public XSSFImportFromXML(XSSFMap map) {
  56. _map = map;
  57. }
  58. /**
  59. * Imports an XML into the XLSX using the Custom XML mapping defined
  60. *
  61. * @param xmlInputString the XML to import
  62. * @throws SAXException if error occurs during XML parsing
  63. * @throws XPathExpressionException if error occurs during XML navigation
  64. * @throws ParserConfigurationException if there are problems with XML parser configuration
  65. * @throws IOException if there are problems reading the input string
  66. */
  67. public void importFromXML(String xmlInputString) throws SAXException, XPathExpressionException, IOException {
  68. DocumentBuilder builder = DocumentHelper.newDocumentBuilder();
  69. Document doc = builder.parse(new InputSource(new StringReader(xmlInputString.trim())));
  70. List<XSSFSingleXmlCell> singleXmlCells = _map.getRelatedSingleXMLCell();
  71. List<XSSFTable> tables = _map.getRelatedTables();
  72. XPathFactory xpathFactory = XPathFactory.newInstance();
  73. XPath xpath = xpathFactory.newXPath();
  74. // Setting namespace context to XPath
  75. // Assuming that the namespace prefix in the mapping xpath is the
  76. // same as the one used in the document
  77. xpath.setNamespaceContext(new DefaultNamespaceContext(doc));
  78. for (XSSFSingleXmlCell singleXmlCell : singleXmlCells) {
  79. String xpathString = singleXmlCell.getXpath();
  80. Node result = (Node) xpath.evaluate(xpathString, doc, XPathConstants.NODE);
  81. String textContent = result.getTextContent();
  82. logger.log(POILogger.DEBUG, "Extracting with xpath " + xpathString + " : value is '" + textContent + "'");
  83. XSSFCell cell = singleXmlCell.getReferencedCell();
  84. logger.log(POILogger.DEBUG, "Setting '" + textContent + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
  85. + cell.getSheet().getSheetName());
  86. cell.setCellValue(textContent);
  87. }
  88. for (XSSFTable table : tables) {
  89. String commonXPath = table.getCommonXpath();
  90. NodeList result = (NodeList) xpath.evaluate(commonXPath, doc, XPathConstants.NODESET);
  91. int rowOffset = table.getStartCellReference().getRow() + 1;// the first row contains the table header
  92. int columnOffset = table.getStartCellReference().getCol() - 1;
  93. for (int i = 0; i < result.getLength(); i++) {
  94. // TODO: implement support for denormalized XMLs (see
  95. // OpenOffice part 4: chapter 3.5.1.7)
  96. for (XSSFXmlColumnPr xmlColumnPr : table.getXmlColumnPrs()) {
  97. int localColumnId = (int) xmlColumnPr.getId();
  98. int rowId = rowOffset + i;
  99. int columnId = columnOffset + localColumnId;
  100. String localXPath = xmlColumnPr.getLocalXPath();
  101. localXPath = localXPath.substring(localXPath.substring(1).indexOf('/') + 1);
  102. // Build an XPath to select the right node (assuming
  103. // that the commonXPath != "/")
  104. String nodeXPath = commonXPath + "[" + (i + 1) + "]" + localXPath;
  105. // TODO: convert the data to the cell format
  106. String value = (String) xpath.evaluate(nodeXPath, result.item(i), XPathConstants.STRING);
  107. logger.log(POILogger.DEBUG, "Extracting with xpath " + nodeXPath + " : value is '" + value + "'");
  108. XSSFRow row = table.getXSSFSheet().getRow(rowId);
  109. if (row == null) {
  110. row = table.getXSSFSheet().createRow(rowId);
  111. }
  112. XSSFCell cell = row.getCell(columnId);
  113. if (cell == null) {
  114. cell = row.createCell(columnId);
  115. }
  116. logger.log(POILogger.DEBUG, "Setting '" + value + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
  117. + table.getXSSFSheet().getSheetName());
  118. cell.setCellValue(value.trim());
  119. }
  120. }
  121. }
  122. }
  123. private static final class DefaultNamespaceContext implements NamespaceContext {
  124. /**
  125. * Node from which to start searching for a xmlns attribute that binds a
  126. * prefix to a namespace.
  127. */
  128. private final Element _docElem;
  129. public DefaultNamespaceContext(Document doc) {
  130. _docElem = doc.getDocumentElement();
  131. }
  132. public String getNamespaceURI(String prefix) {
  133. return getNamespaceForPrefix(prefix);
  134. }
  135. /**
  136. * @param prefix Prefix to resolve.
  137. * @return uri of Namespace that prefix resolves to, or
  138. * <code>null</code> if specified prefix is not bound.
  139. */
  140. private String getNamespaceForPrefix(String prefix) {
  141. // Code adapted from Xalan's org.apache.xml.utils.PrefixResolverDefault.getNamespaceForPrefix()
  142. if (prefix.equals("xml")) {
  143. return "http://www.w3.org/XML/1998/namespace";
  144. }
  145. Node parent = _docElem;
  146. while (parent != null) {
  147. int type = parent.getNodeType();
  148. if (type == Node.ELEMENT_NODE) {
  149. if (parent.getNodeName().startsWith(prefix + ":")) {
  150. return parent.getNamespaceURI();
  151. }
  152. NamedNodeMap nnm = parent.getAttributes();
  153. for (int i = 0; i < nnm.getLength(); i++) {
  154. Node attr = nnm.item(i);
  155. String aname = attr.getNodeName();
  156. boolean isPrefix = aname.startsWith("xmlns:");
  157. if (isPrefix || aname.equals("xmlns")) {
  158. int index = aname.indexOf(':');
  159. String p = isPrefix ? aname.substring(index + 1) : "";
  160. if (p.equals(prefix)) {
  161. return attr.getNodeValue();
  162. }
  163. }
  164. }
  165. } else if (type == Node.ENTITY_REFERENCE_NODE) {
  166. continue;
  167. } else {
  168. break;
  169. }
  170. parent = parent.getParentNode();
  171. }
  172. return null;
  173. }
  174. // Dummy implementation - not used!
  175. public Iterator getPrefixes(String val) {
  176. return null;
  177. }
  178. // Dummy implementation - not used!
  179. public String getPrefix(String uri) {
  180. return null;
  181. }
  182. }
  183. }