comparison flys-artifacts/src/main/java/de/intevation/flys/themes/DefaultTheme.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 b36fd8f21e6a
children 6566c7868456
comparison
equal deleted inserted replaced
618:6fd6c223b81e 751:8d5bd3a08dd1
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
27 /** The map storing the fields of this theme.*/
28 protected Map<String, ThemeField> fields;
29
30 /** The map storing the attributes of this theme.*/
31 protected Map<String, String> attr;
32
33
34 /**
35 * Initializes the components of this Theme.
36 */
37 public DefaultTheme(String name, String description) {
38 this.name = name;
39 this.description = description;
40 this.fields = new HashMap<String, ThemeField>();
41 this.attr = new HashMap<String, String>();
42 }
43
44
45 public void init(Node config) {
46 }
47
48
49 public String getName() {
50 return name;
51 }
52
53
54 public String getDescription() {
55 return description;
56 }
57
58
59 public void addAttribute(String name, String value) {
60 if (name != null && value != null) {
61 attr.put(name, value);
62 }
63 }
64
65
66 public String getAttribute(String name) {
67 return attr.get(name);
68 }
69
70
71 public void addField(String name, ThemeField field) {
72 if (name != null && field != null) {
73 fields.put(name, field);
74 }
75 }
76
77
78 public void setFieldValue(String name, Object value) {
79 if (name != null && value != null) {
80 ThemeField field = fields.get(name);
81
82 if (field != null) {
83 field.setValue(value);
84 }
85 }
86 }
87
88
89 public ThemeField getField(String name) {
90 return fields.get(name);
91 }
92
93
94 public String getFieldType(String name) {
95 ThemeField field = fields.get(name);
96
97 return field != null ? field.getType() : null;
98 }
99
100
101 public Object getFieldValue(String name) {
102 ThemeField field = fields.get(name);
103
104 return field != null ? field.getValue() : null;
105 }
106
107
108 public Document toXML() {
109 Document doc = XMLUtils.newDocument();
110
111 ElementCreator cr = new ElementCreator(doc, null, null);
112
113 Element theme = cr.create("theme");
114
115 appendAttributes(cr, theme);
116 appendFields(cr, theme);
117
118 doc.appendChild(theme);
119
120 return doc;
121 }
122
123
124 /**
125 * Appends the attributes configured for this theme.
126 *
127 * @param cr The ElementCreator.
128 * @param theme The document root element.
129 */
130 protected void appendAttributes(ElementCreator cr, Element theme) {
131 Iterator<String> iter = attr.keySet().iterator();
132
133 while (iter.hasNext()) {
134 String key = iter.next();
135 String val = getAttribute(key);
136
137 if (key == null || val == null) {
138 continue;
139 }
140
141 cr.addAttr(theme, key, val);
142 }
143 }
144
145
146 /**
147 * Appends the fields configured for this theme.
148 *
149 * @param cr The ElementCreator.
150 * @param theme The document root element.
151 */
152 protected void appendFields(ElementCreator cr, Element theme) {
153 Iterator<String> iter = fields.keySet().iterator();
154
155 while (iter.hasNext()) {
156 String name = iter.next();
157
158 ThemeField field = getField(name);
159
160 Document doc = field.toXML();
161 Node root = doc.getFirstChild();
162
163 theme.appendChild(theme.getOwnerDocument().importNode(root, true));
164 }
165 }
166 }
167 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org