@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2014 the original author or authors .
* Copyright 2002 - 2015 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -24,14 +24,13 @@ import org.springframework.util.Assert;
@@ -24,14 +24,13 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils ;
/ * *
* { @link java . beans . PropertyEditor } implementation for
* { @link java . beans . PropertyEditor } implementation for standard JDK
* { @link java . util . ResourceBundle ResourceBundles } .
*
* < p > Only supports conversion < i > from < / i > a String , but not
* < i > to < / i > a String .
* < p > Only supports conversion < i > from < / i > a String , but not < i > to < / i > a String .
*
* Find below some examples of using this class in a
* ( properly configured ) Spring container using XML - based metadata :
* Find below some examples of using this class in a ( properly configured )
* Spring container using XML - based metadata :
*
* < pre class = "code" > & lt ; bean id = "errorDialog" class = "..." & gt ;
* & lt ; ! - -
@ -68,13 +67,14 @@ import org.springframework.util.StringUtils;
@@ -68,13 +67,14 @@ import org.springframework.util.StringUtils;
* < p > Thanks to David Leal Valmana for the suggestion and initial prototype .
*
* @author Rick Evans
* @author Juergen Hoeller
* @since 2 . 0
* /
public class ResourceBundleEditor extends PropertyEditorSupport {
/ * *
* The separator used to distinguish between the base name and the
* locale ( if any ) when { @link # setAsText ( String ) converting from a String } .
* The separator used to distinguish between the base name and the locale
* ( if any ) when { @link # setAsText ( String ) converting from a String } .
* /
public static final String BASE_NAME_SEPARATOR = "_" ;
@ -82,25 +82,23 @@ public class ResourceBundleEditor extends PropertyEditorSupport {
@@ -82,25 +82,23 @@ public class ResourceBundleEditor extends PropertyEditorSupport {
@Override
public void setAsText ( String text ) throws IllegalArgumentException {
Assert . hasText ( text , "'text' must not be empty" ) ;
ResourceBundle bundle ;
String rawBaseName = text . trim ( ) ;
int indexOfBa seNameS eparator = rawBaseN ame. indexOf ( BASE_NAME_SEPARATOR ) ;
if ( indexOfBa seNameS eparator = = - 1 ) {
bundle = ResourceBundle . getBundle ( rawBaseName ) ;
String name = text . trim ( ) ;
int separator = n ame. indexOf ( BASE_NAME_SEPARATOR ) ;
if ( separator = = - 1 ) {
setValue ( ResourceBundle . getBundle ( name ) ) ;
}
else {
// it potentially has locale information
String baseName = rawBaseN ame. substring ( 0 , indexOfBa seNameS eparator) ;
// Tge basename potentially has locale information
String baseName = n ame. substring ( 0 , separator ) ;
if ( ! StringUtils . hasText ( baseName ) ) {
throw new IllegalArgumentException ( "Bad ResourceBundle name : received '" + text + "' as argument to 'setAsText(String value)'. " ) ;
throw new IllegalArgumentException ( "Invalid ResourceBundle name: '" + text + "'" ) ;
}
String localeString = rawBaseN ame. substring ( indexOfBa seNameS eparator + 1 ) ;
String localeString = n ame. substring ( separator + 1 ) ;
Locale locale = StringUtils . parseLocaleString ( localeString ) ;
bundle = ( StringUtils . hasText ( localeString ) )
? ResourceBundle . getBundle ( baseName , locale )
: ResourceBundle . getBundle ( baseName ) ;
setValue ( ( StringUtils . hasText ( localeString ) ) ? ResourceBundle . getBundle ( baseName , locale ) :
ResourceBundle . getBundle ( baseName ) ) ;
}
setValue ( bundle ) ;
}
}