changeset 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 b279f2d4bc78
children 5287440b57b3
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
diffstat 1 files changed, 20 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Mar 27 10:25:45 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Mar 27 10:59:55 2013 +0100
@@ -21,6 +21,7 @@
 
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
 
@@ -97,6 +98,8 @@
         protected Deque<Pair<NamedConnection, ResultData>> connectionsStack;
         protected Deque<NodeList>                          macroBodies;
         protected FunctionResolver                         functionResolver;
+        protected Map<String, XPathExpression>             expressions;
+
 
         public BuildHelper(
             Node                  output,
@@ -115,6 +118,7 @@
             owner            = getOwnerDocument(output);
             macroBodies      = new ArrayDeque<NodeList>();
             functionResolver = new FunctionResolver(this);
+            expressions      = new HashMap<String, XPathExpression>();
             statements       =
                 new HashMap<String, CompiledStatement.Instance>();
         }
@@ -531,6 +535,20 @@
             }
         }
 
+        protected XPathExpression getXPathExpression(String expr)
+        throws XPathExpressionException
+        {
+            XPathExpression x = expressions.get(expr);
+            if (x == null) {
+                XPath xpath = XPATH_FACTORY.newXPath();
+                xpath.setXPathVariableResolver(frames);
+                xpath.setXPathFunctionResolver(functionResolver);
+                x = xpath.compile(expr);
+                expressions.put(expr, x);
+            }
+            return x;
+        }
+
         protected Object evaluateXPath(String expr, QName returnType) {
 
             if (log.isDebugEnabled()) {
@@ -538,10 +556,8 @@
             }
 
             try {
-                XPath xpath = XPATH_FACTORY.newXPath();
-                xpath.setXPathVariableResolver(frames);
-                xpath.setXPathFunctionResolver(functionResolver);
-                return xpath.evaluate(expr, EVAL_DOCUMENT, returnType);
+                XPathExpression x = getXPathExpression(expr);
+                return x.evaluate(EVAL_DOCUMENT, returnType);
             }
             catch (XPathExpressionException xpee) {
                 log.error("expression: " + expr, xpee);

http://dive4elements.wald.intevation.org