KickJava   Java API By Example, From Geeks To Geeks.

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


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.springframework.context.MessageSourceResolvable;
29 import org.springframework.context.support.DelegatingMessageSource;
30 import org.springframework.util.StringUtils;
31
32 /**
33  * MessageSource that reveals the code(s) used to look-up a message.
34  *
35  * @author Felix Gnass [fgnass at neteye dot de]
36  * @since 6.4
37  */

38 public class CodeRevealingMessageSource extends DelegatingMessageSource {
39     
40     private boolean revealCodes = true;
41
42     public boolean isRevealCodes() {
43         return this.revealCodes;
44     }
45
46     public void setRevealCodes(boolean revealAllCodes) {
47         this.revealCodes = revealAllCodes;
48     }
49
50     protected String JavaDoc revealCode(String JavaDoc message, String JavaDoc code) {
51         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
52         if (message != null) {
53             sb.append(message);
54         }
55         sb.append("<span class=\"messageCode\" title=\"")
56             .append(code).append("\"></span>");
57         
58         return sb.toString();
59     }
60     
61     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, String JavaDoc defaultMessage,
62             Locale JavaDoc locale) {
63         
64         if (revealCodes) {
65             return revealCode(super.getMessage(code, args,
66                     defaultMessage, locale), code);
67         }
68         return super.getMessage(code, args, defaultMessage, locale);
69     }
70
71     public String JavaDoc getMessage(String JavaDoc code, Object JavaDoc[] args, Locale JavaDoc locale) {
72         return getMessage(code, args, null, locale);
73     }
74
75     public String JavaDoc getMessage(MessageSourceResolvable resolvable, Locale JavaDoc locale) {
76         if (revealCodes) {
77             return revealCode(super.getMessage(resolvable, locale),
78                     StringUtils.arrayToDelimitedString(
79                     resolvable.getCodes(), " | "));
80         }
81         return super.getMessage(resolvable, locale);
82     }
83 }
84
Popular Tags