changeset 4740:fb135e1dfa35

Added 'type' attribute to <dc:variable/> element. If an optional 'type' attribute is given the result of the XPATH expression is interpreted as this type. Valid values are 'number', 'bool', 'node' and 'nodeset'. All other defaults to 'string' which also is the default if nor type is given.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 02 Jan 2013 15:31:53 +0100
parents d93748043cbc
children f6d0ad25f1ee
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
diffstat 1 files changed, 43 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Jan 02 15:15:45 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Jan 02 15:31:53 2013 +0100
@@ -1,34 +1,36 @@
 package de.intevation.flys.artifacts.datacage.templating;
 
-import java.util.regex.Pattern;
-import java.util.regex.Matcher;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Deque;
-import java.util.ArrayDeque;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.NodeList;
-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;
-
 import de.intevation.artifacts.common.utils.XMLUtils;
 
 import de.intevation.flys.utils.Pair;
 
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.xml.namespace.QName;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
 import org.apache.log4j.Logger;
 
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
 
 public class Builder
 {
@@ -82,7 +84,6 @@
         }
     } // class NamedConnection
 
-
     public class BuildHelper
     {
         protected Node                                     output;
@@ -402,7 +403,7 @@
             }
         }
 
-        protected Object evaluateXPath(String expr) {
+        protected Object evaluateXPath(String expr, QName returnType) {
 
             if (log.isDebugEnabled()) {
                 log.debug("evaluate: '" + expr + "'");
@@ -412,17 +413,17 @@
                 XPath xpath = XPATH_FACTORY.newXPath();
                 xpath.setXPathVariableResolver(frames);
                 xpath.setXPathFunctionResolver(FunctionResolver.FUNCTIONS);
-                return xpath.evaluate(expr, EVAL_DOCUMENT, XPathConstants.BOOLEAN);
+                return xpath.evaluate(expr, EVAL_DOCUMENT, returnType);
             }
-            catch (XPathExpressionException xfce) {
-                log.error("expression: " + expr, xfce);
+            catch (XPathExpressionException xpee) {
+                log.error("expression: " + expr, xpee);
             }
             return null;
         }
 
         protected Boolean evaluateXPathToBoolean(String expr) {
 
-            Object result = evaluateXPath(expr);
+            Object result = evaluateXPath(expr, XPathConstants.BOOLEAN);
 
             return result instanceof Boolean
                 ? (Boolean)result
@@ -442,16 +443,20 @@
             }
         }
 
+
         protected void variable(Element current) {
 
             String varName = expand(current.getAttribute("name"));
             String expr    = current.getAttribute("expr");
+            String type    = current.getAttribute("type");
 
             if (varName.length() == 0 || expr.length() == 0) {
                 log.error("dc:variable 'name' or 'expr' empty.");
             }
             else {
-                frames.put(varName.toUpperCase(), evaluateXPath(expr));
+                frames.put(
+                    varName.toUpperCase(),
+                    evaluateXPath(expr, typeToQName(type)));
             }
         }
 
@@ -563,6 +568,14 @@
         compileStatements();
     }
 
+    protected static QName typeToQName(String type) {
+        if ("number" .equals(type)) return XPathConstants.NUMBER;
+        if ("bool"   .equals(type)) return XPathConstants.BOOLEAN;
+        if ("node"   .equals(type)) return XPathConstants.NODE;
+        if ("nodeset".equals(type)) return XPathConstants.NODESET;
+        return XPathConstants.STRING;
+    }
+
     /** Handle <dc:statement> elements. */
     protected void compileStatements() {
 

http://dive4elements.wald.intevation.org