1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.dataprovider.bean;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.xulux.api.dataprovider.IConverter;
26 import org.xulux.api.dataprovider.IDataProvider;
27 import org.xulux.api.dataprovider.IMapping;
28 import org.xulux.utils.ClassLoaderUtils;
29
30
31
32
33
34
35
36
37 public final class BeanDataProvider implements IDataProvider {
38
39
40
41
42 private HashMap mappings;
43
44
45
46
47 private Class baseClass;
48
49
50
51
52
53 private ArrayList mappingCache;
54
55
56
57 private int mappingDepth = 0;
58
59
60
61
62 private HashMap converters;
63
64
65
66
67 public BeanDataProvider() {
68 }
69
70
71
72
73
74 public IMapping getMapping(String name) {
75 IMapping mapping = null;
76 if (mappings != null) {
77 mapping = (BeanMapping) mappings.get(name);
78 }
79 if (mapping == null) {
80 mapping = getMapping(ClassLoaderUtils.getClass(name));
81 }
82 return mapping;
83 }
84
85
86
87
88
89
90
91
92 public Map getMappings() {
93 if (mappings == null) {
94 return new HashMap();
95 }
96 return (HashMap) mappings.clone();
97 }
98
99
100
101
102 public void addMapping(IMapping mapping) {
103 if (mappings == null) {
104 mappings = new HashMap();
105 }
106 if (mappingCache == null) {
107 mappingCache = new ArrayList();
108 }
109 BeanMapping beanMapping = (BeanMapping) mapping;
110 beanMapping.setDataProvider(this);
111 mappingCache.add(beanMapping.getBean());
112 mappingDepth++;
113 beanMapping.discover();
114 mappings.put(beanMapping.getName(), beanMapping);
115 mappingDepth--;
116 if (mappingDepth == 0) {
117 mappingCache = null;
118 }
119 }
120
121
122
123
124
125 public IMapping getMapping(Class clazz) {
126 if (clazz == null) {
127 return null;
128 }
129 return getMapping(clazz, false);
130 }
131
132
133
134
135
136
137
138
139
140
141 public IMapping getMapping(Object object) {
142 if (object == null) {
143 return null;
144 }
145 if (object instanceof String) {
146 return getMapping((String) object);
147 } else if (object instanceof Class) {
148 return getMapping((Class) object);
149 }
150 return getMapping(object.getClass());
151 }
152
153
154
155
156
157
158
159
160 public BeanMapping getMapping(Class clazz, boolean newMapping) {
161 String name = null;
162 if (newMapping) {
163 name = getPossibleMappingName(clazz);
164 } else {
165 name = getPlainBeanName(clazz);
166 }
167 return getMapping(clazz, name);
168 }
169
170
171
172
173
174
175 public BeanMapping getMapping(Class clazz, String preferredName) {
176
177
178
179
180
181 BeanMapping mapping = (BeanMapping) getMapping(preferredName);
182 if (mapping == null) {
183 mapping = new BeanMapping(preferredName);
184 mapping.setBean(clazz);
185 mapping.setDiscovery(true);
186 addMapping(mapping);
187 }
188 return mapping;
189 }
190
191
192
193
194
195
196
197
198 private BeanMapping createMapping(Object bean, String name) {
199
200 BeanMapping mapping = new BeanMapping(name);
201 return mapping;
202 }
203
204
205
206
207
208 public String getPlainBeanName(Class clazz) {
209 int pLength = clazz.getPackage().getName().length();
210 String mapName = clazz.getName().substring(pLength + 1);
211 return mapName;
212 }
213
214
215
216
217
218 public String getPossibleMappingName(Class clazz) {
219 String mapName = getPlainBeanName(clazz);
220 int i = 1;
221 if (getMapping(mapName) != null) {
222 while (true) {
223 if (getMapping(mapName + i) == null) {
224 mapName += i;
225 break;
226 }
227 i++;
228 }
229 }
230 return mapName;
231 }
232
233
234
235
236 public void initialize(Object object) {
237 if (object instanceof InputStream) {
238 initialize((InputStream) object);
239 } else {
240 System.out.println("Cannot initialize dictionary, object is not an InputStream");
241 }
242 }
243
244
245
246
247
248
249
250 public void initialize(InputStream stream) {
251 if (stream == null) {
252 return;
253 }
254
255 try {
256 stream.close();
257 } catch (IOException e) {
258 e.printStackTrace();
259 }
260 }
261
262
263
264
265 public Class getBaseClass() {
266 return baseClass;
267 }
268
269
270
271
272
273 public void setBaseClass(Class baseClass) {
274 this.baseClass = baseClass;
275 }
276
277
278
279
280 public void clearMappings() {
281 if (mappings != null) {
282 mappings.clear();
283 }
284 }
285
286
287
288
289
290 public void reset() {
291 clearMappings();
292 if (converters != null) {
293 converters.clear();
294 }
295 }
296
297
298
299
300
301
302
303
304
305 public boolean isInCache(Class clazz) {
306 if (mappingCache == null) {
307 return false;
308 }
309 return mappingCache.indexOf(clazz) != -1;
310 }
311
312
313
314
315
316 public List getCache() {
317 if (mappingCache != null) {
318 return (List) mappingCache.clone();
319 }
320 return null;
321 }
322
323
324
325
326 public void addConverter(Class clazz) {
327 if (converters == null) {
328 converters = new HashMap();
329 }
330 Object object = ClassLoaderUtils.getObjectFromClass(clazz);
331 if (object instanceof IConverter) {
332 IConverter c = (IConverter) object;
333 converters.put(c.getType(), c);
334 }
335
336
337
338
339
340
341
342
343 }
344
345
346
347
348
349
350
351
352 public void addConverter(String clazz) {
353 if (clazz == null) {
354 return;
355 }
356 Class clz = ClassLoaderUtils.getClass(clazz);
357 if (clz != null) {
358 addConverter(clz);
359 }
360
361
362
363
364
365 }
366
367
368
369
370
371
372
373 public IConverter getConverter(Object object) {
374 if (object != null) {
375 return getConverter(object.getClass());
376 }
377 return null;
378 }
379
380
381
382
383 public Map getConverters() {
384 return converters;
385 }
386
387
388
389
390
391
392 public IConverter getConverter(Class clazz) {
393 if (clazz != null && converters != null) {
394 return (IConverter) converters.get(clazz);
395 }
396 return null;
397 }
398
399
400
401
402 public void initialize() {
403 }
404
405
406
407
408
409 public Object setValue(Object mapping, String field, Object object, Object value) {
410 IMapping iMapping = getMapping(mapping);
411 if (iMapping != null) {
412 return iMapping.setValue(field, object, value);
413 }
414 return null;
415 }
416
417
418
419
420 public Object getValue(Object mapping, String field, Object object) {
421 return null;
422 }
423
424
425
426
427 public boolean needsPartValue() {
428 return false;
429 }
430
431
432
433
434 public void setProperty(String name, String value) {
435 if (name == null) {
436 return;
437 }
438 if (name.equalsIgnoreCase("baseclass")) {
439 setBaseClass(ClassLoaderUtils.getClass(value));
440 }
441 }
442
443
444
445
446 public IMapping createMapping(Object object) {
447 if (object == null) {
448 return null;
449 }
450 if (object instanceof String) {
451 BeanMapping mapping = new BeanMapping((String) object);
452 mapping.setDataProvider(this);
453 return mapping;
454 }
455 return null;
456 }
457
458 }