package org.ibex.xt;
import java.io.*;
-import java.net.*;
import java.util.*;
import javax.servlet.ServletContext;
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); }
}
}
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);
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)));
// 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);
}
}
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()));