KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Assert;
20 import org.springframework.util.ClassUtils;
21 import org.springframework.util.StringUtils;
22
23 /**
24  * Converts a textual representation of a class object to a <code>Class</code>
25  * instance.
26  * @author Keith Donald
27  */

28 public class TextToClass extends ConversionServiceAwareConverter {
29
30     private static final String JavaDoc ALIAS_PREFIX = "type:";
31
32     private static final String JavaDoc CLASS_PREFIX = "class:";
33
34     public Class JavaDoc[] getSourceClasses() {
35         return new Class JavaDoc[] { String JavaDoc.class };
36     }
37
38     public Class JavaDoc[] getTargetClasses() {
39         return new Class JavaDoc[] { Class JavaDoc.class };
40     }
41
42     protected Object JavaDoc doConvert(Object JavaDoc source, Class JavaDoc targetClass, ConversionContext context) throws Exception JavaDoc {
43         String JavaDoc text = (String JavaDoc)source;
44         if (StringUtils.hasText(text)) {
45             String JavaDoc classNameOrAlias = text.trim();
46             if (classNameOrAlias.startsWith(CLASS_PREFIX)) {
47                 return ClassUtils.forName(text.substring(CLASS_PREFIX.length()));
48             }
49             else if (classNameOrAlias.startsWith(ALIAS_PREFIX)) {
50                 Class JavaDoc clazz = getConversionService().getClassByAlias(text);
51                 Assert.notNull(clazz, "No class found associated with type alias '" + classNameOrAlias + "'");
52                 return clazz;
53             }
54             else {
55                 // try first an aliased based lookup
56
if (getConversionService() != null) {
57                     Class JavaDoc aliasedClass = getConversionService().getClassByAlias(text);
58                     if (aliasedClass != null) {
59                         return aliasedClass;
60                     }
61                 }
62                 // treat as a class name
63
return ClassUtils.forName(classNameOrAlias);
64             }
65         }
66         else {
67             return null;
68         }
69     }
70 }
Popular Tags