KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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.text.MessageFormat JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * Simple implementation of {@link org.springframework.context.MessageSource}
26  * which allows messages to be registered programmatically.
27  * This MessageSource supports basic internationalization.
28  *
29  * <p>Intended for testing rather than for use in production systems.
30  *
31  * @author Rod Johnson
32  * @author Juergen Hoeller
33  */

34 public class StaticMessageSource extends AbstractMessageSource {
35
36     /** Map from 'code + locale' keys to message Strings */
37     private final Map JavaDoc messages = new HashMap JavaDoc();
38
39
40     protected MessageFormat JavaDoc resolveCode(String JavaDoc code, Locale JavaDoc locale) {
41         return (MessageFormat JavaDoc) this.messages.get(code + "_" + locale.toString());
42     }
43
44     /**
45      * Associate the given message with the given code.
46      * @param code the lookup code
47    * @param locale the locale that the message should be found within
48      * @param msg the message associated with this lookup code
49      */

50     public void addMessage(String JavaDoc code, Locale JavaDoc locale, String JavaDoc msg) {
51         this.messages.put(code + "_" + locale.toString(), createMessageFormat(msg, locale));
52         if (logger.isDebugEnabled()) {
53             logger.debug("Added message [" + msg + "] for code [" + code + "] and Locale [" + locale + "]");
54         }
55     }
56
57
58     public String JavaDoc toString() {
59         return getClass().getName() + ": " + this.messages;
60     }
61
62 }
63
Popular Tags