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();
30 } else if (uri.equals("http://xt.ibex.org/")) {
31 //#switch(e.getLocalName())
32 case "js": e = new Template.JSTag(e); break;
33 case "foreach": e = new Template.ForEach(e); break;
34 case "children": e = new Template.Children(e); break;
35 case "redirect": e = new Template.Redirect(e); break;
36 case "transaction": e = new Template.Transaction(e, s); break;
39 } else if (uri.startsWith("http://xt.ibex.org/")) {
40 //#switch(uri.substring(19))
41 case "io": System.out.println("ibex.xt.io not yet implemented"); // TODO
43 //throw new JSElement.Exn("Unknown XT library: "+uri);
45 } else if (uri.startsWith("local:")) {
46 // merge a new template into this tree
47 String path = uri.substring(6) + e.getLocalName() + ".xt";
48 Template t = parse(s.getLocalPath() + path, s);
50 List c = e.getChildren();
52 // move all children from e to placeholder
53 XML.Element placeholder = findPlaceholder(t);
54 if (placeholder == null) throw new JSElement.Exn(
55 "<"+e.getQName()+"> attempted to include children into a " +
56 "template which does not contain an <xt:children /> tag.");
58 placeholder.getChildren().addAll(e.getChildren());
59 e.getChildren().clear();
62 XML.Element merged = new JSElement.Merge(t, e);
64 // remap the parent of the original element
65 if (e.getParent() != null) {
66 List ch = e.getParent().getChildren();
67 ch.set(ch.indexOf(e), merged);
70 return wrap(merged, s);
73 e = new Template.AttributeEval(e);
76 List c = e.getChildren();
77 for (int i=0; i < c.size(); i++)
78 if (c.get(i) instanceof XML.Element) wrap((XML.Element)c.get(i), s);
83 /** Returns the first Template.Children child found. */
84 private static XML.Element findPlaceholder(XML.Element e) {
85 if ("http://xt.ibex.org/".equals(e.getUri()) && "children".equals(e.getLocalName()))
88 List c = e.getChildren();
89 for (int i=0; i < c.size(); i++) {
90 if (!(c.get(i) instanceof XML.Element)) continue;
91 XML.Element ret = findPlaceholder((XML.Element)c.get(i));
92 if (ret != null) return ret;
97 private Template.Scope tscope;
99 public Template(XML.Element w, Template.Scope t) { super(w); tscope = t; }
101 public JSScope getParentScope() { return tscope; }
104 /** Processes ${...} blocks in attributes, loads applicable
105 * attributes into the JS scope and processes global attributes. */
106 public static class AttributeEval extends JSElement implements XML.Attributes {
107 protected XML.Attributes a;
109 public AttributeEval(XML.Element wrapped) { super(wrapped); a = wrapped.getAttributes(); }
111 public XML.Attributes getAttributes() { return this; }
113 public int getIndex(String q) { return a.getIndex(q); }
114 public int getIndex(String u, String k) { return a.getIndex(u, k); }
115 public String getKey(int i) { return a.getKey(i); }
116 public String getVal(int i) { return (String)eval(a.getVal(i)); }
117 public String getUri(int i) { return a.getUri(i); }
118 public String getPrefix(int i) { return a.getPrefix(i); }
119 public String getQName(int i) { return a.getQName(i); }
120 public int attrSize() { return a.attrSize(); }
122 public void out(Writer w) throws IOException {
124 // FIXME: questionable abuse of XML namespaces here
125 boolean xturi = "http://xt.ibex.org/".equals(getUri());
126 for(int i=0; i < a.attrSize(); i++) {
127 if (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue;
129 //#switch (a.getKey(i))
130 case "if": if (!"true".equals(eval(a.getVal(i)))) return;
132 Object d = eval(a.getVal(i));
133 if (!(d instanceof String)) throw new JSElement.Exn(
134 "attribute "+getPrefix()+":declare can only contain a "+
135 "space seperated list of variable names to declare.");
136 StringTokenizer st = new StringTokenizer((String)d, " ");
137 while (st.hasMoreTokens()) declare(st.nextToken());
141 declare(a.getKey(i));
142 put(a.getKey(i), eval(a.getVal(i)));
144 } catch (JSExn e) { throw new Exn(e); }
150 public static final class JSTag extends JSElement {
151 public JSTag(XML.Element e) {
153 List c = getChildren();
154 for (int i=0; i < c.size(); i++)
155 if (c.get(i) instanceof XML.Element) throw new JSElement.Exn(
156 "<"+getPrefix()+":js> tags may not have child elements");
159 public void out(Writer w) throws IOException {
160 List c = getChildren();
161 StringWriter s = new StringWriter();
162 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s);
167 public static final class ForEach extends JSElement {
168 public ForEach(XML.Element e) { super(e); }
170 public void out(Writer w) throws IOException {
172 Object varIn = get("in"); if (varIn != null) undeclare("in");
173 Object varPut = get("put"); if (varPut != null) undeclare("put");
175 varIn = exec("return (" + varIn + ");");
176 if (varIn == null || (varIn instanceof JSArray)) throw new JSElement.Exn(
177 "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
178 "the name of a valid js array in the current scope, not in='"+varIn+"'.");
180 if (varPut == null) varPut = "x";
181 else if (!(varPut instanceof String) || get(varPut) != null)
182 throw new JSElement.Exn(
183 "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
184 "an undeclared variable, not put='"+varPut+"'.");
185 if (get(varPut) != null) throw new JSElement.Exn(
186 "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
187 "default variable 'x' already exists in the current scope.");
189 List c = getChildren();
191 declare((String)varPut);
192 Iterator it = ((JSArray)varIn).toList().iterator(); while (it.hasNext()) {
193 put(varPut, it.next());
194 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w);
196 } catch (JSExn e) { throw new JSElement.Exn(e); }
200 public static final class Children extends JSElement {
201 public Children(XML.Element e) { super(e); }
204 public static final class Redirect extends JSElement {
205 public Redirect(XML.Element e) { super(e); }
207 public void out(Writer w) throws IOException {
209 Object p = get("page"); if (p != null) undeclare("page");
210 if (p == null || !(p instanceof String) || ((String)p).trim().equals(""))
211 throw new JSElement.Exn("<"+getPrefix()+":redirect> requires 'page' "+
212 "attribute to be a valid template path");
213 throw new RedirectSignal((String)p);
214 } catch (JSExn e) { throw new JSElement.Exn(e); }
219 public static final class Transaction extends JSElement {
220 private final Template.Scope scope; // FIXME: HACK. unstatisise all tags, or do this to all
221 public Transaction(XML.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
223 public void out(Writer w) throws IOException {
225 List c = getChildren();
226 StringWriter sw = new StringWriter();
227 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(sw);
228 JS t = JS.fromReader("input", 0, new StringReader(sw.toString()));
229 t = JS.cloneWithNewParentScope(t, new JSScope(null));
230 scope.transaction(t);
234 public abstract static class Scope extends JSScope {
235 public Scope(JSScope j) { super(j); }
237 /** Returns the template path for local:/ namespace. */
238 public abstract String getLocalPath();
240 /** Registers a new Prevayler transaction. */
241 public abstract void transaction(JS t);
244 public static class Signal extends RuntimeException {}
245 public static class ReturnSignal extends Signal { }
246 public static class RedirectSignal extends Signal {
247 protected String target;
248 public RedirectSignal(String target) { super(); this.target = target; }
249 public String getTarget() { return target; }