KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > convert > support > AbstractConverter


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.binding.convert.support;
17
18 import org.springframework.binding.convert.ConversionContext;
19 import org.springframework.binding.convert.ConversionException;
20 import org.springframework.binding.convert.Converter;
21
22 /**
23  * Base class for converters provided as a convenience to implementors.
24  *
25  * @author Keith Donald
26  */

27 public abstract class AbstractConverter implements Converter {
28
29     /**
30      * Convenience convert method that converts the provided source to the first
31      * target object supported by this converter. Useful when a converter only
32      * supports conversion to a single target.
33      * @param source The source to convert
34      * @return the converted object
35      * @throws ConversionException a exception occured converting the source
36      * value
37      */

38     public Object JavaDoc convert(Object JavaDoc source) throws ConversionException {
39         return convert(source, getTargetClasses()[0], null);
40     }
41
42     /**
43      * Convenience convert method that converts the provided source to the
44      * target class specified with an empty conversion context.
45      * @param source The source to convert
46      * @param targetClass the target class to convert the source to, must be one
47      * of the supported <code>targetClasses</code>
48      * @return the converted object
49      * @throws ConversionException a exception occured converting the source
50      * value
51      */

52     public Object JavaDoc convert(Object JavaDoc source, Class JavaDoc targetClass) throws ConversionException {
53         return convert(source, targetClass, null);
54     }
55
56     /**
57      * Convenience convert method that converts the provided source to the first
58      * target object supported by this converter. Useful when a converter only
59      * supports conversion to a single target.
60      * @param source The source to convert
61      * @param context the conversion context, useful for influencing the
62      * behavior of the converter.
63      * @return the converted object
64      * @throws ConversionException a exception occured converting the source
65      * value
66      */

67     public Object JavaDoc convert(Object JavaDoc source, ConversionContext context) throws ConversionException {
68         return convert(source, getTargetClasses()[0], context);
69     }
70
71     public Object JavaDoc convert(Object JavaDoc source, Class JavaDoc targetClass, ConversionContext context) throws ConversionException {
72         try {
73             return doConvert(source, targetClass, context);
74         } catch (ConversionException e) {
75             throw e;
76         } catch (Throwable JavaDoc e) {
77             if (targetClass == null) {
78                 targetClass = getTargetClasses()[0];
79             }
80             throw new ConversionException(source, targetClass, e);
81         }
82     }
83
84     /**
85      * Template method subclasses should override to actually perform the type
86      * conversion.
87      * @param source the source to convert from
88      * @param targetClass the target type to convert to
89      * @param context an optional conversion context that may be used to
90      * influence the conversion process, guaranteed to be non-null.
91      * @return the converted source value
92      * @throws Exception an exception occured, will be wrapped in a conversion
93      * exception if necessary
94      */

95     protected abstract Object JavaDoc doConvert(Object JavaDoc source, Class JavaDoc targetClass, ConversionContext context) throws Exception JavaDoc;
96
97 }
Popular Tags