KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > MessageSourceResourceBundle


1 /*
2  * Copyright 2002-2005 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.context.support;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.ResourceBundle JavaDoc;
22
23 import org.springframework.context.MessageSource;
24 import org.springframework.context.NoSuchMessageException;
25 import org.springframework.util.Assert;
26
27 /**
28  * Helper class that allows for accessing a MessageSource as a ResourceBundle.
29  * Used for example to expose a Spring MessageSource to JSTL web views.
30  *
31  * @author Juergen Hoeller
32  * @since 27.02.2003
33  * @see org.springframework.context.MessageSource
34  * @see java.util.ResourceBundle
35  * @see org.springframework.web.servlet.support.JstlUtils#exposeLocalizationContext
36  */

37 public class MessageSourceResourceBundle extends ResourceBundle JavaDoc {
38
39     private final MessageSource messageSource;
40
41     private final Locale JavaDoc locale;
42
43
44     /**
45      * Create a new MessageSourceResourceBundle for the given MessageSource and Locale.
46      * @param source the MessageSource to retrieve messages from
47      * @param locale the Locale to retrieve messages for
48      */

49     public MessageSourceResourceBundle(MessageSource source, Locale JavaDoc locale) {
50         Assert.notNull(source, "MessageSource is required");
51         this.messageSource = source;
52         this.locale = locale;
53     }
54
55
56     /**
57      * This implementation resolves the code in the MessageSource.
58      * Returns null if the message could not be resolved.
59      */

60     protected Object JavaDoc handleGetObject(String JavaDoc code) {
61         try {
62             return this.messageSource.getMessage(code, null, this.locale);
63         }
64         catch (NoSuchMessageException ex) {
65             return null;
66         }
67     }
68
69     /**
70      * This implementation returns <code>null</code>, as a MessageSource does
71      * not allow for enumerating the defined message codes.
72      */

73     public Enumeration JavaDoc getKeys() {
74         return null;
75     }
76
77 }
78
Popular Tags