1. /*
  2. * Copyright 2002-2004 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. package org.springframework.context.support;
  17. import java.text.MessageFormat;
  18. import java.util.HashMap;
  19. import java.util.Locale;
  20. import java.util.Map;
  21. /**
  22. * Simple implementation of MessageSource that allows messages
  23. * to be held in a Java object, and added programmatically.
  24. * This MessageSource supports internationalization.
  25. *
  26. * <p>Intended for testing rather than use in production systems.
  27. *
  28. * @author Rod Johnson
  29. * @author Juergen Hoeller
  30. */
  31. public class StaticMessageSource extends AbstractMessageSource {
  32. private final Map messages = new HashMap();
  33. protected MessageFormat resolveCode(String code, Locale locale) {
  34. return (MessageFormat) this.messages.get(code + "_" + locale.toString());
  35. }
  36. /**
  37. * Associate the given message with the given code.
  38. * @param code lookup code
  39. * @param locale locale message should be found within
  40. * @param message message associated with this lookup code
  41. */
  42. public void addMessage(String code, Locale locale, String message) {
  43. this.messages.put(code + "_" + locale.toString(), new MessageFormat(message));
  44. logger.info("Added message [" + message + "] for code [" + code + "] and Locale [" + locale + "]");
  45. }
  46. public String toString() {
  47. return getClass().getName() + ": " + this.messages;
  48. }
  49. }