introduce a signalling exception and improve error messages
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / JSElement.java
index 4de51f0..c6eee6e 100644 (file)
@@ -1,14 +1,15 @@
-package ibex.xt;
+package org.ibex.xt;
 
 import java.io.StringReader;
 import java.io.Writer;
+import java.io.OutputStream;
 import java.io.IOException;
-
 import java.util.*;
-import ibex.util.*;
 
-import ibex.js.JS;
-import ibex.js.JSScope;
+import org.ibex.util.*;
+import org.ibex.js.JS;
+import org.ibex.js.JSScope;
+import org.ibex.js.JSExn;
 
 public class JSElement extends JSScope implements XML.Element {
     protected XML.Element wrapped;
@@ -22,22 +23,27 @@ public class JSElement extends JSScope implements XML.Element {
         // remap parent and children
         if (wrapped.getParent() != null) {
             List c = wrapped.getParent().getChildren();
-            c.remove(wrapped); c.add(this);
+            c.set(c.indexOf(wrapped), this);
         }
         List c = wrapped.getChildren();
-        for (int i=c.size(); i >= 0; i--) ((XML.Block)c.get(i)).setParent(this);
+        for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).setParent(this);
     }
 
-    public void toXML(Writer w) throws IOException {
-        // grab all related attributes
+    public void out(OutputStream o) throws IOException { wrapped.out(o); }
+    public void out(Writer w) throws IOException { wrapped.out(w); }
+
+    /** Load the attributes into the js scope. */
+    protected void loadAttr() {
         try {
             XML.Attributes a = getAttributes();
+            // FIXME: questionable abuse of XML namespaces here
+            boolean xturi = "http://xt.ibex.org/".equals(getUri());
             for(int i=0; i < a.attrSize(); i++) {
-                if (!"http://xt.ibex.org/".equals(a.getUri(i))) continue;
+                if (!xturi && !"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); }
+        } catch (JSExn e) { throw new Exn(e); }
     }
 
     private Object eval(String s) {
@@ -53,9 +59,9 @@ public class JSElement extends JSScope implements XML.Element {
                   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());
+                throw new Exn("javascripts within ${...} can only return " +
+                              "strings, numbers, and booleans; not a " +
+                              app.getClass().getName());
 
             ret.append(app == null ? "null" : app.toString());
         }
@@ -67,16 +73,18 @@ public class JSElement extends JSScope implements XML.Element {
         try {
             return JS.eval(JS.cloneWithNewParentScope(
                            JS.fromReader("input", 0, new StringReader(s)), this));
-        } catch (Exception e) {
+        } catch (IOException e) {
             e.printStackTrace();
-            throw new RuntimeException(e);
+            throw new Exn("impossible IOException, reading from StringReader");
+        } catch (JSExn e) {
+            throw new Exn(e);
         }
     }
 
     // Pass Through ///////////////////////////////////////////////////////////
 
-    public void setParent(XML.Element p)  { wrapped.setParent(p); }
-    public XML.Element getParent()        { return wrapped.getParent(); }
+    public void setParent(Tree.Node p)    { wrapped.setParent(p); }
+    public Tree.Node getParent()          { return wrapped.getParent(); }
     public XML.Attributes getAttributes() { return wrapped.getAttributes(); }
     public XML.Prefixes getPrefixes()     { return wrapped.getPrefixes(); }
     public List getChildren()             { return wrapped.getChildren(); }
@@ -86,7 +94,7 @@ public class JSElement extends JSScope implements XML.Element {
     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) {
+    private static JSScope findScope(Tree.Node e) {
         while (e != null && !(e instanceof JSScope)) e = e.getParent();
         return (JSScope)e;
     }
@@ -142,4 +150,10 @@ public class JSElement extends JSScope implements XML.Element {
             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 String toString() { return "JSElement.Exn: "+getMessage(); }
+    }
 }