initial support for XML.Document
[org.ibex.xt-crawshaw.git] / src / ibex / xt / JSElement.java
diff --git a/src/ibex/xt/JSElement.java b/src/ibex/xt/JSElement.java
new file mode 100644 (file)
index 0000000..1cd6879
--- /dev/null
@@ -0,0 +1,146 @@
+package ibex.xt;
+
+import ibex.util.XML;
+import org.ibex.js.JS;
+import org.ibex.js.JSScope;
+
+import ibex.collection.*;
+import java.util.*;
+
+import java.io.StringReader;
+import java.io.Writer;
+import java.io.IOException;
+
+public class JSElement extends JSScope implements XML.Element {
+    protected XML.Element wrapped;
+
+    /** Creates an Element around <tt>wrapped</tt>, replacing
+     *  references to it in its parent and children with this object. */
+    public JSElement(XML.Element wrapped) {
+        super(findScope(wrapped));
+        this.wrapped = wrapped;
+
+        // remap parent and children
+        if (wrapped.getParent() != null) {
+            List c = wrapped.getParent().getChildren();
+            c.remove(wrapped); c.add(this);
+        }
+        List c = wrapped.getChildren();
+        for (int i=c.size(); i >= 0; i--) ((XML.Block)c.get(i)).setParent(this);
+    }
+
+    public void toXML(Writer w) throws IOException {
+        // grab all related attributes
+        try {
+            XML.Attributes a = getAttributes();
+            for(int i=0; i < a.attrSize(); i++) {
+                if (!"http://xt.ibex.org/".equals(a.getUri(i))) continue;
+                declare(a.getKey(i));
+                put(a.getKey(i), eval(a.getVal(i)));
+            }
+        } catch (Exception e) { throw new RuntimeException(e); }
+    }
+
+    private 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 RuntimeException("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();
+    }
+
+    public Object exec(String s) {
+        try {
+            return JS.eval(JS.cloneWithNewParentScope(
+                           JS.fromReader("input", 0, new StringReader(s)), this));
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
+    }
+
+    // Pass Through ///////////////////////////////////////////////////////////
+
+    public void setParent(XML.Element p)  { wrapped.setParent(p); }
+    public XML.Element getParent()        { return wrapped.getParent(); }
+    public XML.Attributes getAttributes() { return wrapped.getAttributes(); }
+    public XML.Prefixes getPrefixes()     { return wrapped.getPrefixes(); }
+    public List getChildren()             { return wrapped.getChildren(); }
+    public String getQName()              { return wrapped.getQName(); }
+    public String getLocalName()          { return wrapped.getLocalName(); }
+    public String getPrefix()             { return wrapped.getPrefix(); }
+    public String getUri()                { return wrapped.getUri(); }
+
+    /** Works up the Element object model until an instance of a JSScope is found. */
+    private static JSScope findScope(XML.Element e) {
+        while (e != null && !(e instanceof JSScope)) e = e.getParent();
+        return (JSScope)e;
+    }
+
+    /** A JSElement with the element attributes merged with a second
+     *  element.
+     *
+     *  All functions of the XML.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 JSElement {
+        private final XML.Attributes a;
+        public Merge(XML.Element wrapped, XML.Element merge) {
+            super(wrapped);
+            a = new MergeAttr(merge.getAttributes(), wrapped.getAttributes());
+        }
+        public XML.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: toXML() produces invalid XML if qname in both a and b.
+     */
+    public static final class MergeAttr implements XML.Attributes {
+        private final XML.Attributes a, b;
+        public MergeAttr(XML.Attributes primary, XML.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(); }
+    }
+}