add redirect tag support
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / Template.java
index 445a6ee..ac29e8f 100644 (file)
@@ -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);
@@ -29,6 +30,7 @@ public class Template extends JSElement {
             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,7 +38,7 @@ 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);
@@ -45,7 +47,7 @@ public class Template extends JSElement {
             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 <xt:children /> tag.");
 
@@ -102,7 +104,7 @@ public class Template extends JSElement {
             try {
                 Object varIf = get("if"); if (varIf != null) undeclare("if");
                 if (varIf != null && !"true".equals(varIf)) return;
-            } catch (JSExn e) { throw new RuntimeException(e); }
+            } catch (JSExn e) { throw new JSElement.Exn(e); }
 
             wrapped.out(w);
         }
@@ -113,7 +115,7 @@ 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");
         }
 
@@ -138,16 +140,16 @@ public class Template extends JSElement {
                 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 +160,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,6 +168,22 @@ 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 {
+            loadAttr();
+
+            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
@@ -184,11 +202,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 +212,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; }
+    }
 }