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.StringUtils;
22:
23: /**
24: * Custom {@link java.beans.PropertyEditor} for {@link String String[]} arrays.
25: *
26: * <p>Strings must be in CSV format, with a customizable separator.
27: *
28: * @author Rod Johnson
29: * @author Juergen Hoeller
30: * @see org.springframework.util.StringUtils#delimitedListToStringArray
31: * @see org.springframework.util.StringUtils#arrayToDelimitedString
32: */
33: public class StringArrayPropertyEditor extends PropertyEditorSupport {
34:
35: /**
36: * Default separator for splitting a String: a comma (",")
37: */
38: public static final String DEFAULT_SEPARATOR = ",";
39:
40: private final String separator;
41:
42: /**
43: * Creates a new instance of the {@link StringArrayPropertyEditor} with the
44: * {@link #DEFAULT_SEPARATOR}.
45: */
46: public StringArrayPropertyEditor() {
47: this .separator = DEFAULT_SEPARATOR;
48: }
49:
50: /**
51: * Creates a new instance of the {@link StringArrayPropertyEditor} with
52: * the given separator.
53: * @param separator the separator to use for splitting a {@link String}
54: */
55: public StringArrayPropertyEditor(String separator) {
56: this .separator = separator;
57: }
58:
59: public void setAsText(String text) throws IllegalArgumentException {
60: String[] array = StringUtils.delimitedListToStringArray(text,
61: this .separator);
62: setValue(array);
63: }
64:
65: public String getAsText() {
66: String[] array = (String[]) this.getValue();
67: return StringUtils
68: .arrayToDelimitedString(array, this.separator);
69: }
70:
71: }
|