KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > ClassArrayEditor


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
17 package org.springframework.beans.propertyeditors;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20
21 import org.springframework.util.ClassUtils;
22 import org.springframework.util.StringUtils;
23
24 /**
25  * Property editor for an array of {@link java.lang.Class Classes}, to enable
26  * the direct population of a <code>Class[]</code> property without having to
27  * use a <code>String</code> class name property as bridge.
28  *
29  * <p>Also supports "java.lang.String[]"-style array class names, in contrast
30  * to the standard {@link Class#forName(String)} method.
31  *
32  * @author Rob Harrop
33  * @since 2.0
34  */

35 public class ClassArrayEditor extends PropertyEditorSupport JavaDoc {
36
37     private final ClassLoader JavaDoc classLoader;
38
39
40     /**
41      * Create a default <code>ClassEditor</code>, using the thread
42      * context <code>ClassLoader</code>.
43      */

44     public ClassArrayEditor() {
45         this(null);
46     }
47
48     /**
49      * Create a default <code>ClassArrayEditor</code>, using the given
50      * <code>ClassLoader</code>.
51      * @param classLoader the <code>ClassLoader</code> to use
52      * (or pass <code>null</code> for the thread context <code>ClassLoader</code>)
53      */

54     public ClassArrayEditor(ClassLoader JavaDoc classLoader) {
55         this.classLoader = classLoader != null
56                 ? classLoader : ClassUtils.getDefaultClassLoader();
57     }
58
59
60     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
61         if (StringUtils.hasText(text)) {
62             String JavaDoc[] classNames = StringUtils.commaDelimitedListToStringArray(text);
63             Class JavaDoc[] classes = new Class JavaDoc[classNames.length];
64             for (int i = 0; i < classNames.length; i++) {
65                 String JavaDoc className = classNames[i].trim();
66                 classes[i] = ClassUtils.resolveClassName(className, this.classLoader);
67             }
68             setValue(classes);
69         }
70         else {
71             setValue(null);
72         }
73     }
74
75     public String JavaDoc getAsText() {
76         Class JavaDoc[] classes = (Class JavaDoc[]) getValue();
77         if (classes == null || classes.length == 0) {
78             return "";
79         }
80         return toCommaDelimitedString(classes);
81     }
82
83
84     private static String JavaDoc toCommaDelimitedString(Class JavaDoc[] classes) {
85         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
86         for (int i = 0; i < classes.length; ++i) {
87             if (i > 0) {
88                 buffer.append(",");
89             }
90             buffer.append(ClassUtils.getQualifiedName(classes[i]));
91         }
92         return buffer.toString();
93     }
94
95 }
96
Popular Tags