}
public static class FromXML extends Node.Stream {
- private final XML.Pull xml;
- private XML.Element parent = null;
- private XML.Element e;
+ private final XML.Stream xml;
+ private XML.Elem parent = null;
+ private XML.Elem e;
private int currentdelta = 0;
- public FromXML(Reader r) { this.xml = new XML.Pull(r); }
+ public FromXML(Reader r) { this.xml = new XML.Stream(r); }
protected boolean _read(Node n) { try {
- Object ret = xml.read();
+ Object ret = xml.next();
if (ret == null) return false;
if (ret instanceof String) {
n.cdata = (String)ret;
- n.delta = xml.level - currentdelta;
- currentdelta = xml.level;
+ n.delta = xml.getDepth() - currentdelta;
+ currentdelta = xml.getDepth();
return true;
}
- XML.Element e = (XML.Element)ret;
+ XML.Elem e = (XML.Elem)ret;
n.name = e.getLocalName();
n.uri = e.getUri();
- n.numattrs = e.getAttrLen();
- n.delta = e.level - currentdelta;
- currentdelta = e.level;
+ n.numattrs = e.getAttributes().attrSize();
+ n.delta = xml.getDepth() - currentdelta;
+ currentdelta = xml.getDepth();
if (n.attrs == null || n.attrs.length < n.numattrs*2) n.attrs = new String[n.numattrs*4];
- for(int i=0; i<n.numattrs; i++) { n.attrs[i*2] = e.getAttrKey(i); n.attrs[i*2+1] = e.getAttrVal(i); }
+ for(int i=0; i<n.numattrs; i++) {
+ n.attrs[i*2] = e.getAttributes().getKey(i);
+ n.attrs[i*2+1] = e.getAttributes().getVal(i);
+ }
return true;
} catch (Exception e) { throw new RuntimeException(e); } }
}
String base = cx.getRealPath("/") + "WEB-INF" + File.separatorChar + "prevalent";
System.err.println("prevayling to " + base);
pf.configurePrevalenceBase(base);
- XStreamSnapshotManager manager = new XStreamSnapshotManager(new JS(), base, null) {
+ /*
+ XmlSnapshotManager manager = new XmlSnapshotManager(new JS(), base, null); {
protected XStream createXStream() {
XStream xstream = new XStream();
xstream.alias("js", JS.class);
xstream.alias("jsdate", JSDate.class);
return xstream;
}
- };
+ };
System.err.println("configuring with " + manager);
pf.configureSnapshotManager(manager);
+ */
//pf.configureSnapshotManager(new SnapshotManager(new JS(), base));
//pf.configureClassLoader(JSTransaction.class.getClassLoader());
prevayler = pf.create();
public static class JSTransaction implements Transaction {
public static final long serialVersionUid = 0xfb2aa281;
private JS js;
- JSScope newscope;
- Vec v;
+ Template.Scope newscope;
+ String[] 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.elementAt(i))) continue;
- newscope.put(v.elementAt(i), JS.getParentScope(js).get(v.elementAt(i)));
+ newscope = new Template.Scope(null);
+ this.js = JSU.cloneWithNewGlobalScope(js, newscope);
+ v = this.js.getFormalArgs();
+ for(int i=0; i<v.length; i++) {
+ if (JSU.S("prevalent").equals(v[i])) continue;
+ newscope.put(JSU.S(v[i]), js.get(JSU.S(v[i])));
}
}
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.elementAt(0));
- Object b = v.size() <= 1 ? null : newscope.get(v.elementAt(1));
- Object c = v.size() <= 2 ? null : newscope.get(v.elementAt(2));
- Object[] rest = v.size() <= 3 ? null : new Object[v.size() - 3];
- for(int i=3; i<v.size(); i++) rest[i-3] = v.elementAt(i);
- js.call(a, b, c, rest, v.size());
+ newscope.put(JSU.S("prevalent"), (JS)o);
+ newscope.put(JSU.S("now"), new JSDate(now.getTime()));
+ JS[] args = new JS[v.length];
+ for(int i=0; i<v.length; i++) args[i] = JSU.S(v[i]);
+ js.call(null, args);
} 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 = JSU.cloneWithNewGlobalScope(js, null); this.a = a; }
public Object query(Object o, Date now) {
try {
- return js.call(o, a, new JSDate(now.getTime()), null, 3);
+ return js.call(null, new JS[] { (JS)o, (JS)a, new JSDate(now.getTime()) });
} catch (Exception e) { throw new RuntimeException(e); }
}
}
public class Servlet extends HttpServlet {
+ public static final JS METHOD = new JS.Method();
private ServletScope servletscope = null;
private String path;
private Prevayler prevayler;
servletscope = new ServletScope(request, response, cx);
path = cx.getRealPath(((HttpServletRequest)request).getServletPath());
Reader xmlreader = new InputStreamReader(new FileInputStream(path));
- new Template(servletscope, new JSScope(servletscope), xmlreader).wrap(null).toXML(response.getWriter());
+ new Template(servletscope, servletscope, xmlreader).wrap(null).toXML(response.getWriter());
}
- public class ServletScope extends JSScope {
+ public class ServletScope extends JS.Obj {
HttpServletRequest request;
HttpServletResponse response;
ServletContext cx;
public String getRealPath(String s) { return cx.getRealPath(s); }
public ServletScope(ServletRequest request, ServletResponse response, ServletContext cx) {
- super(null);
+ super();
this.request = (HttpServletRequest)request;
this.response = (HttpServletResponse)response;
this.cx = cx;
}
- private JS params = new JS() {
- public Object get(Object key) { return request.getParameter(JS.toString(key)); }
- public Enumeration keys() { return request.getParameterNames(); }
+ private JS params = new JS.Obj() {
+ public JS get(JS key) throws JSExn { return JSU.S(request.getParameter(JSU.toString(key))); }
+ public Enumeration keys() throws JSExn { return new JS.Enumeration.JavaStringEnumeration(request.getParameterNames()); }
};
- private JS cookies = new JS() {
+ private JS cookies = new JS.Obj() {
/*
- public Object get(Object key) { return request.getCookie(JS.toString(key)); }
+ public Object get(Object key) { return request.getCookie(JSU.toString(key)); }
public Enumeration keys() { return request.getCookieNames(); }
*/
};
- private JS sessionAttributes = new JS() {
- public Object get(Object key) { return request.getSession(true).getAttribute(JS.toString(key)); }
- public void put(Object key, Object val) {
- if (val == null) request.getSession(true).removeAttribute(JS.toString(key));
- else request.setAttribute(JS.toString(key), val); }
- public Enumeration keys() { return request.getSession(true).getAttributeNames(); }
+ private JS sessionAttributes = new JS.Obj() {
+ public JS get(JS key) throws JSExn { return JSU.S(request.getSession(true).getAttribute(JSU.toString(key)).toString()); }
+ public void put(JS key, JS val) throws JSExn {
+ if (val == null) request.getSession(true).removeAttribute(JSU.toString(key));
+ else request.setAttribute(JSU.toString(key), val); }
+ public Enumeration keys() throws JSExn { return new JS.Enumeration.JavaStringEnumeration(request.getSession(true).getAttributeNames()); }
};
- private JS requestHeader = new JS() {
- public Object get(Object key) { return request.getHeader(JS.toString(key)); }
- public Enumeration keys() { return request.getHeaderNames(); }
+ private JS requestHeader = new JS.Obj() {
+ public JS get(JS key) throws JSExn { return JSU.S(request.getHeader(JSU.toString(key))); }
+ public Enumeration keys() throws JSExn { return new JS.Enumeration.JavaStringEnumeration(request.getHeaderNames()); }
};
- private JS responseHeader = new JS() {
- public void put(Object key, Object val) { response.setHeader(JS.toString(key), JS.toString(val)); }
+ private JS responseHeader = new JS.Obj() {
+ public void put(JS key, JS val) throws JSExn { response.setHeader(JSU.toString(key), JSU.toString(val)); }
};
/** lets us put multi-level get/put/call keys all in the same method */
- private class Sub extends JS {
- Object key;
- Sub(Object key) { this.key = key; }
- public void put(Object key, Object val) throws JSExn {
- ServletScope.this.put(JS.toString(this.key) + "." + JS.toString(key), val); }
- public Object get(Object key) throws JSExn {
- return ServletScope.this.get(JS.toString(this.key) + "." + JS.toString(key)); }
- public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
- return ServletScope.this.callMethod(this.key, a0, a1, a2, rest, nargs);
- }
- public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
- return ServletScope.this.callMethod(JS.toString(this.key) + "."
- + JS.toString(method), a0, a1, a2, rest, nargs);
+ private class Sub extends JS.Obj {
+ JS key;
+ Sub(JS key) { this.key = key; }
+ public void put(JS key, JS val) throws JSExn {
+ ServletScope.this.put(JSU.S(JSU.toString(this.key) + "." + JSU.toString(key)), val); }
+ public JS get(JS key) throws JSExn {
+ return ServletScope.this.get(JSU.S(JSU.toString(this.key) + "." + JSU.toString(key))); }
+ public JS call(JS method, JS[] args) throws JSExn {
+ return ServletScope.this.call(JSU.S(JSU.toString(this.key) + "." + JSU.toString(method)), args);
}
}
- private Sub getSub(String key) { return new Sub(key); }
+ private Sub getSub(String key) { return new Sub(JSU.S(key)); }
- public Object callMethod(Object method, final Object a, final Object b, Object c, Object[] rest, int nargs) throws JSExn {
- //#switch(method)
+ public JS call(JS method, JS[] args) throws JSExn {
+ //#switch(JSU.toString(method))
case "session.invalidate": request.getSession(true).invalidate(); return null;
case "context.list":
- String path = JS.toString(a);
+ String path = JSU.toString(args[0]);
if (path.indexOf("..") != -1) throw new JSExn("cannot use .. in paths");
File f = new File(cx.getRealPath("/") + File.separatorChar + path);
if (!f.isDirectory()) return null;
String[] contents = f.list();
JSArray ret = new JSArray(contents.length);
- for(int i=0; i<contents.length; i++) ret.addElement(contents[i]);
+ for(int i=0; i<contents.length; i++) ret.put(JSU.N(ret.size()), JSU.S(contents[i]));
return ret;
//#end
return null;
}
- public Object get(Object key) throws JSExn {
- //#switch(key)
+ public JS get(JS key) throws JSExn {
+ //#switch(JSU.toString(key))
case "body":
case "arg": return null;
- case "prevalent": return prevayler.prevalentSystem();
+ case "prevalent": return (JS)prevayler.prevalentSystem();
case "request": return getSub("request");
- case "request.user": return request.getRemoteUser();
+ case "request.user": return JSU.S(request.getRemoteUser());
case "request.header": return requestHeader;
- case "request.method": return request.getMethod();
+ case "request.method": return JSU.S(request.getMethod());
case "request.remote": return getSub("request.remote");
- case "request.remote.ip": return request.getRemoteAddr();
- case "request.remote.host": return request.getRemoteHost();
- case "request.ssl": return request.isSecure();
- case "request.path": return request.getPathInfo();
+ case "request.remote.ip": return JSU.S(request.getRemoteAddr());
+ case "request.remote.host": return JSU.S(request.getRemoteHost());
+ case "request.ssl": return JSU.B(request.isSecure());
+ case "request.path": return JSU.S(request.getPathInfo());
case "response": return getSub("response");
case "response.header": return responseHeader;
case "session": return getSub("session");
//#end
return null;
}
- public void put(Object key, Object val) throws JSExn {
+ public void put(JS key, JS val) throws JSExn {
try {
- //#switch(JS.toString(key))
+ //#switch(JSU.toString(key))
case "transaction":
try { prevayler.execute(new Prevalence.JSTransaction((JS)val));
} catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); }
- case "response.code": response.setStatus(JS.toInt(val));
- case "response.redirect": response.sendRedirect(JS.toString(val));
- case "response.contentType": response.setContentType(JS.toString(val));
+ case "response.code": response.setStatus(JSU.toInt(val));
+ case "response.redirect": response.sendRedirect(JSU.toString(val));
+ case "response.contentType": response.setContentType(JSU.toString(val));
//#end
- } catch (IOException e) {
- throw new JSExn(e);
- }
+ } catch (IOException e) { throw new JSExn(JSU.S(e.toString())); }
}
}
public class Template extends Node.Stream.Filter implements Node.Stream.Functor {
- static Template newTemplate(Servlet.ServletScope servletscope, JSScope scope, String str) {
+ static Template newTemplate(Servlet.ServletScope servletscope, Scope scope, String str) {
try {
File f = new File(str);
if (!f.exists()) f = new File(str + ".xt");
} catch (Exception e) { throw new RuntimeException(e); }
}
- static JSScope copyNodeToScope(Node n, JSScope scope) {
+ static Scope copyNodeToScope(Node n, Scope scope) {
try {
for(int i=0; i<n.numattrs; i++) {
scope.declare(n.attrs[i*2]);
- scope.put(n.attrs[i*2], n.attrs[i*2+1]);
+ scope.put(JSU.S(n.attrs[i*2]), JSU.S(n.attrs[i*2+1]));
}
return scope;
} catch (Exception e) { throw new RuntimeException(e); }
private class JSRewriter extends Node.Stream {
private Node.Stream in;
- private JSScope scope;
- public JSRewriter(Node.Stream in, JSScope scope) { this.in = in; this.scope = scope; }
+ private Scope scope;
+ public JSRewriter(Node.Stream in, Scope scope) { this.in = in; this.scope = scope; }
protected boolean _read(Node n) { if (!in.read(n)) return false; transform(n, scope); return true; }
}
- public static Node transform(Node n, JSScope scope) {
+ public static Node transform(Node n, Scope scope) {
if (n.cdata != null) n.cdata = eval(n.cdata, scope).toString();
else for(int i=1; i<n.numattrs*2; i+=2) n.attrs[i] = eval(n.attrs[i], scope).toString();
return n;
}
- private static Object eval(String s, JSScope scope) {
+ private static Object eval(String s, Scope scope) {
if (s == null) return null;
StringBuffer ret = new StringBuffer();
for(boolean first = true; s.indexOf("${") != -1; first = false) {
return ret.toString();
}
- public static Object exec(String s, JSScope scope) {
+ public static Object exec(String s, Scope scope) {
try {
- return JS.eval(JS.cloneWithNewParentScope(JS.fromReader("input", 0, new StringReader(s)), scope));
+ return JSU.cloneWithNewGlobalScope(JSU.fromReader("input", 0, new StringReader(s)), scope).call(null,null);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
- private JSScope scope;
+ private Scope scope;
private Servlet.ServletScope servletscope;
private Node.Stream children;
public Node.Stream wrap(Node.Stream children) { this.children = children; return this; }
- public Template(Servlet.ServletScope servletscope, JSScope scope, Reader template) {
+ public Template(Servlet.ServletScope servletscope, JS scope, Reader template) {
super(new Node.Stream.FromXML(template));
- this.scope = scope;
+ this.scope = new Scope(scope);
this.servletscope = servletscope;
}
public boolean _read(Node n) { boolean ret = __read(n); if (ret) transform(n, scope); return ret; }
final String rest = uri.substring(uri.indexOf(':')+1);
if (uri.equals("http://www.w3.org/1999/xhtml")) { return true;
} else if (method.equals("webinf")) {
- return graft(newTemplate(servletscope, copyNodeToScope(transform(n, scope), new JSScope(servletscope)),
+ return graft(newTemplate(servletscope, copyNodeToScope(transform(n, scope), new Scope(servletscope)),
servletscope.getRealPath("/") + "/WEB-INF/" + rest + name), n).upstreamRead(n);
} else if (uri.equals("http://xt.ibex.org/")) {
//#switch(name)
case "if":
transform(n, scope);
- return graft("true".equals(n.attr("if")) ? new DropTag() : new DropAll(), n).upstreamRead(n);
+ return graft((Node.Stream.Functor)("true".equals(n.attr("if")) ? new DropTag() : new DropAll()), n).upstreamRead(n);
case "js": return graft(new JsTag(scope), n).upstreamRead(n);
case "foreach": return graft(new ForEach(n, scope), n).upstreamRead(n);
case "children":
private class ForEach extends Node.Stream.Filter implements Node.Stream.Functor {
private Node[] nodes = null;
private Vec array = new Vec();
- private JSScope scope;
- public ForEach(Node n, JSScope s) {
+ private Scope scope;
+ public ForEach(Node n, Scope s) {
super(Node.Stream.NULL);
- Vec v = ((JSArray)exec("return (" + n.attr("in").toString() + ");", this.scope = s)).toVec();
- while(true) { Object o = v.pop(); if (o == null) break; array.push(o); }
+ try {
+ JSArray a = ((JSArray)exec("return (" + n.attr("in").toString() + ");", this.scope = s));
+ while(true) {
+ JS o = a.call(JSU.S("pop"), new JS[] { });
+ if (o == null) break;
+ array.push(o);
+ }
+ } catch (JSExn e) {
+ throw new RuntimeException(e);
+ }
}
public Node.Stream wrap(Node.Stream kids) {
Vec nodes = new Vec();
protected boolean _read(Node n) {
if (upstreamRead(n)) return true;
if (array.size() == 0) return false;
- JSScope scope2 = new JSScope(scope);
- try { scope2.declare("x"); scope2.put("x", array.pop()); } catch (JSExn e) { throw new RuntimeException(e); }
+ Scope scope2 = new Scope(scope);
+ try { scope2.declare("x"); scope2.put(JSU.S("x"), (JS)array.pop()); } catch (JSExn e) { throw new RuntimeException(e); }
return graft(new ConstantFunctor(new JSRewriter(new Node.Stream() {
private int i = 0;
protected boolean _read(Node n) {
}
}
+ public static class Scope extends JS.Immutable {
+ private final JS parent;
+ private final Hash declared = new Hash();
+ public Scope(JS parent) { this.parent = parent; }
+ public JS get(JS key) throws JSExn {
+ if (declared.get(key)!=null) return super.get(key);
+ return parent.get(key);
+ }
+ public void put(JS key, JS val) throws JSExn {
+ if (declared.get(key)!=null) super.put(key, val);
+ else parent.put(key, val);
+ }
+ public void declare(JS key) { declared.put(key, Boolean.TRUE); }
+ public void declare(String key) { declare(JSU.S(key)); }
+ public void undeclare(JS key) { declared.remove(key); }
+ public void undeclare(String key) { undeclare(JSU.S(key)); }
+ }
+
private class DropTag implements Node.Stream.Functor {
public Node.Stream wrap(Node.Stream kids) {
return kids;
} }
private class JsTag implements Node.Stream.Functor {
- JSScope scope;
- public JsTag(JSScope scope) { this.scope = scope; }
+ Scope scope;
+ public JsTag(Scope scope) { this.scope = scope; }
public Node.Stream wrap(final Node.Stream s) {
return new Node.Stream() {
protected boolean _read(Node n) {