seperate JSScope and update for new Tree interfaces
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / JSLeaf.java
diff --git a/src/java/org/ibex/xt/JSLeaf.java b/src/java/org/ibex/xt/JSLeaf.java
new file mode 100644 (file)
index 0000000..2def4c1
--- /dev/null
@@ -0,0 +1,166 @@
+package org.ibex.xt;
+
+import java.io.Serializable;
+import java.io.StringReader;
+import java.io.Writer;
+import java.io.OutputStream;
+import java.io.IOException;
+import java.util.*;
+
+import org.ibex.util.*;
+import org.ibex.js.JS;
+import org.ibex.js.JSScope;
+import org.ibex.js.JSExn;
+
+public class JSLeaf implements Tree.Leaf, Serializable {
+    private transient JSScope scope = null;
+    protected Tree.Leaf wrapped;
+
+    /** Creates a Leaf around <tt>wrapped</tt>, replacing
+     *  references to it in its parent with this object. */
+    public JSLeaf(Tree.Leaf wrapped) {
+        this.wrapped = wrapped;
+
+        // remap parent and children
+        if (wrapped.getParent() != null) {
+            List c = wrapped.getParent().getChildren();
+            c.set(c.indexOf(wrapped), this);
+        }
+    }
+
+    public JSScope scope() {
+        if (scope == null) {
+            Tree.Leaf e = wrapped;
+            while (e != null && !(e instanceof JSLeaf)) e = e.getParent();
+            scope = new JSScope(e == null ? null : ((JSLeaf)e).scope());
+        }
+
+        return scope;
+    }
+
+    public void out(OutputStream o) throws IOException { wrapped.out(o); }
+    public void out(Writer w) throws IOException { wrapped.out(w); }
+
+    protected Object eval(String s) {
+        if (s == null) return null;
+        StringBuffer ret = new StringBuffer();
+        while (s.indexOf("${") != -1) {
+            ret.append(s.substring(0, s.indexOf("${")));
+            String s2 = s.substring(s.indexOf("${")+2);
+            Object app = exec("return (" + s2.substring(0, s2.indexOf('}')) + ");\n");
+            s = s.substring(s.indexOf('}') + 1);
+
+            if (!(app == null ||
+                  app instanceof String ||
+                  app instanceof Number ||
+                  app instanceof Boolean))
+                throw new Exn("javascripts within ${...} can only return " +
+                              "strings, numbers, and booleans; not a " +
+                              app.getClass().getName());
+
+            ret.append(app == null ? "null" : app.toString());
+        }
+        ret.append(s);
+        return ret.toString();
+    }
+
+    protected Object exec(String s) {
+        try {
+            return JS.eval(JS.cloneWithNewParentScope(
+                           JS.fromReader("input", 0, new StringReader(s)), scope()));
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw new Exn("error parsing script", e);
+        } catch (JSExn e) {
+            throw new Exn(e);
+        }
+    }
+
+    // Pass Through ///////////////////////////////////////////////////////////
+
+    public void setParent(Tree.Node p) { wrapped.setParent(p); }
+    public Tree.Node getParent()       { return wrapped.getParent(); }
+
+    public static class Node extends JSLeaf implements Tree.Node {
+        public Node(Tree.Node wrapped) {
+            super(wrapped);
+            List c = wrapped.getChildren();
+            for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).setParent(this);
+        }
+
+        public List getChildren() { return ((Tree.Node)wrapped).getChildren(); }
+    }
+
+    public static class Element extends Node implements Tree.Element {
+        public Element(Tree.Element wrapped) { super(wrapped); }
+
+        public Tree.Attributes getAttributes() { return ((Tree.Element)wrapped).getAttributes(); }
+        public Tree.Prefixes getPrefixes()     { return ((Tree.Element)wrapped).getPrefixes(); }
+
+        public String getQName()     { return ((Tree.Element)wrapped).getQName(); }
+        public String getLocalName() { return ((Tree.Element)wrapped).getLocalName(); }
+        public String getPrefix()    { return ((Tree.Element)wrapped).getPrefix(); }
+        public String getUri()       { return ((Tree.Element)wrapped).getUri(); }
+    }
+
+    /** A JSLeaf.Element with the element attributes merged with a second
+     *  element.
+     *
+     *  All functions of the Tree.Element interface are mapped onto the
+     *  primary element, except <tt>getAttributes()</tt>. This function
+     *  returns a MergedAttr instance with the <b>secondary</b> element
+     *  acting as the primary attribute source.
+     */
+    public static class Merge extends Element {
+        private final Tree.Attributes a;
+        public Merge(Tree.Element wrapped, Tree.Element merge) {
+            super(wrapped);
+            a = new MergeAttr(merge.getAttributes(), wrapped.getAttributes());
+        }
+        public Tree.Attributes getAttributes() { return a; }
+    }
+
+    /** Creates a single view onto two sets of Attributes, first
+     *  checking the <tt>primary</tt> array for an entry, or
+     *  otherwise returning any matching entry in the
+     *  <tt>secondary</tt> Attributes object.
+     *
+     *  FIXME: out(Writer) produces invalid XML if qname in both a and b.
+     *         create org.ibex.util.XMLHelper and have a proper version of
+     *         this as a subclass.
+     */
+    public static final class MergeAttr implements Tree.Attributes {
+        private final Tree.Attributes a, b;
+        public MergeAttr(Tree.Attributes primary, Tree.Attributes secondary) {
+            a = primary; b = secondary;
+        }
+        public int getIndex(String qname) {
+            int i = a.getIndex(qname); if (i >= 0) return i;
+                i = b.getIndex(qname); if (i >= 0) return i + a.attrSize();
+            return -1;
+        }
+        public int getIndex(String uri, String key) {
+            int i = a.getIndex(uri, key); if (i >= 0) return i;
+                i = b.getIndex(uri, key); if (i >= 0) return i + b.attrSize();
+            return -1;
+        }
+        public String getKey(int i) {
+            return i >= a.attrSize() ? b.getKey(i-a.attrSize()) : a.getKey(i); }
+        public String getVal(int i) {
+            return i >= a.attrSize() ? b.getVal(i-a.attrSize()) : a.getVal(i); }
+        public String getUri(int i) {
+            return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
+        public String getPrefix(int i) {
+            return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
+        public String getQName(int i) {
+            return i >= a.attrSize() ? b.getUri(i-a.attrSize()) : a.getUri(i); }
+        public int attrSize() { return a.attrSize() + b.attrSize(); }
+    }
+
+    public static class Exn extends RuntimeException {
+        public Exn(String cause) { super(cause); }
+        public Exn(JSExn e) { super(e); }
+        public Exn(String msg, Exception e) { super(msg + ": " + e.getMessage()); }
+        public String toString() { return "JSElement.Exn: "+getMessage(); }
+    }
+}