comparison artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/Builder.java @ 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
parents 9a3ca7532f2a
children af13ceeba52a
comparison
equal deleted inserted replaced
5973:21581eacc1de 5974:95cb104a871b
15 import java.sql.Connection; 15 import java.sql.Connection;
16 import java.sql.SQLException; 16 import java.sql.SQLException;
17 17
18 import java.util.ArrayDeque; 18 import java.util.ArrayDeque;
19 import java.util.ArrayList; 19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
20 import java.util.Collections; 22 import java.util.Collections;
21 import java.util.Deque; 23 import java.util.Deque;
22 import java.util.HashMap; 24 import java.util.HashMap;
23 import java.util.List; 25 import java.util.List;
24 import java.util.Map; 26 import java.util.Map;
455 return groupExprStack.isEmpty() 457 return groupExprStack.isEmpty()
456 ? null 458 ? null
457 : groupExprStack.peek(); 459 : groupExprStack.peek();
458 } 460 }
459 461
462 protected void iterate(Node parent, Element current)
463 throws SQLException
464 {
465 log.debug("dc:iterate");
466 String container = expand(current.getAttribute("container"));
467 String var = expand(current.getAttribute("var")).toUpperCase();
468
469 if (container.isEmpty()) {
470 log.warn("'container' not set.");
471 return;
472 }
473
474 if (var.isEmpty()) {
475 log.warn("'var' not set.");
476 return;
477 }
478
479 Object [] result = new Object[1];
480
481 log.debug("Foo");
482
483 if (frames.getStore(container, result)) {
484 Object c = result[0];
485 if (c instanceof Object []) {
486 c = Arrays.asList((Object [])c);
487 }
488 if (c instanceof Collection) {
489 frames.enter();
490 try {
491 Collection<?> col = (Collection<?>)c;
492 for (Object o: col) {
493 if (o instanceof String) {
494 o = ((String)o).toLowerCase();
495 }
496 frames.put(var, o);
497 NodeList subs = current.getChildNodes();
498 for (int i = 0, N = subs.getLength(); i < N; ++i) {
499 build(parent, subs.item(i));
500 }
501 }
502 }
503 finally {
504 frames.leave();
505 }
506 }
507 }
508 }
509
510
460 /** 511 /**
461 * Kind of foreach over results of a statement within a context. 512 * Kind of foreach over results of a statement within a context.
462 */ 513 */
463 protected void foreach(Node parent, Element current) 514 protected void foreach(Node parent, Element current)
464 throws SQLException 515 throws SQLException
835 else if ("element".equals(localName)) { 886 else if ("element".equals(localName)) {
836 element(parent, curr); 887 element(parent, curr);
837 } 888 }
838 else if ("for-each".equals(localName)) { 889 else if ("for-each".equals(localName)) {
839 foreach(parent, curr); 890 foreach(parent, curr);
891 }
892 else if ("iterate".equals(localName)) {
893 iterate(parent, curr);
840 } 894 }
841 else if ("filter".equals(localName)) { 895 else if ("filter".equals(localName)) {
842 filter(parent, curr); 896 filter(parent, curr);
843 } 897 }
844 else if ("group".equals(localName)) { 898 else if ("group".equals(localName)) {

http://dive4elements.wald.intevation.org