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