[Dive4elements-commits] [PATCH] Add <dc:variable name="variable" expr="xpath"/> construct to datacage language

Wald Commits scm-commit at wald.intevation.org
Wed Jan 2 14:05:47 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1357131921 -3600
# Node ID 718adea968e2fdc08ab30858b01f4cafb5c322b3
# Parent  b195fede1c3b0fe4234303f31975fb71f6f69700
Add <dc:variable name="variable" expr="xpath"/> construct to datacage language.

Each time during traversal of the template this element is found a variable
called 'variable' is set to the result of the the evaluation of the
XPpath 'xpath'.

Example:
    <dc:variable name="x" expr="$x + 1"/> <!-- Assign x+1 to x -->

diff -r b195fede1c3b -r 718adea968e2 flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Jan 02 13:18:31 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Jan 02 14:05:21 2013 +0100
@@ -346,7 +346,7 @@
                 return;
             }
 
-            Boolean result = evaluateXPath(test);
+            Boolean result = evaluateXPathToBoolean(test);
 
             if (result != null && result.booleanValue()) {
                 NodeList subs = current.getChildNodes();
@@ -380,7 +380,7 @@
                         continue;
                     }
 
-                    Boolean result = evaluateXPath(test);
+                    Boolean result = evaluateXPathToBoolean(test);
                     if (result != null && result.booleanValue()) {
                         branch = child;
                         break;
@@ -402,7 +402,7 @@
             }
         }
 
-        protected Boolean evaluateXPath(String expr) {
+        protected Object evaluateXPath(String expr) {
 
             if (log.isDebugEnabled()) {
                 log.debug("evaluate: '" + expr + "'");
@@ -412,12 +412,7 @@
                 XPath xpath = XPATH_FACTORY.newXPath();
                 xpath.setXPathVariableResolver(frames);
                 xpath.setXPathFunctionResolver(FunctionResolver.FUNCTIONS);
-                Object result = xpath.evaluate(
-                    expr, EVAL_DOCUMENT, XPathConstants.BOOLEAN);
-
-                return result instanceof Boolean
-                    ? (Boolean)result
-                    : null;
+                return xpath.evaluate(expr, EVAL_DOCUMENT, XPathConstants.BOOLEAN);
             }
             catch (XPathExpressionException xfce) {
                 log.error("expression: " + expr, xfce);
@@ -425,7 +420,16 @@
             return null;
         }
 
-        protected void convert(Node parent, Element current) {
+        protected Boolean evaluateXPathToBoolean(String expr) {
+
+            Object result = evaluateXPath(expr);
+
+            return result instanceof Boolean
+                ? (Boolean)result
+                : null;
+        }
+
+        protected void convert(Element current) {
 
             String variable = expand(current.getAttribute("var"));
             String type     = expand(current.getAttribute("type"));
@@ -438,6 +442,19 @@
             }
         }
 
+        protected void variable(Element current) {
+
+            String varName = expand(current.getAttribute("name"));
+            String expr    = current.getAttribute("expr");
+
+            if (varName.length() == 0 || expr.length() == 0) {
+                log.error("dc:variable 'name' or 'expr' empty.");
+            }
+            else {
+                frames.put(varName.toUpperCase(), evaluateXPath(expr));
+            }
+        }
+
         protected String expand(String s) {
             Matcher m = CompiledStatement.VAR.matcher(s);
 
@@ -496,12 +513,15 @@
                     else if ("text".equals(localName)) {
                         text(parent, (Element)current);
                     }
+                    else if ("variable".equals(localName)) {
+                        variable((Element)current);
+                    }
                     else if ("comment".equals(localName)
                          ||  "statement".equals(localName)) {
                         // ignore comments and statements in output
                     }
                     else if ("convert".equals(localName)) {
-                        convert(parent, (Element)current);
+                        convert((Element)current);
                     }
                     else {
                         log.warn("unknown '" + localName + "' -> ignore");


More information about the Dive4elements-commits mailing list