Add Saxon benchmark.
[tatoo.git] / tests / XPathExample.java
1 import org.xml.sax.*;\r
2 import org.w3c.dom.*;\r
3 import javax.xml.transform.*;\r
4 import javax.xml.transform.dom.*;\r
5 import javax.xml.transform.stream.*;\r
6 import java.util.ArrayList;\r
7 import net.sf.saxon.Configuration;\r
8 import net.sf.saxon.lib.NamespaceConstant;\r
9 import net.sf.saxon.om.DocumentInfo;\r
10 import net.sf.saxon.om.NodeInfo;\r
11 import net.sf.saxon.xpath.XPathFactoryImpl;\r
12 import org.xml.sax.InputSource;\r
13 \r
14 import javax.xml.namespace.QName;\r
15 import javax.xml.transform.sax.SAXSource;\r
16 import javax.xml.xpath.*;\r
17 import java.io.BufferedReader;\r
18 import java.io.File;\r
19 import java.io.InputStreamReader;\r
20 import java.util.Iterator;\r
21 import java.util.List;\r
22 import net.sf.saxon.type.Type;\r
23 \r
24 \r
25 public class XPathExample {\r
26 \r
27 \r
28     public static void main (String args[])   {\r
29 \r
30         if (args.length != 2) {\r
31             System.err.println("Usage: java XPathEvalSaxon input-file '/xpath/expression'");\r
32             System.exit(1);\r
33         }\r
34 \r
35         try {\r
36 \r
37           System.setProperty("javax.xml.xpath.XPathFactory:"+NamespaceConstant.OBJECT_MODEL_SAXON,\r
38                            "net.sf.saxon.xpath.XPathFactoryImpl");\r
39 \r
40           XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);\r
41 \r
42           XPath xpe = xpf.newXPath();\r
43 \r
44           System.err.println("Loaded XPath Provider " + xpe.getClass().getName());\r
45 \r
46           long startTime = System.nanoTime ();\r
47           InputSource is = new InputSource(args[0]);\r
48           SAXSource ss = new SAXSource(is);\r
49           Configuration config = ((XPathFactoryImpl) xpf).getConfiguration();\r
50           NodeInfo doc = config.buildDocumentTree(ss).getRootNode();\r
51           long stopTime = System.nanoTime ();\r
52           System.err.println ("Loading document : " + (stopTime - startTime) / 1000000 + "ms");\r
53 \r
54           startTime = System.nanoTime ();\r
55           XPathExpression cexpr = xpe.compile(args[1]);\r
56           stopTime = System.nanoTime ();\r
57           System.err.println ("Compiling XPath expression : " + (stopTime - startTime) / 1000000 + "ms");\r
58 \r
59           startTime = System.nanoTime ();\r
60           @SuppressWarnings("unchecked")\r
61             ArrayList<NodeInfo> nodes = (ArrayList<NodeInfo>) cexpr.evaluate(doc, XPathConstants.NODESET);\r
62           stopTime = System.nanoTime ();\r
63           long firstRun = (stopTime - startTime);\r
64           System.err.println ("Evaluating XPath expression (1st run): " + firstRun / 1000000 + "ms");\r
65 \r
66           startTime = System.nanoTime ();\r
67           for (int i = 1; i < 10; i++) {\r
68 \r
69             cexpr.evaluate(doc, XPathConstants.NODESET);\r
70 \r
71           };\r
72 \r
73           stopTime = System.nanoTime ();\r
74           System.err.println ("Evaluating XPath expression (average 10 runs): " + (stopTime - startTime + firstRun) / (10 * 1000000) + "ms");\r
75 \r
76           Transformer serializer = TransformerFactory.newInstance().newTransformer();\r
77           serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");\r
78           StreamResult o = new StreamResult(System.out);\r
79           startTime = System.nanoTime();\r
80           System.out.println("<xml_result num=\"1\">");\r
81           for(int i = 0; i < nodes.size(); i++){\r
82             NodeInfo n = nodes.get(i);\r
83             switch (n.getNodeKind()) {\r
84             case Type.ATTRIBUTE:\r
85               System.out.print (n.getLocalPart() + "=" + n.getStringValue());\r
86               break;\r
87             default:\r
88               serializer.transform(n, o);\r
89               break;\r
90             };\r
91             System.out.println();\r
92           };\r
93           System.out.println("</xml_result>");\r
94           stopTime = System.nanoTime();\r
95           System.err.println ("Serializing document : " + (stopTime - startTime) / 1000000 + "ms");\r
96 \r
97         } catch (XPathException e) {\r
98           System.out.println (e.getCause());\r
99         } catch (Exception e) {\r
100           System.out.println(e);\r
101         };\r
102 \r
103     }\r
104 \r
105 }\r