changeset 4896:27d58b7b5edd

Builder: When looking for a statement in contexts, also search in macro definitions.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 29 Jan 2013 10:52:57 +0100
parents 065a78d65007
children 9c388de4b4d5
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
diffstat 1 files changed, 59 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Mon Jan 28 17:31:49 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Tue Jan 29 10:52:57 2013 +0100
@@ -135,6 +135,43 @@
         }
 
         /**
+         * Return first statement node in NodeList, respecting
+         * macros.
+         */
+        private Node findStatementNode(NodeList nodes) {
+            int S = nodes.getLength();
+
+            // Check direct children and take special care of macros.
+            for (int i = 0; i < S; ++i) {
+                Node node = nodes.item(i);
+                String ns;
+                // Regular statement node.
+                if (node.getNodeType() == Node.ELEMENT_NODE
+                && node.getLocalName().equals("statement")
+                && (ns = node.getNamespaceURI()) != null
+                && ns.equals(DC_NAMESPACE_URI)) {
+                    return node;
+                }
+                // Macro node. Descend.
+                else if (node.getNodeType() == Node.ELEMENT_NODE
+                    && node.getLocalName().equals("call-macro")
+                    && (ns = node.getNamespaceURI()) != null
+                    && ns.equals(DC_NAMESPACE_URI)) {
+
+                    String macroName = ((Element)node).getAttribute("name");
+                    Node inMacroNode =
+                        findStatementNode(getMacroChildren(macroName));
+                    if (inMacroNode != null) {
+                        return inMacroNode;
+                    }
+                }
+
+            }
+
+            return null;
+        }
+
+        /**
          * Handle a \<context\> node.
          */
         protected void context(Node parent, Element current)
@@ -143,22 +180,9 @@
             log.debug("dc:context");
 
             NodeList subs = current.getChildNodes();
+            Node stmntNode = findStatementNode(subs);
             int S = subs.getLength();
 
-            // Check only direct children.
-            Node stmntNode = null;
-            for (int i = 0; i < S; ++i) {
-                Node node = subs.item(i);
-                String ns;
-                if (node.getNodeType() == Node.ELEMENT_NODE
-                && node.getLocalName().equals("statement")
-                && (ns = node.getNamespaceURI()) != null
-                && ns.equals(DC_NAMESPACE_URI)) {
-                    stmntNode = node;
-                    break;
-                }
-            }
-
             if (stmntNode == null) {
                 log.warn("dc:context: cannot find statement");
                 return;
@@ -370,6 +394,27 @@
             }
         }
 
+        /** Get macro node children, not resolving bodies. */
+        protected NodeList getMacroChildren(String name) {
+            NodeList macros = template.getElementsByTagNameNS(
+                DC_NAMESPACE_URI, "macro");
+
+            Element macro = null;
+
+            for (int i = 0, N = macros.getLength(); i < N; ++i) {
+                Element m = (Element) macros.item(i);
+                if (name.equals(m.getAttribute("name"))) {
+                    macro = m;
+                    break;
+                }
+            }
+
+            if (macro != null) {
+                return macro.getChildNodes();
+            }
+            return null;
+        }
+
         protected void ifClause(Node parent, Element current)
         throws SQLException
         {

http://dive4elements.wald.intevation.org