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 JSLeaf.Element {
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(); // FIXME: switch to Tree.Stream
22 return new Template(doc.getRoot(), s);
25 public static Tree.Leaf wrap(Tree.Leaf leaf, Template.Scope s) throws IOException {
26 if (!(leaf instanceof Tree.Element)) return new Text(leaf);
28 Tree.Element e = (Tree.Element)leaf;
29 final String uri = e.getUri();
33 } else if (uri.equals("http://xt.ibex.org/")) {
34 //#switch(e.getLocalName())
35 case "js": e = new Template.JSTag(e); break;
36 case "foreach": e = new Template.ForEach(e); break;
37 case "children": e = new Template.Children(e); break;
38 case "redirect": e = new Template.Redirect(e); break;
39 case "transaction": e = new Template.Transaction(e, s); break;
42 } else if (uri.startsWith("http://xt.ibex.org/")) {
43 //#switch(uri.substring(19))
44 case "io": System.out.println("ibex.xt.io not yet implemented"); // TODO
46 //throw new JSLeaf.Exn("Unknown XT library: "+uri);
48 } else if (uri.startsWith("local:")) {
49 // merge a new template into this tree
50 String path = uri.substring(6) + e.getLocalName() + ".xt";
51 Template t = parse(s.getLocalPath() + path, s);
53 List c = e.getChildren();
55 // move all children from e to placeholder
56 Tree.Element placeholder = findPlaceholder(t);
57 if (placeholder == null) throw new JSLeaf.Exn(
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 Tree.Element merged = new JSLeaf.Merge(t, e);
67 // remap the parent of the original element
68 if (e.getParent() != null) {
69 List ch = e.getParent().getChildren();
70 ch.set(ch.indexOf(e), merged);
73 return wrap(merged, s);
76 e = new Template.AttributeEval(e);
79 List c = e.getChildren();
80 for (int i=0; i < c.size(); i++) wrap((Tree.Leaf)c.get(i), s);
85 /** Returns the first Template.Children child found. */
86 private static Tree.Element findPlaceholder(Tree.Element e) {
87 if ("http://xt.ibex.org/".equals(e.getUri()) && "children".equals(e.getLocalName()))
90 List c = e.getChildren();
91 for (int i=0; i < c.size(); i++) {
92 if (!(c.get(i) instanceof Tree.Element)) continue;
93 Tree.Element ret = findPlaceholder((Tree.Element)c.get(i));
94 if (ret != null) return ret;
99 private transient Template.Scope tscope;
100 private transient JSScope scope = null;
102 public Template(Tree.Element w, Template.Scope t) { super(w); tscope = t; }
104 public JSScope scope() { return scope == null ? scope = new JSScope(tscope) : scope; }
107 /** Processes ${...} blocks in attributes, loads applicable
108 * attributes into the JS scope and processes global attributes. */
109 public static class AttributeEval extends JSLeaf.Element implements Tree.Attributes {
110 protected Tree.Attributes a;
112 public AttributeEval(Tree.Element wrapped) { super(wrapped); a = wrapped.getAttributes(); }
114 public Tree.Attributes getAttributes() { return this; }
116 public int getIndex(String q) { return a.getIndex(q); }
117 public int getIndex(String u, String k) { return a.getIndex(u, k); }
118 public String getKey(int i) { return a.getKey(i); }
119 public String getVal(int i) { return (String)eval(a.getVal(i)); }
120 public String getUri(int i) { return a.getUri(i); }
121 public String getPrefix(int i) { return a.getPrefix(i); }
122 public String getQName(int i) { return a.getQName(i); }
123 public int attrSize() { return a.attrSize(); }
125 public void out(Writer w) throws IOException {
127 // FIXME: questionable abuse of namespaces here
128 boolean xturi = "http://xt.ibex.org/".equals(getUri());
129 for(int i=0; i < a.attrSize(); i++) {
130 if (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue;
132 //#switch (a.getKey(i))
133 case "if": if (!"true".equals(eval(a.getVal(i)))) return;
135 Object d = eval(a.getVal(i));
136 if (!(d instanceof String)) throw new JSLeaf.Exn(
137 "attribute "+getPrefix()+":declare can only contain a "+
138 "space seperated list of variable names to declare.");
139 StringTokenizer st = new StringTokenizer((String)d, " ");
140 while (st.hasMoreTokens()) scope().declare(st.nextToken());
144 scope().declare(a.getKey(i));
145 scope().put(a.getKey(i), eval(a.getVal(i)));
147 } catch (JSExn e) { throw new Exn(e); }
153 public static final class JSTag extends JSLeaf.Element {
154 public JSTag(Tree.Element e) {
156 List c = getChildren();
157 for (int i=0; i < c.size(); i++)
158 if (c.get(i) instanceof Tree.Element) throw new JSLeaf.Exn(
159 "<"+getPrefix()+":js> tags may not have child elements");
162 public void out(Writer w) throws IOException {
163 List c = getChildren();
164 StringWriter s = new StringWriter();
165 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s);
170 public static final class ForEach extends JSLeaf.Element {
171 public ForEach(Tree.Element e) { super(e); }
173 public void out(Writer w) throws IOException {
176 Object varIn = s.get("in"); if (varIn != null) s.undeclare("in");
177 Object varPut = s.get("put"); if (varPut != null) s.undeclare("put");
179 varIn = exec("return (" + varIn + ");");
180 if (varIn == null || (varIn instanceof JSArray)) throw new JSLeaf.Exn(
181 "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
182 "the name of a valid js array in the current scope, not in='"+varIn+"'.");
184 if (varPut == null) varPut = "x";
185 else if (!(varPut instanceof String) || s.get(varPut) != null)
186 throw new JSLeaf.Exn(
187 "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
188 "an undeclared variable, not put='"+varPut+"'.");
189 if (scope().get(varPut) != null) throw new JSLeaf.Exn(
190 "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
191 "default variable 'x' already exists in the current scope.");
193 List c = getChildren();
195 s.declare((String)varPut);
196 Iterator it = ((JSArray)varIn).toList().iterator(); while (it.hasNext()) {
197 s.put(varPut, it.next());
198 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w);
200 } catch (JSExn e) { throw new JSLeaf.Exn(e); }
204 public static final class Children extends JSLeaf.Element {
205 public Children(Tree.Element e) { super(e); }
208 public static final class Redirect extends JSLeaf.Element {
209 public Redirect(Tree.Element e) { super(e); }
211 public void out(Writer w) throws IOException {
213 Object p = scope().get("page"); if (p != null) scope().undeclare("page");
214 if (p == null || !(p instanceof String) || ((String)p).trim().equals(""))
215 throw new JSLeaf.Exn("<"+getPrefix()+":redirect> requires 'page' "+
216 "attribute to be a valid template path");
217 throw new RedirectSignal((String)p);
218 } catch (JSExn e) { throw new JSLeaf.Exn(e); }
223 public static final class Transaction extends JSLeaf.Element {
224 private final Template.Scope scope; // FIXME: HACK. unstatisise all tags, or do this to all
225 public Transaction(Tree.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
227 public void out(Writer w) throws IOException {
229 List c = getChildren();
230 StringWriter sw = new StringWriter();
231 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(sw);
232 JS t = JS.fromReader("input", 0, new StringReader(sw.toString()));
233 t = JS.cloneWithNewParentScope(t, new JSScope(null));
234 scope.transaction(t);
238 public static final class Text extends JSLeaf {
239 public Text(Tree.Leaf w) { super(w); }
240 public void out(Writer w) throws IOException {
241 // FIXME: make eval() take a writer
242 StringWriter sw = new StringWriter();
244 w.write((String)eval(sw.toString()));
248 public abstract static class Scope extends JSScope {
249 public Scope(JSScope j) { super(j); }
251 /** Returns the template path for local:/ namespace. */
252 public abstract String getLocalPath();
254 /** Registers a new Prevayler transaction. */
255 public abstract void transaction(JS t);
258 public static class Signal extends RuntimeException {}
259 public static class ReturnSignal extends Signal { }
260 public static class RedirectSignal extends Signal {
261 protected String target;
262 public RedirectSignal(String target) { super(); this.target = target; }
263 public String getTarget() { return target; }