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