1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.util;
19
20import java.io.IOException;
21import java.io.InputStream;
22import java.io.Reader;
23
24/**
25 * {@code PropertyResourceBundle} loads resources from an {@code InputStream}. All resources are
26 * Strings. The resources must be of the form {@code key=value}, one
27 * resource per line (see Properties).
28 *
29 * @see ResourceBundle
30 * @see Properties
31 * @since 1.1
32 */
33public class PropertyResourceBundle extends ResourceBundle {
34
35    Properties resources;
36
37    /**
38     * Constructs a new instance of {@code PropertyResourceBundle} and loads the
39     * properties file from the specified {@code InputStream}.
40     *
41     * @param stream
42     *            the {@code InputStream}.
43     * @throws IOException
44     *             if an error occurs during a read operation on the
45     *             {@code InputStream}.
46     */
47    public PropertyResourceBundle(InputStream stream) throws IOException {
48        if (stream == null) {
49            throw new NullPointerException("stream == null");
50        }
51        resources = new Properties();
52        resources.load(stream);
53    }
54
55    /**
56     * Constructs a new resource bundle with properties read from {@code reader}.
57     *
58     * @param reader the {@code Reader}
59     * @throws IOException
60     * @since 1.6
61     */
62    public PropertyResourceBundle(Reader reader) throws IOException {
63        resources = new Properties();
64        resources.load(reader);
65    }
66
67    protected Set<String> handleKeySet(){
68        return resources.stringPropertyNames();
69    }
70
71    @SuppressWarnings("unchecked")
72    private Enumeration<String> getLocalKeys() {
73        return (Enumeration<String>) resources.propertyNames();
74    }
75
76    @Override
77    public Enumeration<String> getKeys() {
78        if (parent == null) {
79            return getLocalKeys();
80        }
81        return new Enumeration<String>() {
82            Enumeration<String> local = getLocalKeys();
83
84            Enumeration<String> pEnum = parent.getKeys();
85
86            String nextElement;
87
88            private boolean findNext() {
89                if (nextElement != null) {
90                    return true;
91                }
92                while (pEnum.hasMoreElements()) {
93                    String next = pEnum.nextElement();
94                    if (!resources.containsKey(next)) {
95                        nextElement = next;
96                        return true;
97                    }
98                }
99                return false;
100            }
101
102            public boolean hasMoreElements() {
103                if (local.hasMoreElements()) {
104                    return true;
105                }
106                return findNext();
107            }
108
109            public String nextElement() {
110                if (local.hasMoreElements()) {
111                    return local.nextElement();
112                }
113                if (findNext()) {
114                    String result = nextElement;
115                    nextElement = null;
116                    return result;
117                }
118                // Cause an exception
119                return pEnum.nextElement();
120            }
121        };
122    }
123
124    @Override
125    public Object handleGetObject(String key) {
126        return resources.get(key);
127    }
128}
129