001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.propertyeditors;
018:
019: import org.springframework.util.Assert;
020: import org.springframework.util.StringUtils;
021:
022: import java.beans.PropertyEditorSupport;
023: import java.util.Locale;
024: import java.util.ResourceBundle;
025:
026: /**
027: * {@link java.beans.PropertyEditor} implementation for
028: * {@link java.util.ResourceBundle ResourceBundles}.
029: *
030: * <p>Only supports conversion <i>from</i> a String, but not
031: * <i>to</i> a String.
032: *
033: * Find below some examples of using this class in a
034: * (properly configured) Spring container using XML-based metadata:
035: *
036: * <pre class="code"> <bean id="errorDialog" class="...">
037: * <!--
038: * the 'messages' property is of type java.util.ResourceBundle.
039: * the 'DialogMessages.properties' file exists at the root of the CLASSPATH
040: * -->
041: * <property name="messages" value="DialogMessages"/>
042: * </bean></pre>
043: *
044: * <pre class="code"> <bean id="errorDialog" class="...">
045: * <!--
046: * the 'DialogMessages.properties' file exists in the 'com/messages' package
047: * -->
048: * <property name="messages" value="com/messages/DialogMessages"/>
049: * </bean></pre>
050: *
051: * <p>A 'properly configured' Spring {@link org.springframework.context.ApplicationContext container}
052: * might contain a {@link org.springframework.beans.factory.config.CustomEditorConfigurer}
053: * definition such that the conversion can be effected transparently:
054: *
055: * <pre class="code"> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
056: * <property name="customEditors">
057: * <map>
058: * <entry key="java.util.ResourceBundle">
059: * <bean class="org.springframework.beans.propertyeditors.ResourceBundleEditor"/>
060: * </entry>
061: * </map>
062: * </property>
063: * </bean></pre>
064: *
065: * <p>Please note that this {@link java.beans.PropertyEditor} is
066: * <b>not</b> registered by default with any of the Spring infrastructure.
067: *
068: * <p>Thanks to David Leal Valmana for the suggestion and initial prototype.
069: *
070: * @author Rick Evans
071: * @since 2.0
072: */
073: public class ResourceBundleEditor extends PropertyEditorSupport {
074:
075: /**
076: * The separator used to distinguish between the base name and the
077: * locale (if any) when {@link #setAsText(String) converting from a String}.
078: */
079: public static final String BASE_NAME_SEPARATOR = "_";
080:
081: public void setAsText(String text) throws IllegalArgumentException {
082: Assert.hasText(text, "'text' must not be empty");
083: ResourceBundle bundle;
084: String rawBaseName = text.trim();
085: int indexOfBaseNameSeparator = rawBaseName
086: .indexOf(BASE_NAME_SEPARATOR);
087: if (indexOfBaseNameSeparator == -1) {
088: bundle = ResourceBundle.getBundle(rawBaseName);
089: } else {
090: // it potentially has locale information
091: String baseName = rawBaseName.substring(0,
092: indexOfBaseNameSeparator);
093: if (!StringUtils.hasText(baseName)) {
094: throw new IllegalArgumentException(
095: "Bad ResourceBundle name : received '"
096: + text
097: + "' as argument to 'setAsText(String value)'.");
098: }
099: String localeString = rawBaseName
100: .substring(indexOfBaseNameSeparator + 1);
101: Locale locale = StringUtils.parseLocaleString(localeString);
102: bundle = (StringUtils.hasText(localeString)) ? ResourceBundle
103: .getBundle(baseName, locale)
104: : ResourceBundle.getBundle(baseName);
105: }
106: setValue(bundle);
107: }
108:
109: }
|