[Dive4elements-commits] [PATCH 1 of 2] Added 'type' attribute to <dc:variable/> element. If an optional 'type' attribute is given

Wald Commits scm-commit at wald.intevation.org
Wed Jan 2 15:42:32 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1357137113 -3600
# Node ID fb135e1dfa35e438827537aea174ab15c07e2b7b
# Parent  d93748043cbc4ae0d27b1c3e6369734d0264aee1
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.

diff -r d93748043cbc -r fb135e1dfa35 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 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() {
 


More information about the Dive4elements-commits mailing list