Skip to content

Commit 36940e0

Browse files
committed
Polishing
1 parent 935d714 commit 36940e0

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

spring-beans/src/main/java/org/springframework/beans/propertyeditors/ResourceBundleEditor.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,14 +24,13 @@
2424
import org.springframework.util.StringUtils;
2525

2626
/**
27-
* {@link java.beans.PropertyEditor} implementation for
27+
* {@link java.beans.PropertyEditor} implementation for standard JDK
2828
* {@link java.util.ResourceBundle ResourceBundles}.
2929
*
30-
* <p>Only supports conversion <i>from</i> a String, but not
31-
* <i>to</i> a String.
30+
* <p>Only supports conversion <i>from</i> a String, but not <i>to</i> a String.
3231
*
33-
* Find below some examples of using this class in a
34-
* (properly configured) Spring container using XML-based metadata:
32+
* Find below some examples of using this class in a (properly configured)
33+
* Spring container using XML-based metadata:
3534
*
3635
* <pre class="code"> &lt;bean id="errorDialog" class="..."&gt;
3736
* &lt;!--
@@ -62,45 +61,44 @@
6261
* &lt;/property&gt;
6362
* &lt;/bean&gt;</pre>
6463
*
65-
* <p>Please note that this {@link java.beans.PropertyEditor} is
66-
* <b>not</b> registered by default with any of the Spring infrastructure.
64+
* <p>Please note that this {@link java.beans.PropertyEditor} is <b>not</b>
65+
* registered by default with any of the Spring infrastructure.
6766
*
6867
* <p>Thanks to David Leal Valmana for the suggestion and initial prototype.
6968
*
7069
* @author Rick Evans
70+
* @author Juergen Hoeller
7171
* @since 2.0
7272
*/
7373
public class ResourceBundleEditor extends PropertyEditorSupport {
7474

7575
/**
76-
* The separator used to distinguish between the base name and the
77-
* locale (if any) when {@link #setAsText(String) converting from a String}.
76+
* The separator used to distinguish between the base name and the locale
77+
* (if any) when {@link #setAsText(String) converting from a String}.
7878
*/
7979
public static final String BASE_NAME_SEPARATOR = "_";
8080

8181

8282
@Override
8383
public void setAsText(String text) throws IllegalArgumentException {
8484
Assert.hasText(text, "'text' must not be empty");
85-
ResourceBundle bundle;
86-
String rawBaseName = text.trim();
87-
int indexOfBaseNameSeparator = rawBaseName.indexOf(BASE_NAME_SEPARATOR);
88-
if (indexOfBaseNameSeparator == -1) {
89-
bundle = ResourceBundle.getBundle(rawBaseName);
85+
String name = text.trim();
86+
87+
int separator = name.indexOf(BASE_NAME_SEPARATOR);
88+
if (separator == -1) {
89+
setValue(ResourceBundle.getBundle(name));
9090
}
9191
else {
92-
// it potentially has locale information
93-
String baseName = rawBaseName.substring(0, indexOfBaseNameSeparator);
92+
// The name potentially contains locale information
93+
String baseName = name.substring(0, separator);
9494
if (!StringUtils.hasText(baseName)) {
95-
throw new IllegalArgumentException("Bad ResourceBundle name : received '" + text + "' as argument to 'setAsText(String value)'.");
95+
throw new IllegalArgumentException("Invalid ResourceBundle name: '" + text + "'");
9696
}
97-
String localeString = rawBaseName.substring(indexOfBaseNameSeparator + 1);
97+
String localeString = name.substring(separator + 1);
9898
Locale locale = StringUtils.parseLocaleString(localeString);
99-
bundle = (StringUtils.hasText(localeString))
100-
? ResourceBundle.getBundle(baseName, locale)
101-
: ResourceBundle.getBundle(baseName);
99+
setValue((StringUtils.hasText(localeString)) ? ResourceBundle.getBundle(baseName, locale) :
100+
ResourceBundle.getBundle(baseName));
102101
}
103-
setValue(bundle);
104102
}
105103

106104
}

spring-beans/src/test/java/org/springframework/beans/propertyeditors/ResourceBundleEditorTests.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2006 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,9 +28,10 @@
2828
* @author Rick Evans
2929
* @author Chris Beams
3030
*/
31-
public final class ResourceBundleEditorTests {
31+
public class ResourceBundleEditorTests {
3232

3333
private static final String BASE_NAME = ResourceBundleEditorTests.class.getName();
34+
3435
private static final String MESSAGE_KEY = "punk";
3536

3637

@@ -40,7 +41,8 @@ public void testSetAsTextWithJustBaseName() throws Exception {
4041
editor.setAsText(BASE_NAME);
4142
Object value = editor.getValue();
4243
assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value);
43-
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle);
44+
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).",
45+
value instanceof ResourceBundle);
4446
ResourceBundle bundle = (ResourceBundle) value;
4547
String string = bundle.getString(MESSAGE_KEY);
4648
assertEquals(MESSAGE_KEY, string);
@@ -52,7 +54,8 @@ public void testSetAsTextWithBaseNameThatEndsInDefaultSeparator() throws Excepti
5254
editor.setAsText(BASE_NAME + "_");
5355
Object value = editor.getValue();
5456
assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value);
55-
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle);
57+
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).",
58+
value instanceof ResourceBundle);
5659
ResourceBundle bundle = (ResourceBundle) value;
5760
String string = bundle.getString(MESSAGE_KEY);
5861
assertEquals(MESSAGE_KEY, string);
@@ -64,7 +67,8 @@ public void testSetAsTextWithBaseNameAndLanguageCode() throws Exception {
6467
editor.setAsText(BASE_NAME + "Lang" + "_en");
6568
Object value = editor.getValue();
6669
assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value);
67-
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle);
70+
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).",
71+
value instanceof ResourceBundle);
6872
ResourceBundle bundle = (ResourceBundle) value;
6973
String string = bundle.getString(MESSAGE_KEY);
7074
assertEquals("yob", string);
@@ -76,7 +80,8 @@ public void testSetAsTextWithBaseNameLanguageAndCountryCode() throws Exception {
7680
editor.setAsText(BASE_NAME + "LangCountry" + "_en_GB");
7781
Object value = editor.getValue();
7882
assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value);
79-
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle);
83+
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).",
84+
value instanceof ResourceBundle);
8085
ResourceBundle bundle = (ResourceBundle) value;
8186
String string = bundle.getString(MESSAGE_KEY);
8287
assertEquals("chav", string);
@@ -88,31 +93,32 @@ public void testSetAsTextWithTheKitchenSink() throws Exception {
8893
editor.setAsText(BASE_NAME + "LangCountryDialect" + "_en_GB_GLASGOW");
8994
Object value = editor.getValue();
9095
assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value);
91-
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle);
96+
assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).",
97+
value instanceof ResourceBundle);
9298
ResourceBundle bundle = (ResourceBundle) value;
9399
String string = bundle.getString(MESSAGE_KEY);
94100
assertEquals("ned", string);
95101
}
96102

