diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java @ 957:e91996b46e3c

Meta data template: Added new choose/when/otherwise construct similiar to XSLT flys-artifacts/trunk@2376 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 20 Jul 2011 14:59:25 +0000
parents c09c9e05ecfa
children a2b20ed3d3b4
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java	Wed Jul 20 14:01:38 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java	Wed Jul 20 14:59:25 2011 +0000
@@ -12,6 +12,11 @@
 import org.w3c.dom.Node;
 import org.w3c.dom.Element;
 
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathFactory;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathConstants;
+
 import java.sql.SQLException;
 import java.sql.Connection;
 
@@ -26,6 +31,14 @@
     public static final String DC_NAMESPACE_URI =
         "http://www.intevation.org/2011/Datacage";
 
+    private static final Document EVAL_DOCUMENT =
+        XMLUtils.newDocument();
+
+    private static final XPathFactory XPATH_FACTORY =
+        XPathFactory.newInstance();
+
+
+
     protected Document template;
 
     public class BuildHelper
@@ -169,6 +182,61 @@
             element.setAttribute(name, value);
         }
 
+        protected void choose(Node parent, Element current)
+        throws SQLException
+        {
+            Node branch = null;
+
+            NodeList children = current.getChildNodes();
+            for (int i = 0, N = children.getLength(); i < N; ++i) {
+                Node child = children.item(i);
+                String ns = child.getNamespaceURI();
+                if (ns == null
+                || !ns.equals(DC_NAMESPACE_URI)
+                || child.getNodeType() != Node.ELEMENT_NODE
+                ) {
+                    continue;
+                }
+                String name = child.getLocalName();
+                if ("when".equals(name)) {
+                    Element when = (Element)child;
+                    String test = when.getAttribute("test");
+                    if (test.length() == 0) {
+                        log.warn("no 'test' attribute found for when");
+                        continue;
+                    }
+
+                    try {
+                        XPath xpath = XPATH_FACTORY.newXPath();
+                        xpath.setXPathVariableResolver(frames);
+                        Object result = xpath.evaluate(
+                            test, EVAL_DOCUMENT, XPathConstants.BOOLEAN);
+
+                        if (result instanceof Boolean
+                        && ((Boolean)result).booleanValue()) {
+                            branch = child;
+                            break;
+                        }
+                    }
+                    catch (XPathExpressionException xfce) {
+                        log.error(xfce);
+                    }
+                    continue;
+                }
+                else if ("otherwise".equals(name)) {
+                    branch = child;
+                    // No break here.
+                }
+            }
+
+            if (branch != null) {
+                NodeList subs = branch.getChildNodes();
+                for (int i = 0, N = subs.getLength(); i < N; ++i) {
+                    build(parent, subs.item(i));
+                }
+            }
+        }
+
         protected void convert(Node parent, Element current) {
 
             String variable = expand(current.getAttribute("var"));
@@ -208,6 +276,9 @@
                     if ("context".equals(localName)) {
                         context(parent, (Element)current);
                     }
+                    else if ("choose".equals(localName)) {
+                        choose(parent, (Element)current);
+                    }
                     else if ("attribute".equals(localName)) {
                         attribute(parent, (Element)current);
                     }

http://dive4elements.wald.intevation.org