changeset 962:2de1808503be

Datacage template: Added a macro mechanism. flys-artifacts/trunk@2381 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 20 Jul 2011 20:51:13 +0000
parents 3ba4f5a88c20
children f48cef242e7f
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java
diffstat 2 files changed, 53 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Jul 20 20:13:49 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Jul 20 20:51:13 2011 +0000
@@ -1,3 +1,23 @@
+2011-07-20  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java:
+	  Added a macro mechanism:
+
+	    <dc:macro name="keine-fuenf">
+	      <dc:text>'5' ist nicht in der Liste der Outs.</dc:text>
+	    </dc:macro>
+	    <dc:call-macro name="keine-fuenf"/>
+	    <dc:call-macro name="keine-fuenf"/>
+	    <dc:call-macro name="keine-fuenf"/>
+
+	  Macros can be defined everywhere in the template
+	  with 'macro'. There bodies can contain all valid elements
+	  including other 'macro's and 'call-macro's. They are
+	  called with their 'name' with 'call-macro'. The control flow
+	  is continued inside the body of the called macro and 
+	  will continue right after the calling 'call-macro' when
+	  the macro body is finished.
+
 2011-07-20  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java	Wed Jul 20 20:13:49 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java	Wed Jul 20 20:51:13 2011 +0000
@@ -180,6 +180,33 @@
             element.setAttribute(name, value);
         }
 
+        protected void callMacro(Node parent, Element current)
+        throws SQLException
+        {
+            String name = current.getAttribute("name");
+
+            if (name.length() == 0) {
+                log.warn("missing 'name' attribute in 'call-macro'");
+                return;
+            }
+
+            NodeList macros = template.getElementsByTagNameNS(
+                DC_NAMESPACE_URI, "macro");
+
+            for (int i = 0, N = macros.getLength(); i < N; ++i) {
+                Element macro = (Element)macros.item(i);
+                if (name.equals(macro.getAttribute("name"))) {
+                    NodeList subs = macro.getChildNodes();
+                    for (int j = 0, M = subs.getLength(); j < M; ++j) {
+                        build(parent, subs.item(j));
+                    }
+                    return;
+                }
+            }
+
+            log.warn("no macro '" + name + "' found.");
+        }
+
         protected void ifClause(Node parent, Element current)
         throws SQLException
         {
@@ -321,6 +348,12 @@
                     else if ("convert".equals(localName)) {
                         convert(parent, (Element)current);
                     }
+                    else if ("call-macro".equals(localName)) {
+                        callMacro(parent, (Element)current);
+                    }
+                    else if ("macro".equals(localName)) {
+                        // simply ignore the definition.
+                    }
                     else {
                         log.warn("unknown '" + localName + "' -> ignore");
                     }

http://dive4elements.wald.intevation.org