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;
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();
22 return new Template(doc.getRoot(), s);
25 public static XML.Element wrap(XML.Element e, Template.Scope s) throws IOException, XML.Exn {
26 final String uri = e.getUri();
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;
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
41 throw new RuntimeException("Unknown XT library: "+uri);
43 } else if (uri.startsWith("local:")) {
44 Template t = parse(s.getLocalPath() + uri.substring(6), s);
46 List c = e.getChildren();
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.");
54 placeholder.getChildren().addAll(e.getChildren());
55 e.getChildren().clear();
58 // merge original attributes with replacement template
59 e = new JSElement.Merge(t, e);
61 } else if (uri.startsWith("java:")) {
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);
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;
85 private Template.Scope tscope;
87 public Template(XML.Element w, Template.Scope t) { super(w); tscope = t; }
89 public JSScope getParentScope() { return tscope; }
92 public static final class If extends JSElement {
93 public If(XML.Element e) { super(e); }
95 public void toXML(Writer w) throws IOException {
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); }
103 List c = getChildren();
104 for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(w);
108 public static final class JSTag extends JSElement {
109 public JSTag(XML.Element 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");
117 public void toXML(Writer w) throws IOException {
121 Object varIf = get("if"); if (varIf != null) undeclare("if");
122 if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
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);
128 } catch (JSExn e) { throw new RuntimeException(e); }
132 public static final class ForEach extends JSElement {
133 public ForEach(XML.Element e) { super(e); }
135 public void toXML(Writer w) throws IOException {
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;
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+"'.");
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.");
158 List c = getChildren();
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);
166 } catch (JSExn e) { throw new RuntimeException(e); }
170 public static final class Children extends JSElement {
171 public Children(XML.Element e) { super(e); }
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
179 public void toXML(Writer w) throws IOException {
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);
192 public static final class Java extends JSElement {
193 // TODO what exactly?
194 public Java(XML.Element w) { super(w); }
197 public abstract static class Scope extends JSScope {
198 public Scope(JSScope j) { super(j); }
200 /** Returns the template path for local:/ namespace. */
201 public abstract String getLocalPath();
203 /** Registers a new Prevayler transaction. */
204 public abstract void transaction(JS t);