KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > i18n > MessageResolver


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.i18n;
25
26 import java.util.Locale JavaDoc;
27
28 import org.riotfamily.common.beans.PropertyUtils;
29 import org.riotfamily.common.util.FormatUtils;
30 import org.springframework.context.MessageSource;
31 import org.springframework.context.MessageSourceResolvable;
32 import org.springframework.context.support.DefaultMessageSourceResolvable;
33 import org.springframework.util.Assert;
34
35 public class MessageResolver {
36     
37     private static final String JavaDoc EMPTY_MESSAGE = new String JavaDoc();
38     
39     private AdvancedMessageCodesResolver messageCodesResolver;
40     
41     private MessageSource messageSource;
42     
43     private Locale JavaDoc locale;
44     
45     
46     public MessageResolver(MessageSource source,
47             AdvancedMessageCodesResolver resolver, Locale JavaDoc locale) {
48         
49         Assert.notNull(source, "MessageSource must not be null");
50         Assert.notNull(resolver, "MessageCodesResolver must not be null");
51         Assert.notNull(locale, "Locale must not be null");
52         
53         this.messageSource = source;
54         this.messageCodesResolver = resolver;
55         this.locale = locale;
56     }
57
58     public String JavaDoc getObjectError(String JavaDoc objectName, Class JavaDoc clazz,
59             String JavaDoc errorCode, Object JavaDoc[] args, String JavaDoc defaultMessage) {
60     
61         String JavaDoc[] codes = messageCodesResolver.resolveMessageCodes(errorCode, objectName);
62         return getMessage(codes, args, defaultMessage);
63     }
64     
65     public String JavaDoc getPropertyError(
66             String JavaDoc objectName, Class JavaDoc clazz, String JavaDoc property, String JavaDoc errorCode,
67             Object JavaDoc[] args, String JavaDoc defaultMessage) {
68     
69         Class JavaDoc fieldType = PropertyUtils.getPropertyType(clazz, property);
70         String JavaDoc[] codes = messageCodesResolver.resolveMessageCodes(errorCode, objectName,
71                 property, fieldType);
72         
73         return getMessage(codes, args, defaultMessage);
74     }
75     
76     
77     public String JavaDoc getPropertyLabel(String JavaDoc objectName,
78             Class JavaDoc clazz, String JavaDoc property) {
79         
80         String JavaDoc[] codes = messageCodesResolver.resolveLabel(objectName, clazz, property);
81         String JavaDoc defaultMessage = FormatUtils.propertyToTitleCase(property);
82         return getMessage(codes, null, defaultMessage);
83     }
84     
85     public String JavaDoc getClassLabel(String JavaDoc objectName, Class JavaDoc clazz) {
86         String JavaDoc[] codes = messageCodesResolver.resolveLabel(objectName, clazz);
87         String JavaDoc defaultMessage = FormatUtils.camelToTitleCase(objectName);
88         return getMessage(codes, null, defaultMessage);
89     }
90         
91     public String JavaDoc getPropertyHint(String JavaDoc objectName,
92             Class JavaDoc clazz, String JavaDoc property) {
93         
94         String JavaDoc[] codes = messageCodesResolver.resolveHint(objectName, clazz, property);
95         return getMessage(codes, null, null);
96     }
97     
98     public String JavaDoc getMessage(String JavaDoc code) {
99         return getMessage(code, null, null);
100     }
101     
102     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args) {
103         return getMessage(code, args, null);
104     }
105     
106     /**
107      * @since 6.4
108      */

109     public String JavaDoc getMessage(String JavaDoc code, String JavaDoc defaultMessage) {
110         return getMessage(code, null, defaultMessage);
111     }
112     
113     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, String JavaDoc defaultMessage) {
114         return messageSource.getMessage(code, args, defaultMessage, locale);
115     }
116     
117     public String JavaDoc getMessage(MessageSourceResolvable resolvable) {
118         return getMessage(resolvable.getCodes(), resolvable.getArguments(),
119                 resolvable.getDefaultMessage());
120     }
121     
122     public String JavaDoc getMessage(String JavaDoc[] codes, Object JavaDoc[] args,
123             String JavaDoc defaultMessage) {
124         
125         if (defaultMessage == null) {
126             defaultMessage = EMPTY_MESSAGE;
127         }
128         MessageSourceResolvable resolvable =
129                 new DefaultMessageSourceResolvable(codes, args, defaultMessage);
130         
131         String JavaDoc message = messageSource.getMessage(resolvable, locale);
132         if (message == EMPTY_MESSAGE) {
133             return null;
134         }
135         return message;
136     }
137
138     public AdvancedMessageCodesResolver getMessageCodesResolver() {
139         return this.messageCodesResolver;
140     }
141     
142     /**
143      * @since 6.4
144      */

145     public Locale JavaDoc getLocale() {
146         return locale;
147     }
148     
149     /**
150      * @since 6.4
151      */

152     public MessageSource getMessageSource() {
153         return messageSource;
154     }
155     
156 }
157
Popular Tags