KickJava   Java API By Example, From Geeks To Geeks.

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


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.Locale JavaDoc;
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 HierarchicalMessageSource {
38
39     private MessageSource parentMessageSource;
40
41
42     public void setParentMessageSource(MessageSource parent) {
43         this.parentMessageSource = parent;
44     }
45
46     public MessageSource getParentMessageSource() {
47         return parentMessageSource;
48     }
49
50
51     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, String JavaDoc defaultMessage, Locale JavaDoc locale) {
52         if (this.parentMessageSource != null) {
53             return this.parentMessageSource.getMessage(code, args, defaultMessage, locale);
54         }
55         else {
56             return defaultMessage;
57         }
58     }
59
60     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, Locale JavaDoc locale) throws NoSuchMessageException {
61         if (this.parentMessageSource != null) {
62             return this.parentMessageSource.getMessage(code, args, locale);
63         }
64         else {
65             throw new NoSuchMessageException(code, locale);
66         }
67     }
68
69     public String JavaDoc getMessage(MessageSourceResolvable resolvable, Locale JavaDoc locale) throws NoSuchMessageException {
70         if (this.parentMessageSource != null) {
71             return this.parentMessageSource.getMessage(resolvable, locale);
72         }
73         else {
74             if (resolvable.getDefaultMessage() != null) {
75                 return resolvable.getDefaultMessage();
76             }
77             String JavaDoc[] codes = resolvable.getCodes();
78             String JavaDoc code = (codes != null && codes.length > 0 ? codes[0] : null);
79             throw new NoSuchMessageException(code, locale);
80         }
81     }
82
83 }
84
Popular Tags