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.portal.language;
016    
017    import com.liferay.portal.kernel.util.PropertiesUtil;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    
022    import java.util.Collections;
023    import java.util.Enumeration;
024    import java.util.HashMap;
025    import java.util.Map;
026    import java.util.Properties;
027    import java.util.ResourceBundle;
028    import java.util.Set;
029    
030    /**
031     * @author Shuyang Zhou
032     * @author Brian Wing Shun Chan
033     */
034    public class LiferayResourceBundle extends ResourceBundle {
035    
036            public LiferayResourceBundle(InputStream inputStream, String charsetName)
037                    throws IOException {
038    
039                    this(null, inputStream, charsetName);
040            }
041    
042            public LiferayResourceBundle(
043                            ResourceBundle parentResourceBundle, InputStream inputStream,
044                            String charsetName)
045                    throws IOException {
046    
047                    setParent(parentResourceBundle);
048    
049                    _map = new HashMap<>();
050    
051                    Properties properties = PropertiesUtil.load(inputStream, charsetName);
052    
053                    LanguageResources.fixValues(_map, properties);
054            }
055    
056            public LiferayResourceBundle(String string) throws IOException {
057                    _map = new HashMap<>();
058    
059                    Properties properties = PropertiesUtil.load(string);
060    
061                    LanguageResources.fixValues(_map, properties);
062            }
063    
064            @Override
065            public Enumeration<String> getKeys() {
066                    Set<String> keys = _map.keySet();
067    
068                    if (parent == null) {
069                            return Collections.enumeration(keys);
070                    }
071    
072                    return new ResourceBundleEnumeration(keys, parent.getKeys());
073            }
074    
075            @Override
076            public Object handleGetObject(String key) {
077                    if (key == null) {
078                            throw new NullPointerException();
079                    }
080    
081                    return _map.get(key);
082            }
083    
084            @Override
085            protected Set<String> handleKeySet() {
086                    return _map.keySet();
087            }
088    
089            private final Map<String, String> _map;
090    
091    }