From 0ce341ef6d77e3b1cd3034d65f6451871be31a4e Mon Sep 17 00:00:00 2001 From: crawshaw Date: Wed, 24 Nov 2004 20:17:28 +0000 Subject: [PATCH] process if attribute on all elements darcs-hash:20041124201728-2eb37-60485346a32d67e69a458de3b5b2c81cab6d7933.gz --- src/java/org/ibex/xt/JSElement.java | 8 ++++--- src/java/org/ibex/xt/Template.java | 41 +++++++++++++++-------------------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/java/org/ibex/xt/JSElement.java b/src/java/org/ibex/xt/JSElement.java index 06a6f72..1f07af2 100644 --- a/src/java/org/ibex/xt/JSElement.java +++ b/src/java/org/ibex/xt/JSElement.java @@ -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++) { diff --git a/src/java/org/ibex/xt/Template.java b/src/java/org/ibex/xt/Template.java index cc2febb..c9b8611 100644 --- a/src/java/org/ibex/xt/Template.java +++ b/src/java/org/ibex/xt/Template.java @@ -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: List c = getChildren(); -- 1.7.10.4