From df6e46b128a57fa77bb3128832edfe4eba836f2c Mon Sep 17 00:00:00 2001 From: David Syer Date: Tue, 27 Oct 2009 13:41:22 +0000 Subject: [PATCH] RESOLVED - issue SPR-6195 git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2194 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../StringArrayPropertyEditor.java | 38 ++++++++++++++++++- .../StringArrayPropertyEditorTests.java | 23 +++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditor.java b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditor.java index 2249266cc30..a40cb3bfd92 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditor.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditor.java @@ -24,10 +24,12 @@ import org.springframework.util.StringUtils; /** * Custom {@link java.beans.PropertyEditor} for String arrays. * - *

Strings must be in CSV format, with a customizable separator. + *

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 true if an empty String array + * is to be transformed into null + * @param trimValues true 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 null */ 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 true if an empty String array + * is to be transformed into null + * @param trimValues true 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); } diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java index 3ef0b9efc7b..894a4d78788 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/StringArrayPropertyEditorTests.java @@ -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");