001    /**
002     * Copyright (c) 2000-2010 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.kernel.util;
016    
017    import java.io.IOException;
018    import java.io.InputStream;
019    
020    import java.util.Enumeration;
021    import java.util.HashMap;
022    import java.util.Iterator;
023    import java.util.Map;
024    import java.util.NoSuchElementException;
025    import java.util.ResourceBundle;
026    import java.util.Set;
027    
028    /**
029     * @author Shuyang Zhou
030     * @author Brian Wing Shun Chan
031     */
032    public class PropertyResourceBundle extends ResourceBundle {
033    
034            @SuppressWarnings("rawtypes")
035            public PropertyResourceBundle(String string, String charsetName)
036                    throws IOException {
037    
038                    _map = new HashMap(PropertiesUtil.load(string, charsetName));
039            }
040    
041            @SuppressWarnings("rawtypes")
042            public PropertyResourceBundle(InputStream inputStream, String charsetName)
043                    throws IOException {
044    
045                    _map = new HashMap(PropertiesUtil.load(inputStream, charsetName));
046            }
047    
048            public Object handleGetObject(String key) {
049                    if (key == null) {
050                            throw new NullPointerException();
051                    }
052    
053                    return _map.get(key);
054            }
055    
056            public Enumeration<String> getKeys() {
057                    final Set<String> keys = _map.keySet();
058    
059                    final Enumeration<String> parentKeys = parent.getKeys();
060    
061                    final Iterator<String> itr = keys.iterator();
062    
063                    return new Enumeration<String>() {
064                            String next = null;
065    
066                            public boolean hasMoreElements() {
067                                    if (next == null) {
068                                            if (itr.hasNext()) {
069                                                    next = itr.next();
070                                            }
071                                            else if (parentKeys != null) {
072                                                    while ((next == null) && parentKeys.hasMoreElements()) {
073                                                            next = parentKeys.nextElement();
074    
075                                                            if (keys.contains(next)) {
076                                                                    next = null;
077                                                            }
078                                                    }
079                                            }
080                                    }
081    
082                                    if (next != null) {
083                                            return true;
084                                    }
085                                    else {
086                                            return false;
087                                    }
088                            }
089    
090                            public String nextElement() {
091                                    if (hasMoreElements()) {
092                                            String result = next;
093    
094                                            next = null;
095    
096                                            return result;
097                                    }
098                                    else {
099                                            throw new NoSuchElementException();
100                                    }
101                            }
102                    };
103            }
104    
105            private Map<String, String> _map;
106    
107    }