comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java @ 4740:fb135e1dfa35

Added 'type' attribute to <dc:variable/> element. If an optional 'type' attribute is given the result of the XPATH expression is interpreted as this type. Valid values are 'number', 'bool', 'node' and 'nodeset'. All other defaults to 'string' which also is the default if nor type is given.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 02 Jan 2013 15:31:53 +0100
parents 718adea968e2
children bf38ea4cb0f7
comparison
equal deleted inserted replaced
4738:d93748043cbc 4740:fb135e1dfa35
1 package de.intevation.flys.artifacts.datacage.templating; 1 package de.intevation.flys.artifacts.datacage.templating;
2 2
3 import de.intevation.artifacts.common.utils.XMLUtils;
4
5 import de.intevation.flys.utils.Pair;
6
7 import java.sql.Connection;
8 import java.sql.SQLException;
9
10 import java.util.ArrayDeque;
11 import java.util.ArrayList;
12 import java.util.Deque;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import java.util.regex.Matcher;
3 import java.util.regex.Pattern; 18 import java.util.regex.Pattern;
4 import java.util.regex.Matcher; 19
5 20 import javax.xml.namespace.QName;
6 import java.util.ArrayList; 21
7 import java.util.List; 22 import javax.xml.xpath.XPath;
8 import java.util.HashMap; 23 import javax.xml.xpath.XPathConstants;
9 import java.util.Map; 24 import javax.xml.xpath.XPathExpressionException;
10 import java.util.Deque; 25 import javax.xml.xpath.XPathFactory;
11 import java.util.ArrayDeque; 26
27 import org.apache.log4j.Logger;
12 28
13 import org.w3c.dom.Document; 29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.w3c.dom.Node;
14 import org.w3c.dom.NodeList; 32 import org.w3c.dom.NodeList;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.Element;
17
18 import javax.xml.xpath.XPath;
19 import javax.xml.xpath.XPathFactory;
20 import javax.xml.xpath.XPathExpressionException;
21 import javax.xml.xpath.XPathConstants;
22
23 import java.sql.SQLException;
24 import java.sql.Connection;
25
26 import de.intevation.artifacts.common.utils.XMLUtils;
27
28 import de.intevation.flys.utils.Pair;
29
30 import org.apache.log4j.Logger;
31 33
32 34
33 public class Builder 35 public class Builder
34 { 36 {
35 private static Logger log = Logger.getLogger(Builder.class); 37 private static Logger log = Logger.getLogger(Builder.class);
79 this.name = name; 81 this.name = name;
80 this.connection = connection; 82 this.connection = connection;
81 this.cached = cached; 83 this.cached = cached;
82 } 84 }
83 } // class NamedConnection 85 } // class NamedConnection
84
85 86
86 public class BuildHelper 87 public class BuildHelper
87 { 88 {
88 protected Node output; 89 protected Node output;
89 protected Document owner; 90 protected Document owner;
400 build(parent, subs.item(i)); 401 build(parent, subs.item(i));
401 } 402 }
402 } 403 }
403 } 404 }
404 405
405 protected Object evaluateXPath(String expr) { 406 protected Object evaluateXPath(String expr, QName returnType) {
406 407
407 if (log.isDebugEnabled()) { 408 if (log.isDebugEnabled()) {
408 log.debug("evaluate: '" + expr + "'"); 409 log.debug("evaluate: '" + expr + "'");
409 } 410 }
410 411
411 try { 412 try {
412 XPath xpath = XPATH_FACTORY.newXPath(); 413 XPath xpath = XPATH_FACTORY.newXPath();
413 xpath.setXPathVariableResolver(frames); 414 xpath.setXPathVariableResolver(frames);
414 xpath.setXPathFunctionResolver(FunctionResolver.FUNCTIONS); 415 xpath.setXPathFunctionResolver(FunctionResolver.FUNCTIONS);
415 return xpath.evaluate(expr, EVAL_DOCUMENT, XPathConstants.BOOLEAN); 416 return xpath.evaluate(expr, EVAL_DOCUMENT, returnType);
416 } 417 }
417 catch (XPathExpressionException xfce) { 418 catch (XPathExpressionException xpee) {
418 log.error("expression: " + expr, xfce); 419 log.error("expression: " + expr, xpee);
419 } 420 }
420 return null; 421 return null;
421 } 422 }
422 423
423 protected Boolean evaluateXPathToBoolean(String expr) { 424 protected Boolean evaluateXPathToBoolean(String expr) {
424 425
425 Object result = evaluateXPath(expr); 426 Object result = evaluateXPath(expr, XPathConstants.BOOLEAN);
426 427
427 return result instanceof Boolean 428 return result instanceof Boolean
428 ? (Boolean)result 429 ? (Boolean)result
429 : null; 430 : null;
430 } 431 }
440 Object object = TypeConverter.convert(result[0], type); 441 Object object = TypeConverter.convert(result[0], type);
441 frames.put(variable.toUpperCase(), object); 442 frames.put(variable.toUpperCase(), object);
442 } 443 }
443 } 444 }
444 445
446
445 protected void variable(Element current) { 447 protected void variable(Element current) {
446 448
447 String varName = expand(current.getAttribute("name")); 449 String varName = expand(current.getAttribute("name"));
448 String expr = current.getAttribute("expr"); 450 String expr = current.getAttribute("expr");
451 String type = current.getAttribute("type");
449 452
450 if (varName.length() == 0 || expr.length() == 0) { 453 if (varName.length() == 0 || expr.length() == 0) {
451 log.error("dc:variable 'name' or 'expr' empty."); 454 log.error("dc:variable 'name' or 'expr' empty.");
452 } 455 }
453 else { 456 else {
454 frames.put(varName.toUpperCase(), evaluateXPath(expr)); 457 frames.put(
458 varName.toUpperCase(),
459 evaluateXPath(expr, typeToQName(type)));
455 } 460 }
456 } 461 }
457 462
458 protected String expand(String s) { 463 protected String expand(String s) {
459 Matcher m = CompiledStatement.VAR.matcher(s); 464 Matcher m = CompiledStatement.VAR.matcher(s);
559 564
560 public Builder(Document template) { 565 public Builder(Document template) {
561 this(); 566 this();
562 this.template = template; 567 this.template = template;
563 compileStatements(); 568 compileStatements();
569 }
570
571 protected static QName typeToQName(String type) {
572 if ("number" .equals(type)) return XPathConstants.NUMBER;
573 if ("bool" .equals(type)) return XPathConstants.BOOLEAN;
574 if ("node" .equals(type)) return XPathConstants.NODE;
575 if ("nodeset".equals(type)) return XPathConstants.NODESET;
576 return XPathConstants.STRING;
564 } 577 }
565 578
566 /** Handle <dc:statement> elements. */ 579 /** Handle <dc:statement> elements. */
567 protected void compileStatements() { 580 protected void compileStatements() {
568 581

http://dive4elements.wald.intevation.org