changeset 5443:3d7e552cc396

Datacage: Index macros with a map. Reduces excessive linear scanning for macros when calling them.
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 26 Mar 2013 22:29:55 +0100
parents 9575264f801d
children 70e0732bd4dd
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
diffstat 1 files changed, 18 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Tue Mar 26 21:55:23 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Tue Mar 26 22:29:55 2013 +0100
@@ -57,6 +57,8 @@
 
     protected Map<String, CompiledStatement> compiledStatements;
 
+    protected Map<String, Element> macros;
+
     /** Connection to either of the databases. */
     public static class NamedConnection {
 
@@ -111,10 +113,10 @@
             this.output      = output;
             frames           = new StackFrames(parameters);
             owner            = getOwnerDocument(output);
-            statements =
+            macroBodies      = new ArrayDeque<NodeList>();
+            functionResolver = new FunctionResolver(this);
+            statements       =
                 new HashMap<String, CompiledStatement.Instance>();
-            macroBodies = new ArrayDeque<NodeList>();
-            functionResolver = new FunctionResolver(this);
         }
 
         public void build() throws SQLException {
@@ -409,18 +411,7 @@
                 return;
             }
 
-            NodeList macros = template.getElementsByTagNameNS(
-                DC_NAMESPACE_URI, "macro");
-
-            Element macro = null;
-
-            for (int i = 0, N = macros.getLength(); i < N; ++i) {
-                Element m = (Element) macros.item(i);
-                if (name.equals(m.getAttribute("name"))) {
-                    macro = m;
-                    break;
-                }
-            }
+            Element macro = macros.get(name);
 
             if (macro != null) {
                 macroBodies.push(current.getChildNodes());
@@ -701,11 +692,13 @@
 
     public Builder() {
         compiledStatements = new HashMap<String, CompiledStatement>();
+        macros             = new HashMap<String, Element>();
     }
 
     public Builder(Document template) {
         this();
         this.template = template;
+        extractMacros();
         compileStatements();
     }
 
@@ -737,6 +730,16 @@
         }
     }
 
+    protected void extractMacros() {
+        NodeList ms = template.getElementsByTagNameNS(
+            DC_NAMESPACE_URI, "macro");
+
+        for (int i = 0, N = ms.getLength(); i < N; ++i) {
+            Element m = (Element)ms.item(i);
+            macros.put(m.getAttribute("name"), m);
+        }
+    }
+
     protected List<Node> rootsToList() {
 
         NodeList roots = template.getElementsByTagNameNS(

http://dive4elements.wald.intevation.org