001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans.propertyeditors;
018
019import java.beans.PropertyEditorSupport;
020
021import org.springframework.util.ObjectUtils;
022import org.springframework.util.StringUtils;
023
024/**
025 * Custom {@link java.beans.PropertyEditor} for String arrays.
026 *
027 * <p>Strings must be in CSV format, with a customizable separator.
028 * By default values in the result are trimmed of whitespace.
029 *
030 * @author Rod Johnson
031 * @author Juergen Hoeller
032 * @author Dave Syer
033 * @see org.springframework.util.StringUtils#delimitedListToStringArray
034 * @see org.springframework.util.StringUtils#arrayToDelimitedString
035 */
036public class StringArrayPropertyEditor extends PropertyEditorSupport {
037
038        /**
039         * Default separator for splitting a String: a comma (",")
040         */
041        public static final String DEFAULT_SEPARATOR = ",";
042
043
044        private final String separator;
045
046        private final String charsToDelete;
047
048        private final boolean emptyArrayAsNull;
049
050        private final boolean trimValues;
051
052
053        /**
054         * Create a new StringArrayPropertyEditor with the default separator
055         * (a comma).
056         * <p>An empty text (without elements) will be turned into an empty array.
057         */
058        public StringArrayPropertyEditor() {
059                this(DEFAULT_SEPARATOR, null, false);
060        }
061
062        /**
063         * Create a new StringArrayPropertyEditor with the given separator.
064         * <p>An empty text (without elements) will be turned into an empty array.
065         * @param separator the separator to use for splitting a {@link String}
066         */
067        public StringArrayPropertyEditor(String separator) {
068                this(separator, null, false);
069        }
070
071        /**
072         * Create a new StringArrayPropertyEditor with the given separator.
073         * @param separator the separator to use for splitting a {@link String}
074         * @param emptyArrayAsNull {@code true} if an empty String array
075         * is to be transformed into {@code null}
076         */
077        public StringArrayPropertyEditor(String separator, boolean emptyArrayAsNull) {
078                this(separator, null, emptyArrayAsNull);
079        }
080
081        /**
082         * Create a new StringArrayPropertyEditor with the given separator.
083         * @param separator the separator to use for splitting a {@link String}
084         * @param emptyArrayAsNull {@code true} if an empty String array
085         * is to be transformed into {@code null}
086         * @param trimValues {@code true} if the values in the parsed arrays
087         * are to be trimmed of whitespace (default is true).
088         */
089        public StringArrayPropertyEditor(String separator, boolean emptyArrayAsNull, boolean trimValues) {
090                this(separator, null, emptyArrayAsNull, trimValues);
091        }
092
093        /**
094         * Create a new StringArrayPropertyEditor with the given separator.
095         * @param separator the separator to use for splitting a {@link String}
096         * @param charsToDelete a set of characters to delete, in addition to
097         * trimming an input String. Useful for deleting unwanted line breaks:
098         * e.g. "\r\n\f" will delete all new lines and line feeds in a String.
099         * @param emptyArrayAsNull {@code true} if an empty String array
100         * is to be transformed into {@code null}
101         */
102        public StringArrayPropertyEditor(String separator, String charsToDelete, boolean emptyArrayAsNull) {
103                this(separator, charsToDelete, emptyArrayAsNull, true);
104        }
105
106        /**
107         * Create a new StringArrayPropertyEditor with the given separator.
108         * @param separator the separator to use for splitting a {@link String}
109         * @param charsToDelete a set of characters to delete, in addition to
110         * trimming an input String. Useful for deleting unwanted line breaks:
111         * e.g. "\r\n\f" will delete all new lines and line feeds in a String.
112         * @param emptyArrayAsNull {@code true} if an empty String array
113         * is to be transformed into {@code null}
114         * @param trimValues {@code true} if the values in the parsed arrays
115         * are to be trimmed of whitespace (default is true).
116         */
117        public StringArrayPropertyEditor(String separator, String charsToDelete, boolean emptyArrayAsNull, boolean trimValues) {
118                this.separator = separator;
119                this.charsToDelete = charsToDelete;
120                this.emptyArrayAsNull = emptyArrayAsNull;
121                this.trimValues = trimValues;
122        }
123
124        @Override
125        public void setAsText(String text) throws IllegalArgumentException {
126                String[] array = StringUtils.delimitedListToStringArray(text, this.separator, this.charsToDelete);
127                if (trimValues) {
128                        array = StringUtils.trimArrayElements(array);
129                }
130                if (this.emptyArrayAsNull && array.length == 0) {
131                        setValue(null);
132                }
133                else {
134                        setValue(array);
135                }
136        }
137
138        @Override
139        public String getAsText() {
140                return StringUtils.arrayToDelimitedString(ObjectUtils.toObjectArray(getValue()), this.separator);
141        }
142
143}