Merge pull request 'bema/fix/xalan_xpath' (#23) from bema/fix/xalan_xpath into dev
Reviewed-on: R11/release11-tools-web#23
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
package com.r11.tools.xslt.processors;
|
package com.r11.tools.xslt.processors;
|
||||||
|
|
||||||
|
import net.sf.saxon.lib.RawResult;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.apache.xpath.XPathAPI;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.traversal.NodeIterator;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
|
|
||||||
import javax.xml.XMLConstants;
|
import javax.xml.XMLConstants;
|
||||||
@@ -19,8 +23,7 @@ import javax.xml.xpath.XPath;
|
|||||||
import javax.xml.xpath.XPathConstants;
|
import javax.xml.xpath.XPathConstants;
|
||||||
import javax.xml.xpath.XPathExpression;
|
import javax.xml.xpath.XPathExpression;
|
||||||
import javax.xml.xpath.XPathFactory;
|
import javax.xml.xpath.XPathFactory;
|
||||||
import java.io.StringReader;
|
import java.io.*;
|
||||||
import java.io.StringWriter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for Xalan engine
|
* Handler for Xalan engine
|
||||||
@@ -53,25 +56,55 @@ public class Xalan {
|
|||||||
return sw.toString();
|
return sw.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isTextNode(Node n) {
|
||||||
|
if (n == null)
|
||||||
|
return false;
|
||||||
|
short nodeType = n.getNodeType();
|
||||||
|
return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process xpath and return either node or wrapped atomic value
|
* Process xpath and return either node or wrapped atomic value
|
||||||
* @deprecated
|
|
||||||
* Xalan needs assumption of the outcome, which is not implemented. Therefore method is deprecated
|
|
||||||
* @param data xml
|
* @param data xml
|
||||||
* @param transform xpath
|
* @param transform xpath
|
||||||
* @return xml processed using given xpath
|
* @return xml processed using given xpath
|
||||||
* @throws Exception thrown on node building errors or invalid xpath
|
* @throws Exception thrown on node building errors or invalid xpath
|
||||||
*/
|
*/
|
||||||
public static String processXPath(String data, String transform) throws Exception {
|
public static String processXPath(String data, String transform) throws Exception {
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
||||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
||||||
|
|
||||||
XPath xpath = XPathFactory.newInstance().newXPath();
|
// Set up a DOM tree to query.
|
||||||
|
InputSource in = new InputSource(new StringReader(data));
|
||||||
|
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
|
||||||
|
dfactory.setNamespaceAware(true);
|
||||||
|
Document doc = dfactory.newDocumentBuilder().parse(in);
|
||||||
|
|
||||||
xpath.setNamespaceContext(new XalanNamespaceResolver(builder.parse(new InputSource(new StringReader(data))), true));
|
// Set up an identity transformer to use as serializer.
|
||||||
XPathExpression exp = xpath.compile(transform);
|
Transformer serializer = TransformerFactory.newInstance().newTransformer();
|
||||||
exp.evaluate(new InputSource(new StringReader(data)), XPathConstants.NODESET);
|
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||||
return exp.evaluate(new InputSource(new StringReader(data)));
|
|
||||||
|
// Use the simple XPath API to select a nodeIterator.
|
||||||
|
NodeIterator nl = XPathAPI.selectNodeIterator(doc, transform);
|
||||||
|
|
||||||
|
// Serialize the found nodes to result object.
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
Node n;
|
||||||
|
while ((n = nl.nextNode())!= null) {
|
||||||
|
StringBuilder sb;
|
||||||
|
if (isTextNode(n)) {
|
||||||
|
// DOM may have more than one node corresponding to a
|
||||||
|
// single XPath text node. Coalesce all contiguous text nodes
|
||||||
|
// at this level
|
||||||
|
for (Node nn = n.getNextSibling(); isTextNode(nn); nn = nn.getNextSibling()) {
|
||||||
|
result.append(nn.getNodeValue());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(outputStream)));
|
||||||
|
result.append(outputStream);
|
||||||
|
}
|
||||||
|
result.append("\n");
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
<label for="processors">Select XPath processor:</label>
|
<label for="processors">Select XPath processor:</label>
|
||||||
<select name="processors" id="processors">
|
<select name="processors" id="processors">
|
||||||
<option value="saxon">Saxon</option>
|
<option value="saxon">Saxon</option>
|
||||||
|
<option value="xalan">Xalan</option>
|
||||||
<option value="libxml">libXML</option>
|
<option value="libxml">libXML</option>
|
||||||
</select>
|
</select>
|
||||||
<label for="versions">XPath version:</label>
|
<label for="versions">XPath version:</label>
|
||||||
|
|||||||
Reference in New Issue
Block a user