File Source: I18nMessages.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.util;
20
21 import java.text.MessageFormat;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.Map;
27 import java.util.ResourceBundle;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31
32 /**
33 * A utility class for handling i18n messaging.
34 */
35 public final class I18nMessages {
36
/*
P/P * Method: org.apache.roller.weblogger.util.I18nMessages__static_init
*
* Postconditions:
* init'ed(log)
* init'ed(messagesMap)
*/
37 private static final Log log = LogFactory.getLog(I18nMessages.class);
38
39 // locale and bundle we are using for messaging
40 private final Locale locale;
41 private final ResourceBundle bundle;
42
43 // a map of cached messages instances, keyed by locale
44 private static Map<Locale, I18nMessages> messagesMap =
45 Collections.synchronizedMap(new HashMap());
46
47
/*
P/P * Method: void org.apache.roller.weblogger.util.I18nMessages(String)
*
* Postconditions:
* init'ed(this.bundle)
* init'ed(this.locale)
* new Locale(toLocale#1) num objects <= 1
* new Locale(toLocale#2) num objects <= 1
* new Locale(toLocale#3) num objects <= 1
*/
48 private I18nMessages(String locale) {
49 Locale loc = I18nUtils.toLocale(locale);
50 this.locale = loc;
51 this.bundle = ResourceBundle.getBundle("ApplicationResources", loc);
52 }
53
/*
P/P * Method: void org.apache.roller.weblogger.util.I18nMessages(Locale)
*
* Postconditions:
* init'ed(this.bundle)
* this.locale == locale
* init'ed(this.locale)
*/
54 private I18nMessages(Locale locale) {
55 this.locale = locale;
56 this.bundle = ResourceBundle.getBundle("ApplicationResources", locale);
57 }
58
59
60 /**
61 * Get an instance for a given locale.
62 */
63 public static I18nMessages getMessages(String locale) {
64
/*
P/P * Method: I18nMessages getMessages(String)
*
* Preconditions:
* messagesMap != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@37 != null
*
* Postconditions:
* return_value != null
* new I18nMessages(getMessages#2) num objects <= 1
* init'ed(new I18nMessages(getMessages#2).bundle)
* init'ed(new I18nMessages(getMessages#2).locale)
* new Locale(toLocale#1) num objects <= 1
* new Locale(toLocale#2) num objects <= 1
* new Locale(toLocale#3) num objects <= 1
*
* Test Vectors:
* java.util.Map:get(...)@68: Inverse{null}, Addr_Set{null}
*/
65 log.debug("request for messages in locale = "+locale);
66
67 // check if we already have a message utils created for that locale
68 I18nMessages messages = messagesMap.get(locale);
69
70 // if no utils for that language yet then construct
71 if(messages == null) {
72 messages = new I18nMessages(locale);
73
74 // keep a reference to it
75 messagesMap.put(messages.getLocale(), messages);
76 }
77
78 return messages;
79 }
80
81
82 /**
83 * Get an instance for a given locale.
84 */
85 public static I18nMessages getMessages(Locale locale) {
86
/*
P/P * Method: I18nMessages getMessages(Locale)
*
* Preconditions:
* locale != null
* messagesMap != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@37 != null
*
* Postconditions:
* return_value != null
* new I18nMessages(getMessages#2) num objects <= 1
* init'ed(new I18nMessages(getMessages#2).bundle)
* new I18nMessages(getMessages#2).locale == locale
* new I18nMessages(getMessages#2).locale != null
*
* Test Vectors:
* java.util.Map:get(...)@90: Inverse{null}, Addr_Set{null}
*/
87 log.debug("request for messages in locale = "+locale.toString());
88
89 // check if we already have a message utils created for that locale
90 I18nMessages messages = messagesMap.get(locale);
91
92 // if no utils for that language yet then construct
93 if(messages == null) {
94 messages = new I18nMessages(locale);
95
96 // keep a reference to it
97 messagesMap.put(messages.getLocale(), messages);
98 }
99
100 return messages;
101 }
102
103
104 /**
105 * The locale representing this message utils.
106 */
107 public final Locale getLocale() {
/*
P/P * Method: Locale getLocale()
*
* Postconditions:
* return_value == this.locale
* init'ed(return_value)
*/
108 return this.locale;
109 }
110
111
112 /**
113 * Get a message from the bundle.
114 */
115 public final String getString(String key) {
116
117 try {
/*
P/P * Method: String getString(String)
*
* Preconditions:
* (soft) this.bundle != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@37 != null
*
* Postconditions:
* init'ed(return_value)
*/
118 return bundle.getString(key);
119 } catch (Exception e) {
120 // send a warning in the logs
121 log.warn("Error getting key "+key);
122 return key;
123 }
124 }
125
126
127 /**
128 * Get a message from the bundle and substitute the given args into
129 * the message contents.
130 */
131 public final String getString(String key, List args) {
132
133 try {
/*
P/P * Method: String getString(String, List)
*
* Preconditions:
* (soft) args != null
* (soft) this.bundle != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@37 != null
*
* Postconditions:
* init'ed(return_value)
*/
134 String msg = bundle.getString(key);
135 return MessageFormat.format(msg, args.toArray());
136 } catch (Exception e) {
137 // send a warning in the logs
138 log.warn("Error getting key "+key, e);
139 return key;
140 }
141 }
142
143
144 /**
145 * Get a message from the bundle and substitute the given args into
146 * the message contents.
147 */
148 public final String getString(String key, Object[] args) {
149
150 try {
/*
P/P * Method: String getString(String, Object[])
*
* Preconditions:
* (soft) this.bundle != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@37 != null
*
* Postconditions:
* init'ed(return_value)
*/
151 String msg = bundle.getString(key);
152 return MessageFormat.format(msg, args);
153 } catch (Exception e) {
154 // send a warning in the logs
155 log.warn("Error getting key "+key, e);
156 return key;
157 }
158 }
159
160 }
SofCheck Inspector Build Version : 2.18479
| I18nMessages.java |
2009-Jan-02 14:24:46 |
| I18nMessages.class |
2009-Sep-04 03:12:32 |