seperate JSScope and update for new Tree interfaces
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / Template.java
index 7f75bd2..c30ece6 100644 (file)
@@ -14,15 +14,18 @@ import java.util.*;
 import org.ibex.util.*;
 import org.ibex.js.*;
 
-public class Template extends JSElement {
+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 == null) {
@@ -40,7 +43,7 @@ public class Template extends JSElement {
             //#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
@@ -50,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.");
 
@@ -59,7 +62,7 @@ public class Template extends JSElement {
                 e.getChildren().clear();
             }
 
-            XML.Element merged = new JSElement.Merge(t, e);
+            Tree.Element merged = new JSLeaf.Merge(t, e);
 
             // remap the parent of the original element
             if (e.getParent() != null) {
@@ -74,41 +77,41 @@ public class Template extends JSElement {
 
         // 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; }
 
 
     /** Processes ${...} blocks in attributes, loads applicable
      *  attributes into the JS scope and processes global attributes. */
-    public static class AttributeEval extends JSElement implements XML.Attributes {
-        protected XML.Attributes a;
+    public static class AttributeEval extends JSLeaf.Element implements Tree.Attributes {
+        protected Tree.Attributes a;
 
-        public AttributeEval(XML.Element wrapped) { super(wrapped); a = wrapped.getAttributes(); }
+        public AttributeEval(Tree.Element wrapped) { super(wrapped); a = wrapped.getAttributes(); }
 
-        public XML.Attributes getAttributes() { return this; }
+        public Tree.Attributes getAttributes() { return this; }
 
         public int getIndex(String q) { return a.getIndex(q); }
         public int getIndex(String u, String k) { return a.getIndex(u, k); }
@@ -121,7 +124,7 @@ public class Template extends JSElement {
 
         public void out(Writer w) throws IOException {
             try {
-                // FIXME: questionable abuse of XML namespaces here
+                // 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;
@@ -130,16 +133,16 @@ public class Template extends JSElement {
                     case "if": if (!"true".equals(eval(a.getVal(i)))) return;
                     case "declare":
                         Object d = eval(a.getVal(i));
-                        if (!(d instanceof String)) throw new JSElement.Exn(
+                        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()) declare(st.nextToken());
+                        while (st.hasMoreTokens()) scope().declare(st.nextToken());
                         continue;
                     //#end
 
-                    declare(a.getKey(i));
-                    put(a.getKey(i), eval(a.getVal(i)));
+                    scope().declare(a.getKey(i));
+                    scope().put(a.getKey(i), eval(a.getVal(i)));
                 }
             } catch (JSExn e) { throw new Exn(e); }
 
@@ -147,12 +150,12 @@ public class Template extends JSElement {
         }
     }
 
-    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");
         }
 
@@ -164,61 +167,62 @@ 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 {
             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 {
             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 {
+    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(XML.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
+        public Transaction(Tree.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
 
         public void out(Writer w) throws IOException {
             // TODO: <xt:use />
@@ -231,6 +235,16 @@ public class Template extends JSElement {
         }
     }
 
+    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()));
+        }
+    }
+
     public abstract static class Scope extends JSScope {
         public Scope(JSScope j) { super(j); }