KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > common > util > ApplicationResourceStorage


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 package com.blandware.atleap.common.util;
17
18 import java.io.Serializable JavaDoc;
19 import java.text.MessageFormat JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.HashSet JavaDoc;
24
25 /**
26  * <p>Contains set of bundles, each contains set of keys mapped to string values</p>
27  * <p>Does not accept <code>null</code> values.
28  * <p><a HREF="ApplicationResourceStorage.java.htm"><i>View Source</i></a></p>
29  *
30  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
31  * @version $Revision: 1.8 $ $Date: 2006/03/16 11:09:36 $
32  */

33 public class ApplicationResourceStorage implements Serializable JavaDoc {
34
35     /**
36      * Stored bundles
37      */

38     protected BundleStorage bundles = new BundleStorage();
39
40     /**
41      * Adds message for specified key and locale
42      *
43      * @param locale Locale of message
44      * @param key Key to associate value with
45      * @param value Value to associate with key
46      */

47     public synchronized void putMessage(Locale JavaDoc locale, String JavaDoc key, String JavaDoc value) {
48         if ( value == null ) {
49             throw new NullPointerException JavaDoc("Value cannot be null");
50         }
51
52         Bundle bundle = bundles.getBundle(locale);
53         if ( bundle == null ) {
54             bundle = new Bundle(locale);
55             bundles.addBundle(bundle);
56         }
57
58         bundles.getBundle(locale).putMessage(key, value);
59     }
60
61     /**
62      * Returns set of keys stored in bundle associated with specified locale
63      *
64      * @param locale Locale of bundle to get keys
65      * @return Set of keys, stored in bundle associated with specified locale
66      */

67     public Set JavaDoc getKeys(Locale JavaDoc locale) {
68         Bundle bundle = bundles.getBundle(locale);
69         Set JavaDoc keys = new HashSet JavaDoc();
70         if ( bundle != null ) {
71             keys = bundle.getKeys();
72         }
73         return keys;
74     }
75
76     /**
77      * Adds specified bundle to the storage. Replace existing one in storage.
78      *
79      * @param bundle Bundle to add to storage
80      */

81     public void mergeBundle(Bundle bundle) {
82         bundles.addBundle(bundle);
83     }
84
85     /**
86      * Removes all messages for specified locale
87      *
88      * @param locale Locale to remove messages
89      */

90     public void dropBundle(Locale JavaDoc locale) {
91         bundles.removeBundle(locale);
92     }
93
94     /**
95      * Returns a text message after parametric replacement of the specified parameter placeholders
96      *
97      * @param locale Locale to get message for
98      * @param key Key to get associated value
99      * @param args Arguments to put in message
100      * @return Formatted message for specified key and locale
101      */

102     public String JavaDoc getMessage(Locale JavaDoc locale, String JavaDoc key, Object JavaDoc[] args) {
103         Bundle bundle = bundles.getBundle(locale);
104         return bundle != null ? bundle.getMessage(key, args) : null;
105     }
106
107     /**
108      * Returns <code>true</code> if message for specified key and locale is present and <code>false</code> otherwise
109      *
110      * @param locale Locale to get bundle for
111      * @param key Key to search message
112      * @return whether message is present
113      */

114     public boolean isMessagePresent(Locale JavaDoc locale, String JavaDoc key) {
115         return getMessage(locale, key, null) != null;
116     }
117
118     /**
119      * Represents list of mappings <code>key</code> -&gt; <code>value</code>
120      */

121     public static class Bundle implements Serializable JavaDoc {
122
123         /**
124          * Stored messages
125          */

126         protected HashMap JavaDoc messages = new HashMap JavaDoc();
127
128         /**
129          * Cached <code>java.text.MessageFormat</code> objects
130          */

131         protected HashMap JavaDoc formats = new HashMap JavaDoc();
132
133         /**
134          * Locale associated with this bundle
135          */

136         protected Locale JavaDoc locale;
137
138         /**
139          * Creates new instance of bundle
140          *
141          * @param locale Locale to associate with bundle
142          */

143         public Bundle(Locale JavaDoc locale) {
144             if ( locale == null ) {
145                 throw new IllegalArgumentException JavaDoc("Locale cannot be null");
146             }
147             this.locale = locale;
148         }
149
150         /**
151          * Returns locale of this bundle
152          *
153          * @return Locale of this bundle
154          */

155         public Locale JavaDoc getLocale() {
156             return locale;
157         }
158
159         /**
160          * Returns a text message without any parametric replacement of placeholders,
161          * which are probably presented in string
162          *
163          * @param key Key to search message for
164          * @return Value associated with specified key or <code>null</code> if none found
165          */

166         public String JavaDoc getMessage(String JavaDoc key) {
167             return (String JavaDoc) messages.get(key);
168         }
169
170         /**
171          * Returns a text message after parametric replacement of the specified parameter placeholders
172          *
173          * @param key Key to retrieve message for
174          * @param args Array of arguments
175          * @return Formatted message, or <code>null</code> if none found
176          */

177         public String JavaDoc getMessage(String JavaDoc key, Object JavaDoc[] args) {
178
179             if ( args == null || args.length == 0 ) {
180                 return getMessage(key);
181             }
182
183             MessageFormat JavaDoc format = null;
184             synchronized ( formats ) {
185                 format = (MessageFormat JavaDoc) formats.get(key);
186                 if ( format == null ) {
187                     String JavaDoc msg = getMessage(key);
188
189                     if ( msg == null ) {
190                         return null;
191                     }
192
193                     format = new MessageFormat JavaDoc(escape(msg));
194                     format.setLocale(locale);
195
196                     formats.put(key, format);
197                 }
198             }
199
200             return format.format(args);
201         }
202
203         /**
204          * Returns set of keys stored in this bundle
205          *
206          * @return Set of keys
207          */

208         public Set JavaDoc getKeys() {
209             return messages.keySet();
210         }
211
212         /**
213          * Adds new key-value pair to the bundle
214          *
215          * @param key Key to associate value with
216          * @param value Value to associate with key
217          */

218         public void putMessage(String JavaDoc key, String JavaDoc value) {
219             messages.put(key, value);
220             
221             // flush format associated with this key if any
222
formats.remove(key);
223         }
224
225         /**
226          * Returns number of stored messages
227          *
228          * @return Number of stored messages
229          */

230         public int getSize() {
231             return messages.size();
232         }
233
234         /**
235          * Escapes any single quote characters in string
236          *
237          * @param string The string to escape single quote characters in
238          * @return escaped string
239          */

240         protected String JavaDoc escape(String JavaDoc string) {
241
242             if ( (string == null) || (string.indexOf('\'') < 0) ) {
243                 return string;
244             }
245
246             int n = string.length();
247             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(n);
248
249             for ( int i = 0; i < n; i++ ) {
250                 char ch = string.charAt(i);
251
252                 if ( ch == '\'' ) {
253                     sb.append('\'');
254                 }
255
256                 sb.append(ch);
257             }
258
259             return sb.toString();
260
261         }
262     }
263
264     /**
265      * Stores bundles for different locales
266      */

267     protected static class BundleStorage implements Serializable JavaDoc {
268
269         /**
270          * Stored bundles
271          */

272         protected HashMap JavaDoc bundles = new HashMap JavaDoc();
273
274         /**
275          * Adds new bundle to storage and associates it with corresponding locale
276          *
277          * @param bundle Bundle to add to storage
278          */

279         public void addBundle(Bundle bundle) {
280             Locale JavaDoc locale = bundle.getLocale();
281             String JavaDoc localeKey = getLocaleKey(locale);
282             bundles.put(localeKey, bundle);
283         }
284
285         /**
286          * Removes bundle from storage
287          *
288          * @param locale Locale of bundle to remove
289          */

290         public void removeBundle(Locale JavaDoc locale) {
291             bundles.remove(getLocaleKey(locale));
292         }
293
294         /**
295          * Returns bundle associated with specified locale
296          *
297          * @param locale Locale to search bundles for
298          * @return Bundle associated with specified locale
299          */

300         public Bundle getBundle(Locale JavaDoc locale) {
301             return (Bundle) bundles.get(getLocaleKey(locale));
302         }
303
304         /**
305          * Returns key representing specified locale to use in map of bundles
306          *
307          * @param locale Locale to get key for
308          * @return Key representing specified locale to use in map of bundles
309          */

310         protected String JavaDoc getLocaleKey(Locale JavaDoc locale) {
311             return locale.toString();
312         }
313     }
314
315 }
316
Popular Tags