01: /**
02: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
03: *
04: * Permission is hereby granted, free of charge, to any person obtaining a copy
05: * of this software and associated documentation files (the "Software"), to deal
06: * in the Software without restriction, including without limitation the rights
07: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
08: * copies of the Software, and to permit persons to whom the Software is
09: * furnished to do so, subject to the following conditions:
10: *
11: * The above copyright notice and this permission notice shall be included in
12: * all copies or substantial portions of the Software.
13: *
14: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20: * SOFTWARE.
21: */package com.liferay.util;
22:
23: import java.text.MessageFormat;
24:
25: import java.util.Locale;
26: import java.util.ResourceBundle;
27:
28: import org.apache.commons.logging.Log;
29: import org.apache.commons.logging.LogFactory;
30:
31: /**
32: * <a href="ResourceBundleUtil.java.html"><b><i>View Source</i></b></a>
33: *
34: * <p>
35: * This class provides a convenience routine named getString() that
36: * gets the value of a ResourceBundle key, but also provides the ability
37: * to perform search & replace on tokens within the value of the key.
38: * </p>
39: *
40: * @author Neil Griffin
41: *
42: */
43: public class ResourceBundleUtil {
44:
45: public static String getString(ResourceBundle bundle,
46: Locale locale, String key, Object[] args) {
47:
48: String value = null;
49:
50: if (bundle == null) {
51: if (_log.isErrorEnabled()) {
52: _log.error("Resource bundle is null");
53: }
54: } else {
55:
56: // Get the value associated with the specified key, and substitute
57: // any arguuments like {0}, {1}, {2}, etc. with the specified
58: // argument values.
59:
60: value = bundle.getString(key);
61:
62: if (value == null) {
63: if (_log.isWarnEnabled()) {
64: _log.warn("No value found for key " + key);
65: }
66: } else {
67: if ((args != null) && (args.length > 0)) {
68: MessageFormat format = new MessageFormat(value,
69: locale);
70:
71: value = format.format(args);
72: }
73: }
74: }
75:
76: if (value == null) {
77: value = key;
78: }
79:
80: return value;
81: }
82:
83: private static Log _log = LogFactory
84: .getLog(ResourceBundleUtil.class);
85:
86: }
|