process if attribute on all elements
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / Template.java
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();