comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.java @ 963:f48cef242e7f

Datacage: recommendation mechanism now accepts nodes to append results. flys-artifacts/trunk@2382 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 20 Jul 2011 22:03:50 +0000
parents 2de1808503be
children 0c8aca463bd4
comparison
equal deleted inserted replaced
962:2de1808503be 963:f48cef242e7f
37 private static final XPathFactory XPATH_FACTORY = 37 private static final XPathFactory XPATH_FACTORY =
38 XPathFactory.newInstance(); 38 XPathFactory.newInstance();
39 39
40 protected Document template; 40 protected Document template;
41 41
42 protected static Document getOwnerDocument(Node node) {
43 Document document = node.getOwnerDocument();
44 return document != null ? document : (Document)node;
45 }
46
47
42 public class BuildHelper 48 public class BuildHelper
43 { 49 {
44 protected Document output; 50 protected Node output;
51 protected Document owner;
45 protected StackFrames frames; 52 protected StackFrames frames;
46 protected Connection connection; 53 protected Connection connection;
47 protected Map<String, CompiledStatement> statements; 54 protected Map<String, CompiledStatement> statements;
48 55
49 public BuildHelper( 56 public BuildHelper(
50 Document output, 57 Node output,
51 Connection connection, 58 Connection connection,
52 Map<String, Object> parameters 59 Map<String, Object> parameters
53 ) { 60 ) {
54 this.output = output; 61 this.output = output;
55 this.connection = connection; 62 this.connection = connection;
56 frames = new StackFrames(parameters); 63 frames = new StackFrames(parameters);
57 statements = new HashMap<String, CompiledStatement>(); 64 statements = new HashMap<String, CompiledStatement>();
65 owner = getOwnerDocument(output);
58 } 66 }
59 67
60 public void build(List<Node> elements) throws SQLException { 68 public void build(List<Node> elements) throws SQLException {
61 try { 69 try {
62 for (Node current: elements) { 70 for (Node current: elements) {
144 if (attr.length() == 0) { 152 if (attr.length() == 0) {
145 log.warn("no name attribute found"); 153 log.warn("no name attribute found");
146 return; 154 return;
147 } 155 }
148 156
149 Element element = output.createElement(attr); 157 Element element = owner.createElement(attr);
150 158
151 NodeList children = current.getChildNodes(); 159 NodeList children = current.getChildNodes();
152 for (int i = 0, N = children.getLength(); i < N; ++i) { 160 for (int i = 0, N = children.getLength(); i < N; ++i) {
153 build(element, children.item(i)); 161 build(element, children.item(i));
154 } 162 }
159 protected void text(Node parent, Element current) 167 protected void text(Node parent, Element current)
160 throws SQLException 168 throws SQLException
161 { 169 {
162 log.debug("dc:text"); 170 log.debug("dc:text");
163 String value = expand(current.getTextContent()); 171 String value = expand(current.getTextContent());
164 parent.appendChild(output.createTextNode(value)); 172 parent.appendChild(owner.createTextNode(value));
165 } 173 }
166 174
167 175
168 protected void attribute(Node parent, Element current) { 176 protected void attribute(Node parent, Element current) {
169 177
366 if (txt != null && txt.trim().length() == 0) { 374 if (txt != null && txt.trim().length() == 0) {
367 return; 375 return;
368 } 376 }
369 } 377 }
370 378
371 Node copy = output.importNode(current, false); 379 Node copy = owner.importNode(current, false);
372 380
373 NodeList children = current.getChildNodes(); 381 NodeList children = current.getChildNodes();
374 for (int i = 0, N = children.getLength(); i < N; ++i) { 382 for (int i = 0, N = children.getLength(); i < N; ++i) {
375 build(copy, children.item(i)); 383 build(copy, children.item(i));
376 } 384 }
384 392
385 public Builder(Document template) { 393 public Builder(Document template) {
386 this.template = template; 394 this.template = template;
387 } 395 }
388 396
389 public Document build(Connection connection) 397 protected static List<Node> rootsToList(NodeList roots) {
390 throws SQLException
391 {
392 return build(connection, XMLUtils.newDocument(), null);
393 }
394
395 public Document build(
396 Connection connection,
397 Document output,
398 Map<String, Object> parameters
399 )
400 throws SQLException
401 {
402 NodeList roots = template.getElementsByTagNameNS(
403 DC_NAMESPACE_URI, "template");
404
405 BuildHelper helper = new BuildHelper(output, connection, parameters);
406 398
407 List<Node> elements = new ArrayList<Node>(); 399 List<Node> elements = new ArrayList<Node>();
408 400
409 for (int i = 0, N = roots.getLength(); i < N; ++i) { 401 for (int i = 0, N = roots.getLength(); i < N; ++i) {
410 NodeList rootChildren = roots.item(i).getChildNodes(); 402 NodeList rootChildren = roots.item(i).getChildNodes();
413 if (child.getNodeType() == Node.ELEMENT_NODE) { 405 if (child.getNodeType() == Node.ELEMENT_NODE) {
414 elements.add(child); 406 elements.add(child);
415 } 407 }
416 } 408 }
417 } 409 }
418 helper.build(elements); 410
419 411 return elements;
420 return output; 412 }
413
414 public void build(
415 Connection connection,
416 Node output,
417 Map<String, Object> parameters
418 )
419 throws SQLException
420 {
421 BuildHelper helper = new BuildHelper(output, connection, parameters);
422
423 NodeList roots = template.getElementsByTagNameNS(
424 DC_NAMESPACE_URI, "template");
425
426 helper.build(rootsToList(roots));
421 } 427 }
422 } 428 }
423 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 429 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org