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