1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.config;
20
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.dom4j.Element;
26 import org.springframework.extensions.config.ConfigElement;
27 import org.springframework.extensions.config.element.ConfigElementAdapter;
28
29
30
31
32 public class WebStudioConfigElement extends ConfigElementAdapter implements
33 WebStudioConfigProperties
34 {
35 public static final String CONFIG_ELEMENT_ID = "web-studio";
36
37 protected HashMap<String, ApplicationDescriptor> applications = null;
38
39 protected boolean developerMode = false;
40
41
42
43
44 public WebStudioConfigElement()
45 {
46 super(CONFIG_ELEMENT_ID);
47
48 this.applications = new HashMap<String, ApplicationDescriptor>(16, 1.0f);
49 this.developerMode = false;
50 }
51
52
53
54
55
56
57 public ConfigElement combine(ConfigElement element)
58 {
59 WebStudioConfigElement configElement = (WebStudioConfigElement) element;
60
61
62 WebStudioConfigElement combinedElement = new WebStudioConfigElement();
63
64
65 combinedElement.applications.putAll(this.applications);
66
67
68 combinedElement.applications.putAll(configElement.applications);
69
70 combinedElement.developerMode = this.developerMode;
71 if (configElement.developerMode)
72 {
73 combinedElement.developerMode = configElement.developerMode;
74 }
75
76 return combinedElement;
77 }
78
79
80
81
82
83
84 public boolean isDeveloperMode()
85 {
86 return this.developerMode;
87 }
88
89
90
91
92
93
94 public void setDeveloperMode(boolean developerMode)
95 {
96 this.developerMode = developerMode;
97 }
98
99
100
101
102
103
104 public String[] getApplicationIds()
105 {
106 return this.applications.keySet().toArray(
107 new String[this.applications.size()]);
108 }
109
110
111
112
113
114
115 public ApplicationDescriptor getApplication(String id)
116 {
117 return (ApplicationDescriptor) this.applications.get(id);
118 }
119
120
121
122 public static class Descriptor
123 {
124 private static final String ID = "id";
125
126 HashMap<String, Object> map;
127
128 Descriptor(Element el)
129 {
130 List elements = el.elements();
131 for (int i = 0; i < elements.size(); i++)
132 {
133 Element element = (Element) elements.get(i);
134 put(element);
135 }
136 }
137
138 public void put(Element el)
139 {
140 if (this.map == null)
141 {
142 this.map = new HashMap<String, Object>();
143 }
144
145 String key = el.getName();
146 Object value = (Object) el.getTextTrim();
147 if (value != null)
148 {
149 this.map.put(key, value);
150 }
151 }
152
153 public Object get(String key)
154 {
155 if (this.map == null)
156 {
157 this.map = new HashMap<String, Object>();
158 }
159
160 return (Object) this.map.get(key);
161 }
162
163 public String getId()
164 {
165 return (String) get(ID);
166 }
167
168 public Object getProperty(String key)
169 {
170 return get(key);
171 }
172
173 public String getStringProperty(String key)
174 {
175 return (String) get(key);
176 }
177
178 public String getStringPropertyEx(String key)
179 {
180 String value = getStringProperty(key);
181 if (value != null)
182 {
183 if (value.indexOf("${id}") > -1)
184 {
185 value = value.replace("${id}", getId());
186 }
187 }
188 return value;
189 }
190 }
191
192 public static class ApplicationDescriptor extends Descriptor
193 {
194 private static final String TITLE = "title";
195 private static final String DESCRIPTION = "description";
196 private static final String IMAGEURL = "image-url";
197 private static final String CLASSNAME = "classname";
198
199 private Map<String, PanelDescriptor> panelDescriptors;
200
201 ApplicationDescriptor(Element el)
202 {
203 super(el);
204
205 this.panelDescriptors = new HashMap<String, PanelDescriptor>();
206
207
208 Element panelsElement = el.element("panels");
209 if (panelsElement != null)
210 {
211 List panelElements = panelsElement.elements("panel");
212
213 for (int z = 0; z < panelElements.size(); z++)
214 {
215 Element panelElement = (Element) panelElements.get(z);
216
217 PanelDescriptor panelDescriptor = new PanelDescriptor(panelElement);
218 panelDescriptors.put(panelDescriptor.getId(), panelDescriptor);
219 }
220 }
221 }
222
223 public String getTitle()
224 {
225 return getStringProperty(TITLE);
226 }
227
228 public String getDescription()
229 {
230 return getStringProperty(DESCRIPTION);
231 }
232
233 public String getImageUrl()
234 {
235 return getStringProperty(IMAGEURL);
236 }
237
238 public String getClassName()
239 {
240 return getStringPropertyEx(CLASSNAME);
241 }
242
243 public String[] getPanelIds()
244 {
245 return this.panelDescriptors.keySet().toArray(new String[this.panelDescriptors.keySet().size()]);
246 }
247
248 public PanelDescriptor getPanelDescriptor(String panelId)
249 {
250 return this.panelDescriptors.get(panelId);
251 }
252 }
253
254 public static class PanelDescriptor extends Descriptor
255 {
256 private static final String TITLE = "title";
257 private static final String CLASSNAME = "classname";
258
259 PanelDescriptor(Element el)
260 {
261 super(el);
262 }
263
264 public String getTitle()
265 {
266 return getStringProperty(TITLE);
267 }
268
269 public String getClassName()
270 {
271 return getStringPropertyEx(CLASSNAME);
272 }
273 }
274
275 protected static WebStudioConfigElement newInstance(Element elem)
276 {
277 WebStudioConfigElement configElement = new WebStudioConfigElement();
278
279
280 String _developerMode = (String) elem.elementTextTrim("developer-mode");
281 if (_developerMode != null)
282 {
283 configElement.developerMode = ("true"
284 .equalsIgnoreCase(_developerMode));
285 }
286
287
288 Element applications = elem.element("applications");
289 if (applications != null)
290 {
291 List list = applications.elements("application");
292 for (int i = 0; i < list.size(); i++)
293 {
294 Element el = (Element) list.get(i);
295
296 ApplicationDescriptor applicationDescriptor = new ApplicationDescriptor(
297 el);
298 configElement.applications.put(applicationDescriptor.getId(),
299 applicationDescriptor);
300 }
301 }
302
303 return configElement;
304 }
305 }