comparison flys-artifacts/src/main/java/de/intevation/flys/themes/DefaultTheme.java @ 3468:f37e7e8907cb

merged flys-artifacts/2.8.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:39 +0200
parents 6566c7868456
children fe29b0226faf
comparison
equal deleted inserted replaced
3387:5ffad8bde8ad 3468:f37e7e8907cb
1 package de.intevation.flys.themes;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import org.w3c.dom.Document;
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
10
11 import de.intevation.artifacts.common.utils.XMLUtils;
12 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
13
14
15 /**
16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
17 */
18 public class DefaultTheme implements Theme {
19
20 /** The name of the theme.*/
21 protected String name;
22
23 /** The description of the theme.*/
24 protected String description;
25
26 protected String facet;
27
28 protected int index;
29
30
31 /** The map storing the fields of this theme.*/
32 protected Map<String, ThemeField> fields;
33
34 /** The map storing the attributes of this theme.*/
35 protected Map<String, String> attr;
36
37
38 /**
39 * Initializes the components of this Theme.
40 */
41 public DefaultTheme(String name, String description) {
42 this.name = name;
43 this.description = description;
44 this.fields = new HashMap<String, ThemeField>();
45 this.attr = new HashMap<String, String>();
46 }
47
48
49 public void init(Node config) {
50 }
51
52
53 public String getName() {
54 return name;
55 }
56
57
58 public String getDescription() {
59 return description;
60 }
61
62
63 public String getFacet() {
64 return facet;
65 }
66
67
68 public void setFacet(String facet) {
69 this.facet = facet;
70 }
71
72
73 public int getIndex() {
74 return index;
75 }
76
77
78 public void setIndex(int index) {
79 this.index = index;
80 }
81
82
83 public void addAttribute(String name, String value) {
84 if (name != null && value != null) {
85 attr.put(name, value);
86 }
87 }
88
89
90 public String getAttribute(String name) {
91 return attr.get(name);
92 }
93
94
95 public void addField(String name, ThemeField field) {
96 if (name != null && field != null) {
97 fields.put(name, field);
98 }
99 }
100
101
102 public void setFieldValue(String name, Object value) {
103 if (name != null && value != null) {
104 ThemeField field = fields.get(name);
105
106 if (field != null) {
107 field.setValue(value);
108 }
109 }
110 }
111
112
113 public ThemeField getField(String name) {
114 return fields.get(name);
115 }
116
117
118 public String getFieldType(String name) {
119 ThemeField field = fields.get(name);
120
121 return field != null ? field.getType() : null;
122 }
123
124
125 public Object getFieldValue(String name) {
126 ThemeField field = fields.get(name);
127
128 return field != null ? field.getValue() : null;
129 }
130
131
132 public Document toXML() {
133 Document doc = XMLUtils.newDocument();
134
135 ElementCreator cr = new ElementCreator(doc, null, null);
136
137 Element theme = cr.create("theme");
138 theme.setAttribute("facet", facet);
139 theme.setAttribute("index", String.valueOf(index));
140
141 appendAttributes(cr, theme);
142 appendFields(cr, theme);
143
144 doc.appendChild(theme);
145
146 return doc;
147 }
148
149
150 /**
151 * Appends the attributes configured for this theme.
152 *
153 * @param cr The ElementCreator.
154 * @param theme The document root element.
155 */
156 protected void appendAttributes(ElementCreator cr, Element theme) {
157 Iterator<String> iter = attr.keySet().iterator();
158
159 while (iter.hasNext()) {
160 String key = iter.next();
161 String val = getAttribute(key);
162
163 if (key == null || val == null) {
164 continue;
165 }
166
167 cr.addAttr(theme, key, val);
168 }
169 }
170
171
172 /**
173 * Appends the fields configured for this theme.
174 *
175 * @param cr The ElementCreator.
176 * @param theme The document root element.
177 */
178 protected void appendFields(ElementCreator cr, Element theme) {
179 Iterator<String> iter = fields.keySet().iterator();
180
181 while (iter.hasNext()) {
182 String name = iter.next();
183
184 ThemeField field = getField(name);
185
186 Document doc = field.toXML();
187 Node root = doc.getFirstChild();
188
189 theme.appendChild(theme.getOwnerDocument().importNode(root, true));
190 }
191 }
192 }
193 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org