Skip to content

Commit 4be237d

Browse files
author
David Syer
committed
RESOLVED - issue SPR-6195
1 parent a29253f commit 4be237d

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
/**
2525
* Custom {@link java.beans.PropertyEditor} for String arrays.
2626
*
27-
* <p>Strings must be in CSV format, with a customizable separator.
27+
* <p>Strings must be in CSV format, with a customizable separator. By default values in the result are trimmed
28+
* of whitespace.
2829
*
2930
* @author Rod Johnson
3031
* @author Juergen Hoeller
32+
* @author Dave Syer
3133
* @see org.springframework.util.StringUtils#delimitedListToStringArray
3234
* @see org.springframework.util.StringUtils#arrayToDelimitedString
3335
*/
@@ -44,6 +46,8 @@ public class StringArrayPropertyEditor extends PropertyEditorSupport {
4446
private final String charsToDelete;
4547

4648
private final boolean emptyArrayAsNull;
49+
50+
private final boolean trimValues;
4751

4852

4953
/**
@@ -74,6 +78,18 @@ public StringArrayPropertyEditor(String separator, boolean emptyArrayAsNull) {
7478
this(separator, null, emptyArrayAsNull);
7579
}
7680

81+
/**
82+
* Create a new StringArrayPropertyEditor with the given separator.
83+
* @param separator the separator to use for splitting a {@link String}
84+
* @param emptyArrayAsNull <code>true</code> if an empty String array
85+
* is to be transformed into <code>null</code>
86+
* @param trimValues <code>true</code> if the values in the parsed arrays
87+
* are to be be trimmed of whitespace (default is true).
88+
*/
89+
public StringArrayPropertyEditor(String separator, boolean emptyArrayAsNull, boolean trimValues) {
90+
this(separator, null, emptyArrayAsNull, trimValues);
91+
}
92+
7793
/**
7894
* Create a new StringArrayPropertyEditor with the given separator.
7995
* @param separator the separator to use for splitting a {@link String}
@@ -84,15 +100,33 @@ public StringArrayPropertyEditor(String separator, boolean emptyArrayAsNull) {
84100
* is to be transformed into <code>null</code>
85101
*/
86102
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</code> if an empty String array
113+
* is to be transformed into <code>null</code>
114+
* @param trimValues <code>true</code> if the values in the parsed arrays
115+
* are to be be trimmed of whitespace (default is true).
116+
*/
117+
public StringArrayPropertyEditor(String separator, String charsToDelete, boolean emptyArrayAsNull, boolean trimValues) {
87118
this.separator = separator;
88119
this.charsToDelete = charsToDelete;
89120
this.emptyArrayAsNull = emptyArrayAsNull;
121+
this.trimValues = trimValues;
90122
}
91123

92-
93124
@Override
94125
public void setAsText(String text) throws IllegalArgumentException {
95126
String[] array = StringUtils.delimitedListToStringArray(text, this.separator, this.charsToDelete);
127+
if (trimValues) {
128+
array = StringUtils.trimArrayElements(array);
129+
}
96130
if (this.emptyArrayAsNull && array.length == 0) {
97131
setValue(null);
98132
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ public void testWithDefaultSeparator() throws Exception {
3737
assertEquals("0,1,2", editor.getAsText());
3838
}
3939

40+
public void testTrimByDefault() throws Exception {
41+
StringArrayPropertyEditor editor = new StringArrayPropertyEditor();
42+
editor.setAsText(" 0,1 , 2 ");
43+
Object value = editor.getValue();
44+
String[] array = (String[]) value;
45+
for (int i = 0; i < array.length; ++i) {
46+
assertEquals("" + i, array[i]);
47+
}
48+
assertEquals("0,1,2", editor.getAsText());
49+
}
50+
51+
public void testNoTrim() throws Exception {
52+
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(",",false,false);
53+
editor.setAsText(" 0,1 , 2 ");
54+
Object value = editor.getValue();
55+
String[] array = (String[]) value;
56+
for (int i = 0; i < array.length; ++i) {
57+
assertEquals(3, array[i].length());
58+
assertEquals("" + i, array[i].trim());
59+
}
60+
assertEquals(" 0,1 , 2 ", editor.getAsText());
61+
}
62+
4063
public void testWithCustomSeparator() throws Exception {
4164
StringArrayPropertyEditor editor = new StringArrayPropertyEditor(":");
4265
editor.setAsText("0:1:2");

0 commit comments

Comments
 (0)