Mercurial > dive4elements > river
comparison flys-client/src/main/java/org/dive4elements/river/client/server/DataFactory.java @ 5834:f507086aa94b
Repaired internal references.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:31:32 +0200 |
parents | flys-client/src/main/java/de/intevation/flys/client/server/DataFactory.java@d0a9acddbea2 |
children | 821a02bbfb4e |
comparison
equal
deleted
inserted
replaced
5833:a2bdc0f524e8 | 5834:f507086aa94b |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import javax.xml.xpath.XPathConstants; | |
4 | |
5 import org.w3c.dom.Element; | |
6 import org.w3c.dom.NodeList; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 | |
10 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
11 import de.intevation.artifacts.common.utils.XMLUtils; | |
12 | |
13 import de.intevation.flys.client.shared.model.Data; | |
14 import de.intevation.flys.client.shared.model.DataItem; | |
15 import de.intevation.flys.client.shared.model.DefaultData; | |
16 import de.intevation.flys.client.shared.model.DefaultDataItem; | |
17 import de.intevation.flys.client.shared.model.DoubleArrayData; | |
18 import de.intevation.flys.client.shared.model.IntegerArrayData; | |
19 import de.intevation.flys.client.shared.model.IntegerData; | |
20 import de.intevation.flys.client.shared.model.IntegerOptionsData; | |
21 import de.intevation.flys.client.shared.model.IntegerRangeData; | |
22 import de.intevation.flys.client.shared.model.StringData; | |
23 import de.intevation.flys.client.shared.model.StringOptionsData; | |
24 import de.intevation.flys.client.shared.model.LongRangeData; | |
25 | |
26 import de.intevation.flys.client.shared.model.IntDataItem; | |
27 | |
28 /** | |
29 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
30 */ | |
31 public class DataFactory { | |
32 | |
33 private static final Logger logger = Logger.getLogger(DataFactory.class); | |
34 | |
35 public static final String NS_URI = ArtifactNamespaceContext.NAMESPACE_URI; | |
36 | |
37 | |
38 /** | |
39 * Creates a new Data instance based on the <i>art:type</i> attribute of | |
40 * <i>element</i>. | |
41 * | |
42 * @param element The Data element. | |
43 * | |
44 * @return a Data instance. | |
45 */ | |
46 public static Data createDataFromElement(Element element) { | |
47 String name = element.getAttributeNS(NS_URI, "name"); | |
48 String type = element.getAttributeNS(NS_URI, "type"); | |
49 String label = element.getAttributeNS(NS_URI, "label"); | |
50 | |
51 label = label != null && label.length() > 0 ? label : name; | |
52 | |
53 try { | |
54 logger.debug("Create Data instance for: " + name + " | " + type); | |
55 | |
56 if (type == null || type.length() == 0) { | |
57 return createDefaultData(element, name, label); | |
58 } | |
59 | |
60 type = type.toLowerCase(); | |
61 | |
62 if (type.equals(StringData.TYPE)) { | |
63 return createStringData(element, name, label); | |
64 } | |
65 else if (type.equals(IntegerData.TYPE)) { | |
66 return createIntegerData(element, name, label); | |
67 } | |
68 else if (type.equals(StringOptionsData.TYPE)) { | |
69 return createStringOptionsData(element, name, label); | |
70 } | |
71 else if (type.equals(IntegerOptionsData.TYPE)) { | |
72 return createIntegerOptionsData(element, name, label); | |
73 } | |
74 else if (type.equals(IntegerRangeData.TYPE)) { | |
75 return createIntegerRangeData(element, name, label); | |
76 } | |
77 else if (type.equals(IntegerArrayData.TYPE)) { | |
78 return createIntegerArrayData(element, name, label); | |
79 } | |
80 else if (type.equals(DoubleArrayData.TYPE)) { | |
81 return createDoubleArrayData(element, name, label); | |
82 } | |
83 else if (type.equals(LongRangeData.TYPE)) { | |
84 return createLongRangeData(element, name, label); | |
85 } | |
86 else { | |
87 return createDefaultData(element, name, label); | |
88 } | |
89 } | |
90 catch (Exception e) { | |
91 logger.error("Error while data creation for: " + name); | |
92 } | |
93 | |
94 return null; | |
95 } | |
96 | |
97 | |
98 /** | |
99 * This method creates a new instance of DefaultData which has no real type | |
100 * set. | |
101 * | |
102 * @param ele The Data element. | |
103 * @param name The name of the Data instance. | |
104 * | |
105 * @return an instance of DefaultData. | |
106 */ | |
107 protected static Data createDefaultData(Element ele, String name, String label) { | |
108 logger.debug("Create new DefaultData"); | |
109 return new DefaultData(name, label, "default", extractDataItems(ele)); | |
110 } | |
111 | |
112 | |
113 /** | |
114 * This method creates a new instance of StringData which has a type | |
115 * "string" set. | |
116 * | |
117 * @param ele The Data element. | |
118 * @param name The name of the Data instance. | |
119 * | |
120 * @return an instance of StringData. | |
121 */ | |
122 protected static Data createStringData(Element ele, String name, String label) { | |
123 return new StringData(name, label, extractDataItems(ele)); | |
124 } | |
125 | |
126 | |
127 /** | |
128 * This method creates a new instance of DefaultData which has a type | |
129 * "integer" set. | |
130 * | |
131 * @param ele The Data element. | |
132 * @param name The name of the Data instance. | |
133 * | |
134 * @return an instance of IntegerData. | |
135 */ | |
136 protected static Data createIntegerData(Element ele, String name, String label) { | |
137 return new IntegerData(name, label, extractDataItems(ele)); | |
138 } | |
139 | |
140 | |
141 /** | |
142 * This method creates a new instance of StringOptionsData which has a type | |
143 * "options" set. | |
144 * | |
145 * @param ele The Data element. | |
146 * @param name The name of the Data instance. | |
147 * | |
148 * @return an instance of StringOptionsData. | |
149 */ | |
150 protected static Data createStringOptionsData(Element ele, String name, String label) { | |
151 return new StringOptionsData(name, label, extractDataItems(ele)); | |
152 } | |
153 | |
154 | |
155 /** | |
156 * This method creates a new instance of DefaultData which has a type | |
157 * "intoptions" set. | |
158 * | |
159 * @param ele The Data element. | |
160 * @param name The name of the Data instance. | |
161 * | |
162 * @return an instance of IntegerOptionsData. | |
163 */ | |
164 protected static Data createIntegerOptionsData(Element ele, String name, String label) { | |
165 return new IntegerOptionsData(name, label, extractDataItems(ele)); | |
166 } | |
167 | |
168 | |
169 /** | |
170 * This method creates a new instance of DefaultData which has a type | |
171 * "intrange" set. | |
172 * | |
173 * @param ele The Data element. | |
174 * @param name The name of the Data instance. | |
175 * | |
176 * @return an instance of IntegerRangeData. | |
177 */ | |
178 protected static Data createIntegerRangeData(Element ele, String name, String label) { | |
179 DataItem[] items = extractDataItems(ele); | |
180 String rawValue = items[0].getStringValue(); | |
181 | |
182 String[] minmax = rawValue.split(";"); | |
183 | |
184 return new IntegerRangeData( | |
185 name, | |
186 label, | |
187 Integer.valueOf(minmax[0]), | |
188 Integer.valueOf(minmax[1])); | |
189 } | |
190 | |
191 | |
192 /** | |
193 * This method creates a new instance of DefaultData which has a type | |
194 * "integerarray" set. | |
195 * | |
196 * @param ele The Data element. | |
197 * @param name The name of the Data instance. | |
198 * | |
199 * @return an instance of IntegerArrayData. | |
200 */ | |
201 protected static Data createIntegerArrayData(Element ele, String name, String label) { | |
202 IntDataItem[] items = extractIntDataItems(ele); | |
203 return new IntegerArrayData(name, label, items); | |
204 } | |
205 | |
206 | |
207 /** | |
208 * This method creates a new instance of DefaultData which has a type | |
209 * "doublearray" set. | |
210 * | |
211 * @param ele The Data element. | |
212 * @param name The name of the Data instance. | |
213 * | |
214 * @return an instance of DoubleArrayData. | |
215 */ | |
216 protected static Data createDoubleArrayData(Element ele, String name, String label) { | |
217 DataItem[] items = extractDataItems(ele); | |
218 String rawValue = items[0].getStringValue(); | |
219 | |
220 String[] values = rawValue.split(";"); | |
221 double[] doubles = new double[values.length]; | |
222 | |
223 for (int i = 0; i < values.length; i++) { | |
224 try { | |
225 doubles[i] = Double.valueOf(values[i]); | |
226 } | |
227 catch (NumberFormatException nfe) { | |
228 logger.warn("Error while parsing DoubleArrayData: " + nfe); | |
229 } | |
230 } | |
231 | |
232 return new DoubleArrayData(name, label, doubles); | |
233 } | |
234 | |
235 | |
236 /** | |
237 * This method extracts the art:item elements placed under <i>elements</i>. | |
238 * | |
239 * @param element A data node that contains items. | |
240 * | |
241 * @return a list of DataItems. | |
242 */ | |
243 protected static DataItem[] extractDataItems(Element element) { | |
244 NodeList itemList = (NodeList) XMLUtils.xpath( | |
245 element, | |
246 "art:item", | |
247 XPathConstants.NODESET, | |
248 ArtifactNamespaceContext.INSTANCE); | |
249 | |
250 if (itemList == null || itemList.getLength() == 0) { | |
251 logger.debug("No data items found."); | |
252 return null; | |
253 } | |
254 | |
255 int count = itemList.getLength(); | |
256 | |
257 DataItem[] items = new DataItem[count]; | |
258 | |
259 logger.debug("There are " + count + " data items in element."); | |
260 | |
261 for (int i = 0; i < count; i++) { | |
262 Element tmp = (Element) itemList.item(i); | |
263 | |
264 String value = tmp.getAttributeNS(NS_URI, "value"); | |
265 String label = tmp.getAttributeNS(NS_URI, "label"); | |
266 | |
267 logger.debug("Found data item:"); | |
268 logger.debug(" label: " + label); | |
269 logger.debug(" value: " + value); | |
270 | |
271 items[i] = new DefaultDataItem(label, label, value); | |
272 } | |
273 | |
274 return items; | |
275 } | |
276 | |
277 | |
278 /** | |
279 * This method extracts the art:item elements placed under <i>elements</i>. | |
280 * | |
281 * @param element A data node that contains items. | |
282 * | |
283 * @return a list of DataItems. | |
284 */ | |
285 protected static IntDataItem[] extractIntDataItems(Element element) { | |
286 NodeList itemList = (NodeList) XMLUtils.xpath( | |
287 element, | |
288 "art:item", | |
289 XPathConstants.NODESET, | |
290 ArtifactNamespaceContext.INSTANCE); | |
291 | |
292 if (itemList == null || itemList.getLength() == 0) { | |
293 logger.debug("No old data items found."); | |
294 return null; | |
295 } | |
296 | |
297 int count = itemList.getLength(); | |
298 | |
299 IntDataItem[] items = new IntDataItem[count]; | |
300 | |
301 for (int i = 0; i < count; i++) { | |
302 Element tmp = (Element) itemList.item(i); | |
303 | |
304 String value = tmp.getAttributeNS(NS_URI, "value"); | |
305 String label = tmp.getAttributeNS(NS_URI, "label"); | |
306 | |
307 try { | |
308 int data = Integer.parseInt(value); | |
309 items[i] = new IntDataItem(label, label, data); | |
310 } | |
311 catch(NumberFormatException nfe) { | |
312 logger.debug(nfe, nfe); | |
313 } | |
314 } | |
315 return items; | |
316 } | |
317 | |
318 /** | |
319 * This method creates a new instance of LongRangeData which has a type | |
320 * "longrange" set. | |
321 * | |
322 * @param ele The Data element. | |
323 * @param name The name of the Data instance. | |
324 * | |
325 * @return an instance of IntegerRangeData. | |
326 */ | |
327 protected static Data createLongRangeData(Element ele, String name, String label) { | |
328 DataItem[] items = extractDataItems(ele); | |
329 String rawValue = items[0].getStringValue(); | |
330 | |
331 String[] minmax = rawValue.split(";"); | |
332 | |
333 return new LongRangeData( | |
334 name, | |
335 label, | |
336 Long.valueOf(minmax[0]), | |
337 Long.valueOf(minmax[1])); | |
338 } | |
339 | |
340 } | |
341 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |