comparison flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java @ 346:16161de47662

The Attributes of a collection are written into its DESCRIBE now. flys-artifacts/trunk@1748 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 26 Apr 2011 13:29:18 +0000
parents
children 8378683fa07a
comparison
equal deleted inserted replaced
345:88a669785863 346:16161de47662
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 Iterator<String> iter = newAttr.keySet().iterator();
50
51 while (iter.hasNext()) {
52 String outName = iter.next();
53
54 Output a = newAttr.get(outName);
55 Output b = oldAttr.get(outName);
56
57 writeOutput(doc, cr, a, b);
58 }
59
60 return doc;
61 }
62
63
64 protected void writeOutput(
65 Document doc,
66 ElementCreator cr,
67 Output a,
68 Output b)
69 {
70 Element output = cr.create("output");
71 cr.addAttr(output, "name", a.getName());
72
73 doc.appendChild(output);
74
75 List<Facet> facetsA = a.getFacets();
76 List<Facet> facetsB = null;
77
78 if (b != null) {
79 facetsB = b.getFacets();
80 }
81
82 writeFacets(doc, cr, output, facetsA, facetsB);
83 }
84
85
86 protected void writeFacets(
87 Document doc,
88 ElementCreator cr,
89 Element output,
90 List<Facet> a,
91 List<Facet> b)
92 {
93 int num = a.size();
94
95 for (int i = 0; i < num; i++) {
96 ManagedFacet fA = (ManagedFacet) a.get(i);
97
98 if (!mergeFacets(doc, cr, output, fA, b)) {
99 writeFacet(doc, cr, output, fA);
100 }
101 }
102 }
103
104
105 protected boolean mergeFacets(
106 Document doc,
107 ElementCreator cr,
108 Element output,
109 ManagedFacet a,
110 List<Facet> list)
111 {
112 String name = a.getName();
113
114 if (list == null) {
115 return false;
116 }
117
118 for (Facet facet: list) {
119 if (name.equals(facet.getName())) {
120 writeFacet(doc, cr, output, (ManagedFacet) facet);
121 return true;
122 }
123 }
124
125 return false;
126 }
127
128
129 protected void writeFacet(
130 Document doc,
131 ElementCreator cr,
132 Node output,
133 ManagedFacet f)
134 {
135 Element theme = cr.create("theme");
136 cr.addAttr(theme, "artifact", f.getArtifact(), true);
137 cr.addAttr(theme, "facet", f.getName(), true);
138 cr.addAttr(theme, "pos", Integer.toString(f.getPosition()), true);
139 cr.addAttr(theme, "active", Integer.toString(f.getActive()), true);
140
141 output.appendChild(theme);
142 }
143 }
144

http://dive4elements.wald.intevation.org