# HG changeset patch # User Sascha L. Teichmann # Date 1364454194 -3600 # Node ID 3b5e1535a459e909e1263d69dcb8d7a70be34dfe # Parent 8f492672d1e54eca628cb196494b99541cd14281 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 . diff -r 8f492672d1e5 -r 3b5e1535a459 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 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); }