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.portlet;
016    
017    import com.liferay.portal.kernel.util.JavaConstants;
018    import com.liferay.portal.model.PortletInfo;
019    
020    import java.util.Enumeration;
021    import java.util.Locale;
022    import java.util.MissingResourceException;
023    import java.util.ResourceBundle;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Eduardo Lundgren
028     */
029    public class PortletResourceBundle extends ResourceBundle {
030    
031            public PortletResourceBundle(
032                    ResourceBundle parentResourceBundle, PortletInfo portletInfo) {
033    
034                    _parentResourceBundle = parentResourceBundle;
035                    _portletInfo = portletInfo;
036            }
037    
038            public Enumeration<String> getKeys() {
039                    return _parentResourceBundle.getKeys();
040            }
041    
042            public Locale getLocale() {
043                    return _parentResourceBundle.getLocale();
044            }
045    
046            protected Object handleGetObject(String key) {
047                    try {
048                            if (_parentResourceBundle == null) {
049                                    return _getJavaxPortletString(key);
050                            }
051                            else {
052                                    return _parentResourceBundle.getObject(key);
053                            }
054                    }
055                    catch (MissingResourceException mre) {
056                            String value = _getJavaxPortletString(key);
057    
058                            if (value != null) {
059                                    return value;
060                            }
061                            else {
062                                    throw mre;
063                            }
064                    }
065            }
066            private String _getJavaxPortletString(String key) {
067                    if (key == null) {
068                            return null;
069                    }
070                    else if (key.equals(JavaConstants.JAVAX_PORTLET_TITLE)) {
071                            return _portletInfo.getTitle();
072                    }
073                    else if (key.equals(JavaConstants.JAVAX_PORTLET_SHORT_TITLE)) {
074                            return _portletInfo.getShortTitle();
075                    }
076                    else if (key.equals(JavaConstants.JAVAX_PORTLET_KEYWORDS)) {
077                            return _portletInfo.getKeywords();
078                    }
079                    else if (key.equals(JavaConstants.JAVAX_PORTLET_DESCRIPTION)) {
080                            return _portletInfo.getDescription();
081                    }
082    
083                    return null;
084            }
085    
086            private ResourceBundle _parentResourceBundle;
087            private PortletInfo _portletInfo;
088    
089    }