01: /*
02: * Copyright 2002-2005 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.util.Locale;
20:
21: import org.springframework.context.HierarchicalMessageSource;
22: import org.springframework.context.MessageSource;
23: import org.springframework.context.MessageSourceResolvable;
24: import org.springframework.context.NoSuchMessageException;
25:
26: /**
27: * Empty MessageSource that delegates all calls to the parent MessageSource.
28: * If no parent is available, it simply won't resolve any message.
29: *
30: * <p>Used as placeholder by AbstractApplicationContext, if the context doesn't
31: * define its own MessageSource. Not intended for direct use in applications.
32: *
33: * @author Juergen Hoeller
34: * @since 1.1.5
35: * @see AbstractApplicationContext
36: */
37: public class DelegatingMessageSource implements
38: HierarchicalMessageSource {
39:
40: private MessageSource parentMessageSource;
41:
42: public void setParentMessageSource(MessageSource parent) {
43: this .parentMessageSource = parent;
44: }
45:
46: public MessageSource getParentMessageSource() {
47: return parentMessageSource;
48: }
49:
50: public String getMessage(String code, Object[] args,
51: String defaultMessage, Locale locale) {
52: if (this .parentMessageSource != null) {
53: return this .parentMessageSource.getMessage(code, args,
54: defaultMessage, locale);
55: } else {
56: return defaultMessage;
57: }
58: }
59:
60: public String getMessage(String code, Object[] args, Locale locale)
61: throws NoSuchMessageException {
62: if (this .parentMessageSource != null) {
63: return this .parentMessageSource.getMessage(code, args,
64: locale);
65: } else {
66: throw new NoSuchMessageException(code, locale);
67: }
68: }
69:
70: public String getMessage(MessageSourceResolvable resolvable,
71: Locale locale) throws NoSuchMessageException {
72: if (this .parentMessageSource != null) {
73: return this .parentMessageSource.getMessage(resolvable,
74: locale);
75: } else {
76: if (resolvable.getDefaultMessage() != null) {
77: return resolvable.getDefaultMessage();
78: }
79: String[] codes = resolvable.getCodes();
80: String code = (codes != null && codes.length > 0 ? codes[0]
81: : null);
82: throw new NoSuchMessageException(code, locale);
83: }
84: }
85:
86: }
|