Mercurial > dive4elements > river
changeset 957:e91996b46e3c
Meta data template: Added new choose/when/otherwise construct similiar to XSLT
flys-artifacts/trunk@2376 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 20 Jul 2011 14:59:25 +0000 |
parents | 1cf7b4ee7b6d |
children | ae198bef4ba0 |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/App.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/StackFrames.java |
diffstat | 4 files changed, 121 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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 <sascha.teichmann@intevation.de> + + * 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 + + <dc:choose> + <dc:when test="$river = 'Mosel'"> + <dc:text>Es ist die Mosel.</dc:text> + </dc:when> + <dc:when test="$river = 'Saar'"> + <dc:text>Es ist die Saar.</dc:text> + </dc:when> + <dc:otherwise> + <dc:text>Es ist weder Mosel noch Saar.</dc:text> + </dc:otherwise> + </dc:choose> + + 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 <ingo@intevation.de> * doc/conf/conf.xml: Added a config section for floodmaps. Currently, the
--- 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<String, Object> parameters = getParameters(); Session session = SessionFactoryProvider
--- 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); }
--- 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<Map<String, Object>> 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 :