Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java @ 430:7ab81ff32111 2.3
merged flys-artifacts/2.3
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:10 +0200 |
parents | 8378683fa07a |
children | 9c565eb46f06 |
comparison
equal
deleted
inserted
replaced
290:a6f56ed9238b | 430:7ab81ff32111 |
---|---|
1 package de.intevation.flys.collections; | |
2 | |
3 import java.util.Iterator; | |
4 import java.util.List; | |
5 import java.util.Map; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 | |
9 import org.w3c.dom.Document; | |
10 import org.w3c.dom.Element; | |
11 import org.w3c.dom.Node; | |
12 | |
13 import de.intevation.artifacts.ArtifactNamespaceContext; | |
14 | |
15 import de.intevation.artifactdatabase.state.Facet; | |
16 import de.intevation.artifactdatabase.state.Output; | |
17 | |
18 import de.intevation.artifacts.common.utils.XMLUtils; | |
19 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; | |
20 | |
21 import de.intevation.flys.artifacts.model.ManagedFacet; | |
22 | |
23 | |
24 public class AttributeWriter { | |
25 | |
26 protected Map<String, Output> oldAttr; | |
27 protected Map<String, Output> newAttr; | |
28 | |
29 private static Logger logger = Logger.getLogger(AttributeWriter.class); | |
30 | |
31 | |
32 public AttributeWriter( | |
33 Map<String, Output> oldAttr, | |
34 Map<String, Output> newAttr) | |
35 { | |
36 this.oldAttr = oldAttr; | |
37 this.newAttr = newAttr; | |
38 } | |
39 | |
40 | |
41 protected Document write() { | |
42 Document doc = XMLUtils.newDocument(); | |
43 | |
44 ElementCreator cr = new ElementCreator( | |
45 doc, | |
46 ArtifactNamespaceContext.NAMESPACE_URI, | |
47 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
48 | |
49 Element outs = cr.create("outputs"); | |
50 doc.appendChild(outs); | |
51 | |
52 Iterator<String> iter = newAttr.keySet().iterator(); | |
53 | |
54 while (iter.hasNext()) { | |
55 String outName = iter.next(); | |
56 | |
57 Output a = newAttr.get(outName); | |
58 Output b = oldAttr.get(outName); | |
59 | |
60 writeOutput(doc, outs, cr, a, b); | |
61 } | |
62 | |
63 return doc; | |
64 } | |
65 | |
66 | |
67 protected void writeOutput( | |
68 Document doc, | |
69 Node outs, | |
70 ElementCreator cr, | |
71 Output a, | |
72 Output b) | |
73 { | |
74 Element output = cr.create("output"); | |
75 cr.addAttr(output, "name", a.getName()); | |
76 | |
77 outs.appendChild(output); | |
78 | |
79 List<Facet> facetsA = a.getFacets(); | |
80 List<Facet> facetsB = null; | |
81 | |
82 if (b != null) { | |
83 facetsB = b.getFacets(); | |
84 } | |
85 | |
86 writeFacets(doc, cr, output, facetsA, facetsB); | |
87 } | |
88 | |
89 | |
90 protected void writeFacets( | |
91 Document doc, | |
92 ElementCreator cr, | |
93 Element output, | |
94 List<Facet> a, | |
95 List<Facet> b) | |
96 { | |
97 int num = a.size(); | |
98 | |
99 for (int i = 0; i < num; i++) { | |
100 ManagedFacet fA = (ManagedFacet) a.get(i); | |
101 | |
102 if (!mergeFacets(doc, cr, output, fA, b)) { | |
103 writeFacet(doc, cr, output, fA); | |
104 } | |
105 } | |
106 } | |
107 | |
108 | |
109 protected boolean mergeFacets( | |
110 Document doc, | |
111 ElementCreator cr, | |
112 Element output, | |
113 ManagedFacet a, | |
114 List<Facet> list) | |
115 { | |
116 String name = a.getName(); | |
117 | |
118 if (list == null) { | |
119 return false; | |
120 } | |
121 | |
122 for (Facet facet: list) { | |
123 if (name.equals(facet.getName())) { | |
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 | |
145 output.appendChild(theme); | |
146 } | |
147 } | |
148 |