001    /**
002     * Copyright (c) 2000-2012 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(String string, String charsetName)
041                    throws IOException {
042    
043                    _map = new HashMap<String, String>();
044    
045                    Properties properties = PropertiesUtil.load(string, charsetName);
046    
047                    LanguageResources.fixValues(_map, properties);
048            }
049    
050            public LiferayResourceBundle(InputStream inputStream, String charsetName)
051                    throws IOException {
052    
053                    this(null, inputStream, charsetName);
054            }
055    
056            public LiferayResourceBundle(
057                            ResourceBundle parentResourceBundle, InputStream inputStream,
058                            String charsetName)
059                    throws IOException {
060    
061                    setParent(parentResourceBundle);
062    
063                    _map = new HashMap<String, String>();
064    
065                    Properties properties = PropertiesUtil.load(inputStream, 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            @Override
122            public Object handleGetObject(String key) {
123                    if (key == null) {
124                            throw new NullPointerException();
125                    }
126    
127                    String value = _map.get(key);
128    
129                    if ((value == null) && ResourceBundleThreadLocal.isReplace()) {
130                            if (parent != null) {
131                                    try {
132                                            value = parent.getString(key);
133                                    }
134                                    catch (MissingResourceException mre) {
135                                    }
136                            }
137    
138                            if (value == null) {
139                                    value = ResourceBundleUtil.NULL_VALUE;
140                            }
141                    }
142    
143                    return value;
144            }
145    
146            private Map<String, String> _map;
147    
148    }