KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc {
34
35     /**
36      * Default separator for splitting a String: a comma (",")
37      */

38     public static final String JavaDoc DEFAULT_SEPARATOR = ",";
39
40     private final String JavaDoc separator;
41
42
43     /**
44      * Creates a new instance of the {@link StringArrayPropertyEditor} with the
45      * {@link #DEFAULT_SEPARATOR}.
46      */

47     public StringArrayPropertyEditor() {
48         this.separator = DEFAULT_SEPARATOR;
49     }
50
51     /**
52      * Creates a new instance of the {@link StringArrayPropertyEditor} with
53      * the given separator.
54      * @param separator the separator to use for splitting a {@link String}
55      */

56     public StringArrayPropertyEditor(String JavaDoc separator) {
57         this.separator = separator;
58     }
59
60
61     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
62         String JavaDoc[] array = StringUtils.delimitedListToStringArray(text, this.separator);
63         setValue(array);
64     }
65
66     public String JavaDoc getAsText() {
67         String JavaDoc[] array = (String JavaDoc[]) this.getValue();
68         return StringUtils.arrayToDelimitedString(array, this.separator);
69     }
70
71 }
72
Popular Tags