提交 e2d9142c 编写于 作者: J Juergen Hoeller

LocaleEditor and StringToLocaleConverter do not restrict variant part through validation (SPR-8637)

上级 0c9e3fb3
......@@ -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>.
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册