Writing an extension with DOM4J/JDOM params
Added by Anonymous about 17 years ago
Legacy ID: #4620786 Legacy Poster: Pimousse (pimousse76)
Hi all, First, bit of background: - Java 5.0 under Eclipse 3.3 - classpath: + saxon9.jar + jdom.jar (1.0) and dom4j-1.6.1.jar + saxon9-dom.jar, saxon9-dom4j.jar, saxon9-jdom.jar My aim: Creating an XSLT extension for Saxon which accept as a parameter a DOM4J or JDOM object (such as org.dom4j.Element or org.dom4j.Node). I know I can do it using "native" Saxon objects such as NodeInfo, but this POC (for my boss) should validate it's possible to do it with DOM4j of JDOM objects - cf doc: "Saxon recognizes methods that expect nodes in an external object model (DOM, DOM4J, JDOM, or XOM) only if the supporting JAR file is on the classpath (that is, saxon9-dom.jar, saxon9-dom4j.jar, saxon9-jdom.jar, or saxon9-xom.jar)." What's done so far: I succeeded to create an XSLT extension with org.w3c.dom objects as parameter, such as: + public static void domNode(org.w3c.dom.Node node) + public static void domElem(org.w3c.dom.Element elem) + public static void domNodeList(org.w3c.dom.NodeList nodelist) These methods work 'perfectly', I am able to access the DOM objects, browse the tree, etc. However, as specified in the doc, the Saxon wrapper is a read-only DOM implementation (see http://www.saxonica.com/documentation/extensibility/converting-args/converting-node.html), and I would appreciate a read/write functionality. And DOM is not the most powerful implementation (nor in terms of speed or footprint), compared to JDOM of DOM4j (cf:http://saxonica.blogharbor.com/blog/_archives/2007/3/31/2848654.html). So I would like to do the same, but with DOM4j or JDOM objects as parameters of my extension. If I use such objects, e.g. + public static void dom4jNode(org.dom4j.Node node) + public static void dom4jElem(org.dom4j.Element elem) + public static void jdomElem(org.jdom.Element elem) I systematically get a SXJE0021 error such as: net.sf.saxon.trans.XPathException: Error in call to extension function {public static void com.amadeus.cif.xslt.Extension.jdomElem(org.jdom.Element)}: Cannot convert supplied XPath value to the required type for the extension function at net.sf.saxon.functions.ExtensionFunctionCall.iterate(ExtensionFunctionCall.java:151) I maybe missed something in the doc, but I did neither find a clue in the resources/samples - there are examples about how to load an xml doc in JDOM or DOM4j from a source file, but that's not what I want to do - nor on the web. If anyone has a pointer to a doc/sample or a tip, I would greatly appreciate ;o) Regards.
Replies (2)
RE: Writing an extension with DOM4J/JDOM para - Added by Anonymous about 17 years ago
Legacy ID: #4621783 Legacy Poster: Michael Kay (mhkay)
There's reasonable support for extension functions expecting a DOM or XOM node as an argument, but none for JDOM or DOM4J, I'm afraid. However, implementing the following method in classes JDOMObjectModel and DOM4JObjectModel seems to get past the first hurdle: public Object convertXPathValueToObject(Value value, Class targetClass, XPathContext context) { if (value instanceof SingletonNode) { NodeInfo node = ((SingletonNode)value).getNode(); if (node instanceof VirtualNode) { Object u = ((VirtualNode)node).getUnderlyingNode(); if (targetClass.isAssignableFrom(u.getClass())) { return u; } } } return null; } With this method in place, an extension function that expects an argument of class (JDOM) Element, say, will detect that the node being passed to the function is a wrapper round a JDOM element and pass the underlying element. You can see a more sophisticated version of the method in the XOMObjectModel class - this also handles collections of nodes. XOM also allows a node to be returned from an extension function - I'm not entirely convinced this is wise, since I think it leads to node identity problems if you return the same node that you were passed as an argument. I would advise against attempting to update the tree from an extension function. Because Saxon expects functions to be free of side-effects and the order of execution is undefined, all sorts of things could potentially go wrong. If you need to do this, there might be something wrong with your application architecture.
RE: Writing an extension with DOM4J/JDOM para - Added by Anonymous about 17 years ago
Legacy ID: #4622641 Legacy Poster: Pimousse (pimousse76)
Hi Michael, Thanks a lot for your answer. I now have a clear view of what's possible or not - and of what's not recommended ;o) Regards.
Please register to reply