View Javadoc

1   /*
2    * Copyright 2004-2005, 2007 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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  	//not sure why public?
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 		// let runtime ConvertUtils exceptions be thrown if there are any (but
112 		// I'm pretty sure they're all checked so will have to be wrapped)
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 }