process if attribute on all elements
authorcrawshaw <crawshaw@ibex.org>
Wed, 24 Nov 2004 20:17:28 +0000 (20:17 +0000)
committercrawshaw <crawshaw@ibex.org>
Wed, 24 Nov 2004 20:17:28 +0000 (20:17 +0000)
darcs-hash:20041124201728-2eb37-60485346a32d67e69a458de3b5b2c81cab6d7933.gz

src/java/org/ibex/xt/JSElement.java
src/java/org/ibex/xt/Template.java

index 06a6f72..1f07af2 100644 (file)
@@ -28,9 +28,11 @@ public class JSElement extends JSScope implements XML.Element {
         for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).setParent(this);
     }
 
-    public void out(OutputStream o) throws IOException { throw new UnsupportedOperationException(); }
-    public void out(Writer w) throws IOException {
-        // grab all related attributes
+    public void out(OutputStream o) throws IOException { wrapped.out(o); }
+    public void out(Writer w) throws IOException { wrapped.out(w); }
+
+    /** Load the attributes into the js scope. */
+    protected void loadAttr() {
         try {
             XML.Attributes a = getAttributes();
             for(int i=0; i < a.attrSize(); i++) {
index cc2febb..c9b8611 100644 (file)
@@ -26,7 +26,6 @@ public class Template extends JSElement {
 
         if (uri.equals("http://xt.ibex.org/")) {
             //#switch(e.getLocalName())
-            case "if":          e = new Template.If(e); break;
             case "js":          e = new Template.JSTag(e); break;
             case "foreach":     e = new Template.ForEach(e); break;
             case "children":    e = new Template.Children(e); break;
@@ -56,9 +55,14 @@ public class Template extends JSElement {
 
             // merge original attributes with replacement template
             e = new JSElement.Merge(t, e);
+        }
 
-        } else if (uri.startsWith("java:")) {
-            e = new Java(e);
+        XML.Attributes a = e.getAttributes();
+        for (int i=0; i < a.attrSize(); i++) {
+            if ("if".equals(a.getKey(i)) &&
+                "http://xt.ibex.org/".equals(a.getUri(i))) {
+                e = new Template.IfWrap(e);
+            }
         }
 
         // wrap children
@@ -87,20 +91,18 @@ public class Template extends JSElement {
 
     public JSScope getParentScope() { return tscope; }
 
-
-    public static final class If extends JSElement {
-        public If(XML.Element e) { super(e); }
+    public static final class IfWrap extends JSElement {
+        public IfWrap(XML.Element e) { super(e); }
 
         public void out(Writer w) throws IOException {
-            super.out(w);
+            loadAttr();
 
             try {
                 Object varIf = get("if"); if (varIf != null) undeclare("if");
                 if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
             } catch (JSExn e) { throw new RuntimeException(e); }
 
-            List c = getChildren();
-            for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(w);
+            wrapped.out(w);
         }
     }
 
@@ -114,17 +116,12 @@ public class Template extends JSElement {
         }
 
         public void out(Writer w) throws IOException {
-            super.out(w);
+            loadAttr();
 
-            try {
-                Object varIf = get("if"); if (varIf != null) undeclare("if");
-                if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
-
-                List c = getChildren();
-                StringWriter s = new StringWriter();
-                for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s);
-                exec(s.toString());
-            } catch (JSExn e) { throw new RuntimeException(e); }
+            List c = getChildren();
+            StringWriter s = new StringWriter();
+            for (int i=0; i < c.size(); i++) ((Tree.Leaf)c.get(i)).out(s);
+            exec(s.toString());
         }
     }
 
@@ -132,13 +129,11 @@ public class Template extends JSElement {
         public ForEach(XML.Element e) { super(e); }
 
         public void out(Writer w) throws IOException {
-            super.out(w);
+            loadAttr();
 
             try {
                 Object varIn = get("in"); if (varIn != null) undeclare("in");
                 Object varPut = get("put"); if (varPut != null) undeclare("put");
-                Object varIf = get("if"); if (varIf != null) undeclare("if");
-                if (varIf != null && !Boolean.getBoolean((String)varIf)) return;
 
                 varIn = exec("return (" + varIn + ");");
                 if (varIn == null || (varIn instanceof JSArray)) throw new RuntimeException(
@@ -175,7 +170,7 @@ public class Template extends JSElement {
         public Transaction(XML.Element e, Template.Scope s) { super(e); scope = s;} // TODO: check kids
 
         public void out(Writer w) throws IOException {
-            super.out(w);
+            loadAttr();
 
             // TODO: <xt:use />
             List c = getChildren();