LocaleEditor and StringToLocaleConverter do not restrict variant part through validation (SPR-8637)
This commit is contained in:
parent
0c9e3fb3bd
commit
e2d9142c5a
|
@ -665,16 +665,11 @@ public abstract class StringUtils {
|
|||
* @return a corresponding <code>Locale</code> instance
|
||||
*/
|
||||
public static Locale parseLocaleString(String localeString) {
|
||||
for (int i = 0; i < localeString.length(); i++) {
|
||||
char ch = localeString.charAt(i);
|
||||
if (ch != '_' && ch != ' ' && !Character.isLetterOrDigit(ch)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Locale value \"" + localeString + "\" contains invalid characters");
|
||||
}
|
||||
}
|
||||
String[] parts = tokenizeToStringArray(localeString, "_ ", false, false);
|
||||
String language = (parts.length > 0 ? parts[0] : "");
|
||||
String country = (parts.length > 1 ? parts[1] : "");
|
||||
validateLocalePart(language);
|
||||
validateLocalePart(country);
|
||||
String variant = "";
|
||||
if (parts.length >= 2) {
|
||||
// There is definitely a variant, and it is everything after the country
|
||||
|
@ -689,6 +684,16 @@ public abstract class StringUtils {
|
|||
return (language.length() > 0 ? new Locale(language, country, variant) : null);
|
||||
}
|
||||
|
||||
private static void validateLocalePart(String localePart) {
|
||||
for (int i = 0; i < localePart.length(); i++) {
|
||||
char ch = localePart.charAt(i);
|
||||
if (ch != '_' && ch != ' ' && !Character.isLetterOrDigit(ch)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Locale part \"" + localePart + "\" contains invalid characters");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the RFC 3066 compliant language tag,
|
||||
* as used for the HTTP "Accept-Language" header.
|
||||
|
|
|
@ -566,6 +566,16 @@ public class StringUtilsTests extends TestCase {
|
|||
assertNull("When given an empty Locale string, must return null.", locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-8637">See SPR-8637</a>.
|
||||
*/
|
||||
public void testParseLocaleWithMultiSpecialCharactersInVariant() throws Exception {
|
||||
final String variant = "proper-northern";
|
||||
final String localeString = "en_GB_" + variant;
|
||||
Locale locale = StringUtils.parseLocaleString(localeString);
|
||||
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
|
||||
}
|
||||
|
||||
/**
|
||||
* <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-3671">See SPR-3671</a>.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue