aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/java/org/apache/poi
diff options
context:
space:
mode:
authorJosh Micich <josh@apache.org>2009-10-26 19:08:54 +0000
committerJosh Micich <josh@apache.org>2009-10-26 19:08:54 +0000
commit9743058a89b26423979ae653706be4d29f6b2723 (patch)
treecd33e3159ec8f50cffb44983fd7734118c64cbdf /src/ooxml/java/org/apache/poi
parent3bc57ef584692747ffd9236e5f8003e0a7d78c9d (diff)
downloadpoi-9743058a89b26423979ae653706be4d29f6b2723.tar.gz
poi-9743058a89b26423979ae653706be4d29f6b2723.zip
simplified code copied from xalan (in r829781) to bare minimum required by XSSFImportFromXML
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@829903 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org/apache/poi')
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java110
1 files changed, 81 insertions, 29 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java
index e3ddc3fb74..854dc305bb 100644
--- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java
+++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java
@@ -39,16 +39,15 @@ import org.apache.poi.xssf.usermodel.XSSFMap;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell;
import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
-import org.apache.xml.utils.PrefixResolver;
-import org.apache.xml.utils.PrefixResolverDefault;
import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
- *
* Imports data from an external XML to an XLSX according to one of the mappings
* defined.The output XML Schema must respect this limitations:
* <ul>
@@ -59,21 +58,21 @@ import org.xml.sax.SAXException;
*/
public class XSSFImportFromXML {
- private XSSFMap map;
+ private final XSSFMap _map;
private static POILogger logger = POILogFactory.getLogger(XSSFImportFromXML.class);
public XSSFImportFromXML(XSSFMap map) {
- this.map = map;
+ _map = map;
}
/**
* Imports an XML into the XLSX using the Custom XML mapping defined
- *
+ *
* @param xmlInputString the XML to import
* @throws SAXException if error occurs during XML parsing
* @throws XPathExpressionException if error occurs during XML navigation
- * @throws ParserConfigurationException if there are problems with XML parser configuration
+ * @throws ParserConfigurationException if there are problems with XML parser configuration
* @throws IOException if there are problems reading the input string
*/
public void importFromXML(String xmlInputString) throws SAXException, XPathExpressionException, ParserConfigurationException, IOException {
@@ -84,9 +83,9 @@ public class XSSFImportFromXML {
Document doc = builder.parse(new InputSource(new StringReader(xmlInputString.trim())));
- List<XSSFSingleXmlCell> singleXmlCells = map.getRelatedSingleXMLCell();
+ List<XSSFSingleXmlCell> singleXmlCells = _map.getRelatedSingleXMLCell();
- List<Table> tables = map.getRelatedTables();
+ List<Table> tables = _map.getRelatedTables();
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
@@ -94,25 +93,7 @@ public class XSSFImportFromXML {
// Setting namespace context to XPath
// Assuming that the namespace prefix in the mapping xpath is the
// same as the one used in the document
- final PrefixResolver resolver = new PrefixResolverDefault(doc.getDocumentElement());
-
- NamespaceContext ctx = new NamespaceContext() {
-
- public String getNamespaceURI(String prefix) {
- return resolver.getNamespaceForPrefix(prefix);
- }
-
- // Dummy implementation - not used!
- public Iterator getPrefixes(String val) {
- return null;
- }
-
- // Dummy implemenation - not used!
- public String getPrefix(String uri) {
- return null;
- }
- };
- xpath.setNamespaceContext(ctx);
+ xpath.setNamespaceContext(new DefaultNamespaceContext(doc));
for (XSSFSingleXmlCell singleXmlCell : singleXmlCells) {
@@ -165,12 +146,83 @@ public class XSSFImportFromXML {
logger.log(POILogger.DEBUG, "Setting '" + value + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
+ table.getXSSFSheet().getSheetName());
cell.setCellValue(value.trim());
-
}
+ }
+ }
+ }
+
+ private static final class DefaultNamespaceContext implements NamespaceContext {
+ /**
+ * Node from which to start searching for a xmlns attribute that binds a
+ * prefix to a namespace.
+ */
+ private final Element _docElem;
+
+ public DefaultNamespaceContext(Document doc) {
+ _docElem = doc.getDocumentElement();
+ }
+
+ public String getNamespaceURI(String prefix) {
+ return getNamespaceForPrefix(prefix);
+ }
+ /**
+ * @param prefix Prefix to resolve.
+ * @return uri of Namespace that prefix resolves to, or
+ * <code>null</code> if specified prefix is not bound.
+ */
+ private String getNamespaceForPrefix(String prefix) {
+
+ // Code adapted from Xalan's org.apache.xml.utils.PrefixResolverDefault.getNamespaceForPrefix()
+
+ if (prefix.equals("xml")) {
+ return "http://www.w3.org/XML/1998/namespace";
}
+ Node parent = _docElem;
+
+ while (parent != null) {
+
+ int type = parent.getNodeType();
+ if (type == Node.ELEMENT_NODE) {
+ if (parent.getNodeName().startsWith(prefix + ":")) {
+ return parent.getNamespaceURI();
+ }
+ NamedNodeMap nnm = parent.getAttributes();
+
+ for (int i = 0; i < nnm.getLength(); i++) {
+ Node attr = nnm.item(i);
+ String aname = attr.getNodeName();
+ boolean isPrefix = aname.startsWith("xmlns:");
+
+ if (isPrefix || aname.equals("xmlns")) {
+ int index = aname.indexOf(':');
+ String p = isPrefix ? aname.substring(index + 1) : "";
+
+ if (p.equals(prefix)) {
+ return attr.getNodeValue();
+ }
+ }
+ }
+ } else if (type == Node.ENTITY_REFERENCE_NODE) {
+ continue;
+ } else {
+ break;
+ }
+ parent = parent.getParentNode();
+ }
+
+ return null;
}
+ // Dummy implementation - not used!
+ public Iterator getPrefixes(String val) {
+ return null;
+ }
+
+ // Dummy implementation - not used!
+ public String getPrefix(String uri) {
+ return null;
+ }
}
}