move transaction scope management into Template.java
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / Template.java
index 01b8d7e..323fb00 100644 (file)
@@ -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: <xt:use />
-            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()));