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