reorganise for new collections
[org.ibex.xt-crawshaw.git] / src / java / ibex / xt / Template.java
diff --git a/src/java/ibex/xt/Template.java b/src/java/ibex/xt/Template.java
new file mode 100644 (file)
index 0000000..ac6ea78
--- /dev/null
@@ -0,0 +1,207 @@
+package ibex.xt;
+
+import ibex.js.*;
+import ibex.util.*;
+import java.util.*;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.io.IOException;
+
+
+public class Template extends JSElement {
+    public static Template parse(String path, Template.Scope s) throws IOException, XML.Exn {
+        Reader xmlreader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
+        XML.Document doc = new XML.Document();
+        doc.parse(xmlreader);
+        return new Template(doc.getRoot(), s);
+    }
+
+    public static XML.Element wrap(XML.Element e, Template.Scope s) throws IOException, XML.Exn {
+        final String uri = e.getUri();
+
+        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 "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);
+
+        } else if (uri.startsWith("local:")) {
+            Template t = parse(s.getLocalPath() + uri.substring(6), 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(
+                    "<"+e.getQName()+"> attempted to include children into a " +
+                    "template which does not contain an <xt:children /> tag.");
+
+                placeholder.getChildren().addAll(e.getChildren());
+                e.getChildren().clear();
+            }
+
+            // merge original attributes with replacement template
+            e = new JSElement.Merge(t, e);
+
+        } else if (uri.startsWith("java:")) {
+            e = new Java(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);
+
+        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;
+        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 (ret != null) return ret;
+        }
+        return null;
+    }
+
+    private Template.Scope tscope;
+
+    public Template(XML.Element w, Template.Scope t) { super(w); tscope = t; }
+
+    public JSScope getParentScope() { return tscope; }
+
+
+    public static final class If extends JSElement {
+        public If(XML.Element e) { super(e); }
+
+        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;
+            } catch (JSExn e) { throw new RuntimeException(e); }
+
+            List c = getChildren();
+            for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(w);
+        }
+    }
+
+    public static final class JSTag extends JSElement {
+        public JSTag(XML.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(
+                    "<"+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 static final class ForEach extends JSElement {
+        public ForEach(XML.Element e) { super(e); }
+
+        public void toXML(Writer w) throws IOException {
+            super.toXML(w);
+
+            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;
+
+                varIn = exec("return (" + varIn + ");");
+                if (varIn == null || (varIn instanceof JSArray)) throw new RuntimeException(
+                    "<"+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(
+                    "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
+                    "an undeclared variable, not put='"+varPut+"'.");
+                if (get(varPut) != null) throw new RuntimeException(
+                    "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
+                    "default variable 'x' already exists in the current scope.");
+
+                List c = getChildren();
+
+                declare((String)varPut);
+                Vec v = ((JSArray)varIn).toVec();
+                for (int j=0; j < v.size(); j++) {
+                    put(varPut, v.elementAt(j));
+                    for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(w);
+                }
+            } catch (JSExn e) { throw new RuntimeException(e); }
+        }
+    }
+
+    public static final class Children extends JSElement {
+        public Children(XML.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 void toXML(Writer w) throws IOException {
+            super.toXML(w);
+
+            // TODO: <xt:use />
+            List c = getChildren();
+            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));
+            scope.transaction(t);
+        }
+    }
+
+    public static final class Java extends JSElement {
+        // TODO what exactly?
+        public Java(XML.Element w) { super(w); }
+    }
+
+    public abstract static class Scope extends JSScope {
+        public Scope(JSScope j) { super(j); }
+
+        /** Returns the template path for local:/ namespace. */
+        public abstract String getLocalPath();
+
+        /** Registers a new Prevayler transaction. */
+        public abstract void transaction(JS t);
+    }
+
+}