01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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 {
36:
37: private final ClassLoader classLoader;
38:
39: /**
40: * Create a default <code>ClassEditor</code>, using the thread
41: * context <code>ClassLoader</code>.
42: */
43: public ClassArrayEditor() {
44: this (null);
45: }
46:
47: /**
48: * Create a default <code>ClassArrayEditor</code>, using the given
49: * <code>ClassLoader</code>.
50: * @param classLoader the <code>ClassLoader</code> to use
51: * (or pass <code>null</code> for the thread context <code>ClassLoader</code>)
52: */
53: public ClassArrayEditor(ClassLoader classLoader) {
54: this .classLoader = classLoader != null ? classLoader
55: : ClassUtils.getDefaultClassLoader();
56: }
57:
58: public void setAsText(String text) throws IllegalArgumentException {
59: if (StringUtils.hasText(text)) {
60: String[] classNames = StringUtils
61: .commaDelimitedListToStringArray(text);
62: Class[] classes = new Class[classNames.length];
63: for (int i = 0; i < classNames.length; i++) {
64: String className = classNames[i].trim();
65: classes[i] = ClassUtils.resolveClassName(className,
66: this .classLoader);
67: }
68: setValue(classes);
69: } else {
70: setValue(null);
71: }
72: }
73:
74: public String getAsText() {
75: Class[] classes = (Class[]) getValue();
76: if (classes == null || classes.length == 0) {
77: return "";
78: }
79: return toCommaDelimitedString(classes);
80: }
81:
82: private static String toCommaDelimitedString(Class[] classes) {
83: StringBuffer buffer = new StringBuffer();
84: for (int i = 0; i < classes.length; ++i) {
85: if (i > 0) {
86: buffer.append(",");
87: }
88: buffer.append(ClassUtils.getQualifiedName(classes[i]));
89: }
90: return buffer.toString();
91: }
92:
93: }
|