From: Kim Nguyễn Date: Fri, 4 Mar 2016 17:52:16 +0000 (+0100) Subject: Add Saxon evaluator. X-Git-Url: http://git.nguyen.vg/gitweb/?p=tatoo.git;a=commitdiff_plain;h=664f69cd4642ecd088df72d21228fbec81d0030d Add Saxon evaluator. --- diff --git a/tests/xpath/XPathEvalSaxon.java b/tests/xpath/XPathEvalSaxon.java new file mode 100644 index 0000000..0f8fd2e --- /dev/null +++ b/tests/xpath/XPathEvalSaxon.java @@ -0,0 +1,59 @@ +//import javax.xml.xpath.*; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathException; +import net.sf.saxon.xpath.XPathFactoryImpl; +import net.sf.saxon.om.NodeInfo; +import org.xml.sax.*; +import org.w3c.dom.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; +import java.util.ArrayList; +import net.sf.saxon.type.Type; + + +public class XPathEvalSaxon { + + + public static void main(String args[]) { + try { + + + XPath xpath = (new XPathFactoryImpl()).newXPath(); + String expression = args[1]; + InputSource inputSource = new InputSource(args[0]); + long startTime = System.nanoTime(); + @SuppressWarnings("unchecked") + ArrayList nodes = (ArrayList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); + long evalTime = (System.nanoTime() - startTime) / 1000000; + Transformer serializer = TransformerFactory.newInstance().newTransformer(); + serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + StreamResult o = new StreamResult(System.out); + startTime = System.nanoTime(); + System.out.println(""); + for(int i = 0; i < nodes.size(); i++){ + NodeInfo n = nodes.get(i); + switch (n.getNodeKind()) { + case net.sf.saxon.type.Type.ATTRIBUTE: + System.out.print (n.getLocalPart() + "=" + n.getStringValue()); + break; + default: + serializer.transform(n, o); + break; + }; + System.out.println(); + }; + System.out.println(""); + long printTime = (System.nanoTime() - startTime) / 1000000; + System.err.println("evaluation time: " + evalTime + "ms"); + System.err.println("serialization time: " + printTime + "ms"); + } catch (XPathException e) { + System.out.println (e.getCause()); + } catch (Exception e) { + System.out.println(e); + }; + + + } +} diff --git a/tests/xpath/XPathExample.java b/tests/xpath/XPathExample.java new file mode 100644 index 0000000..326cb03 --- /dev/null +++ b/tests/xpath/XPathExample.java @@ -0,0 +1,105 @@ +import org.xml.sax.*; +import org.w3c.dom.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; +import java.util.ArrayList; +import net.sf.saxon.Configuration; +import net.sf.saxon.lib.NamespaceConstant; +import net.sf.saxon.om.DocumentInfo; +import net.sf.saxon.om.NodeInfo; +import net.sf.saxon.xpath.XPathFactoryImpl; +import org.xml.sax.InputSource; + +import javax.xml.namespace.QName; +import javax.xml.transform.sax.SAXSource; +import javax.xml.xpath.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.util.Iterator; +import java.util.List; +import net.sf.saxon.type.Type; + + +public class XPathExample { + + + public static void main (String args[]) { + + if (args.length != 2) { + System.err.println("Usage: java XPathEvalSaxon input-file '/xpath/expression'"); + System.exit(1); + } + + try { + + System.setProperty("javax.xml.xpath.XPathFactory:"+NamespaceConstant.OBJECT_MODEL_SAXON, + "net.sf.saxon.xpath.XPathFactoryImpl"); + + XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); + + XPath xpe = xpf.newXPath(); + + System.err.println("Loaded XPath Provider " + xpe.getClass().getName()); + + long startTime = System.nanoTime (); + InputSource is = new InputSource(args[0]); + SAXSource ss = new SAXSource(is); + Configuration config = ((XPathFactoryImpl) xpf).getConfiguration(); + NodeInfo doc = config.buildDocumentTree(ss).getRootNode(); + long stopTime = System.nanoTime (); + System.err.println ("Loading document : " + (stopTime - startTime) / 1000000 + "ms"); + + startTime = System.nanoTime (); + XPathExpression cexpr = xpe.compile(args[1]); + stopTime = System.nanoTime (); + System.err.println ("Compiling XPath expression : " + (stopTime - startTime) / 1000000 + "ms"); + + startTime = System.nanoTime (); + @SuppressWarnings("unchecked") + ArrayList nodes = (ArrayList) cexpr.evaluate(doc, XPathConstants.NODESET); + stopTime = System.nanoTime (); + long firstRun = (stopTime - startTime); + System.err.println ("Evaluating XPath expression (1st run): " + firstRun / 1000000 + "ms"); + + startTime = System.nanoTime (); + for (int i = 1; i < 10; i++) { + + cexpr.evaluate(doc, XPathConstants.NODESET); + + }; + + stopTime = System.nanoTime (); + System.err.println ("Evaluating XPath expression (average 10 runs): " + (stopTime - startTime + firstRun) / (10 * 1000000) + "ms"); + + Transformer serializer = TransformerFactory.newInstance().newTransformer(); + serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + StreamResult o = new StreamResult(System.out); + startTime = System.nanoTime(); + System.out.println(""); + for(int i = 0; i < nodes.size(); i++){ + NodeInfo n = nodes.get(i); + switch (n.getNodeKind()) { + case Type.ATTRIBUTE: + System.out.print (n.getLocalPart() + "=" + n.getStringValue()); + break; + default: + serializer.transform(n, o); + break; + }; + System.out.println(); + }; + System.out.println(""); + stopTime = System.nanoTime(); + System.err.println ("Serializing document : " + (stopTime - startTime) / 1000000 + "ms"); + + } catch (XPathException e) { + System.out.println (e.getCause()); + } catch (Exception e) { + System.out.println(e); + }; + + } + +}