001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.text.MessageFormat;
021    
022    import java.util.Locale;
023    import java.util.ResourceBundle;
024    
025    /**
026     * @author Neil Griffin
027     */
028    public class ResourceBundleUtil {
029    
030            public static String getString(
031                    ResourceBundle resourceBundle, Locale locale, String key,
032                    Object[] arguments) {
033    
034                    String value = null;
035    
036                    if (resourceBundle == null) {
037                            if (_log.isErrorEnabled()) {
038                                    _log.error("Resource bundle is null");
039                            }
040                    }
041                    else {
042    
043                            // Get the value associated with the specified key, and substitute
044                            // any arguuments like {0}, {1}, {2}, etc. with the specified
045                            // argument values.
046    
047                            value = resourceBundle.getString(key);
048    
049                            if (value == null) {
050                                    if (_log.isWarnEnabled()) {
051                                            _log.warn("No value found for key " + key);
052                                    }
053                            }
054                            else {
055                                    if ((arguments != null) && (arguments.length > 0)) {
056                                            MessageFormat messageFormat = new MessageFormat(
057                                                    value, locale);
058    
059                                            value = messageFormat.format(arguments);
060                                    }
061                            }
062                    }
063    
064                    if (value == null) {
065                            value = key;
066                    }
067    
068                    return value;
069            }
070    
071            private static Log _log = LogFactoryUtil.getLog(ResourceBundleUtil.class);
072    
073    }