diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java @ 1023:5cb84ceee2a0

Datacage: Templates now can nest <dc:elements> into other elements in the body of <dc:context> flys-artifacts/trunk@2481 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 10 Aug 2011 10:08:01 +0000
parents 8637756275e5
children 3f3988bb6284
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Aug 10 08:43:38 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Aug 10 10:08:01 2011 +0000
@@ -3,6 +3,7 @@
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
 
+import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.HashMap;
@@ -87,6 +88,7 @@
         protected List<NamedConnection>                   connections;
         protected Map<String, CompiledStatement.Instance> statements;
         protected Deque<NamedConnection>                  connectionsStack;
+        protected Deque<ResultData>                       resultsStack;
 
         public BuildHelper(
             Node                  output,
@@ -99,6 +101,7 @@
 
             this.connections = connections;
             connectionsStack = new ArrayDeque<NamedConnection>();
+            resultsStack     = new ArrayDeque<ResultData>();
             this.output      = output;
             frames           = new StackFrames(parameters);
             owner            = getOwnerDocument(output);
@@ -129,33 +132,33 @@
         protected void context(Node parent, Element current)
         throws SQLException
         {
-            NodeList elements = current.getElementsByTagNameNS(
-                DC_NAMESPACE_URI, "elements");
+            log.debug("dc:context");
 
-            if (elements.getLength() < 1) {
-                log.warn("no elements found -> ignore");
-                return;
+            NodeList subs = current.getChildNodes();
+            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;
+                }
             }
 
-            NodeList subs = elements.item(0).getChildNodes();
-            int S = subs.getLength();
-
-            if (S < 1) {
-                log.warn("elements is empty -> ignore");
-                return;
-            }
-
-            NodeList stmntNode = current.getElementsByTagNameNS(
-                DC_NAMESPACE_URI, "statement");
-
-            if (stmntNode.getLength() < 1) {
-                log.warn("dc:context: too less statements");
+            if (stmntNode == null) {
+                log.warn("dc:context: cannot find statement");
                 return;
             }
 
             String con = current.getAttribute("connection");
 
-            String stmntText = stmntNode.item(0).getTextContent();
+            String stmntText = stmntNode.getTextContent();
 
             String key = con + "-" + stmntText;
 
@@ -180,30 +183,68 @@
                 }
             }
 
-            connectionsStack.push(connection);
-            try {
-                ResultData rd = csi.execute(
-                    connection.connection,
-                    frames,
-                    connection.cached);
-
-                String [] columns = rd.getColumnLabels();
+            ResultData rd = csi.execute(
+                connection.connection,
+                frames,
+                connection.cached);
 
-                for (Object [] row: rd.getRows()) {
-                    frames.enter();
-                    try {
-                        frames.put(columns, row);
-                        for (int i = 0; i < S; ++i) {
-                            build(parent, subs.item(i));
-                        }
-                    }
-                    finally {
-                        frames.leave();
+            // only descent if there are results
+            if (!rd.isEmpty()) {
+                resultsStack.push(rd);
+                connectionsStack.push(connection);
+                try {
+                    for (int i = 0; i < S; ++i) {
+                        build(parent, subs.item(i));
                     }
                 }
+                finally {
+                    connectionsStack.pop();
+                    resultsStack.pop();
+                }
             }
-            finally {
-                connectionsStack.pop();
+        }
+
+        protected void elements(Node parent, Element current) 
+        throws SQLException
+        {
+            log.debug("dc:elements");
+
+            if (resultsStack.isEmpty()) {
+                log.warn("dc:elements without having results");
+                return;
+            }
+
+            NodeList subs = current.getChildNodes();
+            int S = subs.getLength();
+
+            if (S == 0) {
+                log.debug("dc:elements has no children");
+                return;
+            }
+
+            ResultData rd = resultsStack.peek();
+
+            String [] columns = rd.getColumnLabels();
+
+            //if (log.isDebugEnabled()) {
+            //    log.debug("pushing vars: "
+            //        + java.util.Arrays.toString(columns));
+            //}
+
+            for (Object [] row: rd.getRows()) {
+                frames.enter();
+                try {
+                    frames.put(columns, row);
+                    //if (log.isDebugEnabled()) {
+                    //    log.debug("current vars: " + frames.dump());
+                    //}
+                    for (int i = 0; i < S; ++i) {
+                        build(parent, subs.item(i));
+                    }
+                }
+                finally {
+                    frames.leave();
+                }
             }
         }
 
@@ -428,11 +469,15 @@
                     else if ("element".equals(localName)) {
                         element(parent, (Element)current);
                     }
+                    else if ("elements".equals(localName)) {
+                        elements(parent, (Element)current);
+                    }
                     else if ("text".equals(localName)) {
                         text(parent, (Element)current);
                     }
-                    else if ("comment".equals(localName)) {
-                        // ignore comments in output
+                    else if ("comment".equals(localName)
+                         ||  "statement".equals(localName)) {
+                        // ignore comments and statements in output
                     }
                     else if ("convert".equals(localName)) {
                         convert(parent, (Element)current);

http://dive4elements.wald.intevation.org