076251bcc47bc2854694b8ffb017454d303b981c
[org.ibex.xt-crawshaw.git] / src / ibex / xt / Template.java
1 package ibex.xt;
2
3 import ibex.util.XML;
4 import ibex.util.Vec; //FIXME remove
5
6 import org.ibex.js.JS;
7 import org.ibex.js.JSScope;
8 import org.ibex.js.JSArray;
9 import org.ibex.js.JSExn;
10
11 import ibex.collection.*;
12 import java.util.*;
13
14 import java.io.BufferedReader;
15 import java.io.FileInputStream;
16 import java.io.InputStreamReader;
17 import java.io.Reader;
18 import java.io.StringReader;
19 import java.io.StringWriter;
20 import java.io.Writer;
21 import java.io.IOException;
22
23
24 public class Template extends JSElement {
25     public static Template parse(String path, Template.Scope s) throws IOException, XML.Exn {
26         Reader xmlreader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
27         XML.Document doc = new XML.Document();
28         doc.parse(xmlreader);
29         return new Template(doc.getRoot(), s);
30     }
31
32     public static XML.Element wrap(XML.Element e, Template.Scope s) throws IOException, XML.Exn {
33         final String uri = e.getUri();
34
35         if (uri.equals("http://xt.ibex.org/")) {
36             //#switch(e.getLocalName())
37             case "if":          e = new Template.If(e); break;
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 "transaction": e = new Template.Transaction(e, s); break;
42             //#end
43
44         } else if (uri.startsWith("http://xt.ibex.org/")) {
45             //#switch(uri.substring(19))
46             case "io": System.out.println("ibex.xt.io not yet implemented"); // TODO
47             //#end
48             throw new RuntimeException("Unknown XT library: "+uri);
49
50         } else if (uri.startsWith("local:")) {
51             Template t = parse(s.getLocalPath() + uri.substring(6), s);
52
53             List c = e.getChildren();
54             if (c.size() > 0) {
55                 // move all children from e to placeholder
56                 XML.Element placeholder = findPlaceholder(t);
57                 if (placeholder == null) throw new RuntimeException(
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 original attributes with replacement template
66             e = new JSElement.Merge(t, e);
67
68         } else if (uri.startsWith("java:")) {
69             e = new Java(e);
70         }
71
72         // wrap children
73         List c = e.getChildren();
74         for (int i=0; i < c.size(); i++)
75             if (c.get(i) instanceof XML.Element) wrap((XML.Element)c.get(i), s);
76
77         return e;
78     }
79
80     /** Returns the first Template.Children child found. */
81     private static Template.Children findPlaceholder(XML.Element e) {
82         if (e instanceof Template.Children) return (Template.Children)e;
83         List c = e.getChildren();
84         for (int i=0; i < c.size(); i++) {
85             if (!(c.get(i) instanceof XML.Element)) continue;
86             Template.Children ret = findPlaceholder((XML.Element)c.get(i));
87             if (ret != null) return ret;
88         }
89         return null;
90     }
91
92     private Template.Scope tscope;
93
94     public Template(XML.Element w, Template.Scope t) { super(w); tscope = t; }
95
96     public JSScope getParentScope() { return tscope; }
97
98
99     public static final class If extends JSElement {
100         public If(XML.Element e) { super(e); }
101
102         public void toXML(Writer w) throws IOException {
103             super.toXML(w);
104
105             try {
106                 Object varIf = get("if"); if (varIf != null) undeclare("if");
107                 if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
108             } catch (JSExn e) { throw new RuntimeException(e); }
109
110             List c = getChildren();
111             for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(w);
112         }
113     }
114
115     public static final class JSTag extends JSElement {
116         public JSTag(XML.Element e) {
117             super(e);
118             List c = getChildren();
119             for (int i=0; i < c.size(); i++)
120                 if (c.get(i) instanceof XML.Element) throw new RuntimeException(
121                     "<"+getPrefix()+":js> tags may not have child elements");
122         }
123
124         public void toXML(Writer w) throws IOException {
125             super.toXML(w);
126
127             try {
128                 Object varIf = get("if"); if (varIf != null) undeclare("if");
129                 if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
130
131                 List c = getChildren();
132                 StringWriter s = new StringWriter();
133                 for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(s);
134                 exec(s.toString());
135             } catch (JSExn e) { throw new RuntimeException(e); }
136         }
137     }
138
139     public static final class ForEach extends JSElement {
140         public ForEach(XML.Element e) { super(e); }
141
142         public void toXML(Writer w) throws IOException {
143             super.toXML(w);
144
145             try {
146                 Object varIn = get("in"); if (varIn != null) undeclare("in");
147                 Object varPut = get("put"); if (varPut != null) undeclare("put");
148                 Object varIf = get("if"); if (varIf != null) undeclare("if");
149                 if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
150
151                 varIn = exec("return (" + varIn + ");");
152                 if (varIn == null || (varIn instanceof JSArray)) throw new RuntimeException(
153                     "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
154                     "the name of a valid js array in the current scope, not in='"+varIn+"'.");
155
156                 if (varPut == null) varPut = "x";
157                 else if (!(varPut instanceof String) || get(varPut) != null)
158                     throw new RuntimeException(
159                     "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
160                     "an undeclared variable, not put='"+varPut+"'.");
161                 if (get(varPut) != null) throw new RuntimeException(
162                     "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
163                     "default variable 'x' already exists in the current scope.");
164
165                 List c = getChildren();
166
167                 declare((String)varPut);
168                 Vec v = ((JSArray)varIn).toVec();
169                 for (int j=0; j < v.size(); j++) {
170                     put(varPut, v.elementAt(j));
171                     for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(w);
172                 }
173             } catch (JSExn e) { throw new RuntimeException(e); }
174         }
175     }
176
177     public static final class Children extends JSElement {
178         public Children(XML.Element e) { super(e); }
179     }
180
181     public static final class Transaction extends JSElement {
182         private final Template.Scope scope; // FIXME: HACK. unstatisise all tags, or do this to all
183         public Transaction(XML.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
184
185         public void toXML(Writer w) throws IOException {
186             super.toXML(w);
187
188             // FIXME: what about scope import? <xt:use /> children?
189             List c = getChildren();
190             StringWriter sw = new StringWriter();
191             for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(sw);
192             JS t = JS.fromReader("input", 0, new StringReader(sw.toString()));
193             t = JS.cloneWithNewParentScope(t, new JSScope(null));
194             scope.transaction(t);
195         }
196     }
197
198     public static final class Java extends JSElement {
199         // TODO what exactly?
200         public Java(XML.Element w) { super(w); }
201     }
202
203     public abstract static class Scope extends JSScope {
204         public Scope(JSScope j) { super(j); }
205
206         /** Returns the template path for local:/ namespace. */
207         public abstract String getLocalPath();
208
209         /** Registers a new Prevayler transaction. */
210         public abstract void transaction(JS t);
211     }
212
213 }