1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.integration.commons.beanutils;
17
18 import java.util.Locale;
19
20 import net.sf.morph.transform.Converter;
21 import net.sf.morph.transform.DecoratedConverter;
22 import net.sf.morph.transform.TransformationException;
23 import net.sf.morph.transform.transformers.BaseTransformer;
24
25 import org.apache.commons.beanutils.ConvertUtilsBean;
26
27 /**
28 * A converter which delegates to Commons BeanUtils.
29 *
30 * @author Matt Sgarlata
31 * @since October 25, 2004
32 */
33 public class BeanUtilsConverter extends BaseTransformer implements Converter, DecoratedConverter {
34
35
36 /**
37 * Source-destination classes.
38 */
39 public static final Class[] ALL_OBJECTS = new Class[] { Object.class };
40
41 private ConvertUtilsBean convertUtilsBean;
42
43 private org.apache.commons.beanutils.Converter getConverter(Class destinationClass) {
44 return getConvertUtilsBean().lookup(destinationClass);
45 }
46
47 /**
48 * {@inheritDoc}
49 */
50 protected boolean isTransformableImpl(Class destinationType, Class sourceType) {
51 return getConverter(destinationType) != null;
52 }
53
54 /**
55 * {@inheritDoc}
56 */
57 protected Object convertImpl(Class destinationClass, Object source, Locale locale)
58 throws TransformationException {
59 org.apache.commons.beanutils.Converter c =
60 getConvertUtilsBean().lookup(destinationClass);
61 return c.convert(destinationClass, source);
62 }
63
64 /**
65 * Returns the ConvertUtilsBean set with the setConvertUtilsBean method, or
66 * the default ConvertUtilsBean instance that is used by the static methods
67 * in ConvertUtils.
68 * @return the ConvertUtilsBean set with the setConvertUtilsBean method, or
69 * the default ConvertUtilsBean instance that is used by the static methods
70 * in ConvertUtils.
71 */
72 public ConvertUtilsBean getConvertUtilsBean() {
73 if (convertUtilsBean == null) {
74 setConvertUtilsBean(MyConvertUtilsBeanHack.getInstance());
75 }
76 return convertUtilsBean;
77 }
78
79 /**
80 * Set the ConvertUtilsBean to use.
81 * @param convertUtilsBean
82 */
83 public void setConvertUtilsBean(ConvertUtilsBean convertUtilsBean) {
84 this.convertUtilsBean = convertUtilsBean;
85 }
86
87 /**
88 * A hack that allows us to directly access the default ConvertUtilsBean
89 * instance that is used by the static methods in ConvertUtils
90 */
91 private static class MyConvertUtilsBeanHack extends ConvertUtilsBean {
92 public static ConvertUtilsBean getInstance() {
93 return ConvertUtilsBean.getInstance();
94 }
95 }
96
97 /**
98 * Learn whether the given source-destination combination is "naively" convertible.
99 * @param destinationClass
100 * @param sourceClass
101 * @return boolean
102 */
103 protected boolean isNaivelyConvertible(Class destinationClass, Class sourceClass) {
104 return true;
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 protected boolean isWrappingRuntimeExceptions() {
111
112
113 return false;
114 }
115
116 /**
117 * {@inheritDoc}
118 */
119 public Class[] getSourceClassesImpl() {
120 return ALL_OBJECTS;
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 public Class[] getDestinationClassesImpl() {
127 return ALL_OBJECTS;
128 }
129
130 }