001    /**
002     * Copyright (c) 2000-2013 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.kernel.util.ResourceBundleThreadLocal;
019    import com.liferay.portal.kernel.util.ResourceBundleUtil;
020    import com.liferay.portal.model.PortletInfo;
021    
022    import java.util.Enumeration;
023    import java.util.Locale;
024    import java.util.MissingResourceException;
025    import java.util.ResourceBundle;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Eduardo Lundgren
030     * @author Shuyang Zhou
031     */
032    public class PortletResourceBundle extends ResourceBundle {
033    
034            public PortletResourceBundle(PortletInfo portletInfo) {
035                    this(null, portletInfo);
036            }
037    
038            public PortletResourceBundle(
039                    ResourceBundle parentResourceBundle, PortletInfo portletInfo) {
040    
041                    parent = parentResourceBundle;
042    
043                    _portletInfo = portletInfo;
044            }
045    
046            @Override
047            public Enumeration<String> getKeys() {
048                    return parent.getKeys();
049            }
050    
051            @Override
052            public Locale getLocale() {
053                    return parent.getLocale();
054            }
055    
056            @Override
057            protected Object handleGetObject(String key) {
058                    if (key == null) {
059                            throw new NullPointerException();
060                    }
061    
062                    String value = null;
063    
064                    if (parent != null) {
065                            try {
066                                    value = parent.getString(key);
067                            }
068                            catch (MissingResourceException mre) {
069                            }
070                    }
071    
072                    if ((value == null) || (value == ResourceBundleUtil.NULL_VALUE)) {
073                            value = _getJavaxPortletString(key);
074                    }
075    
076                    if ((value == null) && ResourceBundleThreadLocal.isReplace()) {
077                            value = ResourceBundleUtil.NULL_VALUE;
078                    }
079    
080                    return value;
081            }
082    
083            private String _getJavaxPortletString(String key) {
084                    if (key.equals(JavaConstants.JAVAX_PORTLET_TITLE)) {
085                            return _portletInfo.getTitle();
086                    }
087                    else if (key.equals(JavaConstants.JAVAX_PORTLET_SHORT_TITLE)) {
088                            return _portletInfo.getShortTitle();
089                    }
090                    else if (key.equals(JavaConstants.JAVAX_PORTLET_KEYWORDS)) {
091                            return _portletInfo.getKeywords();
092                    }
093                    else if (key.equals(JavaConstants.JAVAX_PORTLET_DESCRIPTION)) {
094                            return _portletInfo.getDescription();
095                    }
096    
097                    return null;
098            }
099    
100            private PortletInfo _portletInfo;
101    
102    }