RESOLVED - issue SPR-6195

This commit is contained in:
David Syer
2009-10-27 13:41:22 +00:00
parent a29253f2ca
commit 4be237dd84
2 changed files with 59 additions and 2 deletions

View File

@@ -24,10 +24,12 @@ import org.springframework.util.StringUtils;
/**
* Custom {@link java.beans.PropertyEditor} for String arrays.
*
* <p>Strings must be in CSV format, with a customizable separator.
* <p>Strings must be in CSV format, with a customizable separator. By default values in the result are trimmed
* of whitespace.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Dave Syer
* @see org.springframework.util.StringUtils#delimitedListToStringArray
* @see org.springframework.util.StringUtils#arrayToDelimitedString
*/
@@ -44,6 +46,8 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
private final String charsToDelete;
private final boolean emptyArrayAsNull;
private final boolean trimValues;
/**
@@ -74,6 +78,18 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
this(separator, null, emptyArrayAsNull);
}
/**
* Create a new StringArrayPropertyEditor with the given separator.
* @param separator the separator to use for splitting a {@link String}
* @param emptyArrayAsNull <code>true</code> if an empty String array
* is to be transformed into <code>null</code>
* @param trimValues <code>true</code> if the values in the parsed arrays
* are to be be trimmed of whitespace (default is true).
*/
public StringArrayPropertyEditor(String separator, boolean emptyArrayAsNull, boolean trimValues) {
this(separator, null, emptyArrayAsNull, trimValues);
}
/**
* Create a new StringArrayPropertyEditor with the given separator.
* @param separator the separator to use for splitting a {@link String}
@@ -84,15 +100,33 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
* is to be transformed into <code>null</code>
*/
public StringArrayPropertyEditor(String separator, String charsToDelete, boolean emptyArrayAsNull) {
this(separator, charsToDelete, emptyArrayAsNull, true);
}
/**
* Create a new StringArrayPropertyEditor with the given separator.
* @param separator the separator to use for splitting a {@link String}
* @param charsToDelete a set of characters to delete, in addition to
* trimming an input String. Useful for deleting unwanted line breaks:
* e.g. "\r\n\f" will delete all new lines and line feeds in a String.
* @param emptyArrayAsNull <code>true</code> if an empty String array
* is to be transformed into <code>null</code>
* @param trimValues <code>true</code> if the values in the parsed arrays
* are to be be trimmed of whitespace (default is true).
*/
public StringArrayPropertyEditor(String separator, String charsToDelete, boolean emptyArrayAsNull, boolean trimValues) {
this.separator = separator;
this.charsToDelete = charsToDelete;
this.emptyArrayAsNull = emptyArrayAsNull;
this.trimValues = trimValues;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] array = StringUtils.delimitedListToStringArray(text, this.separator, this.charsToDelete);
if (trimValues) {
array = StringUtils.trimArrayElements(array);
}
if (this.emptyArrayAsNull && array.length == 0) {
setValue(null);
}