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;
11 import java.io.FileNotFoundException;
14 import org.ibex.util.*;
17 public class Template extends JSElement {
18 public static Template parse(String path, Template.Scope s) throws FileNotFoundException, IOException {
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 {
26 final String uri = e.getUri();
28 if (uri.equals("http://xt.ibex.org/")) {
29 //#switch(e.getLocalName())
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 "redirect": e = new Template.Redirect(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 JSElement.Exn("Unknown XT library: "+uri);
43 } else if (uri.startsWith("local:")) {
44 // merge a new template into this tree
45 String path = uri.substring(6) + e.getLocalName() + ".xt";
46 Template t = parse(s.getLocalPath() + path, s);
48 List c = e.getChildren();
50 // move all children from e to placeholder
51 XML.Element placeholder = findPlaceholder(t);
52 if (placeholder == null) throw new JSElement.Exn(
53 "<"+e.getQName()+"> attempted to include children into a " +
54 "template which does not contain an <xt:children /> tag.");
56 placeholder.getChildren().addAll(e.getChildren());
57 e.getChildren().clear();
60 XML.Element merged = new JSElement.Merge(t, e);
62 // remap the parent of the original element
63 if (e.getParent() != null) {
64 List ch = e.getParent().getChildren();
65 ch.set(ch.indexOf(e), merged);
68 return wrap(merged, s);
71 XML.Attributes a = e.getAttributes();
72 for (int i=0; i < a.attrSize(); i++) {
73 // FIXME: questionable abuse of XML namespaces here
74 if ("if".equals(a.getKey(i)) && (
75 "http://xt.ibex.org/".equals(e.getUri()) ||
76 "http://xt.ibex.org/".equals(a.getUri(i)))) {
77 e = new Template.IfWrap(e);
82 List c = e.getChildren();
83 for (int i=0; i < c.size(); i++)
84 if (c.get(i) instanceof XML.Element) wrap((XML.Element)c.get(i), s);
89 /** Returns the first Template.Children child found. */
90 private static XML.Element findPlaceholder(XML.Element e) {
91 if ("http://xt.ibex.org/".equals(e.getUri()) && "children".equals(e.getLocalName()))
94 List c = e.getChildren();
95 for (int i=0; i < c.size(); i++) {
96 if (!(c.get(i) instanceof XML.Element)) continue;
97 XML.Element ret = findPlaceholder((XML.Element)c.get(i));
98 if (ret != null) return ret;
103 private Template.Scope tscope;
105 public Template(XML.Element w, Template.Scope t) { super(w); tscope = t; }
107 public JSScope getParentScope() { return tscope; }
109 public static final class IfWrap extends JSElement {
110 public IfWrap(XML.Element e) { super(e); }
112 public void out(Writer w) throws IOException {
116 Object varIf = get("if"); if (varIf != null) undeclare("if");
117 if (varIf != null && !"true".equals(varIf)) return;
118 } catch (JSExn e) { throw new JSElement.Exn(e); }
124 public static final class JSTag extends JSElement {
125 public JSTag(XML.Element e) {
127 List c = getChildren();
128 for (int i=0; i < c.size(); i++)
129 if (c.get(i) instanceof XML.Element) throw new JSElement.Exn(
130 "<"+getPrefix()+":js> tags may not have child elements");
133 public void out(Writer w) throws IOException {
136 List c = getChildren();
137 StringWriter s = new StringWriter();
138 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s);
143 public static final class ForEach extends JSElement {
144 public ForEach(XML.Element e) { super(e); }
146 public void out(Writer w) throws IOException {
150 Object varIn = get("in"); if (varIn != null) undeclare("in");
151 Object varPut = get("put"); if (varPut != null) undeclare("put");
153 varIn = exec("return (" + varIn + ");");
154 if (varIn == null || (varIn instanceof JSArray)) throw new JSElement.Exn(
155 "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
156 "the name of a valid js array in the current scope, not in='"+varIn+"'.");
158 if (varPut == null) varPut = "x";
159 else if (!(varPut instanceof String) || get(varPut) != null)
160 throw new JSElement.Exn(
161 "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
162 "an undeclared variable, not put='"+varPut+"'.");
163 if (get(varPut) != null) throw new JSElement.Exn(
164 "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
165 "default variable 'x' already exists in the current scope.");
167 List c = getChildren();
169 declare((String)varPut);
170 Iterator it = ((JSArray)varIn).toList().iterator(); while (it.hasNext()) {
171 put(varPut, it.next());
172 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w);
174 } catch (JSExn e) { throw new JSElement.Exn(e); }
178 public static final class Children extends JSElement {
179 public Children(XML.Element e) { super(e); }
182 public static final class Redirect extends JSElement {
183 public Redirect(XML.Element e) { super(e); }
185 public void out(Writer w) throws IOException {
189 Object p = get("page"); if (p != null) undeclare("page");
190 if (p == null || !(p instanceof String) || ((String)p).trim().equals(""))
191 throw new JSElement.Exn("<"+getPrefix()+":redirect> requires 'page' "+
192 "attribute to be a valid template path");
193 throw new RedirectSignal((String)p);
194 } catch (JSExn e) { throw new JSElement.Exn(e); }
199 public static final class Transaction extends JSElement {
200 private final Template.Scope scope; // FIXME: HACK. unstatisise all tags, or do this to all
201 public Transaction(XML.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
203 public void out(Writer w) throws IOException {
207 List c = getChildren();
208 StringWriter sw = new StringWriter();
209 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(sw);
210 JS t = JS.fromReader("input", 0, new StringReader(sw.toString()));
211 t = JS.cloneWithNewParentScope(t, new JSScope(null));
212 scope.transaction(t);
216 public abstract static class Scope extends JSScope {
217 public Scope(JSScope j) { super(j); }
219 /** Returns the template path for local:/ namespace. */
220 public abstract String getLocalPath();
222 /** Registers a new Prevayler transaction. */
223 public abstract void transaction(JS t);
226 public static class Signal extends RuntimeException {}
227 public static class ReturnSignal extends Signal { }
228 public static class RedirectSignal extends Signal {
229 protected String target;
230 public RedirectSignal(String target) { super(); this.target = target; }
231 public String getTarget() { return target; }