[Dive4elements-commits] [PATCH 1 of 3] Add dc:iterate function

Wald Commits scm-commit at wald.intevation.org
Fri May 10 15:23:47 CEST 2013


# HG changeset patch
# User Andre Heinecke <aheinecke at intevation.de>
# Date 1368178311 -7200
# Node ID 95cb104a871bda0b2570a3e7a8c0e6793c493010
# Parent  21581eacc1de61526e0e87d3231dc515b823b690
Add dc:iterate function

    This function can be used to iterate over a collection setting
    var to the current element.

    Patch provided by Sascha Teichmann.

diff -r 21581eacc1de -r 95cb104a871b artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/Builder.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/Builder.java	Fri May 10 13:26:34 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/Builder.java	Fri May 10 11:31:51 2013 +0200
@@ -17,6 +17,8 @@
 
 import java.util.ArrayDeque;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Deque;
 import java.util.HashMap;
@@ -457,6 +459,55 @@
                 : groupExprStack.peek();
         }
 
+        protected void iterate(Node parent, Element current)
+        throws SQLException
+        {
+            log.debug("dc:iterate");
+            String container = expand(current.getAttribute("container"));
+            String var = expand(current.getAttribute("var")).toUpperCase();
+
+            if (container.isEmpty()) {
+                log.warn("'container' not set.");
+                return;
+            }
+
+            if (var.isEmpty()) {
+                log.warn("'var' not set.");
+                return;
+            }
+
+            Object [] result = new Object[1];
+
+            log.debug("Foo");
+
+            if (frames.getStore(container, result)) {
+                Object c = result[0];
+                if (c instanceof Object []) {
+                    c = Arrays.asList((Object [])c);
+                }
+                if (c instanceof Collection) {
+                    frames.enter();
+                    try {
+                        Collection<?> col = (Collection<?>)c;
+                        for (Object o: col) {
+                            if (o instanceof String) {
+                                o = ((String)o).toLowerCase();
+                            }
+                            frames.put(var, o);
+                            NodeList subs = current.getChildNodes();
+                            for (int i = 0, N = subs.getLength(); i < N; ++i) {
+                                build(parent, subs.item(i));
+                            }
+                        }
+                    }
+                    finally {
+                        frames.leave();
+                    }
+                }
+            }
+        }
+
+
         /**
          * Kind of foreach over results of a statement within a context.
          */
@@ -838,6 +889,9 @@
                     else if ("for-each".equals(localName)) {
                         foreach(parent, curr);
                     }
+                    else if ("iterate".equals(localName)) {
+                        iterate(parent, curr);
+                    }
                     else if ("filter".equals(localName)) {
                         filter(parent, curr);
                     }


More information about the Dive4elements-commits mailing list