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