97-
@Test(expected=IllegalArgumentException.class)
103+
@Test(expected = IllegalArgumentException.class)
98104
public void testSetAsTextWithNull() throws Exception {
99105
ResourceBundleEditor editor = new ResourceBundleEditor();
100106
editor.setAsText(null);
101107
}
102108

103-
@Test(expected=IllegalArgumentException.class)
109+
@Test(expected = IllegalArgumentException.class)
104110
public void testSetAsTextWithEmptyString() throws Exception {
105111
ResourceBundleEditor editor = new ResourceBundleEditor();
106112
editor.setAsText("");
107113
}
108114

109-
@Test(expected=IllegalArgumentException.class)
115+
@Test(expected = IllegalArgumentException.class)
110116
public void testSetAsTextWithWhiteSpaceString() throws Exception {
111117
ResourceBundleEditor editor = new ResourceBundleEditor();
112118
editor.setAsText(" ");
113119
}
114120

115-
@Test(expected=IllegalArgumentException.class)
121+
@Test(expected = IllegalArgumentException.class)
116122
public void testSetAsTextWithJustSeparatorString() throws Exception {
117123
ResourceBundleEditor editor = new ResourceBundleEditor();
118124
editor.setAsText("_");

spring-webmvc/src/main/java/org/springframework/web/servlet/i18n/CookieLocaleResolver.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -111,6 +111,7 @@ protected Locale getDefaultLocale() {
111111

112112
/**
113113
* Set a fixed TimeZone that this resolver will return if no cookie found.
114+
* @since 4.0
114115
*/
115116
public void setDefaultTimeZone(TimeZone defaultTimeZone) {
116117
this.defaultTimeZone = defaultTimeZone;
@@ -119,6 +120,7 @@ public void setDefaultTimeZone(TimeZone defaultTimeZone) {
119120
/**
120121
* Return the fixed TimeZone that this resolver will return if no cookie found,
121122
* if any.
123+
* @since 4.0
122124
*/
123125
protected TimeZone getDefaultTimeZone() {
124126
return this.defaultTimeZone;
@@ -171,7 +173,7 @@ private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
171173
}
172174
}
173175
request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
174-
(locale != null ? locale: determineDefaultLocale(request)));
176+
(locale != null ? locale : determineDefaultLocale(request)));
175177
request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
176178
(timeZone != null ? timeZone : determineDefaultTimeZone(request)));
177179
}
@@ -197,7 +199,7 @@ public void setLocaleContext(HttpServletRequest request, HttpServletResponse res
197199
removeCookie(response);
198200
}
199201
request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
200-
(locale != null ? locale: determineDefaultLocale(request)));
202+
(locale != null ? locale : determineDefaultLocale(request)));
201203
request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
202204
(timeZone != null ? timeZone : determineDefaultTimeZone(request)));
203205
}

0 commit comments

Comments
 (0)