Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java @ 751:8d5bd3a08dd1
merged flys-artifacts/2.4
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:14 +0200 |
parents | 853dceead0f4 |
children | 59ae2a823e73 |
comparison
equal
deleted
inserted
replaced
618:6fd6c223b81e | 751:8d5bd3a08dd1 |
---|---|
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 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : | |
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, | |
71 Output b) | |
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, | |
94 List<Facet> b) | |
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 writeFacet(doc, cr, output, fA); | |
103 } | |
104 } | |
105 } | |
106 | |
107 | |
108 protected boolean mergeFacets( | |
109 Document doc, | |
110 ElementCreator cr, | |
111 Element output, | |
112 ManagedFacet a, | |
113 List<Facet> list) | |
114 { | |
115 String name = a.getName() + a.getIndex(); | |
116 | |
117 if (list == null) { | |
118 logger.debug("No old facets found."); | |
119 return false; | |
120 } | |
121 | |
122 for (Facet facet: list) { | |
123 if (name.equals(facet.getName() + facet.getIndex())) { | |
124 writeFacet(doc, cr, output, (ManagedFacet) facet); | |
125 return true; | |
126 } | |
127 } | |
128 | |
129 return false; | |
130 } | |
131 | |
132 | |
133 protected void writeFacet( | |
134 Document doc, | |
135 ElementCreator cr, | |
136 Node output, | |
137 ManagedFacet f) | |
138 { | |
139 Element theme = cr.create("theme"); | |
140 cr.addAttr(theme, "artifact", f.getArtifact(), true); | |
141 cr.addAttr(theme, "facet", f.getName(), true); | |
142 cr.addAttr(theme, "pos", Integer.toString(f.getPosition()), true); | |
143 cr.addAttr(theme, "active", Integer.toString(f.getActive()), true); | |
144 cr.addAttr(theme, "index", Integer.toString(f.getIndex()), true); | |
145 cr.addAttr(theme, "description", f.getDescription(), true); | |
146 | |
147 output.appendChild(theme); | |
148 } | |
149 } | |
150 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |