X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fibex%2Fxt%2FJSElement.java;h=35ee51ef55b9dd35251086d94c6817a244692d24;hb=628fd95c0956c0ef41189f9dcb42677c2daca320;hp=06a6f72e6f4feec00c1cd8977ff432dae3067fb4;hpb=4f3ad62d8bc5e2f2cc1f6674935bb9a088fa400a;p=org.ibex.xt-crawshaw.git diff --git a/src/java/org/ibex/xt/JSElement.java b/src/java/org/ibex/xt/JSElement.java index 06a6f72..35ee51e 100644 --- a/src/java/org/ibex/xt/JSElement.java +++ b/src/java/org/ibex/xt/JSElement.java @@ -9,6 +9,7 @@ import java.util.*; 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; @@ -28,20 +29,10 @@ public class JSElement extends JSScope implements XML.Element { for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).setParent(this); } - public void out(OutputStream o) throws IOException { throw new UnsupportedOperationException(); } - public void out(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); } - } + public void out(OutputStream o) throws IOException { wrapped.out(o); } + public void out(Writer w) throws IOException { wrapped.out(w); } - private Object eval(String s) { + protected Object eval(String s) { if (s == null) return null; StringBuffer ret = new StringBuffer(); while (s.indexOf("${") != -1) { @@ -54,9 +45,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()); } @@ -68,9 +59,11 @@ 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("error parsing script", e); + } catch (JSExn e) { + throw new Exn(e); } } @@ -143,4 +136,11 @@ 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 Exn(String msg, Exception e) { super(msg + ": " + e.getMessage()); } + public String toString() { return "JSElement.Exn: "+getMessage(); } + } }