3 import javax.xml.parsers.DocumentBuilderFactory;
5 import org.w3c.dom.Document;
6 import org.w3c.dom.Node;
7 import org.w3c.dom.NodeList;
8 import org.w3c.dom.NamedNodeMap;
9 import java.util.Vector;
12 public class TatooEngine {
14 System.loadLibrary("tatoo-java");
16 private static native void unregister(long v);
18 public static class CustomBlock<T> {
19 // Stores a pointer to a C++ heap allocated pointer
20 // to an OCaml value. Once this object becomes unreachable on the
21 // Java side, we can de-register the value pointer in the OCaml
23 // T is a phantom type denoting the type of values on the OCaml side.
24 private long value_ptr;
25 private CustomBlock(long value_ptr)
27 this.value_ptr = value_ptr;
29 protected void finalise() {
30 System.err.println("Finalizing a CustomBlock!");
31 unregister (value_ptr);
36 public static int decorate(Node n, int preorder)
38 if (n == null) return preorder ;
40 n.setUserData("", new Integer (preorder), null);
42 NamedNodeMap att = n.getAttributes();
44 for(int i = 0; i < att.getLength(); i++) {
45 att.item(i).setUserData("", new Integer (preorder), null);
47 att.item(i).getFirstChild().setUserData("", new Integer (preorder), null);
51 for (Node c = n.getFirstChild(); c != null; c = c.getNextSibling())
52 preorder = decorate(c, preorder);
56 public static class Tree {}
57 static native CustomBlock<Tree> init_document(Document d, int i);
58 public static CustomBlock<Tree> init_document(Document d)
60 int i = decorate(d, 0);
61 return init_document(d, i);
64 public static class Automaton {}
66 public static native CustomBlock<Automaton> compile(String xpath);
68 public static native NodeList evaluate(CustomBlock<Automaton> automaton,
69 CustomBlock<Tree> tree,
73 public static void main(String[] args) throws Exception {
74 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
75 dbf.setCoalescing(true);
76 dbf.setNamespaceAware(true);
77 Document doc = dbf.newDocumentBuilder().newDocument();
79 MutableNodeList mnl = new MutableNodeList();
81 a = doc.createElement("a");
82 b = doc.createElement("b");
83 c = doc.createElement("c");
84 d = doc.createElement("d");
89 CustomBlock<Tree> tree = init_document (doc);
94 CustomBlock<Automaton> aut = compile("descendant-or-self::*");
95 System.err.println("After compilation");
96 System.err.println(aut.getClass().getName());
97 System.err.println(tree.getClass().getName());
98 System.err.println(mnl.getClass().getName());
99 NodeList nl = evaluate(aut, tree, mnl);
101 System.out.println("NodeList.getLength() = " + nl.getLength());
102 for (int i = 0; i < nl.getLength(); ++i) {
103 System.out.println("NodeList.item(" + i + ").getNodeName() = "
104 + (nl.item(i) != null ? nl.item(i).getNodeName() : null));
106 System.out.println(doc.getNodeName());