KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > i18n > MessagesUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.i18n;
14
15 import java.util.Iterator JavaDoc;
16
17 import org.apache.commons.lang.StringUtils;
18
19
20 /**
21  * Util for handle messages. Allows easy use of chains and provides methods for rendering the javascript messages
22  * @author Philipp Bracher
23  * @version $Revision: 7280 $ ($Author: philipp $)
24  */

25 public class MessagesUtil {
26
27     /**
28      * Adds Variables to a JS witch can be used with the getMessage(key) method
29      * @return Javascript-Construct of this textes
30      */

31     public static String JavaDoc generateJavaScript(Messages messages) {
32         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
33
34         str.append("/* ###################################\n"); //$NON-NLS-1$
35
str.append("### Generated AbstractMessagesImpl\n"); //$NON-NLS-1$
36
str.append("################################### */\n\n"); //$NON-NLS-1$
37

38         for (Iterator JavaDoc iter = messages.keys(); iter.hasNext();) {
39             String JavaDoc key = (String JavaDoc) iter.next();
40
41             if (key.endsWith(".js")) { //$NON-NLS-1$
42
String JavaDoc msg = javaScriptString(messages.get(key));
43                 str.append(AbstractMessagesImpl.JS_OBJECTNAME
44                     + ".add('"
45                     + key
46                     + "','"
47                     + msg
48                     + "','"
49                     + messages.getBasename()
50                     + "');");
51                 str.append("\n"); //$NON-NLS-1$
52
}
53         }
54         return str.toString();
55     }
56
57     /**
58      * @param msgs
59      * @param messages
60      */

61     public static Messages chain(Messages msgs1, Messages msgs2) {
62         MessagesChain msgs = new MessagesChain(msgs1);
63         msgs.chain(msgs2);
64         return msgs;
65     }
66
67     public static Messages chain(String JavaDoc[] basenames) {
68         Messages head = MessagesManager.getMessages(basenames[0]);
69         MessagesChain chain = new MessagesChain(head);
70         for (int i = 1; i < basenames.length; i++) {
71             String JavaDoc basename = basenames[i];
72             Messages msgs = MessagesManager.getMessages(basename);
73             chain.chain(msgs);
74         }
75         chain.chain(MessagesManager.getMessages());
76         return chain;
77     }
78
79     /**
80      * @param messages
81      * @param basename
82      * @return
83      */

84     public static Messages chain(Messages msgs1, String JavaDoc basename) {
85         Messages msgs2 = MessagesManager.getMessages(basename);
86         return chain(msgs1, msgs2);
87     }
88
89     /**
90      * @param messages
91      * @param basename
92      * @return
93      */

94     public static Messages chain(String JavaDoc basename, Messages msgs2) {
95         Messages msgs1 = MessagesManager.getMessages(basename);
96         return chain(msgs1, msgs2);
97     }
98
99     /**
100      * @param string
101      * @return
102      */

103     public static Messages chainWithDefault(String JavaDoc basename) {
104         Messages msgs1 = MessagesManager.getMessages(basename);
105         Messages msgs2 = MessagesManager.getMessages();
106         return chain(msgs1, msgs2);
107     }
108
109     /**
110      * @param title
111      * @return
112      */

113     public static String JavaDoc javaScriptString(String JavaDoc msg) {
114         return StringUtils.replace(StringUtils.replace(msg, "'", "\\'"), "\n", "\\n");
115     }
116     
117     public static String JavaDoc get(String JavaDoc key){
118         return MessagesManager.getMessages().get(key);
119     }
120
121     public static String JavaDoc get(String JavaDoc key,String JavaDoc[] args){
122         return MessagesManager.getMessages().get(key, args);
123     }
124
125     public static String JavaDoc getWithDefault(String JavaDoc key, String JavaDoc dflt){
126         return MessagesManager.getMessages().getWithDefault(key, dflt);
127     }
128
129     public static String JavaDoc getWithDefault(String JavaDoc key, String JavaDoc dflt, String JavaDoc[] args){
130         return MessagesManager.getMessages().getWithDefault(key, args, dflt);
131     }
132
133     public static String JavaDoc get(String JavaDoc key, String JavaDoc basename){
134         return MessagesManager.getMessages(basename).get(key);
135     }
136
137     public static String JavaDoc get(String JavaDoc key, String JavaDoc basename, String JavaDoc[] args){
138         return MessagesManager.getMessages(basename).get(key, args);
139     }
140
141     public static String JavaDoc getWithDefault(String JavaDoc key, String JavaDoc dflt, String JavaDoc basename){
142         return MessagesManager.getMessages(basename).getWithDefault(key, dflt);
143     }
144
145     public static String JavaDoc getWithDefault(String JavaDoc key, String JavaDoc dflt, String JavaDoc basename, String JavaDoc[] args){
146         return MessagesManager.getMessages(basename).getWithDefault(key, args, dflt);
147     }
148
149     public static String JavaDoc getChained(String JavaDoc key, String JavaDoc[] basenames){
150         return chain(basenames).get(key);
151     }
152
153     public static String JavaDoc getChained(String JavaDoc key, String JavaDoc[] basenames, String JavaDoc[] args){
154         return chain(basenames).get(key, args);
155     }
156
157     public static String JavaDoc getChainedWithDefault(String JavaDoc key, String JavaDoc dflt, String JavaDoc[] basenames){
158         return chain(basenames).getWithDefault(key, dflt);
159     }
160
161     public static String JavaDoc getChainedWithDefault(String JavaDoc key, String JavaDoc dflt, String JavaDoc[] basenames, String JavaDoc[] args){
162         return chain(basenames).getWithDefault(key, args, dflt);
163     }
164
165 }
166
Popular Tags