1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.mina.integration.ognl;
18
19 import java.beans.PropertyEditor;
20 import java.lang.reflect.Member;
21 import java.util.Map;
22
23 import ognl.OgnlContext;
24 import ognl.TypeConverter;
25
26 import org.apache.mina.integration.beans.PropertyEditorFactory;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class PropertyTypeConverter implements TypeConverter {
44
45 @SuppressWarnings("unchecked")
46 public Object convertValue(Map ctx, Object target, Member member, String attrName, Object value, Class toType) {
47 if (value == null) {
48 return null;
49 }
50
51 if (attrName == null) {
52
53
54 OgnlContext ognlCtx = (OgnlContext) ctx;
55 attrName = ognlCtx.getCurrentNode().toString().replaceAll("[\" \']+", "");
56 }
57
58 if (toType.isAssignableFrom(value.getClass())) {
59 return value;
60 }
61
62 PropertyEditor e1 = getPropertyEditor(target.getClass(), attrName, value.getClass());
63 if (e1 == null) {
64 throw new IllegalArgumentException("Can't convert " + value.getClass().getSimpleName() + " to "
65 + String.class.getSimpleName());
66 }
67 e1.setValue(value);
68
69 PropertyEditor e2 = getPropertyEditor(target.getClass(), attrName, toType);
70 if (e2 == null) {
71 throw new IllegalArgumentException("Can't convert " + String.class.getSimpleName() + " to "
72 + toType.getSimpleName());
73 }
74
75 e2.setAsText(e1.getAsText());
76 return e2.getValue();
77 }
78
79 protected PropertyEditor getPropertyEditor(Class<?> type, String attrName, Class<?> attrType) {
80 return PropertyEditorFactory.getInstance(attrType);
81 }
82 }