c30ece66ff9d74223df874474a476d48ef5aac74
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / Template.java
1 package org.ibex.xt;
2
3 import java.io.BufferedReader;
4 import java.io.FileInputStream;
5 import java.io.InputStreamReader;
6 import java.io.Reader;
7 import java.io.StringReader;
8 import java.io.StringWriter;
9 import java.io.Writer;
10 import java.io.IOException;
11 import java.io.FileNotFoundException;
12
13 import java.util.*;
14 import org.ibex.util.*;
15 import org.ibex.js.*;
16
17 public class Template extends JSLeaf.Element {
18     public static Template parse(String path, Template.Scope s) throws FileNotFoundException, IOException {
19         Reader xmlreader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
20         XML.Document doc = new XML.Document(); // FIXME: switch to Tree.Stream
21         doc.parse(xmlreader);
22         return new Template(doc.getRoot(), s);
23     }
24
25     public static Tree.Leaf wrap(Tree.Leaf leaf, Template.Scope s) throws IOException {
26         if (!(leaf instanceof Tree.Element)) return new Text(leaf);
27
28         Tree.Element e = (Tree.Element)leaf;
29         final String uri = e.getUri();
30
31         if (uri == null) {
32             // do nothing
33         } else if (uri.equals("http://xt.ibex.org/")) {
34             //#switch(e.getLocalName())
35             case "js":          e = new Template.JSTag(e); break;
36             case "foreach":     e = new Template.ForEach(e); break;
37             case "children":    e = new Template.Children(e); break;
38             case "redirect":    e = new Template.Redirect(e); break;
39             case "transaction": e = new Template.Transaction(e, s); break;
40             //#end
41
42         } else if (uri.startsWith("http://xt.ibex.org/")) {
43             //#switch(uri.substring(19))
44             case "io": System.out.println("ibex.xt.io not yet implemented"); // TODO
45             //#end
46             //throw new JSLeaf.Exn("Unknown XT library: "+uri);
47
48         } else if (uri.startsWith("local:")) {
49             // merge a new template into this tree
50             String path = uri.substring(6) + e.getLocalName() + ".xt";
51             Template t = parse(s.getLocalPath() + path, s);
52
53             List c = e.getChildren();
54             if (c.size() > 0) {
55                 // move all children from e to placeholder
56                 Tree.Element placeholder = findPlaceholder(t);
57                 if (placeholder == null) throw new JSLeaf.Exn(
58                     "<"+e.getQName()+"> attempted to include children into a " +
59                     "template which does not contain an <xt:children /> tag.");
60
61                 placeholder.getChildren().addAll(e.getChildren());
62                 e.getChildren().clear();
63             }
64
65             Tree.Element merged = new JSLeaf.Merge(t, e);
66
67             // remap the parent of the original element
68             if (e.getParent() != null) {
69                 List ch = e.getParent().getChildren();
70                 ch.set(ch.indexOf(e), merged);
71             }
72
73             return wrap(merged, s);
74         }
75
76         e = new Template.AttributeEval(e);
77
78         // wrap children
79         List c = e.getChildren();
80         for (int i=0; i < c.size(); i++) wrap((Tree.Leaf)c.get(i), s);
81
82         return e;
83     }
84
85     /** Returns the first Template.Children child found. */
86     private static Tree.Element findPlaceholder(Tree.Element e) {
87         if ("http://xt.ibex.org/".equals(e.getUri()) && "children".equals(e.getLocalName()))
88             return e;
89
90         List c = e.getChildren();
91         for (int i=0; i < c.size(); i++) {
92             if (!(c.get(i) instanceof Tree.Element)) continue;
93             Tree.Element ret = findPlaceholder((Tree.Element)c.get(i));
94             if (ret != null) return ret;
95         }
96         return null;
97     }
98
99     private transient Template.Scope tscope;
100     private transient JSScope scope = null;
101
102     public Template(Tree.Element w, Template.Scope t) { super(w); tscope = t; }
103
104     public JSScope scope() { return scope == null ? scope = new JSScope(tscope) : scope; }
105
106
107     /** Processes ${...} blocks in attributes, loads applicable
108      *  attributes into the JS scope and processes global attributes. */
109     public static class AttributeEval extends JSLeaf.Element implements Tree.Attributes {
110         protected Tree.Attributes a;
111
112         public AttributeEval(Tree.Element wrapped) { super(wrapped); a = wrapped.getAttributes(); }
113
114         public Tree.Attributes getAttributes() { return this; }
115
116         public int getIndex(String q) { return a.getIndex(q); }
117         public int getIndex(String u, String k) { return a.getIndex(u, k); }
118         public String getKey(int i) { return a.getKey(i); }
119         public String getVal(int i) { return (String)eval(a.getVal(i)); }
120         public String getUri(int i) { return a.getUri(i); }
121         public String getPrefix(int i) { return a.getPrefix(i); }
122         public String getQName(int i) { return a.getQName(i); }
123         public int attrSize() { return a.attrSize(); }
124
125         public void out(Writer w) throws IOException {
126             try {
127                 // FIXME: questionable abuse of namespaces here
128                 boolean xturi = "http://xt.ibex.org/".equals(getUri());
129                 for(int i=0; i < a.attrSize(); i++) {
130                     if (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue;
131
132                     //#switch (a.getKey(i))
133                     case "if": if (!"true".equals(eval(a.getVal(i)))) return;
134                     case "declare":
135                         Object d = eval(a.getVal(i));
136                         if (!(d instanceof String)) throw new JSLeaf.Exn(
137                             "attribute "+getPrefix()+":declare can only contain a "+
138                             "space seperated list of variable names to declare.");
139                         StringTokenizer st = new StringTokenizer((String)d, " ");
140                         while (st.hasMoreTokens()) scope().declare(st.nextToken());
141                         continue;
142                     //#end
143
144                     scope().declare(a.getKey(i));
145                     scope().put(a.getKey(i), eval(a.getVal(i)));
146                 }
147             } catch (JSExn e) { throw new Exn(e); }
148
149             wrapped.out(w);
150         }
151     }
152
153     public static final class JSTag extends JSLeaf.Element {
154         public JSTag(Tree.Element e) {
155             super(e);
156             List c = getChildren();
157             for (int i=0; i < c.size(); i++)
158                 if (c.get(i) instanceof Tree.Element) throw new JSLeaf.Exn(
159                     "<"+getPrefix()+":js> tags may not have child elements");
160         }
161
162         public void out(Writer w) throws IOException {
163             List c = getChildren();
164             StringWriter s = new StringWriter();
165             for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s);
166             exec(s.toString());
167         }
168     }
169
170     public static final class ForEach extends JSLeaf.Element {
171         public ForEach(Tree.Element e) { super(e); }
172
173         public void out(Writer w) throws IOException {
174             try {
175                 JSScope s = scope();
176                 Object varIn = s.get("in"); if (varIn != null) s.undeclare("in");
177                 Object varPut = s.get("put"); if (varPut != null) s.undeclare("put");
178
179                 varIn = exec("return (" + varIn + ");");
180                 if (varIn == null || (varIn instanceof JSArray)) throw new JSLeaf.Exn(
181                     "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
182                     "the name of a valid js array in the current scope, not in='"+varIn+"'.");
183
184                 if (varPut == null) varPut = "x";
185                 else if (!(varPut instanceof String) || s.get(varPut) != null)
186                     throw new JSLeaf.Exn(
187                     "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
188                     "an undeclared variable, not put='"+varPut+"'.");
189                 if (scope().get(varPut) != null) throw new JSLeaf.Exn(
190                     "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
191                     "default variable 'x' already exists in the current scope.");
192
193                 List c = getChildren();
194
195                 s.declare((String)varPut);
196                 Iterator it = ((JSArray)varIn).toList().iterator(); while (it.hasNext()) {
197                     s.put(varPut, it.next());
198                     for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w);
199                 }
200             } catch (JSExn e) { throw new JSLeaf.Exn(e); }
201         }
202     }
203
204     public static final class Children extends JSLeaf.Element {
205         public Children(Tree.Element e) { super(e); }
206     }
207
208     public static final class Redirect extends JSLeaf.Element {
209         public Redirect(Tree.Element e) { super(e); }
210
211         public void out(Writer w) throws IOException {
212             try {
213                 Object p = scope().get("page"); if (p != null) scope().undeclare("page");
214                 if (p == null || !(p instanceof String) || ((String)p).trim().equals(""))
215                     throw new JSLeaf.Exn("<"+getPrefix()+":redirect> requires 'page' "+
216                                             "attribute to be a valid template path");
217                 throw new RedirectSignal((String)p);
218             } catch (JSExn e) { throw new JSLeaf.Exn(e); }
219         }
220     }
221
222     // TODO: finish
223     public static final class Transaction extends JSLeaf.Element {
224         private final Template.Scope scope; // FIXME: HACK. unstatisise all tags, or do this to all
225         public Transaction(Tree.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
226
227         public void out(Writer w) throws IOException {
228             // TODO: <xt:use />
229             List c = getChildren();
230             StringWriter sw = new StringWriter();
231             for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(sw);
232             JS t = JS.fromReader("input", 0, new StringReader(sw.toString()));
233             t = JS.cloneWithNewParentScope(t, new JSScope(null));
234             scope.transaction(t);
235         }
236     }
237
238     public static final class Text extends JSLeaf {
239         public Text(Tree.Leaf w) { super(w); }
240         public void out(Writer w) throws IOException {
241             // FIXME: make eval() take a writer
242             StringWriter sw = new StringWriter();
243             super.out(sw);
244             w.write((String)eval(sw.toString()));
245         }
246     }
247
248     public abstract static class Scope extends JSScope {
249         public Scope(JSScope j) { super(j); }
250
251         /** Returns the template path for local:/ namespace. */
252         public abstract String getLocalPath();
253
254         /** Registers a new Prevayler transaction. */
255         public abstract void transaction(JS t);
256     }
257
258     public static class Signal extends RuntimeException {}
259     public static class ReturnSignal extends Signal { }
260     public static class RedirectSignal extends Signal {
261         protected String target;
262         public RedirectSignal(String target) { super(); this.target = target; }
263         public String getTarget() { return target; }
264     }
265 }