move transaction scope management into Template.java
authorcrawshaw <crawshaw@ibex.org>
Mon, 29 Nov 2004 09:40:28 +0000 (09:40 +0000)
committercrawshaw <crawshaw@ibex.org>
Mon, 29 Nov 2004 09:40:28 +0000 (09:40 +0000)
darcs-hash:20041129094028-2eb37-1e5042a6f51463b1d9de4800163fbb4e726783f8.gz

src/java/org/ibex/xt/Prevalence.java
src/java/org/ibex/xt/Template.java

index 8468769..80d1f85 100644 (file)
@@ -1,7 +1,6 @@
 package org.ibex.xt;
 
 import java.io.*;
-import java.net.*;
 import java.util.*;
 import javax.servlet.ServletContext;
 
@@ -72,27 +71,13 @@ public class Prevalence {
     public static class JSTransaction implements Transaction {
         public static final long serialVersionUid = 0xfb2aa281;
         private JS js;
-        JSScope newscope;
-        List v;
-        public JSTransaction(JS js) throws JSExn {
-            newscope = new JSScope(null);
-            this.js = JS.cloneWithNewParentScope(js, newscope);
-            v = JS.getFormalArgs(this.js);
-            for(int i=0; i<v.size(); i++) {
-                if ("prevalent".equals(v.get(i))) continue;
-                newscope.put(v.get(i), JS.getParentScope(js).get(v.get(i)));
-            }
-        }
+        public JSTransaction(JS js) throws JSExn { this.js = js; }
         public void executeOn(Object o, Date now) {
             try {
-                newscope.put("prevalent", o);
-                newscope.put("now", new JSDate(now.getTime()));
-                Object a = v.size() <= 0 ? null : newscope.get(v.get(0));
-                Object b = v.size() <= 1 ? null : newscope.get(v.get(1));
-                Object c = v.size() <= 2 ? null : newscope.get(v.get(2));
-                Object[] rest = v.size() <= 3 ? null : new Object[v.size() - 3];
-                for(int i=3; i<v.size(); i++) rest[i-3] = v.get(i);
-                js.call(a, b, c, rest, v.size());
+                JSScope scope = JS.getParentScope(js);
+                scope.put("prevalent", o);
+                scope.put("now", new JSDate(now.getTime()));
+                js.call(null, null, null, null, 0);
             } catch (Exception e) { throw new RuntimeException(e); }
         }
     }
@@ -101,7 +86,7 @@ public class Prevalence {
         public static final long serialVersionUid = 0xfb2aa282;
         private JS js;
         private Object a;
-        public JSQuery(JS js, Object a) { this.js = JS.cloneWithNewParentScope(js, null); this.a = a; }
+        public JSQuery(JS js, Object a) { this.js = js; this.a = a; }
         public Object query(Object o, Date now) {
             try {
                 return js.call(o, a, new JSDate(now.getTime()), null, 3);
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()));