Browse Source

RESOLVED - issue SPR-6195

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2194 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
David Syer 16 years ago
parent
commit
df6e46b128
  1. 38
      org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditor.java
  2. 23
      org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java

38
org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditor.java

@ -24,10 +24,12 @@ import org.springframework.util.StringUtils; @@ -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 { @@ -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 { @@ -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 { @@ -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);
}

23
org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java

@ -37,6 +37,29 @@ public class StringArrayPropertyEditorTests extends TestCase { @@ -37,6 +37,29 @@ public class StringArrayPropertyEditorTests extends TestCase {
assertEquals("0,1,2", editor.getAsText());
}
public void testTrimByDefault() throws Exception {
StringArrayPropertyEditor editor = new StringArrayPropertyEditor();
editor.setAsText(" 0,1 , 2 ");
Object value = editor.getValue();
String[] array = (String[]) value;
for (int i = 0; i < array.length; ++i) {
assertEquals("" + i, array[i]);
}
assertEquals("0,1,2", editor.getAsText());
}
public void testNoTrim() throws Exception {
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",",false,false);
editor.setAsText(" 0,1 , 2 ");
Object value = editor.getValue();
String[] array = (String[]) value;
for (int i = 0; i < array.length; ++i) {
assertEquals(3, array[i].length());
assertEquals("" + i, array[i].trim());
}
assertEquals(" 0,1 , 2 ", editor.getAsText());
}
public void testWithCustomSeparator() throws Exception {
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(":");
editor.setAsText("0:1:2");

Loading…
Cancel
Save