move transaction scope management into Template.java
[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 // FIXME: replace scope().get("") with containsKey() check on attributes,
18 //        thereby neatly sidestepping any higher up user defined variables
19 //        (especially when combined with undeclare()
20 public class Template extends JSLeaf.Element {
21     public static Template parse(String path, Template.Scope s) throws FileNotFoundException, IOException {
22         Reader xmlreader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
23         XML.Document doc = new XML.Document(); // FIXME: switch to Tree.Stream
24         doc.parse(xmlreader);
25         return new Template(doc.getRoot(), s);
26     }
27
28     public static Tree.Leaf wrap(Tree.Leaf leaf, Template.Scope s) throws IOException {
29         if (!(leaf instanceof Tree.Element)) return new Text(leaf);
30
31         Tree.Element e = (Tree.Element)leaf;
32         final String uri = e.getUri();
33
34         if (uri == null) {
35             // do nothing
36         } else if (uri.equals("http://xt.ibex.org/")) {
37             //#switch(e.getLocalName())
38             case "js":          e = new Template.JSTag(e); break;
39             case "foreach":     e = new Template.ForEach(e); break;
40             case "children":    e = new Template.Children(e); break;
41             case "redirect":    e = new Template.Redirect(e); break;
42             case "transaction": e = new Template.Transaction(e, s); break;
43             //#end
44
45         } else if (uri.startsWith("http://xt.ibex.org/")) {
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             // merge the attributes of the template and its representative
66             t.setAttributes(new JSLeaf.MergeAttr(e.getAttributes(), t.getAttributes()));
67
68             // remap the parent of the original element
69             if (e.getParent() != null) {
70                 List ch = e.getParent().getChildren();
71                 ch.set(ch.indexOf(e), t);
72             }
73
74             return wrap(t, s);
75         }
76
77         e = new Template.AttributeEval(e);
78
79         // wrap children
80         List c = e.getChildren();
81         for (int i=0; i < c.size(); i++) wrap((Tree.Leaf)c.get(i), s);
82
83         return e;
84     }
85
86     /** Returns the first Template.Children child found. */
87     private static Tree.Element findPlaceholder(Tree.Element e) {
88         if ("http://xt.ibex.org/".equals(e.getUri()) && "children".equals(e.getLocalName()))
89             return e;
90
91         List c = e.getChildren();
92         for (int i=0; i < c.size(); i++) {
93             if (!(c.get(i) instanceof Tree.Element)) continue;
94             Tree.Element ret = findPlaceholder((Tree.Element)c.get(i));
95             if (ret != null) return ret;
96         }
97         return null;
98     }
99
100     private transient Template.Scope tscope;
101     private transient JSScope scope = null;
102
103     public Template(Tree.Element w, Template.Scope t) { super(w); tscope = t; }
104
105     public JSScope scope() { return scope == null ? scope = new JSScope(tscope) : scope; }
106
107
108     /** Processes ${...} blocks in attributes, loads applicable
109      *  attributes into the JS scope and processes global attributes. */
110     public static class AttributeEval extends JSLeaf.Element implements Tree.Attributes {
111         // TODO: hide global attributes from out() function. waiting on util.XMLHelper
112         protected Tree.Attributes a;
113
114         public AttributeEval(Tree.Element wrapped) {
115             super(wrapped);
116             a = wrapped.getAttributes();
117             wrapped.setAttributes(this);
118         }
119
120         public int getIndex(String q) { return a.getIndex(q); }
121         public int getIndex(String u, String k) { return a.getIndex(u, k); }
122         public String getKey(int i) { return a.getKey(i); }
123         public String getVal(int i) { return (String)eval(a.getVal(i)); }
124         public String getUri(int i) { return a.getUri(i); }
125         public String getPrefix(int i) { return a.getPrefix(i); }
126         public String getQName(int i) { return a.getQName(i); }
127         public int attrSize() { return a.attrSize(); }
128
129         public void out(Writer w) throws IOException {
130             try {
131                 // FIXME: questionable abuse of namespaces here
132                 boolean xturi = "http://xt.ibex.org/".equals(getUri());
133                 for(int i=0; i < a.attrSize(); i++) {
134                     if (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue;
135
136                     //#switch (a.getKey(i))
137                     case "if": if (!"true".equals(eval(a.getVal(i)))) return;
138                     case "declare":
139                         Object d = eval(a.getVal(i));
140                         if (!(d instanceof String)) throw new JSLeaf.Exn(
141                             "attribute '"+getPrefix()+":declare' can only contain a "+
142                             "space seperated list of variable names to declare.");
143                         StringTokenizer st = new StringTokenizer((String)d, " ");
144                         while (st.hasMoreTokens()) scope().declare(st.nextToken());
145                         continue;
146                     //#end
147
148                     scope().declare(a.getKey(i));
149                     scope().put(a.getKey(i), eval(a.getVal(i)));
150                 }
151             } catch (JSExn e) { throw new Exn(e); }
152
153             wrapped.out(w);
154         }
155     }
156
157     public static final class JSTag extends JSLeaf.Element {
158         public JSTag(Tree.Element e) {
159             super(e);
160             List c = getChildren();
161             for (int i=0; i < c.size(); i++)
162                 if (c.get(i) instanceof Tree.Element) throw new JSLeaf.Exn(
163                     "<"+getPrefix()+":js> tags may not have child elements");
164         }
165
166         public void out(Writer w) throws IOException {
167             List c = getChildren();
168             StringWriter s = new StringWriter();
169             for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s);
170             exec(s.toString());
171         }
172     }
173
174     public static final class ForEach extends JSLeaf.Element {
175         public ForEach(Tree.Element e) { super(e); }
176
177         public void out(Writer w) throws IOException {
178             try {
179                 JSScope s = scope();
180                 Object varIn = s.get("in"); if (varIn != null) s.undeclare("in");
181                 Object varPut = s.get("put"); if (varPut != null) s.undeclare("put");
182
183                 varIn = exec("return (" + varIn + ");");
184                 if (varIn == null || (varIn instanceof JSArray)) throw new JSLeaf.Exn(
185                     "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
186                     "the name of a valid js array in the current scope, not in='"+varIn+"'.");
187
188                 if (varPut == null) varPut = "x";
189                 else if (!(varPut instanceof String) || s.get(varPut) != null)
190                     throw new JSLeaf.Exn(
191                     "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
192                     "an undeclared variable, not put='"+varPut+"'.");
193                 if (scope().get(varPut) != null) throw new JSLeaf.Exn(
194                     "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
195                     "default variable 'x' already exists in the current scope.");
196
197                 List c = getChildren();
198
199                 s.declare((String)varPut);
200                 Iterator it = ((JSArray)varIn).toList().iterator(); while (it.hasNext()) {
201                     s.put(varPut, it.next());
202                     for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w);
203                 }
204             } catch (JSExn e) { throw new JSLeaf.Exn(e); }
205         }
206     }
207
208     public static final class Children extends JSLeaf.Element {
209         public Children(Tree.Element e) { super(e); }
210     }
211
212     public static final class Redirect extends JSLeaf.Element {
213         public Redirect(Tree.Element e) { super(e); }
214
215         public void out(Writer w) throws IOException {
216             try {
217                 Object p = scope().get("page"); if (p != null) scope().undeclare("page");
218                 if (p == null || !(p instanceof String) || ((String)p).trim().equals(""))
219                     throw new JSLeaf.Exn("<"+getPrefix()+":redirect> requires 'page' "+
220                                             "attribute to be a valid template path");
221                 throw new RedirectSignal((String)p);
222             } catch (JSExn e) { throw new JSLeaf.Exn(e); }
223         }
224     }
225
226     // TODO: finish
227     public static final class Transaction extends JSLeaf.Element {
228         private Template.Scope scope;
229         public Transaction(Tree.Element e, Template.Scope s) { super(e); scope = s; }
230
231         public void out(Writer w) throws IOException {
232             StringWriter sw = new StringWriter();
233             List c = getChildren();
234             for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(sw);
235             StringReader sr = new StringReader(sw.toString());
236
237             JS t;
238             try { t = JS.fromReader("input", 0, sr); }
239             catch (IOException e) { throw new JSLeaf.Exn(e.getMessage()); }
240
241             try {
242                 JSScope scope = JS.getParentScope(t);
243
244                 Object u = scope().get("use"); if (u != null) scope().undeclare("use");
245                 if (u != null) {
246                     if (!(u instanceof String)) throw new JSLeaf.Exn(
247                          "<"+getPrefix()+":transaction> requires 'use' attribute "+
248                          "to be a valid space-seperated list of variables");
249                     StringTokenizer st = new StringTokenizer((String)u);
250                     while (st.hasMoreTokens()) {
251                         String k = st.nextToken();
252                         scope.put(k, scope().get(k));
253                     }
254                 }
255             } catch (JSExn e) { throw new JSLeaf.Exn(e); }
256
257             scope.transaction(t);
258         }
259     }
260
261     public static final class Text extends JSLeaf {
262         public Text(Tree.Leaf w) { super(w); }
263         public void out(Writer w) throws IOException {
264             StringWriter sw = new StringWriter();
265             super.out(sw);
266             w.write((String)eval(sw.toString()));
267         }
268     }
269
270     public abstract static class Scope extends JSScope {
271         public Scope(JSScope j) { super(j); }
272
273         /** Returns the template path for local:/ namespace. */
274         public abstract String getLocalPath();
275
276         /** Registers a new Prevayler transaction. */
277         public abstract void transaction(JS t);
278     }
279
280     public static class Signal extends RuntimeException {}
281     public static class ReturnSignal extends Signal { }
282     public static class RedirectSignal extends Signal {
283         protected String target;
284         public RedirectSignal(String target) { super(); this.target = target; }
285         public String getTarget() { return target; }
286     }
287 }