changeset 5485:3b5e1535a459

Datacage: Experimental support for '{XPath}' expressions in attributes similiar to XSLT. Using this feature you can shorten the written template a lot because it reduces the necessity to create dynamic attributes with <dc:attribute>.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 28 Mar 2013 08:03:14 +0100
parents 8f492672d1e5
children b19f0fd301fc
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
diffstat 1 files changed, 32 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Mar 27 21:01:13 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Thu Mar 28 08:03:14 2013 +0100
@@ -27,6 +27,7 @@
 
 import org.apache.log4j.Logger;
 
+import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -45,6 +46,9 @@
     public static final Pattern STRIP_LINE_INDENT =
         Pattern.compile("\\s*\\r?\\n\\s*");
 
+    public static final Pattern BRACKET_XPATH =
+        Pattern.compile("\\{([^}]+)\\}");
+
     public static final String DC_NAMESPACE_URI =
         "http://www.intevation.org/2011/Datacage";
 
@@ -626,6 +630,26 @@
             return sb.toString();
         }
 
+        protected void evaluateAttributeValue(Attr attr) {
+            String value = attr.getValue();
+            if (value.indexOf('{') >= 0) {
+                StringBuffer sb = new StringBuffer();
+                Matcher m = BRACKET_XPATH.matcher(value);
+                while (m.find()) {
+                    String expr = m.group(1);
+                    Object result = evaluateXPath(expr, XPathConstants.STRING);
+                    if (result instanceof String) {
+                        m.appendReplacement(sb, (String)result);
+                    }
+                    else {
+                        m.appendReplacement(sb, "{" + expr + "}");
+                    }
+                }
+                m.appendTail(sb);
+                attr.setValue(sb.toString());
+            }
+        }
+
         protected void build(Node parent, Node current)
         throws SQLException
         {
@@ -697,9 +721,14 @@
 
             Node copy = owner.importNode(current, false);
 
-            NodeList children = current.getChildNodes();
-            for (int i = 0, N = children.getLength(); i < N; ++i) {
-                build(copy, children.item(i));
+            if (copy.getNodeType() == Node.ATTRIBUTE_NODE) {
+                evaluateAttributeValue((Attr)copy);
+            }
+            else {
+                NodeList children = current.getChildNodes();
+                for (int i = 0, N = children.getLength(); i < N; ++i) {
+                    build(copy, children.item(i));
+                }
             }
             parent.appendChild(copy);
         }

http://dive4elements.wald.intevation.org