From: crawshaw Date: Thu, 25 Nov 2004 11:26:28 +0000 (+0000) Subject: introduce global attribute processing X-Git-Url: http://git.megacz.com/?p=org.ibex.xt-crawshaw.git;a=commitdiff_plain;h=628fd95c0956c0ef41189f9dcb42677c2daca320 introduce global attribute processing darcs-hash:20041125112628-2eb37-0d69a629f49f5992fc590f84045c6126c80543d0.gz --- diff --git a/src/java/org/ibex/xt/JSElement.java b/src/java/org/ibex/xt/JSElement.java index c6eee6e..35ee51e 100644 --- a/src/java/org/ibex/xt/JSElement.java +++ b/src/java/org/ibex/xt/JSElement.java @@ -32,21 +32,7 @@ public class JSElement extends JSScope implements XML.Element { 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 (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue; - declare(a.getKey(i)); - put(a.getKey(i), eval(a.getVal(i))); - } - } catch (JSExn e) { throw new Exn(e); } - } - - private Object eval(String s) { + protected Object eval(String s) { if (s == null) return null; StringBuffer ret = new StringBuffer(); while (s.indexOf("${") != -1) { @@ -75,7 +61,7 @@ public class JSElement extends JSScope implements XML.Element { JS.fromReader("input", 0, new StringReader(s)), this)); } catch (IOException e) { e.printStackTrace(); - throw new Exn("impossible IOException, reading from StringReader"); + throw new Exn("error parsing script", e); } catch (JSExn e) { throw new Exn(e); } @@ -154,6 +140,7 @@ public class JSElement extends JSScope implements XML.Element { 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(); } } } diff --git a/src/java/org/ibex/xt/Template.java b/src/java/org/ibex/xt/Template.java index 4ab16d6..7f75bd2 100644 --- a/src/java/org/ibex/xt/Template.java +++ b/src/java/org/ibex/xt/Template.java @@ -25,7 +25,9 @@ public class Template extends JSElement { public static XML.Element wrap(XML.Element e, Template.Scope s) throws IOException { final String uri = e.getUri(); - if (uri.equals("http://xt.ibex.org/")) { + if (uri == null) { + // do nothing + } else if (uri.equals("http://xt.ibex.org/")) { //#switch(e.getLocalName()) case "js": e = new Template.JSTag(e); break; case "foreach": e = new Template.ForEach(e); break; @@ -68,15 +70,7 @@ public class Template extends JSElement { return wrap(merged, s); } - XML.Attributes a = e.getAttributes(); - for (int i=0; i < a.attrSize(); i++) { - // FIXME: questionable abuse of XML namespaces here - if ("if".equals(a.getKey(i)) && ( - "http://xt.ibex.org/".equals(e.getUri()) || - "http://xt.ibex.org/".equals(a.getUri(i)))) { - e = new Template.IfWrap(e); - } - } + e = new Template.AttributeEval(e); // wrap children List c = e.getChildren(); @@ -106,16 +100,48 @@ public class Template extends JSElement { public JSScope getParentScope() { return tscope; } - public static final class IfWrap extends JSElement { - public IfWrap(XML.Element e) { super(e); } - public void out(Writer w) throws IOException { - loadAttr(); + /** Processes ${...} blocks in attributes, loads applicable + * attributes into the JS scope and processes global attributes. */ + public static class AttributeEval extends JSElement implements XML.Attributes { + protected XML.Attributes a; + + public AttributeEval(XML.Element wrapped) { super(wrapped); a = wrapped.getAttributes(); } + public XML.Attributes getAttributes() { return this; } + + public int getIndex(String q) { return a.getIndex(q); } + public int getIndex(String u, String k) { return a.getIndex(u, k); } + public String getKey(int i) { return a.getKey(i); } + public String getVal(int i) { return (String)eval(a.getVal(i)); } + public String getUri(int i) { return a.getUri(i); } + public String getPrefix(int i) { return a.getPrefix(i); } + public String getQName(int i) { return a.getQName(i); } + public int attrSize() { return a.attrSize(); } + + public void out(Writer w) throws IOException { try { - Object varIf = get("if"); if (varIf != null) undeclare("if"); - if (varIf != null && !"true".equals(varIf)) return; - } catch (JSExn e) { throw new JSElement.Exn(e); } + // FIXME: questionable abuse of XML namespaces here + boolean xturi = "http://xt.ibex.org/".equals(getUri()); + for(int i=0; i < a.attrSize(); i++) { + if (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue; + + //#switch (a.getKey(i)) + case "if": if (!"true".equals(eval(a.getVal(i)))) return; + case "declare": + Object d = eval(a.getVal(i)); + if (!(d instanceof String)) throw new JSElement.Exn( + "attribute "+getPrefix()+":declare can only contain a "+ + "space seperated list of variable names to declare."); + StringTokenizer st = new StringTokenizer((String)d, " "); + while (st.hasMoreTokens()) declare(st.nextToken()); + continue; + //#end + + declare(a.getKey(i)); + put(a.getKey(i), eval(a.getVal(i))); + } + } catch (JSExn e) { throw new Exn(e); } wrapped.out(w); } @@ -131,8 +157,6 @@ public class Template extends JSElement { } public void out(Writer w) throws IOException { - loadAttr(); - List c = getChildren(); StringWriter s = new StringWriter(); for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s); @@ -144,8 +168,6 @@ public class Template extends JSElement { public ForEach(XML.Element e) { super(e); } public void out(Writer w) throws IOException { - loadAttr(); - try { Object varIn = get("in"); if (varIn != null) undeclare("in"); Object varPut = get("put"); if (varPut != null) undeclare("put"); @@ -183,8 +205,6 @@ public class Template extends JSElement { public Redirect(XML.Element e) { super(e); } public void out(Writer w) throws IOException { - loadAttr(); - try { Object p = get("page"); if (p != null) undeclare("page"); if (p == null || !(p instanceof String) || ((String)p).trim().equals("")) @@ -201,8 +221,6 @@ public class Template extends JSElement { public Transaction(XML.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids public void out(Writer w) throws IOException { - loadAttr(); - // TODO: List c = getChildren(); StringWriter sw = new StringWriter();