3 import java.io.BufferedReader;
4 import java.io.FileInputStream;
5 import java.io.InputStreamReader;
7 import java.io.StringReader;
8 import java.io.StringWriter;
10 import java.io.IOException;
13 import org.ibex.util.*;
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();
21 return new Template(doc.getRoot(), s);
24 public static XML.Element wrap(XML.Element e, Template.Scope s) throws IOException {
25 final String uri = e.getUri();
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;
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
39 //throw new RuntimeException("Unknown XT library: "+uri);
41 } else if (uri.startsWith("local:")) {
42 Template t = parse(s.getLocalPath() + uri.substring(6), s);
44 List c = e.getChildren();
46 // move all children from e to placeholder
47 XML.Element placeholder = findPlaceholder(t);
48 if (placeholder == null) throw new RuntimeException(
49 "<"+e.getQName()+"> attempted to include children into a " +
50 "template which does not contain an <xt:children /> tag.");
52 placeholder.getChildren().addAll(e.getChildren());
53 e.getChildren().clear();
56 // merge original attributes with replacement template
57 e = new JSElement.Merge(t, e);
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);
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);
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;
90 private Template.Scope tscope;
92 public Template(XML.Element w, Template.Scope t) { super(w); tscope = t; }
94 public JSScope getParentScope() { return tscope; }
96 public static final class IfWrap extends JSElement {
97 public IfWrap(XML.Element e) { super(e); }
99 public void out(Writer w) throws IOException {
103 Object varIf = get("if"); if (varIf != null) undeclare("if");
104 if (varIf != null && !"true".equals(varIf)) return;
105 } catch (JSExn e) { throw new RuntimeException(e); }
111 public static final class JSTag extends JSElement {
112 public JSTag(XML.Element e) {
114 List c = getChildren();
115 for (int i=0; i < c.size(); i++)
116 if (c.get(i) instanceof XML.Element) throw new RuntimeException(
117 "<"+getPrefix()+":js> tags may not have child elements");
120 public void out(Writer w) throws IOException {
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);
130 public static final class ForEach extends JSElement {
131 public ForEach(XML.Element e) { super(e); }
133 public void out(Writer w) throws IOException {
137 Object varIn = get("in"); if (varIn != null) undeclare("in");
138 Object varPut = get("put"); if (varPut != null) undeclare("put");
140 varIn = exec("return (" + varIn + ");");
141 if (varIn == null || (varIn instanceof JSArray)) throw new RuntimeException(
142 "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
143 "the name of a valid js array in the current scope, not in='"+varIn+"'.");
145 if (varPut == null) varPut = "x";
146 else if (!(varPut instanceof String) || get(varPut) != null)
147 throw new RuntimeException(
148 "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
149 "an undeclared variable, not put='"+varPut+"'.");
150 if (get(varPut) != null) throw new RuntimeException(
151 "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
152 "default variable 'x' already exists in the current scope.");
154 List c = getChildren();
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);
161 } catch (JSExn e) { throw new RuntimeException(e); }
165 public static final class Children extends JSElement {
166 public Children(XML.Element e) { super(e); }
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
174 public void out(Writer w) throws IOException {
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);
187 public static final class Java extends JSElement {
188 // TODO what exactly?
189 public Java(XML.Element w) { super(w); }
192 public abstract static class Scope extends JSScope {
193 public Scope(JSScope j) { super(j); }
195 /** Returns the template path for local:/ namespace. */
196 public abstract String getLocalPath();
198 /** Registers a new Prevayler transaction. */
199 public abstract void transaction(JS t);