# HG changeset patch # User Sascha L. Teichmann # Date 1311173965 0 # Node ID e91996b46e3c6310c1781b426f03b07f15525e8f # Parent 1cf7b4ee7b6ddf81a2de090efeb16ba1098543ca Meta data template: Added new choose/when/otherwise construct similiar to XSLT flys-artifacts/trunk@2376 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 1cf7b4ee7b6d -r e91996b46e3c flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Wed Jul 20 14:01:38 2011 +0000 +++ b/flys-artifacts/ChangeLog Wed Jul 20 14:59:25 2011 +0000 @@ -1,3 +1,37 @@ +2011-07-20 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/artifacts/services/meta/App.java: + Check if builder was created properly before using it. + + * src/main/java/de/intevation/flys/artifacts/services/meta/StackFrames.java: + Implements now variable provider for XPath expressions. + + * src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java: + Added new choose/when/otherwise construct similiar to XSLT + + + + Es ist die Mosel. + + + Es ist die Saar. + + + Es ist weder Mosel noch Saar. + + + + A 'choose' block can contain a list of 'when's and an optional + 'otherwise'. For each 'when' the test attribute is evaluated + as an XPath expression on an empty document. The result of + the evaluation is taken as a boolean value. If its value is + true the control flow is continued inside the corresponding + 'when' and the other choose elements are not tested. + If the value is values the testing continues with the next + 'when'. If no 'test' expression is evaluated to true, the + control flow continues inside the 'otherwise'. If no 'otherwise' + is given nothing happens at all. + 2011-07-20 Ingo Weinzierl * doc/conf/conf.xml: Added a config section for floodmaps. Currently, the diff -r 1cf7b4ee7b6d -r e91996b46e3c flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/App.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/App.java Wed Jul 20 14:01:38 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/App.java Wed Jul 20 14:59:25 2011 +0000 @@ -58,6 +58,12 @@ final Document result = XMLUtils.newDocument(); final Builder builder = dc.getBuilder(); + + if (builder == null) { + System.err.println("No builder created"); + return; + } + final Map parameters = getParameters(); Session session = SessionFactoryProvider diff -r 1cf7b4ee7b6d -r e91996b46e3c flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java Wed Jul 20 14:01:38 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java Wed Jul 20 14:59:25 2011 +0000 @@ -12,6 +12,11 @@ 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; @@ -26,6 +31,14 @@ public static final String DC_NAMESPACE_URI = "http://www.intevation.org/2011/Datacage"; + private static final Document EVAL_DOCUMENT = + XMLUtils.newDocument(); + + private static final XPathFactory XPATH_FACTORY = + XPathFactory.newInstance(); + + + protected Document template; public class BuildHelper @@ -169,6 +182,61 @@ element.setAttribute(name, value); } + protected void choose(Node parent, Element current) + throws SQLException + { + Node branch = null; + + NodeList children = current.getChildNodes(); + for (int i = 0, N = children.getLength(); i < N; ++i) { + Node child = children.item(i); + String ns = child.getNamespaceURI(); + if (ns == null + || !ns.equals(DC_NAMESPACE_URI) + || child.getNodeType() != Node.ELEMENT_NODE + ) { + continue; + } + String name = child.getLocalName(); + if ("when".equals(name)) { + Element when = (Element)child; + String test = when.getAttribute("test"); + if (test.length() == 0) { + log.warn("no 'test' attribute found for when"); + continue; + } + + try { + XPath xpath = XPATH_FACTORY.newXPath(); + xpath.setXPathVariableResolver(frames); + Object result = xpath.evaluate( + test, EVAL_DOCUMENT, XPathConstants.BOOLEAN); + + if (result instanceof Boolean + && ((Boolean)result).booleanValue()) { + branch = child; + break; + } + } + catch (XPathExpressionException xfce) { + log.error(xfce); + } + continue; + } + else if ("otherwise".equals(name)) { + branch = child; + // No break here. + } + } + + if (branch != null) { + NodeList subs = branch.getChildNodes(); + for (int i = 0, N = subs.getLength(); i < N; ++i) { + build(parent, subs.item(i)); + } + } + } + protected void convert(Node parent, Element current) { String variable = expand(current.getAttribute("var")); @@ -208,6 +276,9 @@ if ("context".equals(localName)) { context(parent, (Element)current); } + else if ("choose".equals(localName)) { + choose(parent, (Element)current); + } else if ("attribute".equals(localName)) { attribute(parent, (Element)current); } diff -r 1cf7b4ee7b6d -r e91996b46e3c flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/StackFrames.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/StackFrames.java Wed Jul 20 14:01:38 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/StackFrames.java Wed Jul 20 14:59:25 2011 +0000 @@ -5,7 +5,12 @@ import java.util.HashMap; import java.util.ArrayList; +import javax.xml.xpath.XPathVariableResolver; + +import javax.xml.namespace.QName; + public class StackFrames +implements XPathVariableResolver { protected List> frames; @@ -66,5 +71,10 @@ return def; } + + @Override + public Object resolveVariable(QName variableName) { + return get(variableName.getLocalPart()); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :