changeset 4737:718adea968e2

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 -->
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 02 Jan 2013 14:05:21 +0100
parents b195fede1c3b
children d93748043cbc
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
diffstat 1 files changed, 31 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- 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");

http://dive4elements.wald.intevation.org