comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java @ 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 504a62887217
comparison
equal deleted inserted replaced
5442:9575264f801d 5443:3d7e552cc396
54 XPathFactory.newInstance(); 54 XPathFactory.newInstance();
55 55
56 protected Document template; 56 protected Document template;
57 57
58 protected Map<String, CompiledStatement> compiledStatements; 58 protected Map<String, CompiledStatement> compiledStatements;
59
60 protected Map<String, Element> macros;
59 61
60 /** Connection to either of the databases. */ 62 /** Connection to either of the databases. */
61 public static class NamedConnection { 63 public static class NamedConnection {
62 64
63 protected String name; 65 protected String name;
109 connectionsStack = 111 connectionsStack =
110 new ArrayDeque<Pair<NamedConnection, ResultData>>(); 112 new ArrayDeque<Pair<NamedConnection, ResultData>>();
111 this.output = output; 113 this.output = output;
112 frames = new StackFrames(parameters); 114 frames = new StackFrames(parameters);
113 owner = getOwnerDocument(output); 115 owner = getOwnerDocument(output);
114 statements = 116 macroBodies = new ArrayDeque<NodeList>();
117 functionResolver = new FunctionResolver(this);
118 statements =
115 new HashMap<String, CompiledStatement.Instance>(); 119 new HashMap<String, CompiledStatement.Instance>();
116 macroBodies = new ArrayDeque<NodeList>();
117 functionResolver = new FunctionResolver(this);
118 } 120 }
119 121
120 public void build() throws SQLException { 122 public void build() throws SQLException {
121 try { 123 try {
122 synchronized (template) { 124 synchronized (template) {
407 if (name.length() == 0) { 409 if (name.length() == 0) {
408 log.warn("missing 'name' attribute in 'call-macro'"); 410 log.warn("missing 'name' attribute in 'call-macro'");
409 return; 411 return;
410 } 412 }
411 413
412 NodeList macros = template.getElementsByTagNameNS( 414 Element macro = macros.get(name);
413 DC_NAMESPACE_URI, "macro");
414
415 Element macro = null;
416
417 for (int i = 0, N = macros.getLength(); i < N; ++i) {
418 Element m = (Element) macros.item(i);
419 if (name.equals(m.getAttribute("name"))) {
420 macro = m;
421 break;
422 }
423 }
424 415
425 if (macro != null) { 416 if (macro != null) {
426 macroBodies.push(current.getChildNodes()); 417 macroBodies.push(current.getChildNodes());
427 try { 418 try {
428 NodeList subs = macro.getChildNodes(); 419 NodeList subs = macro.getChildNodes();
699 } // class BuildHelper 690 } // class BuildHelper
700 691
701 692
702 public Builder() { 693 public Builder() {
703 compiledStatements = new HashMap<String, CompiledStatement>(); 694 compiledStatements = new HashMap<String, CompiledStatement>();
695 macros = new HashMap<String, Element>();
704 } 696 }
705 697
706 public Builder(Document template) { 698 public Builder(Document template) {
707 this(); 699 this();
708 this.template = template; 700 this.template = template;
701 extractMacros();
709 compileStatements(); 702 compileStatements();
710 } 703 }
711 704
712 protected static QName typeToQName(String type) { 705 protected static QName typeToQName(String type) {
713 if ("number" .equals(type)) return XPathConstants.NUMBER; 706 if ("number" .equals(type)) return XPathConstants.NUMBER;
732 CompiledStatement cs = new CompiledStatement(stmnt); 725 CompiledStatement cs = new CompiledStatement(stmnt);
733 // For faster lookup store a shortend string into the template. 726 // For faster lookup store a shortend string into the template.
734 stmnt = "s" + i; 727 stmnt = "s" + i;
735 stmntElement.setTextContent(stmnt); 728 stmntElement.setTextContent(stmnt);
736 compiledStatements.put(stmnt, cs); 729 compiledStatements.put(stmnt, cs);
730 }
731 }
732
733 protected void extractMacros() {
734 NodeList ms = template.getElementsByTagNameNS(
735 DC_NAMESPACE_URI, "macro");
736
737 for (int i = 0, N = ms.getLength(); i < N; ++i) {
738 Element m = (Element)ms.item(i);
739 macros.put(m.getAttribute("name"), m);
737 } 740 }
738 } 741 }
739 742
740 protected List<Node> rootsToList() { 743 protected List<Node> rootsToList() {
741 744

http://dive4elements.wald.intevation.org