finished updating xt for new JS API
authoradam <adam@megacz.com>
Fri, 18 Feb 2005 10:51:41 +0000 (10:51 +0000)
committeradam <adam@megacz.com>
Fri, 18 Feb 2005 10:51:41 +0000 (10:51 +0000)
darcs-hash:20050218105141-5007d-853a991ab3a3c8159695155d36af48422b9acbd0.gz

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

index 3f8b2c2..04e7ee6 100644 (file)
@@ -118,8 +118,8 @@ public class Node {
             protected boolean _read(Node n) { try {
                 Object ret = xml.next();
                 if (ret == null) return false;
             protected boolean _read(Node n) { try {
                 Object ret = xml.next();
                 if (ret == null) return false;
-                if (ret instanceof String) {
-                    n.cdata = (String)ret;
+                if (ret instanceof XML.Text) {
+                    n.cdata = ((XML.Text)ret).t;
                     n.delta = xml.getDepth() - currentdelta;
                     currentdelta = xml.getDepth();
                     return true;
                     n.delta = xml.getDepth() - currentdelta;
                     currentdelta = xml.getDepth();
                     return true;
@@ -139,7 +139,11 @@ public class Node {
             } catch (Exception e) { throw new RuntimeException(e); } }
         }
 
             } catch (Exception e) { throw new RuntimeException(e); } }
         }
 
-        public void toXML(Writer writer) throws IOException { Node n = new Node(); if (read(n)) toXML(writer, n); }
+        public void toXML(Writer writer) throws IOException {
+            Node n = new Node();
+            do { if (!read(n)) n = null; } while (n!=null && n.cdata != null);
+            toXML(writer, n);
+        }
         private Node toXML(Writer w, Node n) throws IOException {
             final String name = n.name;
             if (n.cdata != null) {
         private Node toXML(Writer w, Node n) throws IOException {
             final String name = n.name;
             if (n.cdata != null) {
@@ -160,7 +164,7 @@ public class Node {
                     w.write("/>");
                 } else {
                     w.write(">");
                     w.write("/>");
                 } else {
                     w.write(">");
-                    while(n != null && n.delta > 0) n = toXML(w, n);
+                    while(n != null && n.delta > 0) {  n = toXML(w, n); }
                     w.write("</");
                     w.write(name);
                     w.write(">");
                     w.write("</");
                     w.write(name);
                     w.write(">");
index 98f8071..7245386 100644 (file)
@@ -62,7 +62,7 @@ public class Prevalence {
                     System.err.println("configuring with " + manager);
                     pf.configureSnapshotManager(manager);
                     */
                     System.err.println("configuring with " + manager);
                     pf.configureSnapshotManager(manager);
                     */
-                    //pf.configureSnapshotManager(new SnapshotManager(new JS(), base));
+                    pf.configureSnapshotManager(new SnapshotManager(new JS.Obj(), base));
                     //pf.configureClassLoader(JSTransaction.class.getClassLoader());
                     prevayler = pf.create();
                     prevaylers.put(cx, prevayler);
                     //pf.configureClassLoader(JSTransaction.class.getClassLoader());
                     prevayler = pf.create();
                     prevaylers.put(cx, prevayler);
index 38182ca..f00c387 100644 (file)
@@ -44,7 +44,9 @@ public class Servlet extends HttpServlet {
         servletscope = new ServletScope(request, response, cx);
         path = cx.getRealPath(((HttpServletRequest)request).getServletPath());
         Reader xmlreader = new InputStreamReader(new FileInputStream(path));
         servletscope = new ServletScope(request, response, cx);
         path = cx.getRealPath(((HttpServletRequest)request).getServletPath());
         Reader xmlreader = new InputStreamReader(new FileInputStream(path));
-        new Template(servletscope, servletscope, xmlreader).wrap(null).toXML(response.getWriter());
+        Writer w = response.getWriter();
+        new Template(servletscope, servletscope, xmlreader).wrap(null).toXML(w);
+        w.flush();
     }
 
     public class ServletScope extends JS.Obj {
     }
 
     public class ServletScope extends JS.Obj {
index b585033..3a492af 100644 (file)
@@ -41,30 +41,32 @@ public class Template extends Node.Stream.Filter implements Node.Stream.Functor
     }
 
     public static Node transform(Node n, Scope 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;
+        try {
+            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;
+        } catch (JSExn e) { throw new RuntimeException(e); }
     }
 
     }
 
-    private static Object eval(String s, Scope scope) {
+    private static Object eval(String s, Scope scope) throws JSExn {
         if (s == null) return null;
         StringBuffer ret = new StringBuffer();
         for(boolean first = true; s.indexOf("${") != -1; first = false) {
             ret.append(s.substring(0, s.indexOf("${")));
             String s2 = s.substring(s.indexOf("${")+2);
         if (s == null) return null;
         StringBuffer ret = new StringBuffer();
         for(boolean first = true; s.indexOf("${") != -1; first = false) {
             ret.append(s.substring(0, s.indexOf("${")));
             String s2 = s.substring(s.indexOf("${")+2);
-            Object app = exec("return (" + s2.substring(0, s2.indexOf('}')) + ");\n", scope);
+            JS app = exec("return (" + s2.substring(0, s2.indexOf('}')) + ");\n", scope);
             s = s.substring(s.indexOf('}') + 1);
             //if (first && s.trim().length() == 0) return app;
             s = s.substring(s.indexOf('}') + 1);
             //if (first && s.trim().length() == 0) return app;
-            if (!(app == null || app instanceof String || app instanceof Number || app instanceof Boolean))
+            if (!(app == null || app instanceof JSPrimitive))
                 throw new RuntimeException("javascripts within ${...} can only return strings, numbers, and booleans; not a " +
                                            app.getClass().getName());
                 throw new RuntimeException("javascripts within ${...} can only return strings, numbers, and booleans; not a " +
                                            app.getClass().getName());
-            ret.append(app == null ? "null" : app.toString());
+            ret.append(app == null ? "null" : JSU.toString(app));
         }
         ret.append(s);
         return ret.toString();
     }
 
         }
         ret.append(s);
         return ret.toString();
     }
 
-    public static Object exec(String s, Scope scope) {
+    public static JS exec(String s, Scope scope) {
         try {
             return JSU.cloneWithNewGlobalScope(JSU.fromReader("input", 0, new StringReader(s)), scope).call(null,null);
         } catch (Exception e) {
         try {
             return JSU.cloneWithNewGlobalScope(JSU.fromReader("input", 0, new StringReader(s)), scope).call(null,null);
         } catch (Exception e) {
@@ -156,7 +158,7 @@ public class Template extends Node.Stream.Filter implements Node.Stream.Functor
         }
     }
 
         }
     }
 
-    public static class Scope extends JS.Immutable {
+    public static class Scope extends JS.Obj {
         private final JS parent;
         private final Hash declared = new Hash();
         public Scope(JS parent) { this.parent = parent; }
         private final JS parent;
         private final Hash declared = new Hash();
         public Scope(JS parent) { this.parent = parent; }