01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import java.util.HashMap;
21: import java.util.Locale;
22: import java.util.Map;
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 messages = new HashMap();
38:
39: protected MessageFormat resolveCode(String code, Locale locale) {
40: return (MessageFormat) this .messages.get(code + "_"
41: + 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 code, Locale locale, String msg) {
51: this .messages.put(code + "_" + locale.toString(),
52: createMessageFormat(msg, locale));
53: if (logger.isDebugEnabled()) {
54: logger.debug("Added message [" + msg + "] for code ["
55: + code + "] and Locale [" + locale + "]");
56: }
57: }
58:
59: public String toString() {
60: return getClass().getName() + ": " + this.messages;
61: }
62:
63: }
|