X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fibex%2Fxt%2FTemplate.java;h=7f75bd2d274fbdd772c995a831beb8a775032c51;hb=628fd95c0956c0ef41189f9dcb42677c2daca320;hp=445a6ee66a32674b6b825069db887b75b4bf39f4;hpb=6649e6f6761b25310be908ef2101a1efadc537ff;p=org.ibex.xt-crawshaw.git diff --git a/src/java/org/ibex/xt/Template.java b/src/java/org/ibex/xt/Template.java index 445a6ee..7f75bd2 100644 --- a/src/java/org/ibex/xt/Template.java +++ b/src/java/org/ibex/xt/Template.java @@ -8,13 +8,14 @@ import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.io.IOException; +import java.io.FileNotFoundException; import java.util.*; import org.ibex.util.*; import org.ibex.js.*; public class Template extends JSElement { - public static Template parse(String path, Template.Scope s) throws IOException { + public static Template parse(String path, Template.Scope s) throws FileNotFoundException, IOException { Reader xmlreader = new BufferedReader(new InputStreamReader(new FileInputStream(path))); XML.Document doc = new XML.Document(); doc.parse(xmlreader); @@ -24,11 +25,14 @@ public class Template extends JSElement { public static XML.Element wrap(XML.Element e, Template.Scope s) throws IOException { final String uri = e.getUri(); - if (uri.equals("http://xt.ibex.org/")) { + if (uri == null) { + // do nothing + } else if (uri.equals("http://xt.ibex.org/")) { //#switch(e.getLocalName()) case "js": e = new Template.JSTag(e); break; case "foreach": e = new Template.ForEach(e); break; case "children": e = new Template.Children(e); break; + case "redirect": e = new Template.Redirect(e); break; case "transaction": e = new Template.Transaction(e, s); break; //#end @@ -36,16 +40,18 @@ public class Template extends JSElement { //#switch(uri.substring(19)) case "io": System.out.println("ibex.xt.io not yet implemented"); // TODO //#end - //throw new RuntimeException("Unknown XT library: "+uri); + //throw new JSElement.Exn("Unknown XT library: "+uri); } else if (uri.startsWith("local:")) { - Template t = parse(s.getLocalPath() + uri.substring(6), s); + // merge a new template into this tree + String path = uri.substring(6) + e.getLocalName() + ".xt"; + Template t = parse(s.getLocalPath() + path, s); List c = e.getChildren(); if (c.size() > 0) { // move all children from e to placeholder XML.Element placeholder = findPlaceholder(t); - if (placeholder == null) throw new RuntimeException( + if (placeholder == null) throw new JSElement.Exn( "<"+e.getQName()+"> attempted to include children into a " + "template which does not contain an tag."); @@ -53,20 +59,19 @@ public class Template extends JSElement { e.getChildren().clear(); } - // merge original attributes with replacement template - e = new JSElement.Merge(t, e); - } + XML.Element merged = new JSElement.Merge(t, e); - XML.Attributes a = e.getAttributes(); - for (int i=0; i < a.attrSize(); i++) { - // FIXME: questionable abuse of XML namespaces here - if ("if".equals(a.getKey(i)) && ( - "http://xt.ibex.org/".equals(e.getUri()) || - "http://xt.ibex.org/".equals(a.getUri(i)))) { - e = new Template.IfWrap(e); + // remap the parent of the original element + if (e.getParent() != null) { + List ch = e.getParent().getChildren(); + ch.set(ch.indexOf(e), merged); } + + return wrap(merged, s); } + e = new Template.AttributeEval(e); + // wrap children List c = e.getChildren(); for (int i=0; i < c.size(); i++) @@ -76,12 +81,14 @@ public class Template extends JSElement { } /** Returns the first Template.Children child found. */ - private static Template.Children findPlaceholder(XML.Element e) { - if (e instanceof Template.Children) return (Template.Children)e; + private static XML.Element findPlaceholder(XML.Element e) { + if ("http://xt.ibex.org/".equals(e.getUri()) && "children".equals(e.getLocalName())) + return e; + List c = e.getChildren(); for (int i=0; i < c.size(); i++) { if (!(c.get(i) instanceof XML.Element)) continue; - Template.Children ret = findPlaceholder((XML.Element)c.get(i)); + XML.Element ret = findPlaceholder((XML.Element)c.get(i)); if (ret != null) return ret; } return null; @@ -93,16 +100,48 @@ public class Template extends JSElement { public JSScope getParentScope() { return tscope; } - public static final class IfWrap extends JSElement { - public IfWrap(XML.Element e) { super(e); } - public void out(Writer w) throws IOException { - loadAttr(); + /** Processes ${...} blocks in attributes, loads applicable + * attributes into the JS scope and processes global attributes. */ + public static class AttributeEval extends JSElement implements XML.Attributes { + protected XML.Attributes a; + + public AttributeEval(XML.Element wrapped) { super(wrapped); a = wrapped.getAttributes(); } + public XML.Attributes getAttributes() { return this; } + + public int getIndex(String q) { return a.getIndex(q); } + public int getIndex(String u, String k) { return a.getIndex(u, k); } + public String getKey(int i) { return a.getKey(i); } + public String getVal(int i) { return (String)eval(a.getVal(i)); } + public String getUri(int i) { return a.getUri(i); } + public String getPrefix(int i) { return a.getPrefix(i); } + public String getQName(int i) { return a.getQName(i); } + public int attrSize() { return a.attrSize(); } + + public void out(Writer w) throws IOException { try { - Object varIf = get("if"); if (varIf != null) undeclare("if"); - if (varIf != null && !"true".equals(varIf)) return; - } catch (JSExn e) { throw new RuntimeException(e); } + // FIXME: questionable abuse of XML namespaces here + boolean xturi = "http://xt.ibex.org/".equals(getUri()); + for(int i=0; i < a.attrSize(); i++) { + if (!xturi && !"http://xt.ibex.org/".equals(a.getUri(i))) continue; + + //#switch (a.getKey(i)) + case "if": if (!"true".equals(eval(a.getVal(i)))) return; + case "declare": + Object d = eval(a.getVal(i)); + if (!(d instanceof String)) throw new JSElement.Exn( + "attribute "+getPrefix()+":declare can only contain a "+ + "space seperated list of variable names to declare."); + StringTokenizer st = new StringTokenizer((String)d, " "); + while (st.hasMoreTokens()) declare(st.nextToken()); + continue; + //#end + + declare(a.getKey(i)); + put(a.getKey(i), eval(a.getVal(i))); + } + } catch (JSExn e) { throw new Exn(e); } wrapped.out(w); } @@ -113,13 +152,11 @@ public class Template extends JSElement { super(e); List c = getChildren(); for (int i=0; i < c.size(); i++) - if (c.get(i) instanceof XML.Element) throw new RuntimeException( + if (c.get(i) instanceof XML.Element) throw new JSElement.Exn( "<"+getPrefix()+":js> tags may not have child elements"); } public void out(Writer w) throws IOException { - loadAttr(); - List c = getChildren(); StringWriter s = new StringWriter(); for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s); @@ -131,23 +168,21 @@ public class Template extends JSElement { public ForEach(XML.Element e) { super(e); } public void out(Writer w) throws IOException { - loadAttr(); - try { Object varIn = get("in"); if (varIn != null) undeclare("in"); Object varPut = get("put"); if (varPut != null) undeclare("put"); varIn = exec("return (" + varIn + ");"); - if (varIn == null || (varIn instanceof JSArray)) throw new RuntimeException( + if (varIn == null || (varIn instanceof JSArray)) throw new JSElement.Exn( "<"+getPrefix()+":foreach> requires attribute 'in' to specify " + "the name of a valid js array in the current scope, not in='"+varIn+"'."); if (varPut == null) varPut = "x"; else if (!(varPut instanceof String) || get(varPut) != null) - throw new RuntimeException( + throw new JSElement.Exn( "<"+getPrefix()+":foreach> 'put' attribute requires the name of "+ "an undeclared variable, not put='"+varPut+"'."); - if (get(varPut) != null) throw new RuntimeException( + if (get(varPut) != null) throw new JSElement.Exn( "<"+getPrefix()+":foreach> has no 'put' attribute defined and the "+ "default variable 'x' already exists in the current scope."); @@ -158,7 +193,7 @@ public class Template extends JSElement { put(varPut, it.next()); for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w); } - } catch (JSExn e) { throw new RuntimeException(e); } + } catch (JSExn e) { throw new JSElement.Exn(e); } } } @@ -166,14 +201,26 @@ public class Template extends JSElement { public Children(XML.Element e) { super(e); } } + public static final class Redirect extends JSElement { + public Redirect(XML.Element e) { super(e); } + + public void out(Writer w) throws IOException { + try { + Object p = get("page"); if (p != null) undeclare("page"); + if (p == null || !(p instanceof String) || ((String)p).trim().equals("")) + throw new JSElement.Exn("<"+getPrefix()+":redirect> requires 'page' "+ + "attribute to be a valid template path"); + throw new RedirectSignal((String)p); + } catch (JSExn e) { throw new JSElement.Exn(e); } + } + } + // TODO: finish public static final class Transaction extends JSElement { private final Template.Scope scope; // FIXME: HACK. unstatisise all tags, or do this to all public Transaction(XML.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids public void out(Writer w) throws IOException { - loadAttr(); - // TODO: List c = getChildren(); StringWriter sw = new StringWriter(); @@ -184,11 +231,6 @@ public class Template extends JSElement { } } - public static final class Java extends JSElement { - // TODO what exactly? - public Java(XML.Element w) { super(w); } - } - public abstract static class Scope extends JSScope { public Scope(JSScope j) { super(j); } @@ -199,4 +241,11 @@ public class Template extends JSElement { public abstract void transaction(JS t); } + public static class Signal extends RuntimeException {} + public static class ReturnSignal extends Signal { } + public static class RedirectSignal extends Signal { + protected String target; + public RedirectSignal(String target) { super(); this.target = target; } + public String getTarget() { return target; } + } }