001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.i18n;
025:
026: import java.util.Locale;
027:
028: import org.riotfamily.common.beans.PropertyUtils;
029: import org.riotfamily.common.util.FormatUtils;
030: import org.springframework.context.MessageSource;
031: import org.springframework.context.MessageSourceResolvable;
032: import org.springframework.context.support.DefaultMessageSourceResolvable;
033: import org.springframework.util.Assert;
034:
035: public class MessageResolver {
036:
037: private static final String EMPTY_MESSAGE = new String();
038:
039: private AdvancedMessageCodesResolver messageCodesResolver;
040:
041: private MessageSource messageSource;
042:
043: private Locale locale;
044:
045: public MessageResolver(MessageSource source,
046: AdvancedMessageCodesResolver resolver, Locale locale) {
047:
048: Assert.notNull(source, "MessageSource must not be null");
049: Assert.notNull(resolver,
050: "MessageCodesResolver must not be null");
051: Assert.notNull(locale, "Locale must not be null");
052:
053: this .messageSource = source;
054: this .messageCodesResolver = resolver;
055: this .locale = locale;
056: }
057:
058: public String getObjectError(String objectName, Class clazz,
059: String errorCode, Object[] args, String defaultMessage) {
060:
061: String[] codes = messageCodesResolver.resolveMessageCodes(
062: errorCode, objectName);
063: return getMessage(codes, args, defaultMessage);
064: }
065:
066: public String getPropertyError(String objectName, Class clazz,
067: String property, String errorCode, Object[] args,
068: String defaultMessage) {
069:
070: Class fieldType = PropertyUtils
071: .getPropertyType(clazz, property);
072: String[] codes = messageCodesResolver.resolveMessageCodes(
073: errorCode, objectName, property, fieldType);
074:
075: return getMessage(codes, args, defaultMessage);
076: }
077:
078: public String getPropertyLabel(String objectName, Class clazz,
079: String property) {
080:
081: String[] codes = messageCodesResolver.resolveLabel(objectName,
082: clazz, property);
083: String defaultMessage = FormatUtils
084: .propertyToTitleCase(property);
085: return getMessage(codes, null, defaultMessage);
086: }
087:
088: public String getClassLabel(String objectName, Class clazz) {
089: String[] codes = messageCodesResolver.resolveLabel(objectName,
090: clazz);
091: String defaultMessage = FormatUtils
092: .camelToTitleCase(objectName);
093: return getMessage(codes, null, defaultMessage);
094: }
095:
096: public String getPropertyHint(String objectName, Class clazz,
097: String property) {
098:
099: String[] codes = messageCodesResolver.resolveHint(objectName,
100: clazz, property);
101: return getMessage(codes, null, null);
102: }
103:
104: public String getMessage(String code) {
105: return getMessage(code, null, null);
106: }
107:
108: public String getMessage(String code, Object[] args) {
109: return getMessage(code, args, null);
110: }
111:
112: /**
113: * @since 6.4
114: */
115: public String getMessage(String code, String defaultMessage) {
116: return getMessage(code, null, defaultMessage);
117: }
118:
119: public String getMessage(String code, Object[] args,
120: String defaultMessage) {
121: return messageSource.getMessage(code, args, defaultMessage,
122: locale);
123: }
124:
125: public String getMessage(MessageSourceResolvable resolvable) {
126: return getMessage(resolvable.getCodes(), resolvable
127: .getArguments(), resolvable.getDefaultMessage());
128: }
129:
130: public String getMessage(String[] codes, Object[] args,
131: String defaultMessage) {
132:
133: if (defaultMessage == null) {
134: defaultMessage = EMPTY_MESSAGE;
135: }
136: MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(
137: codes, args, defaultMessage);
138:
139: String message = messageSource.getMessage(resolvable, locale);
140: if (message == EMPTY_MESSAGE) {
141: return null;
142: }
143: return message;
144: }
145:
146: public AdvancedMessageCodesResolver getMessageCodesResolver() {
147: return this .messageCodesResolver;
148: }
149:
150: /**
151: * @since 6.4
152: */
153: public Locale getLocale() {
154: return locale;
155: }
156:
157: /**
158: * @since 6.4
159: */
160: public MessageSource getMessageSource() {
161: return messageSource;
162: }
163:
164: }
|