All Downloads are FREE. Search and download functionalities are using the official Maven repository.

infra.beans.propertyeditors.ResourceBundleEditor Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 - 2024 the original author or authors.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see [https://www.gnu.org/licenses/]
 */

package infra.beans.propertyeditors;

import java.beans.PropertyEditorSupport;
import java.util.Locale;
import java.util.ResourceBundle;

import infra.beans.factory.config.CustomEditorConfigurer;
import infra.lang.Assert;
import infra.util.StringUtils;

/**
 * {@link java.beans.PropertyEditor} implementation for standard JDK
 * {@link ResourceBundle ResourceBundles}.
 *
 * 

Only supports conversion from a String, but not to a String. * * Find below some examples of using this class in a (properly configured) * Framework container using XML-based metadata: * *

 <bean id="errorDialog" class="...">
 *    <!--
 *        the 'messages' property is of type java.util.ResourceBundle.
 *        the 'DialogMessages.properties' file exists at the root of the CLASSPATH
 *    -->
 *    <property name="messages" value="DialogMessages"/>
 * </bean>
* *
 <bean id="errorDialog" class="...">
 *    <!--
 *        the 'DialogMessages.properties' file exists in the 'com/messages' package
 *    -->
 *    <property name="messages" value="com/messages/DialogMessages"/>
 * </bean>
* *

A 'properly configured' Framework {@link infra.context.ApplicationContext container} * might contain a {@link CustomEditorConfigurer} * definition such that the conversion can be effected transparently: * *

 <bean class="infra.beans.support.CustomEditorConfigurer">
 *    <property name="customEditors">
 *        <map>
 *            <entry key="java.util.ResourceBundle">
 *                <bean class="infra.beans.propertyeditors.ResourceBundleEditor"/>
 *            </entry>
 *        </map>
 *    </property>
 * </bean>
* *

Please note that this {@link java.beans.PropertyEditor} is not * registered by default with any of the Framework infrastructure. * *

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}. */ public static final String BASE_NAME_SEPARATOR = "_"; @Override public void setAsText(String text) throws IllegalArgumentException { Assert.hasText(text, "'text' must not be empty"); String name = text.trim(); int separator = name.indexOf(BASE_NAME_SEPARATOR); if (separator == -1) { setValue(ResourceBundle.getBundle(name)); } else { // The name potentially contains locale information String baseName = name.substring(0, separator); if (StringUtils.isBlank(baseName)) { throw new IllegalArgumentException("Invalid ResourceBundle name: '" + text + "'"); } String localeString = name.substring(separator + 1); Locale locale = StringUtils.parseLocaleString(localeString); setValue(locale != null ? ResourceBundle.getBundle(baseName, locale) : ResourceBundle.getBundle(baseName)); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy