KickJava   Java API By Example, From Geeks To Geeks.

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


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.ConversionExecutor;
19 import org.springframework.binding.convert.ConversionService;
20 import org.springframework.binding.expression.Expression;
21
22 /**
23  * Base class for converters that use other converters to convert things, thus
24  * they are conversion-service aware.
25  *
26  * @author Keith Donald
27  */

28 public abstract class ConversionServiceAwareConverter extends AbstractConverter implements ConversionServiceAware {
29
30     /**
31      * The conversion service this converter is aware of.
32      */

33     private ConversionService conversionService;
34
35     protected ConversionServiceAwareConverter() {
36
37     }
38
39     protected ConversionServiceAwareConverter(ConversionService conversionService) {
40         setConversionService(conversionService);
41     }
42
43     public ConversionService getConversionService() {
44         if (conversionService == null) {
45             throw new IllegalStateException JavaDoc("Conversion service not yet set: set it first before calling this method");
46         }
47         return conversionService;
48     }
49
50     public void setConversionService(ConversionService conversionService) {
51         this.conversionService = conversionService;
52     }
53
54     /**
55      * Returns a conversion executor capable of converting string objects to the
56      * specified target class.
57      * @param targetClass the target class
58      * @return the conversion executor
59      */

60     protected ConversionExecutor fromStringTo(Class JavaDoc targetClass) {
61         return getConversionService().getConversionExecutor(String JavaDoc.class, targetClass);
62     }
63
64     /**
65      * Returns a conversion executor capable of converting string objects to the
66      * target class aliased by the provided alias.
67      * @param targetAlias the target class alias, e.g "long" or "float"
68      * @return the conversion executor, or <code>null</code> if no suitable
69      * converter exists for alias
70      */

71     protected ConversionExecutor fromStringToAliased(String JavaDoc targetAlias) {
72         return getConversionService().getConversionExecutorByTargetAlias(String JavaDoc.class, targetAlias);
73     }
74
75     /**
76      * Returns a conversion executor capable of converting objects from one
77      * class to another.
78      * @param sourceClass the source class to convert from
79      * @param targetClass the target class to convert to
80      * @return the conversion executor
81      */

82     protected ConversionExecutor converterFor(Class JavaDoc sourceClass, Class JavaDoc targetClass) {
83         return getConversionService().getConversionExecutor(sourceClass, targetClass);
84     }
85
86     /**
87      * Helper that parsers the given expression string into an expression, using
88      * the installed String->Expression converter.
89      * @param expressionString the expression string to parse
90      * @return the parsed, evaluatable expression
91      */

92     protected Expression parseExpression(String JavaDoc expressionString) {
93         return (Expression)converterFor(String JavaDoc.class, Expression.class).execute(expressionString);
94     }
95 }
Popular Tags