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.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, String charsetName)
061                    throws IOException {
062    
063                    _map = new HashMap<String, String>();
064    
065                    Properties properties = PropertiesUtil.load(string, charsetName);
066    
067                    LanguageResources.fixValues(_map, properties);
068            }
069    
070            @Override
071            public Enumeration<String> getKeys() {
072                    final Set<String> keys = _map.keySet();
073    
074                    final Enumeration<String> parentKeys =
075                            (parent == null) ? null : parent.getKeys();
076    
077                    final Iterator<String> itr = keys.iterator();
078    
079                    return new Enumeration<String>() {
080                            String next = null;
081    
082                            public boolean hasMoreElements() {
083                                    if (next == null) {
084                                            if (itr.hasNext()) {
085                                                    next = itr.next();
086                                            }
087                                            else if (parentKeys != null) {
088                                                    while ((next == null) && parentKeys.hasMoreElements()) {
089                                                            next = parentKeys.nextElement();
090    
091                                                            if (keys.contains(next)) {
092                                                                    next = null;
093                                                            }
094                                                    }
095                                            }
096                                    }
097    
098                                    if (next != null) {
099                                            return true;
100                                    }
101                                    else {
102                                            return false;
103                                    }
104                            }
105    
106                            public String nextElement() {
107                                    if (hasMoreElements()) {
108                                            String result = next;
109    
110                                            next = null;
111    
112                                            return result;
113                                    }
114                                    else {
115                                            throw new NoSuchElementException();
116                                    }
117                            }
118    
119                    };
120            }
121    
122            @Override
123            public Object handleGetObject(String key) {
124                    if (key == null) {
125                            throw new NullPointerException();
126                    }
127    
128                    String value = _map.get(key);
129    
130                    if ((value == null) && ResourceBundleThreadLocal.isReplace()) {
131                            if (parent != null) {
132                                    try {
133                                            value = parent.getString(key);
134                                    }
135                                    catch (MissingResourceException mre) {
136                                    }
137                            }
138    
139                            if (value == null) {
140                                    value = ResourceBundleUtil.NULL_VALUE;
141                            }
142                    }
143    
144                    return value;
145            }
146    
147            private Map<String, String> _map;
148    
149    }