comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java @ 4896:27d58b7b5edd

Builder: When looking for a statement in contexts, also search in macro definitions.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 29 Jan 2013 10:52:57 +0100
parents 54762b8ef016
children 9c388de4b4d5
comparison
equal deleted inserted replaced
4895:065a78d65007 4896:27d58b7b5edd
133 } 133 }
134 statements.clear(); 134 statements.clear();
135 } 135 }
136 136
137 /** 137 /**
138 * Handle a \<context\> node. 138 * Return first statement node in NodeList, respecting
139 * macros.
139 */ 140 */
140 protected void context(Node parent, Element current) 141 private Node findStatementNode(NodeList nodes) {
141 throws SQLException 142 int S = nodes.getLength();
142 { 143
143 log.debug("dc:context"); 144 // Check direct children and take special care of macros.
144
145 NodeList subs = current.getChildNodes();
146 int S = subs.getLength();
147
148 // Check only direct children.
149 Node stmntNode = null;
150 for (int i = 0; i < S; ++i) { 145 for (int i = 0; i < S; ++i) {
151 Node node = subs.item(i); 146 Node node = nodes.item(i);
152 String ns; 147 String ns;
148 // Regular statement node.
153 if (node.getNodeType() == Node.ELEMENT_NODE 149 if (node.getNodeType() == Node.ELEMENT_NODE
154 && node.getLocalName().equals("statement") 150 && node.getLocalName().equals("statement")
155 && (ns = node.getNamespaceURI()) != null 151 && (ns = node.getNamespaceURI()) != null
156 && ns.equals(DC_NAMESPACE_URI)) { 152 && ns.equals(DC_NAMESPACE_URI)) {
157 stmntNode = node; 153 return node;
158 break; 154 }
159 } 155 // Macro node. Descend.
160 } 156 else if (node.getNodeType() == Node.ELEMENT_NODE
157 && node.getLocalName().equals("call-macro")
158 && (ns = node.getNamespaceURI()) != null
159 && ns.equals(DC_NAMESPACE_URI)) {
160
161 String macroName = ((Element)node).getAttribute("name");
162 Node inMacroNode =
163 findStatementNode(getMacroChildren(macroName));
164 if (inMacroNode != null) {
165 return inMacroNode;
166 }
167 }
168
169 }
170
171 return null;
172 }
173
174 /**
175 * Handle a \<context\> node.
176 */
177 protected void context(Node parent, Element current)
178 throws SQLException
179 {
180 log.debug("dc:context");
181
182 NodeList subs = current.getChildNodes();
183 Node stmntNode = findStatementNode(subs);
184 int S = subs.getLength();
161 185
162 if (stmntNode == null) { 186 if (stmntNode == null) {
163 log.warn("dc:context: cannot find statement"); 187 log.warn("dc:context: cannot find statement");
164 return; 188 return;
165 } 189 }
368 else { 392 else {
369 log.warn("no current macro"); 393 log.warn("no current macro");
370 } 394 }
371 } 395 }
372 396
397 /** Get macro node children, not resolving bodies. */
398 protected NodeList getMacroChildren(String name) {
399 NodeList macros = template.getElementsByTagNameNS(
400 DC_NAMESPACE_URI, "macro");
401
402 Element macro = null;
403
404 for (int i = 0, N = macros.getLength(); i < N; ++i) {
405 Element m = (Element) macros.item(i);
406 if (name.equals(m.getAttribute("name"))) {
407 macro = m;
408 break;
409 }
410 }
411
412 if (macro != null) {
413 return macro.getChildNodes();
414 }
415 return null;
416 }
417
373 protected void ifClause(Node parent, Element current) 418 protected void ifClause(Node parent, Element current)
374 throws SQLException 419 throws SQLException
375 { 420 {
376 String test = current.getAttribute("test"); 421 String test = current.getAttribute("test");
377 422

http://dive4elements.wald.intevation.org