comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java @ 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 c09c9e05ecfa
children a2b20ed3d3b4
comparison
equal deleted inserted replaced
956:1cf7b4ee7b6d 957:e91996b46e3c
10 import org.w3c.dom.Document; 10 import org.w3c.dom.Document;
11 import org.w3c.dom.NodeList; 11 import org.w3c.dom.NodeList;
12 import org.w3c.dom.Node; 12 import org.w3c.dom.Node;
13 import org.w3c.dom.Element; 13 import org.w3c.dom.Element;
14 14
15 import javax.xml.xpath.XPath;
16 import javax.xml.xpath.XPathFactory;
17 import javax.xml.xpath.XPathExpressionException;
18 import javax.xml.xpath.XPathConstants;
19
15 import java.sql.SQLException; 20 import java.sql.SQLException;
16 import java.sql.Connection; 21 import java.sql.Connection;
17 22
18 import de.intevation.artifacts.common.utils.XMLUtils; 23 import de.intevation.artifacts.common.utils.XMLUtils;
19 24
23 { 28 {
24 private static Logger log = Logger.getLogger(Builder.class); 29 private static Logger log = Logger.getLogger(Builder.class);
25 30
26 public static final String DC_NAMESPACE_URI = 31 public static final String DC_NAMESPACE_URI =
27 "http://www.intevation.org/2011/Datacage"; 32 "http://www.intevation.org/2011/Datacage";
33
34 private static final Document EVAL_DOCUMENT =
35 XMLUtils.newDocument();
36
37 private static final XPathFactory XPATH_FACTORY =
38 XPathFactory.newInstance();
39
40
28 41
29 protected Document template; 42 protected Document template;
30 43
31 public class BuildHelper 44 public class BuildHelper
32 { 45 {
167 Element element = (Element)parent; 180 Element element = (Element)parent;
168 181
169 element.setAttribute(name, value); 182 element.setAttribute(name, value);
170 } 183 }
171 184
185 protected void choose(Node parent, Element current)
186 throws SQLException
187 {
188 Node branch = null;
189
190 NodeList children = current.getChildNodes();
191 for (int i = 0, N = children.getLength(); i < N; ++i) {
192 Node child = children.item(i);
193 String ns = child.getNamespaceURI();
194 if (ns == null
195 || !ns.equals(DC_NAMESPACE_URI)
196 || child.getNodeType() != Node.ELEMENT_NODE
197 ) {
198 continue;
199 }
200 String name = child.getLocalName();
201 if ("when".equals(name)) {
202 Element when = (Element)child;
203 String test = when.getAttribute("test");
204 if (test.length() == 0) {
205 log.warn("no 'test' attribute found for when");
206 continue;
207 }
208
209 try {
210 XPath xpath = XPATH_FACTORY.newXPath();
211 xpath.setXPathVariableResolver(frames);
212 Object result = xpath.evaluate(
213 test, EVAL_DOCUMENT, XPathConstants.BOOLEAN);
214
215 if (result instanceof Boolean
216 && ((Boolean)result).booleanValue()) {
217 branch = child;
218 break;
219 }
220 }
221 catch (XPathExpressionException xfce) {
222 log.error(xfce);
223 }
224 continue;
225 }
226 else if ("otherwise".equals(name)) {
227 branch = child;
228 // No break here.
229 }
230 }
231
232 if (branch != null) {
233 NodeList subs = branch.getChildNodes();
234 for (int i = 0, N = subs.getLength(); i < N; ++i) {
235 build(parent, subs.item(i));
236 }
237 }
238 }
239
172 protected void convert(Node parent, Element current) { 240 protected void convert(Node parent, Element current) {
173 241
174 String variable = expand(current.getAttribute("var")); 242 String variable = expand(current.getAttribute("var"));
175 String type = expand(current.getAttribute("type")); 243 String type = expand(current.getAttribute("type"));
176 244
205 } 273 }
206 else { 274 else {
207 String localName = current.getLocalName(); 275 String localName = current.getLocalName();
208 if ("context".equals(localName)) { 276 if ("context".equals(localName)) {
209 context(parent, (Element)current); 277 context(parent, (Element)current);
278 }
279 else if ("choose".equals(localName)) {
280 choose(parent, (Element)current);
210 } 281 }
211 else if ("attribute".equals(localName)) { 282 else if ("attribute".equals(localName)) {
212 attribute(parent, (Element)current); 283 attribute(parent, (Element)current);
213 } 284 }
214 else if ("element".equals(localName)) { 285 else if ("element".equals(localName)) {

http://dive4elements.wald.intevation.org