1 16 17 package org.springframework.beans.propertyeditors; 18 19 import org.springframework.util.Assert; 20 import org.springframework.util.StringUtils; 21 22 import java.beans.PropertyEditorSupport ; 23 import java.util.Locale ; 24 import java.util.ResourceBundle ; 25 26 73 public class ResourceBundleEditor extends PropertyEditorSupport { 74 75 79 public static final String BASE_NAME_SEPARATOR = "_"; 80 81 82 public void setAsText(String text) throws IllegalArgumentException { 83 Assert.hasText(text, "'text' must not be empty"); 84 ResourceBundle bundle; 85 String rawBaseName = text.trim(); 86 int indexOfBaseNameSeparator = rawBaseName.indexOf(BASE_NAME_SEPARATOR); 87 if (indexOfBaseNameSeparator == -1) { 88 bundle = ResourceBundle.getBundle(rawBaseName); 89 } else { 90 String baseName = rawBaseName.substring(0, indexOfBaseNameSeparator); 92 if (!StringUtils.hasText(baseName)) { 93 throw new IllegalArgumentException ("Bad ResourceBundle name : received '" + text + "' as argument to 'setAsText(String value)'."); 94 } 95 String localeString = rawBaseName.substring(indexOfBaseNameSeparator + 1); 96 Locale locale = StringUtils.parseLocaleString(localeString); 97 bundle = (StringUtils.hasText(localeString)) 98 ? ResourceBundle.getBundle(baseName, locale) 99 : ResourceBundle.getBundle(baseName); 100 } 101 setValue(bundle); 102 } 103 104 } 105 | Popular Tags |