changeset 5974:95cb104a871b

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.
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 10 May 2013 11:31:51 +0200 (2013-05-10)
parents 21581eacc1de
children 95b9e40bbad0
files artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/Builder.java
diffstat 1 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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);
                     }

http://dive4elements.wald.intevation.org