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 "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;
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
40 //throw new RuntimeException("Unknown XT library: "+uri);
42 } else if (uri.startsWith("local:")) {
43 Template t = parse(s.getLocalPath() + uri.substring(6), s);
45 List c = e.getChildren();
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.");
53 placeholder.getChildren().addAll(e.getChildren());
54 e.getChildren().clear();
57 // merge original attributes with replacement template
58 e = new JSElement.Merge(t, e);
60 } else if (uri.startsWith("java:")) {
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);
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;
84 private Template.Scope tscope;
86 public Template(XML.Element w, Template.Scope t) { super(w); tscope = t; }
88 public JSScope getParentScope() { return tscope; }
91 public static final class If extends JSElement {
92 public If(XML.Element e) { super(e); }
94 public void out(Writer w) throws IOException {
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); }
102 List c = getChildren();
103 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w);
107 public static final class JSTag extends JSElement {
108 public JSTag(XML.Element 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");
116 public void out(Writer w) throws IOException {
120 Object varIf = get("if"); if (varIf != null) undeclare("if");
121 if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
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);
127 } catch (JSExn e) { throw new RuntimeException(e); }
131 public static final class ForEach extends JSElement {
132 public ForEach(XML.Element e) { super(e); }
134 public void out(Writer w) throws IOException {
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;
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+"'.");
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.");
157 List c = getChildren();
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++) ((Tree.Leaf)c.get(i)).out(w);
164 } catch (JSExn e) { throw new RuntimeException(e); }
168 public static final class Children extends JSElement {
169 public Children(XML.Element e) { super(e); }
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
177 public void out(Writer w) throws IOException {
181 List c = getChildren();
182 StringWriter sw = new StringWriter();
183 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(sw);
184 JS t = JS.fromReader("input", 0, new StringReader(sw.toString()));
185 t = JS.cloneWithNewParentScope(t, new JSScope(null));
186 scope.transaction(t);
190 public static final class Java extends JSElement {
191 // TODO what exactly?
192 public Java(XML.Element w) { super(w); }
195 public abstract static class Scope extends JSScope {
196 public Scope(JSScope j) { super(j); }
198 /** Returns the template path for local:/ namespace. */
199 public abstract String getLocalPath();
201 /** Registers a new Prevayler transaction. */
202 public abstract void transaction(JS t);