4 import ibex.util.Vec; //FIXME remove
7 import org.ibex.js.JSScope;
8 import org.ibex.js.JSArray;
9 import org.ibex.js.JSExn;
11 import ibex.collection.*;
14 import java.io.BufferedReader;
15 import java.io.FileInputStream;
16 import java.io.InputStreamReader;
17 import java.io.Reader;
18 import java.io.StringReader;
19 import java.io.StringWriter;
20 import java.io.Writer;
21 import java.io.IOException;
24 public class Template extends JSElement {
25 public static Template parse(String path, Template.Scope s) throws IOException, XML.Exn {
26 Reader xmlreader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
27 XML.Document doc = new XML.Document();
29 return new Template(doc.getRoot(), s);
32 public static XML.Element wrap(XML.Element e, Template.Scope s) throws IOException, XML.Exn {
33 final String uri = e.getUri();
35 if (uri.equals("http://xt.ibex.org/")) {
36 //#switch(e.getLocalName())
37 case "if": e = new Template.If(e); break;
38 case "js": e = new Template.JSTag(e); break;
39 case "foreach": e = new Template.ForEach(e); break;
40 case "children": e = new Template.Children(e); break;
41 case "transaction": e = new Template.Transaction(e, s); break;
44 } else if (uri.startsWith("http://xt.ibex.org/")) {
45 //#switch(uri.substring(19))
46 case "io": System.out.println("ibex.xt.io not yet implemented"); // TODO
48 throw new RuntimeException("Unknown XT library: "+uri);
50 } else if (uri.startsWith("local:")) {
51 Template t = parse(s.getLocalPath() + uri.substring(6), s);
53 List c = e.getChildren();
55 // move all children from e to placeholder
56 XML.Element placeholder = findPlaceholder(t);
57 if (placeholder == null) throw new RuntimeException(
58 "<"+e.getQName()+"> attempted to include children into a " +
59 "template which does not contain an <xt:children /> tag.");
61 placeholder.getChildren().addAll(e.getChildren());
62 e.getChildren().clear();
65 // merge original attributes with replacement template
66 e = new JSElement.Merge(t, e);
68 } else if (uri.startsWith("java:")) {
73 List c = e.getChildren();
74 for (int i=0; i < c.size(); i++)
75 if (c.get(i) instanceof XML.Element) wrap((XML.Element)c.get(i), s);
80 /** Returns the first Template.Children child found. */
81 private static Template.Children findPlaceholder(XML.Element e) {
82 if (e instanceof Template.Children) return (Template.Children)e;
83 List c = e.getChildren();
84 for (int i=0; i < c.size(); i++) {
85 if (!(c.get(i) instanceof XML.Element)) continue;
86 Template.Children ret = findPlaceholder((XML.Element)c.get(i));
87 if (ret != null) return ret;
92 private Template.Scope tscope;
94 public Template(XML.Element w, Template.Scope t) { super(w); tscope = t; }
96 public JSScope getParentScope() { return tscope; }
99 public static final class If extends JSElement {
100 public If(XML.Element e) { super(e); }
102 public void toXML(Writer w) throws IOException {
106 Object varIf = get("if"); if (varIf != null) undeclare("if");
107 if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
108 } catch (JSExn e) { throw new RuntimeException(e); }
110 List c = getChildren();
111 for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(w);
115 public static final class JSTag extends JSElement {
116 public JSTag(XML.Element e) {
118 List c = getChildren();
119 for (int i=0; i < c.size(); i++)
120 if (c.get(i) instanceof XML.Element) throw new RuntimeException(
121 "<"+getPrefix()+":js> tags may not have child elements");
124 public void toXML(Writer w) throws IOException {
128 Object varIf = get("if"); if (varIf != null) undeclare("if");
129 if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
131 List c = getChildren();
132 StringWriter s = new StringWriter();
133 for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(s);
135 } catch (JSExn e) { throw new RuntimeException(e); }
139 public static final class ForEach extends JSElement {
140 public ForEach(XML.Element e) { super(e); }
142 public void toXML(Writer w) throws IOException {
146 Object varIn = get("in"); if (varIn != null) undeclare("in");
147 Object varPut = get("put"); if (varPut != null) undeclare("put");
148 Object varIf = get("if"); if (varIf != null) undeclare("if");
149 if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
151 varIn = exec("return (" + varIn + ");");
152 if (varIn == null || (varIn instanceof JSArray)) throw new RuntimeException(
153 "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
154 "the name of a valid js array in the current scope, not in='"+varIn+"'.");
156 if (varPut == null) varPut = "x";
157 else if (!(varPut instanceof String) || get(varPut) != null)
158 throw new RuntimeException(
159 "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
160 "an undeclared variable, not put='"+varPut+"'.");
161 if (get(varPut) != null) throw new RuntimeException(
162 "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
163 "default variable 'x' already exists in the current scope.");
165 List c = getChildren();
167 declare((String)varPut);
168 Vec v = ((JSArray)varIn).toVec();
169 for (int j=0; j < v.size(); j++) {
170 put(varPut, v.elementAt(j));
171 for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(w);
173 } catch (JSExn e) { throw new RuntimeException(e); }
177 public static final class Children extends JSElement {
178 public Children(XML.Element e) { super(e); }
181 public static final class Transaction extends JSElement {
182 private final Template.Scope scope; // FIXME: HACK. unstatisise all tags, or do this to all
183 public Transaction(XML.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
185 public void toXML(Writer w) throws IOException {
188 // FIXME: what about scope import? <xt:use /> children?
189 List c = getChildren();
190 StringWriter sw = new StringWriter();
191 for (int i=0; i < c.size(); i++) ((XML.Block)c.get(i)).toXML(sw);
192 JS t = JS.fromReader("input", 0, new StringReader(sw.toString()));
193 t = JS.cloneWithNewParentScope(t, new JSScope(null));
194 scope.transaction(t);
198 public static final class Java extends JSElement {
199 // TODO what exactly?
200 public Java(XML.Element w) { super(w); }
203 public abstract static class Scope extends JSScope {
204 public Scope(JSScope j) { super(j); }
206 /** Returns the template path for local:/ namespace. */
207 public abstract String getLocalPath();
209 /** Registers a new Prevayler transaction. */
210 public abstract void transaction(JS t);