move transaction scope management into Template.java
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / Template.java
index cbe272d..323fb00 100644 (file)
@@ -1,4 +1,4 @@
-package ibex.xt;
+package org.ibex.xt;
 
 import java.io.BufferedReader;
 import java.io.FileInputStream;
@@ -8,45 +8,53 @@ import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.io.IOException;
+import java.io.FileNotFoundException;
 
 import java.util.*;
-import ibex.util.*;
-import ibex.js.*;
-
-public class Template extends JSElement {
-    public static Template parse(String path, Template.Scope s) throws IOException, XML.Exn {
+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)));
-        XML.Document doc = new XML.Document();
+        XML.Document doc = new XML.Document(); // FIXME: switch to Tree.Stream
         doc.parse(xmlreader);
         return new Template(doc.getRoot(), s);
     }
 
-    public static XML.Element wrap(XML.Element e, Template.Scope s) throws IOException, XML.Exn {
+    public static Tree.Leaf wrap(Tree.Leaf leaf, Template.Scope s) throws IOException {
+        if (!(leaf instanceof Tree.Element)) return new Text(leaf);
+
+        Tree.Element e = (Tree.Element)leaf;
         final String uri = e.getUri();
 
-        if (uri.equals("http://xt.ibex.org/")) {
+        if (uri == null) {
+            // do nothing
+        } else if (uri.equals("http://xt.ibex.org/")) {
             //#switch(e.getLocalName())
-            case "if":          e = new Template.If(e); break;
             case "js":          e = new Template.JSTag(e); break;
             case "foreach":     e = new Template.ForEach(e); break;
             case "children":    e = new Template.Children(e); break;
+            case "redirect":    e = new Template.Redirect(e); break;
             case "transaction": e = new Template.Transaction(e, s); break;
             //#end
 
         } else if (uri.startsWith("http://xt.ibex.org/")) {
-            //#switch(uri.substring(19))
-            case "io": System.out.println("ibex.xt.io not yet implemented"); // TODO
-            //#end
-            throw new RuntimeException("Unknown XT library: "+uri);
+            throw new JSLeaf.Exn("Unknown XT library: "+uri);
 
         } else if (uri.startsWith("local:")) {
-            Template t = parse(s.getLocalPath() + uri.substring(6), s);
+            // merge a new template into this tree
+            String path = uri.substring(6) + e.getLocalName() + ".xt";
+            Template t = parse(s.getLocalPath() + path, s);
 
             List c = e.getChildren();
             if (c.size() > 0) {
                 // move all children from e to placeholder
-                XML.Element placeholder = findPlaceholder(t);
-                if (placeholder == null) throw new RuntimeException(
+                Tree.Element placeholder = findPlaceholder(t);
+                if (placeholder == null) throw new JSLeaf.Exn(
                     "<"+e.getQName()+"> attempted to include children into a " +
                     "template which does not contain an <xt:children /> tag.");
 
@@ -54,142 +62,209 @@ public class Template extends JSElement {
                 e.getChildren().clear();
             }
 
-            // merge original attributes with replacement template
-            e = new JSElement.Merge(t, e);
+            // merge the attributes of the template and its representative
+            t.setAttributes(new JSLeaf.MergeAttr(e.getAttributes(), t.getAttributes()));
+
+            // remap the parent of the original element
+            if (e.getParent() != null) {
+                List ch = e.getParent().getChildren();
+                ch.set(ch.indexOf(e), t);
+            }
 
-        } else if (uri.startsWith("java:")) {
-            e = new Java(e);
+            return wrap(t, s);
         }
 
+        e = new Template.AttributeEval(e);
+
         // wrap children
         List c = e.getChildren();
-        for (int i=0; i < c.size(); i++)
-            if (c.get(i) instanceof XML.Element) wrap((XML.Element)c.get(i), s);
+        for (int i=0; i < c.size(); i++) wrap((Tree.Leaf)c.get(i), s);
 
         return e;
     }
 
     /** Returns the first Template.Children child found. */
-    private static Template.Children findPlaceholder(XML.Element e) {
-        if (e instanceof Template.Children) return (Template.Children)e;
+    private static Tree.Element findPlaceholder(Tree.Element e) {
+        if ("http://xt.ibex.org/".equals(e.getUri()) && "children".equals(e.getLocalName()))
+            return e;
+
         List c = e.getChildren();
         for (int i=0; i < c.size(); i++) {
-            if (!(c.get(i) instanceof XML.Element)) continue;
-            Template.Children ret = findPlaceholder((XML.Element)c.get(i));
+            if (!(c.get(i) instanceof Tree.Element)) continue;
+            Tree.Element ret = findPlaceholder((Tree.Element)c.get(i));
             if (ret != null) return ret;
         }
         return null;
     }
 
-    private Template.Scope tscope;
+    private transient Template.Scope tscope;
+    private transient JSScope scope = null;
 
-    public Template(XML.Element w, Template.Scope t) { super(w); tscope = t; }
+    public Template(Tree.Element w, Template.Scope t) { super(w); tscope = t; }
 
-    public JSScope getParentScope() { return tscope; }
+    public JSScope scope() { return scope == null ? scope = new JSScope(tscope) : scope; }
 
 
-    public static final class If extends JSElement {
-        public If(XML.Element e) { super(e); }
+    /** Processes ${...} blocks in attributes, loads applicable
+     *  attributes into the JS scope and processes global attributes. */
+    public static class AttributeEval extends JSLeaf.Element implements Tree.Attributes {
+        // TODO: hide global attributes from out() function. waiting on util.XMLHelper
+        protected Tree.Attributes a;
+
+        public AttributeEval(Tree.Element wrapped) {
+            super(wrapped);
+            a = wrapped.getAttributes();
+            wrapped.setAttributes(this);
+        }
 
-        public void toXML(Writer w) throws IOException {
-            super.toXML(w);
+        public int getIndex(String q) { return a.getIndex(q); }
+        public int getIndex(String u, String k) { return a.getIndex(u, k); }
+        public String getKey(int i) { return a.getKey(i); }
+        public String getVal(int i) { return (String)eval(a.getVal(i)); }
+        public String getUri(int i) { return a.getUri(i); }
+        public String getPrefix(int i) { return a.getPrefix(i); }
+        public String getQName(int i) { return a.getQName(i); }
+        public int attrSize() { return a.attrSize(); }
 
+        public void out(Writer w) throws IOException {
             try {
-                Object varIf = get("if"); if (varIf != null) undeclare("if");
-                if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
-            } catch (JSExn e) { throw new RuntimeException(e); }
+                // FIXME: questionable abuse of namespaces here
+                boolean xturi = "http://xt.ibex.org/".equals(getUri());
+                for(int i=0; i < a.attrSize(); i++) {
+                    if (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue;
+
+                    //#switch (a.getKey(i))
+                    case "if": if (!"true".equals(eval(a.getVal(i)))) return;
+                    case "declare":
+                        Object d = eval(a.getVal(i));
+                        if (!(d instanceof String)) throw new JSLeaf.Exn(
+                            "attribute '"+getPrefix()+":declare' can only contain a "+
+                            "space seperated list of variable names to declare.");
+                        StringTokenizer st = new StringTokenizer((String)d, " ");
+                        while (st.hasMoreTokens()) scope().declare(st.nextToken());
+                        continue;
+                    //#end
+
+                    scope().declare(a.getKey(i));
+                    scope().put(a.getKey(i), eval(a.getVal(i)));
+                }
+            } catch (JSExn e) { throw new Exn(e); }
 
-            List c = getChildren();
-            for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(w);
+            wrapped.out(w);
         }
     }
 
-    public static final class JSTag extends JSElement {
-        public JSTag(XML.Element e) {
+    public static final class JSTag extends JSLeaf.Element {
+        public JSTag(Tree.Element e) {
             super(e);
             List c = getChildren();
             for (int i=0; i < c.size(); i++)
-                if (c.get(i) instanceof XML.Element) throw new RuntimeException(
+                if (c.get(i) instanceof Tree.Element) throw new JSLeaf.Exn(
                     "<"+getPrefix()+":js> tags may not have child elements");
         }
 
-        public void toXML(Writer w) throws IOException {
-            super.toXML(w);
-
-            try {
-                Object varIf = get("if"); if (varIf != null) undeclare("if");
-                if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
-
-                List c = getChildren();
-                StringWriter s = new StringWriter();
-                for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(s);
-                exec(s.toString());
-            } catch (JSExn e) { throw new RuntimeException(e); }
+        public void out(Writer w) throws IOException {
+            List c = getChildren();
+            StringWriter s = new StringWriter();
+            for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s);
+            exec(s.toString());
         }
     }
 
-    public static final class ForEach extends JSElement {
-        public ForEach(XML.Element e) { super(e); }
-
-        public void toXML(Writer w) throws IOException {
-            super.toXML(w);
+    public static final class ForEach extends JSLeaf.Element {
+        public ForEach(Tree.Element e) { super(e); }
 
+        public void out(Writer w) throws IOException {
             try {
-                Object varIn = get("in"); if (varIn != null) undeclare("in");
-                Object varPut = get("put"); if (varPut != null) undeclare("put");
-                Object varIf = get("if"); if (varIf != null) undeclare("if");
-                if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
+                JSScope s = scope();
+                Object varIn = s.get("in"); if (varIn != null) s.undeclare("in");
+                Object varPut = s.get("put"); if (varPut != null) s.undeclare("put");
 
                 varIn = exec("return (" + varIn + ");");
-                if (varIn == null || (varIn instanceof JSArray)) throw new RuntimeException(
+                if (varIn == null || (varIn instanceof JSArray)) throw new JSLeaf.Exn(
                     "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
                     "the name of a valid js array in the current scope, not in='"+varIn+"'.");
 
                 if (varPut == null) varPut = "x";
-                else if (!(varPut instanceof String) || get(varPut) != null)
-                    throw new RuntimeException(
+                else if (!(varPut instanceof String) || s.get(varPut) != null)
+                    throw new JSLeaf.Exn(
                     "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
                     "an undeclared variable, not put='"+varPut+"'.");
-                if (get(varPut) != null) throw new RuntimeException(
+                if (scope().get(varPut) != null) throw new JSLeaf.Exn(
                     "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
                     "default variable 'x' already exists in the current scope.");
 
                 List c = getChildren();
 
-                declare((String)varPut);
+                s.declare((String)varPut);
                 Iterator it = ((JSArray)varIn).toList().iterator(); while (it.hasNext()) {
-                    put(varPut, it.next());
-                    for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(w);
+                    s.put(varPut, it.next());
+                    for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w);
                 }
-            } catch (JSExn e) { throw new RuntimeException(e); }
+            } catch (JSExn e) { throw new JSLeaf.Exn(e); }
         }
     }
 
-    public static final class Children extends JSElement {
-        public Children(XML.Element e) { super(e); }
+    public static final class Children extends JSLeaf.Element {
+        public Children(Tree.Element e) { super(e); }
     }
 
-    // TODO: finish
-    public static final class Transaction extends JSElement {
-        private final Template.Scope scope; // FIXME: HACK. unstatisise all tags, or do this to all
-        public Transaction(XML.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
+    public static final class Redirect extends JSLeaf.Element {
+        public Redirect(Tree.Element e) { super(e); }
+
+        public void out(Writer w) throws IOException {
+            try {
+                Object p = scope().get("page"); if (p != null) scope().undeclare("page");
+                if (p == null || !(p instanceof String) || ((String)p).trim().equals(""))
+                    throw new JSLeaf.Exn("<"+getPrefix()+":redirect> requires 'page' "+
+                                            "attribute to be a valid template path");
+                throw new RedirectSignal((String)p);
+            } catch (JSExn e) { throw new JSLeaf.Exn(e); }
+        }
+    }
 
-        public void toXML(Writer w) throws IOException {
-            super.toXML(w);
+    // TODO: finish
+    public static final class Transaction extends JSLeaf.Element {
+        private Template.Scope scope;
+        public Transaction(Tree.Element e, Template.Scope s) { super(e); scope = s; }
 
-            // TODO: <xt:use />
-            List c = getChildren();
+        public void out(Writer w) throws IOException {
             StringWriter sw = new StringWriter();
-            for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(sw);
-            JS t = JS.fromReader("input", 0, new StringReader(sw.toString()));
-            t = JS.cloneWithNewParentScope(t, new JSScope(null));
+            List c = getChildren();
+            for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(sw);
+            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 Java extends JSElement {
-        // TODO what exactly?
-        public Java(XML.Element w) { super(w); }
+    public static final class Text extends JSLeaf {
+        public Text(Tree.Leaf w) { super(w); }
+        public void out(Writer w) throws IOException {
+            StringWriter sw = new StringWriter();
+            super.out(sw);
+            w.write((String)eval(sw.toString()));
+        }
     }
 
     public abstract static class Scope extends JSScope {
@@ -202,4 +277,11 @@ public class Template extends JSElement {
         public abstract void transaction(JS t);
     }
 
+    public static class Signal extends RuntimeException {}
+    public static class ReturnSignal extends Signal { }
+    public static class RedirectSignal extends Signal {
+        protected String target;
+        public RedirectSignal(String target) { super(); this.target = target; }
+        public String getTarget() { return target; }
+    }
 }