move transaction scope management into Template.java
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / Template.java
index 4ab16d6..323fb00 100644 (file)
@@ -14,18 +14,26 @@ import java.util.*;
 import org.ibex.util.*;
 import org.ibex.js.*;
 
-public class Template extends JSElement {
+// 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 {
+    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 "js":          e = new Template.JSTag(e); break;
             case "foreach":     e = new Template.ForEach(e); break;
@@ -35,10 +43,7 @@ public class Template extends JSElement {
             //#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 JSElement.Exn("Unknown XT library: "+uri);
+            throw new JSLeaf.Exn("Unknown XT library: "+uri);
 
         } else if (uri.startsWith("local:")) {
             // merge a new template into this tree
@@ -48,8 +53,8 @@ public class Template extends JSElement {
             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 JSElement.Exn(
+                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.");
 
@@ -57,82 +62,108 @@ public class Template extends JSElement {
                 e.getChildren().clear();
             }
 
-            XML.Element merged = 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), merged);
+                ch.set(ch.indexOf(e), t);
             }
 
-            return wrap(merged, s);
+            return wrap(t, s);
         }
 
-        XML.Attributes a = e.getAttributes();
-        for (int i=0; i < a.attrSize(); i++) {
-            // FIXME: questionable abuse of XML namespaces here
-            if ("if".equals(a.getKey(i)) && (
-                "http://xt.ibex.org/".equals(e.getUri()) ||
-                "http://xt.ibex.org/".equals(a.getUri(i)))) {
-                e = new Template.IfWrap(e);
-            }
-        }
+        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 XML.Element findPlaceholder(XML.Element 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;
-            XML.Element 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 IfWrap extends JSElement {
-        public IfWrap(XML.Element e) { super(e); }
 
-        public void out(Writer w) throws IOException {
-            loadAttr();
+    /** 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 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 && !"true".equals(varIf)) return;
-            } catch (JSExn e) { throw new JSElement.Exn(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); }
 
             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 JSElement.Exn(
+                if (c.get(i) instanceof Tree.Element) throw new JSLeaf.Exn(
                     "<"+getPrefix()+":js> tags may not have child elements");
         }
 
         public void out(Writer w) throws IOException {
-            loadAttr();
-
             List c = getChildren();
             StringWriter s = new StringWriter();
             for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s);
@@ -140,79 +171,102 @@ public class Template extends JSElement {
         }
     }
 
-    public static final class ForEach extends JSElement {
-        public ForEach(XML.Element e) { super(e); }
+    public static final class ForEach extends JSLeaf.Element {
+        public ForEach(Tree.Element e) { super(e); }
 
         public void out(Writer w) throws IOException {
-            loadAttr();
-
             try {
-                Object varIn = get("in"); if (varIn != null) undeclare("in");
-                Object varPut = get("put"); if (varPut != null) undeclare("put");
+                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 JSElement.Exn(
+                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 JSElement.Exn(
+                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 JSElement.Exn(
+                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());
+                    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 JSElement.Exn(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); }
     }
 
-    public static final class Redirect extends JSElement {
-        public Redirect(XML.Element e) { super(e); }
+    public static final class Redirect extends JSLeaf.Element {
+        public Redirect(Tree.Element e) { super(e); }
 
         public void out(Writer w) throws IOException {
-            loadAttr();
-
             try {
-                Object p = get("page"); if (p != null) undeclare("page");
+                Object p = scope().get("page"); if (p != null) scope().undeclare("page");
                 if (p == null || !(p instanceof String) || ((String)p).trim().equals(""))
-                    throw new JSElement.Exn("<"+getPrefix()+":redirect> requires 'page' "+
+                    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 JSElement.Exn(e); }
+            } catch (JSExn e) { throw new JSLeaf.Exn(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 Transaction extends JSLeaf.Element {
+        private Template.Scope scope;
+        public Transaction(Tree.Element e, Template.Scope s) { super(e); scope = s; }
 
         public void out(Writer w) throws IOException {
-            loadAttr();
-
-            // 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 {
+            StringWriter sw = new StringWriter();
+            super.out(sw);
+            w.write((String)eval(sw.toString()));
+        }
+    }
+
     public abstract static class Scope extends JSScope {
         public Scope(JSScope j) { super(j); }