comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java @ 5485:3b5e1535a459

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 <dc:attribute>.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 28 Mar 2013 08:03:14 +0100
parents 504a62887217
children 773899d00234
comparison
equal deleted inserted replaced
5484:8f492672d1e5 5485:3b5e1535a459
25 import javax.xml.xpath.XPathExpressionException; 25 import javax.xml.xpath.XPathExpressionException;
26 import javax.xml.xpath.XPathFactory; 26 import javax.xml.xpath.XPathFactory;
27 27
28 import org.apache.log4j.Logger; 28 import org.apache.log4j.Logger;
29 29
30 import org.w3c.dom.Attr;
30 import org.w3c.dom.Document; 31 import org.w3c.dom.Document;
31 import org.w3c.dom.Element; 32 import org.w3c.dom.Element;
32 import org.w3c.dom.Node; 33 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList; 34 import org.w3c.dom.NodeList;
34 35
42 public static final String CONNECTION_SYSTEM = "system"; 43 public static final String CONNECTION_SYSTEM = "system";
43 public static final String DEFAULT_CONNECTION_NAME = CONNECTION_SYSTEM; 44 public static final String DEFAULT_CONNECTION_NAME = CONNECTION_SYSTEM;
44 45
45 public static final Pattern STRIP_LINE_INDENT = 46 public static final Pattern STRIP_LINE_INDENT =
46 Pattern.compile("\\s*\\r?\\n\\s*"); 47 Pattern.compile("\\s*\\r?\\n\\s*");
48
49 public static final Pattern BRACKET_XPATH =
50 Pattern.compile("\\{([^}]+)\\}");
47 51
48 public static final String DC_NAMESPACE_URI = 52 public static final String DC_NAMESPACE_URI =
49 "http://www.intevation.org/2011/Datacage"; 53 "http://www.intevation.org/2011/Datacage";
50 54
51 private static final Document EVAL_DOCUMENT = 55 private static final Document EVAL_DOCUMENT =
624 } 628 }
625 m.appendTail(sb); 629 m.appendTail(sb);
626 return sb.toString(); 630 return sb.toString();
627 } 631 }
628 632
633 protected void evaluateAttributeValue(Attr attr) {
634 String value = attr.getValue();
635 if (value.indexOf('{') >= 0) {
636 StringBuffer sb = new StringBuffer();
637 Matcher m = BRACKET_XPATH.matcher(value);
638 while (m.find()) {
639 String expr = m.group(1);
640 Object result = evaluateXPath(expr, XPathConstants.STRING);
641 if (result instanceof String) {
642 m.appendReplacement(sb, (String)result);
643 }
644 else {
645 m.appendReplacement(sb, "{" + expr + "}");
646 }
647 }
648 m.appendTail(sb);
649 attr.setValue(sb.toString());
650 }
651 }
652
629 protected void build(Node parent, Node current) 653 protected void build(Node parent, Node current)
630 throws SQLException 654 throws SQLException
631 { 655 {
632 String ns = current.getNamespaceURI(); 656 String ns = current.getNamespaceURI();
633 if (ns != null && ns.equals(DC_NAMESPACE_URI)) { 657 if (ns != null && ns.equals(DC_NAMESPACE_URI)) {
695 return; 719 return;
696 } 720 }
697 721
698 Node copy = owner.importNode(current, false); 722 Node copy = owner.importNode(current, false);
699 723
700 NodeList children = current.getChildNodes(); 724 if (copy.getNodeType() == Node.ATTRIBUTE_NODE) {
701 for (int i = 0, N = children.getLength(); i < N; ++i) { 725 evaluateAttributeValue((Attr)copy);
702 build(copy, children.item(i)); 726 }
727 else {
728 NodeList children = current.getChildNodes();
729 for (int i = 0, N = children.getLength(); i < N; ++i) {
730 build(copy, children.item(i));
731 }
703 } 732 }
704 parent.appendChild(copy); 733 parent.appendChild(copy);
705 } 734 }
706 } // class BuildHelper 735 } // class BuildHelper
707 736

http://dive4elements.wald.intevation.org