X-Git-Url: http://git.megacz.com/?p=org.ibex.xt-crawshaw.git;a=blobdiff_plain;f=src%2Fjava%2Forg%2Fibex%2Fxt%2FTemplate.java;h=323fb00532eb3fbd3687d267a1703148d4dc4d22;hp=01b8d7ecb46f759fac87f07d542609267a834555;hb=f452b10d473d1a3f6b9dc45a98fb6cc98d7f3944;hpb=e086a6515bf5d5f1f179240f229056d2bb699a99 diff --git a/src/java/org/ibex/xt/Template.java b/src/java/org/ibex/xt/Template.java index 01b8d7e..323fb00 100644 --- a/src/java/org/ibex/xt/Template.java +++ b/src/java/org/ibex/xt/Template.java @@ -14,6 +14,9 @@ import java.util.*; import org.ibex.util.*; import org.ibex.js.*; +// FIXME: replace scope().get("") with containsKey() check on attributes, +// thereby neatly sidestepping any higher up user defined variables +// (especially when combined with undeclare() public class Template extends JSLeaf.Element { public static Template parse(String path, Template.Scope s) throws FileNotFoundException, IOException { Reader xmlreader = new BufferedReader(new InputStreamReader(new FileInputStream(path))); @@ -222,16 +225,35 @@ public class Template extends JSLeaf.Element { // TODO: finish public static final class Transaction extends JSLeaf.Element { - private final Template.Scope scope; // FIXME: HACK. unstatisise all tags, or do this to all - public Transaction(Tree.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids + private Template.Scope scope; + public Transaction(Tree.Element e, Template.Scope s) { super(e); scope = s; } public void out(Writer w) throws IOException { - // TODO: - List c = getChildren(); StringWriter sw = new StringWriter(); + List c = getChildren(); for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(sw); - JS t = JS.fromReader("input", 0, new StringReader(sw.toString())); - t = JS.cloneWithNewParentScope(t, new JSScope(null)); + StringReader sr = new StringReader(sw.toString()); + + JS t; + try { t = JS.fromReader("input", 0, sr); } + catch (IOException e) { throw new JSLeaf.Exn(e.getMessage()); } + + try { + JSScope scope = JS.getParentScope(t); + + Object u = scope().get("use"); if (u != null) scope().undeclare("use"); + if (u != null) { + if (!(u instanceof String)) throw new JSLeaf.Exn( + "<"+getPrefix()+":transaction> requires 'use' attribute "+ + "to be a valid space-seperated list of variables"); + StringTokenizer st = new StringTokenizer((String)u); + while (st.hasMoreTokens()) { + String k = st.nextToken(); + scope.put(k, scope().get(k)); + } + } + } catch (JSExn e) { throw new JSLeaf.Exn(e); } + scope.transaction(t); } } @@ -239,7 +261,6 @@ public class Template extends JSLeaf.Element { public static final class Text extends JSLeaf { public Text(Tree.Leaf w) { super(w); } public void out(Writer w) throws IOException { - // FIXME: make eval() take a writer StringWriter sw = new StringWriter(); super.out(sw); w.write((String)eval(sw.toString()));