001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.beans.propertyeditors;
018:
019: import java.beans.PropertyEditorSupport;
020:
021: import org.springframework.util.StringUtils;
022:
023: /**
024: * Custom {@link java.beans.PropertyEditor} for {@link String String[]} arrays.
025: *
026: * <p>Strings must be in CSV format, with a customizable separator.
027: *
028: * @author Rod Johnson
029: * @author Juergen Hoeller
030: * @see org.springframework.util.StringUtils#delimitedListToStringArray
031: * @see org.springframework.util.StringUtils#arrayToDelimitedString
032: */
033: public class StringArrayPropertyEditor extends PropertyEditorSupport {
034:
035: /**
036: * Default separator for splitting a String: a comma (",")
037: */
038: public static final String DEFAULT_SEPARATOR = ",";
039:
040: private final String separator;
041:
042: private final String charsToDelete;
043:
044: private final boolean emptyArrayAsNull;
045:
046: /**
047: * Create a new StringArrayPropertyEditor with the default separator
048: * (a comma).
049: * <p>An empty text (without elements) will be turned into an empty array.
050: */
051: public StringArrayPropertyEditor() {
052: this (DEFAULT_SEPARATOR, null, false);
053: }
054:
055: /**
056: * Create a new StringArrayPropertyEditor with the given separator.
057: * <p>An empty text (without elements) will be turned into an empty array.
058: * @param separator the separator to use for splitting a {@link String}
059: */
060: public StringArrayPropertyEditor(String separator) {
061: this (separator, null, false);
062: }
063:
064: /**
065: * Create a new StringArrayPropertyEditor with the given separator.
066: * @param separator the separator to use for splitting a {@link String}
067: * @param emptyArrayAsNull <code>true</code> if an empty String array
068: * is to be transformed into <code>null</code>
069: */
070: public StringArrayPropertyEditor(String separator,
071: boolean emptyArrayAsNull) {
072: this (separator, null, emptyArrayAsNull);
073: }
074:
075: /**
076: * Create a new StringArrayPropertyEditor with the given separator.
077: * @param separator the separator to use for splitting a {@link String}
078: * @param charsToDelete a set of characters to delete, in addition to
079: * trimming an input String. Useful for deleting unwanted line breaks:
080: * e.g. "\r\n\f" will delete all new lines and line feeds in a String.
081: * @param emptyArrayAsNull <code>true</code> if an empty String array
082: * is to be transformed into <code>null</code>
083: */
084: public StringArrayPropertyEditor(String separator,
085: String charsToDelete, boolean emptyArrayAsNull) {
086: this .separator = separator;
087: this .charsToDelete = charsToDelete;
088: this .emptyArrayAsNull = emptyArrayAsNull;
089: }
090:
091: public void setAsText(String text) throws IllegalArgumentException {
092: String[] array = StringUtils.delimitedListToStringArray(text,
093: this .separator, this .charsToDelete);
094: if (this .emptyArrayAsNull && array.length == 0) {
095: setValue(null);
096: } else {
097: setValue(array);
098: }
099: }
100:
101: public String getAsText() {
102: String[] array = (String[]) getValue();
103: return StringUtils
104: .arrayToDelimitedString(array, this.separator);
105: }
106:
107: }
|