Implement the bridge to call Tatoo from java. Very slow at the moment.
[tatoo.git] / src / bindings / java / TatooTest.java
1 import javax.xml.parsers.DocumentBuilderFactory;
2 import org.w3c.dom.Document;
3 import org.w3c.dom.Node;
4 import org.w3c.dom.NodeList;
5 import fxslt.memory.TatooEngine;
6 import fxslt.memory.TatooEngine.CustomBlock;
7 import fxslt.memory.TatooEngine.Tree;
8 import fxslt.memory.TatooEngine.Automaton;
9 import fxslt.memory.MutableNodeList;
10
11 import javax.xml.transform.*;
12 import javax.xml.transform.dom.*;
13 import javax.xml.transform.stream.*;
14
15
16 public class TatooTest {
17   static long timer;
18   public static void start_timer() {
19     timer = System.nanoTime();
20   }
21   public static void stop_timer (String msg) {
22     long time = System.nanoTime() - timer;
23     System.err.println(msg + " " + time/1000000 + "ms");
24     System.err.flush();
25   }
26
27   public static void main(String args[])
28     {
29
30       try {
31
32         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
33         dbf.setCoalescing(true);
34         dbf.setNamespaceAware(true);
35
36         start_timer();
37         Document doc = dbf.newDocumentBuilder().parse(args[0]);
38         stop_timer("Parsing document");
39
40         MutableNodeList mnl = new MutableNodeList();
41         start_timer();
42         CustomBlock<Tree> tree = TatooEngine.init_document (doc);
43         stop_timer("Initializing document");
44
45         mnl.add(doc);
46
47         start_timer();
48         CustomBlock<Automaton> auto = TatooEngine.compile(args[1]);
49         stop_timer("Compiling query");
50
51         start_timer();
52         NodeList nodes = TatooEngine.evaluate(auto, tree, mnl);
53         stop_timer("Evaluating query");
54
55         if (args.length >= 3 && args[2] == "print" ) {
56           start_timer ();
57           Transformer serializer = TransformerFactory.newInstance().newTransformer();
58           serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
59           StreamResult o = new StreamResult(System.out);
60           System.out.println("<xml_result num=\"1\">");
61           for(int i = 0; i < nodes.getLength(); i++){
62             Node n = nodes.item(i);
63             switch (n.getNodeType()) {
64             case Node.ATTRIBUTE_NODE:
65               System.out.print (n.getNodeName() + "=" + n.getNodeValue());
66               break;
67             default:
68               serializer.transform(new DOMSource(nodes.item(i)), o);
69               break;
70             };
71             System.out.println();
72           };
73           System.out.println("</xml_result>");
74           stop_timer("Serializing document");
75         }
76         System.err.println("Number of results: " + nodes.getLength());
77       } catch (Exception e) {
78         System.err.println(e);
79       }
80     }
81
82 }