comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java @ 5449:504a62887217

Datacage: Compile XPath expressions and reuse them. With the introduction of filters in dc:elements XPath expressions are evalutated very often so compiling them should reduce the overhead significantly.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 27 Mar 2013 10:59:55 +0100
parents 3d7e552cc396
children 3b5e1535a459
comparison
equal deleted inserted replaced
5448:b279f2d4bc78 5449:504a62887217
19 19
20 import javax.xml.namespace.QName; 20 import javax.xml.namespace.QName;
21 21
22 import javax.xml.xpath.XPath; 22 import javax.xml.xpath.XPath;
23 import javax.xml.xpath.XPathConstants; 23 import javax.xml.xpath.XPathConstants;
24 import javax.xml.xpath.XPathExpression;
24 import javax.xml.xpath.XPathExpressionException; 25 import javax.xml.xpath.XPathExpressionException;
25 import javax.xml.xpath.XPathFactory; 26 import javax.xml.xpath.XPathFactory;
26 27
27 import org.apache.log4j.Logger; 28 import org.apache.log4j.Logger;
28 29
95 protected List<NamedConnection> connections; 96 protected List<NamedConnection> connections;
96 protected Map<String, CompiledStatement.Instance> statements; 97 protected Map<String, CompiledStatement.Instance> statements;
97 protected Deque<Pair<NamedConnection, ResultData>> connectionsStack; 98 protected Deque<Pair<NamedConnection, ResultData>> connectionsStack;
98 protected Deque<NodeList> macroBodies; 99 protected Deque<NodeList> macroBodies;
99 protected FunctionResolver functionResolver; 100 protected FunctionResolver functionResolver;
101 protected Map<String, XPathExpression> expressions;
102
100 103
101 public BuildHelper( 104 public BuildHelper(
102 Node output, 105 Node output,
103 List<NamedConnection> connections, 106 List<NamedConnection> connections,
104 Map<String, Object> parameters 107 Map<String, Object> parameters
113 this.output = output; 116 this.output = output;
114 frames = new StackFrames(parameters); 117 frames = new StackFrames(parameters);
115 owner = getOwnerDocument(output); 118 owner = getOwnerDocument(output);
116 macroBodies = new ArrayDeque<NodeList>(); 119 macroBodies = new ArrayDeque<NodeList>();
117 functionResolver = new FunctionResolver(this); 120 functionResolver = new FunctionResolver(this);
121 expressions = new HashMap<String, XPathExpression>();
118 statements = 122 statements =
119 new HashMap<String, CompiledStatement.Instance>(); 123 new HashMap<String, CompiledStatement.Instance>();
120 } 124 }
121 125
122 public void build() throws SQLException { 126 public void build() throws SQLException {
529 build(parent, subs.item(i)); 533 build(parent, subs.item(i));
530 } 534 }
531 } 535 }
532 } 536 }
533 537
534 protected Object evaluateXPath(String expr, QName returnType) { 538 protected XPathExpression getXPathExpression(String expr)
535 539 throws XPathExpressionException
536 if (log.isDebugEnabled()) { 540 {
537 log.debug("evaluate: '" + expr + "'"); 541 XPathExpression x = expressions.get(expr);
538 } 542 if (x == null) {
539
540 try {
541 XPath xpath = XPATH_FACTORY.newXPath(); 543 XPath xpath = XPATH_FACTORY.newXPath();
542 xpath.setXPathVariableResolver(frames); 544 xpath.setXPathVariableResolver(frames);
543 xpath.setXPathFunctionResolver(functionResolver); 545 xpath.setXPathFunctionResolver(functionResolver);
544 return xpath.evaluate(expr, EVAL_DOCUMENT, returnType); 546 x = xpath.compile(expr);
547 expressions.put(expr, x);
548 }
549 return x;
550 }
551
552 protected Object evaluateXPath(String expr, QName returnType) {
553
554 if (log.isDebugEnabled()) {
555 log.debug("evaluate: '" + expr + "'");
556 }
557
558 try {
559 XPathExpression x = getXPathExpression(expr);
560 return x.evaluate(EVAL_DOCUMENT, returnType);
545 } 561 }
546 catch (XPathExpressionException xpee) { 562 catch (XPathExpressionException xpee) {
547 log.error("expression: " + expr, xpee); 563 log.error("expression: " + expr, xpee);
548 } 564 }
549 return null; 565 return null;

http://dive4elements.wald.intevation.org