KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > ResourceBundleEditor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25
26 /**
27  * {@link java.beans.PropertyEditor} implementation for
28  * {@link java.util.ResourceBundle ResourceBundles}.
29  *
30  * <p>Only supports conversion <i>from</i> a String, but not
31  * <i>to</i> a String.
32  *
33  * Find below some examples of using this class in a
34  * (properly configured) Spring container using XML-based metadata:
35  *
36  * <pre class="code"> &lt;bean id="errorDialog" class="..."&gt;
37  * &lt;!--
38  * the 'messages' property is of type java.util.ResourceBundle.
39  * the 'DialogMessages.properties' file exists at the root of the CLASSPATH
40  * --&gt;
41  * &lt;property name="messages" value="DialogMessages"/&gt;
42  * &lt;/bean&gt;</pre>
43  *
44  * <pre class="code"> &lt;bean id="errorDialog" class="..."&gt;
45  * &lt;!--
46  * the 'DialogMessages.properties' file exists in the 'com/messages' package
47  * --&gt;
48  * &lt;property name="messages" value="com/messages/DialogMessages"/&gt;
49  * &lt;/bean&gt;</pre>
50  *
51  * <p>A 'properly configured' Spring {@link org.springframework.context.ApplicationContext container}
52  * might contain a {@link org.springframework.beans.factory.config.CustomEditorConfigurer}
53  * definition such that the conversion can be effected transparently:
54  *
55  * <pre class="code"> &lt;bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"&gt;
56  * &lt;property name="customEditors"&gt;
57  * &lt;map&gt;
58  * &lt;entry key="java.util.ResourceBundle"&gt;
59  * &lt;bean class="org.springframework.beans.propertyeditors.ResourceBundleEditor"/&gt;
60  * &lt;/entry&gt;
61  * &lt;/map&gt;
62  * &lt;/property&gt;
63  * &lt;/bean&gt;</pre>
64  *
65  * <p>Please note that this {@link java.beans.PropertyEditor} is
66  * <b>not</b> registered by default with any of the Spring infrastructure.
67  *
68  * <p>Thanks to David Leal Valmana for the suggestion and initial prototype.
69  *
70  * @author Rick Evans
71  * @since 2.0
72  */

73 public class ResourceBundleEditor extends PropertyEditorSupport JavaDoc {
74
75     /**
76      * The separator used to distinguish between the base name and the
77      * locale (if any) when {@link #setAsText(String) converting from a String}.
78      */

79     public static final String JavaDoc BASE_NAME_SEPARATOR = "_";
80
81
82     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
83         Assert.hasText(text, "'text' must not be empty");
84         ResourceBundle JavaDoc bundle;
85         String JavaDoc rawBaseName = text.trim();
86         int indexOfBaseNameSeparator = rawBaseName.indexOf(BASE_NAME_SEPARATOR);
87         if (indexOfBaseNameSeparator == -1) {
88             bundle = ResourceBundle.getBundle(rawBaseName);
89         } else {
90             // it potentially has locale information
91
String JavaDoc baseName = rawBaseName.substring(0, indexOfBaseNameSeparator);
92             if (!StringUtils.hasText(baseName)) {
93                 throw new IllegalArgumentException JavaDoc("Bad ResourceBundle name : received '" + text + "' as argument to 'setAsText(String value)'.");
94             }
95             String JavaDoc localeString = rawBaseName.substring(indexOfBaseNameSeparator + 1);
96             Locale JavaDoc 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