001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.language;
016    
017    import com.liferay.portal.kernel.util.PropertiesUtil;
018    import com.liferay.portal.kernel.util.ResourceBundleThreadLocal;
019    import com.liferay.portal.kernel.util.ResourceBundleUtil;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    
024    import java.util.Enumeration;
025    import java.util.HashMap;
026    import java.util.Iterator;
027    import java.util.Map;
028    import java.util.MissingResourceException;
029    import java.util.NoSuchElementException;
030    import java.util.Properties;
031    import java.util.ResourceBundle;
032    import java.util.Set;
033    
034    /**
035     * @author Shuyang Zhou
036     * @author Brian Wing Shun Chan
037     */
038    public class LiferayResourceBundle extends ResourceBundle {
039    
040            public LiferayResourceBundle(InputStream inputStream, String charsetName)
041                    throws IOException {
042    
043                    this(null, inputStream, charsetName);
044            }
045    
046            public LiferayResourceBundle(
047                            ResourceBundle parentResourceBundle, InputStream inputStream,
048                            String charsetName)
049                    throws IOException {
050    
051                    setParent(parentResourceBundle);
052    
053                    _map = new HashMap<String, String>();
054    
055                    Properties properties = PropertiesUtil.load(inputStream, charsetName);
056    
057                    LanguageResources.fixValues(_map, properties);
058            }
059    
060            public LiferayResourceBundle(String string) throws IOException {
061                    _map = new HashMap<String, String>();
062    
063                    Properties properties = PropertiesUtil.load(string);
064    
065                    LanguageResources.fixValues(_map, properties);
066            }
067    
068            /**
069             * @deprecated As of 6.2.0, with no direct replacement
070             */
071            @Deprecated
072            public LiferayResourceBundle(String string, String charsetName)
073                    throws IOException {
074    
075                    _map = new HashMap<String, String>();
076    
077                    Properties properties = PropertiesUtil.load(string, charsetName);
078    
079                    LanguageResources.fixValues(_map, properties);
080            }
081    
082            @Override
083            public Enumeration<String> getKeys() {
084                    final Set<String> keys = _map.keySet();
085    
086                    final Enumeration<String> parentKeys =
087                            (parent == null) ? null : parent.getKeys();
088    
089                    final Iterator<String> itr = keys.iterator();
090    
091                    return new Enumeration<String>() {
092                            String next = null;
093    
094                            @Override
095                            public boolean hasMoreElements() {
096                                    if (next == null) {
097                                            if (itr.hasNext()) {
098                                                    next = itr.next();
099                                            }
100                                            else if (parentKeys != null) {
101                                                    while ((next == null) && parentKeys.hasMoreElements()) {
102                                                            next = parentKeys.nextElement();
103    
104                                                            if (keys.contains(next)) {
105                                                                    next = null;
106                                                            }
107                                                    }
108                                            }
109                                    }
110    
111                                    if (next != null) {
112                                            return true;
113                                    }
114                                    else {
115                                            return false;
116                                    }
117                            }
118    
119                            @Override
120                            public String nextElement() {
121                                    if (hasMoreElements()) {
122                                            String result = next;
123    
124                                            next = null;
125    
126                                            return result;
127                                    }
128                                    else {
129                                            throw new NoSuchElementException();
130                                    }
131                            }
132    
133                    };
134            }
135    
136            @Override
137            public Object handleGetObject(String key) {
138                    if (key == null) {
139                            throw new NullPointerException();
140                    }
141    
142                    String value = _map.get(key);
143    
144                    if ((value == null) && ResourceBundleThreadLocal.isReplace()) {
145                            if (parent != null) {
146                                    try {
147                                            value = parent.getString(key);
148                                    }
149                                    catch (MissingResourceException mre) {
150                                    }
151                            }
152    
153                            if (value == null) {
154                                    value = ResourceBundleUtil.NULL_VALUE;
155                            }
156                    }
157    
158                    return value;
159            }
160    
161            private Map<String, String> _map;
162    
163    }