Add Saxon evaluator.
[tatoo.git] / tests / xpath / XPathEvalSaxon.java
diff --git a/tests/xpath/XPathEvalSaxon.java b/tests/xpath/XPathEvalSaxon.java
new file mode 100644 (file)
index 0000000..0f8fd2e
--- /dev/null
@@ -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<NodeInfo> nodes = (ArrayList<NodeInfo>) 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("<xml_result num=\"1\">");
+      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("</xml_result>");
+      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);
+    };
+
+
+  }
+}