comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java @ 1190:f514894ec2fd

merged flys-artifacts/2.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:17 +0200
parents a3108f0a2fe7
children 16c74ca3586e
comparison
equal deleted inserted replaced
917:b48c36076e17 1190:f514894ec2fd
1 package de.intevation.flys.collections;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.apache.log4j.Logger;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.Node;
11
12 import de.intevation.artifacts.ArtifactNamespaceContext;
13
14 import de.intevation.artifactdatabase.state.Facet;
15 import de.intevation.artifactdatabase.state.Output;
16
17 import de.intevation.artifacts.common.utils.XMLUtils;
18 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
19
20 import de.intevation.flys.artifacts.model.ManagedFacet;
21
22
23 public class AttributeWriter {
24
25 protected Map<String, Output> oldAttr;
26 protected Map<String, Output> newAttr;
27
28 private static Logger logger = Logger.getLogger(AttributeWriter.class);
29
30
31 public AttributeWriter(
32 Map<String, Output> oldAttr,
33 Map<String, Output> newAttr)
34 {
35 this.oldAttr = oldAttr;
36 this.newAttr = newAttr;
37 }
38
39
40 protected Document write() {
41 Document doc = XMLUtils.newDocument();
42
43 ElementCreator cr = new ElementCreator(
44 doc,
45 ArtifactNamespaceContext.NAMESPACE_URI,
46 ArtifactNamespaceContext.NAMESPACE_PREFIX);
47
48 Element attribute = cr.create("attribute");
49 Element outs = cr.create("outputs");
50
51 attribute.appendChild(outs);
52 doc.appendChild(attribute);
53
54 for (String outName: newAttr.keySet()) {
55
56 Output a = newAttr.get(outName);
57 Output b = oldAttr.get(outName);
58
59 writeOutput(doc, outs, cr, a, b);
60 }
61
62 return doc;
63 }
64
65
66 protected void writeOutput(
67 Document doc,
68 Node outs,
69 ElementCreator cr,
70 Output a, /* new output */
71 Output b) /* old output */
72 {
73 Element output = cr.create("output");
74 cr.addAttr(output, "name", a.getName());
75
76 outs.appendChild(output);
77
78 List<Facet> facetsA = a.getFacets();
79 List<Facet> facetsB = null;
80
81 if (b != null) {
82 facetsB = b.getFacets();
83 }
84
85 writeFacets(doc, cr, output, facetsA, facetsB);
86 }
87
88
89 protected void writeFacets(
90 Document doc,
91 ElementCreator cr,
92 Element output,
93 List<Facet> a, /* new facets */
94 List<Facet> b) /* old facets */
95 {
96 int num = a.size();
97
98 for (int i = 0; i < num; i++) {
99 ManagedFacet fA = (ManagedFacet) a.get(i);
100
101 if (!mergeFacets(doc, cr, output, fA, b)) {
102 Node n = fA.toXML(doc);
103
104 if (n != null) {
105 output.appendChild(n);
106 }
107 }
108 }
109 }
110
111
112 protected boolean mergeFacets(
113 Document doc,
114 ElementCreator cr,
115 Element output,
116 ManagedFacet a, /* new facets */
117 List<Facet> list) /* old facets */
118 {
119 String nameA = a.getName() + a.getIndex();
120
121 if (list == null) {
122 logger.debug("No old facets found.");
123 return false;
124 }
125
126 for (Facet facet: list) {
127 String nameB = facet.getName() + facet.getIndex();
128
129 if (nameA.equals(nameB)) {
130 ManagedFacet b = (ManagedFacet) facet;
131
132 if (!b.getArtifact().equals(a.getArtifact())) {
133 continue;
134 }
135
136 Node n = facet.toXML(doc);
137
138 if (n != null) {
139 output.appendChild(n);
140 }
141
142 return true;
143 }
144 }
145
146 return false;
147 }
148 }
149 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org