001    /**
002     * Copyright (c) 2000-present 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.Collections;
021    import java.util.Enumeration;
022    import java.util.HashMap;
023    import java.util.HashSet;
024    import java.util.Locale;
025    import java.util.Map;
026    import java.util.ResourceBundle;
027    import java.util.Set;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Eduardo Lundgren
032     * @author Shuyang Zhou
033     */
034    public class PortletResourceBundle extends ResourceBundle {
035    
036            public PortletResourceBundle(PortletInfo portletInfo) {
037                    this(null, portletInfo);
038            }
039    
040            public PortletResourceBundle(
041                    ResourceBundle parentResourceBundle, PortletInfo portletInfo) {
042    
043                    parent = parentResourceBundle;
044    
045                    String description = portletInfo.getDescription();
046    
047                    if (description != null) {
048                            _portletInfos.put(
049                                    JavaConstants.JAVAX_PORTLET_DESCRIPTION, description);
050                    }
051    
052                    String keywords = portletInfo.getKeywords();
053    
054                    if (keywords != null) {
055                            _portletInfos.put(JavaConstants.JAVAX_PORTLET_KEYWORDS, keywords);
056                    }
057    
058                    String shortTitle = portletInfo.getShortTitle();
059    
060                    if (shortTitle != null) {
061                            _portletInfos.put(
062                                    JavaConstants.JAVAX_PORTLET_SHORT_TITLE, shortTitle);
063                    }
064    
065                    String title = portletInfo.getTitle();
066    
067                    if (title != null) {
068                            _portletInfos.put(JavaConstants.JAVAX_PORTLET_TITLE, title);
069                    }
070            }
071    
072            @Override
073            public Enumeration<String> getKeys() {
074                    if (parent == null) {
075                            return Collections.enumeration(_portletInfos.keySet());
076                    }
077    
078                    Set<String> keys = new HashSet<>(parent.keySet());
079    
080                    keys.addAll(_portletInfos.keySet());
081    
082                    return Collections.enumeration(keys);
083            }
084    
085            @Override
086            public Locale getLocale() {
087                    if (parent == null) {
088                            return null;
089                    }
090    
091                    return parent.getLocale();
092            }
093    
094            @Override
095            protected Object handleGetObject(String key) {
096                    if (key == null) {
097                            throw new NullPointerException();
098                    }
099    
100                    if ((parent != null) && parent.containsKey(key)) {
101                            return parent.getString(key);
102                    }
103    
104                    return _portletInfos.get(key);
105            }
106    
107            @Override
108            protected Set<String> handleKeySet() {
109                    return _portletInfos.keySet();
110            }
111    
112            private final Map<String, String> _portletInfos = new HashMap<>();
113    
114    }