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