changeset 4890:bf38ea4cb0f7

Added bodies to macros. Use the bodies of <dc:call-macro><body></dc:call-macro> as <dc:macro-body/> in tthe macro. Example: <dc:macro name="greet"><hello><dc:macro-body/></hello></dc:macro> <dc:call-macro name="greet"><planet>Earth</planet></dc:call-macro> Result: <hello><panet>Earth</planet></hello>
author Sascha L. Teichmann <teichmann@intevation.de>
date Mon, 28 Jan 2013 18:55:55 +0100
parents 8118f9b0ee7b
children 089bc08f720e
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
diffstat 1 files changed, 36 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Mon Jan 28 16:33:05 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Mon Jan 28 18:55:55 2013 +0100
@@ -92,6 +92,7 @@
         protected List<NamedConnection>                    connections;
         protected Map<String, CompiledStatement.Instance>  statements;
         protected Deque<Pair<NamedConnection, ResultData>> connectionsStack;
+        protected Deque<NodeList>                          macroBodies;
 
         public BuildHelper(
             Node                  output,
@@ -110,6 +111,7 @@
             owner            = getOwnerDocument(output);
             statements =
                 new HashMap<String, CompiledStatement.Instance>();
+            macroBodies = new ArrayDeque<NodeList>();
         }
 
         public void build() throws SQLException {
@@ -323,18 +325,45 @@
             NodeList macros = template.getElementsByTagNameNS(
                 DC_NAMESPACE_URI, "macro");
 
+            Element macro = null;
+
             for (int i = 0, N = macros.getLength(); i < N; ++i) {
-                Element macro = (Element) macros.item(i);
-                if (name.equals(macro.getAttribute("name"))) {
+                Element m = (Element) macros.item(i);
+                if (name.equals(m.getAttribute("name"))) {
+                    macro = m;
+                    break;
+                }
+            }
+
+            if (macro != null) {
+                macroBodies.push(current.getChildNodes());
+                try {
                     NodeList subs = macro.getChildNodes();
                     for (int j = 0, M = subs.getLength(); j < M; ++j) {
                         build(parent, subs.item(j));
                     }
-                    return;
+                }
+                finally {
+                    macroBodies.pop();
                 }
             }
+            else {
+                log.warn("no macro '" + name + "' found.");
+            }
+        }
 
-            log.warn("no macro '" + name + "' found.");
+        protected void macroBody(Node parent, Element current)
+        throws SQLException
+        {
+            if (!macroBodies.isEmpty()) {
+                NodeList children = macroBodies.peek();
+                for (int i = 0, N = children.getLength(); i < N; ++i) {
+                    build(parent, children.item(i));
+                }
+            }
+            else {
+                log.warn("no current macro");
+            }
         }
 
         protected void ifClause(Node parent, Element current)
@@ -506,6 +535,9 @@
                     else if ("call-macro".equals(localName)) {
                         callMacro(parent, (Element)current);
                     }
+                    else if ("macro-body".equals(localName)) {
+                        macroBody(parent, (Element)current);
+                    }
                     else if ("macro".equals(localName)) {
                         // Simply ignore the definition.
                     }

http://dive4elements.wald.intevation.org