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 // FIXME: replace scope().get("") with containsKey() check on attributes,
18 // thereby neatly sidestepping any higher up user defined variables
19 // (especially when combined with undeclare()
20 public class Template extends JSLeaf.Element {
21 public static Template parse(String path, Template.Scope s) throws FileNotFoundException, IOException {
22 Reader xmlreader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
23 XML.Document doc = new XML.Document(); // FIXME: switch to Tree.Stream
25 return new Template(doc.getRoot(), s);
28 public static Tree.Leaf wrap(Tree.Leaf leaf, Template.Scope s) throws IOException {
29 if (!(leaf instanceof Tree.Element)) return new Text(leaf);
31 Tree.Element e = (Tree.Element)leaf;
32 final String uri = e.getUri();
36 } else if (uri.equals("http://xt.ibex.org/")) {
37 //#switch(e.getLocalName())
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 "redirect": e = new Template.Redirect(e); break;
42 case "transaction": e = new Template.Transaction(e, s); break;
45 } else if (uri.startsWith("http://xt.ibex.org/")) {
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 // merge the attributes of the template and its representative
66 t.setAttributes(new JSLeaf.MergeAttr(e.getAttributes(), t.getAttributes()));
68 // remap the parent of the original element
69 if (e.getParent() != null) {
70 List ch = e.getParent().getChildren();
71 ch.set(ch.indexOf(e), t);
77 e = new Template.AttributeEval(e);
80 List c = e.getChildren();
81 for (int i=0; i < c.size(); i++) wrap((Tree.Leaf)c.get(i), s);
86 /** Returns the first Template.Children child found. */
87 private static Tree.Element findPlaceholder(Tree.Element e) {
88 if ("http://xt.ibex.org/".equals(e.getUri()) && "children".equals(e.getLocalName()))
91 List c = e.getChildren();
92 for (int i=0; i < c.size(); i++) {
93 if (!(c.get(i) instanceof Tree.Element)) continue;
94 Tree.Element ret = findPlaceholder((Tree.Element)c.get(i));
95 if (ret != null) return ret;
100 private transient Template.Scope tscope;
101 private transient JSScope scope = null;
103 public Template(Tree.Element w, Template.Scope t) { super(w); tscope = t; }
105 public JSScope scope() { return scope == null ? scope = new JSScope(tscope) : scope; }
108 /** Processes ${...} blocks in attributes, loads applicable
109 * attributes into the JS scope and processes global attributes. */
110 public static class AttributeEval extends JSLeaf.Element implements Tree.Attributes {
111 // TODO: hide global attributes from out() function. waiting on util.XMLHelper
112 protected Tree.Attributes a;
114 public AttributeEval(Tree.Element wrapped) {
116 a = wrapped.getAttributes();
117 wrapped.setAttributes(this);
120 public int getIndex(String q) { return a.getIndex(q); }
121 public int getIndex(String u, String k) { return a.getIndex(u, k); }
122 public String getKey(int i) { return a.getKey(i); }
123 public String getVal(int i) { return (String)eval(a.getVal(i)); }
124 public String getUri(int i) { return a.getUri(i); }
125 public String getPrefix(int i) { return a.getPrefix(i); }
126 public String getQName(int i) { return a.getQName(i); }
127 public int attrSize() { return a.attrSize(); }
129 public void out(Writer w) throws IOException {
131 // FIXME: questionable abuse of namespaces here
132 boolean xturi = "http://xt.ibex.org/".equals(getUri());
133 for(int i=0; i < a.attrSize(); i++) {
134 if (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue;
136 //#switch (a.getKey(i))
137 case "if": if (!"true".equals(eval(a.getVal(i)))) return;
139 Object d = eval(a.getVal(i));
140 if (!(d instanceof String)) throw new JSLeaf.Exn(
141 "attribute '"+getPrefix()+":declare' can only contain a "+
142 "space seperated list of variable names to declare.");
143 StringTokenizer st = new StringTokenizer((String)d, " ");
144 while (st.hasMoreTokens()) scope().declare(st.nextToken());
148 scope().declare(a.getKey(i));
149 scope().put(a.getKey(i), eval(a.getVal(i)));
151 } catch (JSExn e) { throw new Exn(e); }
157 public static final class JSTag extends JSLeaf.Element {
158 public JSTag(Tree.Element e) {
160 List c = getChildren();
161 for (int i=0; i < c.size(); i++)
162 if (c.get(i) instanceof Tree.Element) throw new JSLeaf.Exn(
163 "<"+getPrefix()+":js> tags may not have child elements");
166 public void out(Writer w) throws IOException {
167 List c = getChildren();
168 StringWriter s = new StringWriter();
169 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s);
174 public static final class ForEach extends JSLeaf.Element {
175 public ForEach(Tree.Element e) { super(e); }
177 public void out(Writer w) throws IOException {
180 Object varIn = s.get("in"); if (varIn != null) s.undeclare("in");
181 Object varPut = s.get("put"); if (varPut != null) s.undeclare("put");
183 varIn = exec("return (" + varIn + ");");
184 if (varIn == null || (varIn instanceof JSArray)) throw new JSLeaf.Exn(
185 "<"+getPrefix()+":foreach> requires attribute 'in' to specify " +
186 "the name of a valid js array in the current scope, not in='"+varIn+"'.");
188 if (varPut == null) varPut = "x";
189 else if (!(varPut instanceof String) || s.get(varPut) != null)
190 throw new JSLeaf.Exn(
191 "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+
192 "an undeclared variable, not put='"+varPut+"'.");
193 if (scope().get(varPut) != null) throw new JSLeaf.Exn(
194 "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+
195 "default variable 'x' already exists in the current scope.");
197 List c = getChildren();
199 s.declare((String)varPut);
200 Iterator it = ((JSArray)varIn).toList().iterator(); while (it.hasNext()) {
201 s.put(varPut, it.next());
202 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w);
204 } catch (JSExn e) { throw new JSLeaf.Exn(e); }
208 public static final class Children extends JSLeaf.Element {
209 public Children(Tree.Element e) { super(e); }
212 public static final class Redirect extends JSLeaf.Element {
213 public Redirect(Tree.Element e) { super(e); }
215 public void out(Writer w) throws IOException {
217 Object p = scope().get("page"); if (p != null) scope().undeclare("page");
218 if (p == null || !(p instanceof String) || ((String)p).trim().equals(""))
219 throw new JSLeaf.Exn("<"+getPrefix()+":redirect> requires 'page' "+
220 "attribute to be a valid template path");
221 throw new RedirectSignal((String)p);
222 } catch (JSExn e) { throw new JSLeaf.Exn(e); }
227 public static final class Transaction extends JSLeaf.Element {
228 private Template.Scope scope;
229 public Transaction(Tree.Element e, Template.Scope s) { super(e); scope = s; }
231 public void out(Writer w) throws IOException {
232 StringWriter sw = new StringWriter();
233 List c = getChildren();
234 for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(sw);
235 StringReader sr = new StringReader(sw.toString());
238 try { t = JS.cloneWithNewParentScope(
239 JS.fromReader("input", 0, sr), new JSScope(null)); }
240 catch (IOException e) { throw new JSLeaf.Exn(e.getMessage()); }
243 JSScope scope = JS.getParentScope(t);
245 Object u = scope().get("use"); if (u != null) scope().undeclare("use");
247 if (!(u instanceof String)) throw new JSLeaf.Exn(
248 "<"+getPrefix()+":transaction> requires 'use' attribute "+
249 "to be a valid space-seperated list of variables");
250 StringTokenizer st = new StringTokenizer((String)u);
251 while (st.hasMoreTokens()) {
252 String k = st.nextToken();
253 scope.put(k, scope().get(k));
256 } catch (JSExn e) { throw new JSLeaf.Exn(e); }
258 scope.transaction(t);
262 public static final class Text extends JSLeaf {
263 public Text(Tree.Leaf w) { super(w); }
264 public void out(Writer w) throws IOException {
265 StringWriter sw = new StringWriter();
267 w.write((String)eval(sw.toString()));
271 public abstract static class Scope extends JSScope {
272 public Scope(JSScope j) { super(j); }
274 /** Returns the template path for local:/ namespace. */
275 public abstract String getLocalPath();
277 /** Registers a new Prevayler transaction. */
278 public abstract void transaction(JS t);
281 public static class Signal extends RuntimeException {}
282 public static class ReturnSignal extends Signal { }
283 public static class RedirectSignal extends Signal {
284 protected String target;
285 public RedirectSignal(String target) { super(); this.target = target; }
286 public String getTarget() { return target; }