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;
22
23/**
24 * {@code PropertyResourceBundle} loads resources from an {@code InputStream}. All resources are
25 * Strings. The resources must be of the form {@code key=value}, one
26 * resource per line (see Properties).
27 *
28 * @see ResourceBundle
29 * @see Properties
30 * @since 1.1
31 */
32public class PropertyResourceBundle extends ResourceBundle {
33
34    Properties resources;
35
36    /**
37     * Constructs a new instance of {@code PropertyResourceBundle} and loads the
38     * properties file from the specified {@code InputStream}.
39     *
40     * @param stream
41     *            the {@code InputStream}.
42     * @throws IOException
43     *             if an error occurs during a read operation on the
44     *             {@code InputStream}.
45     */
46    public PropertyResourceBundle(InputStream stream) throws IOException {
47        resources = new Properties();
48        resources.load(stream);
49    }
50
51    @SuppressWarnings("unchecked")
52    private Enumeration<String> getLocalKeys() {
53        return (Enumeration<String>) resources.propertyNames();
54    }
55
56    /**
57     * Returns the names of the resources contained in this
58     * PropertyResourceBundle.
59     *
60     * @return an Enumeration of the resource names
61     */
62    @Override
63    public Enumeration<String> getKeys() {
64        if (parent == null) {
65            return getLocalKeys();
66        }
67        return new Enumeration<String>() {
68            Enumeration<String> local = getLocalKeys();
69
70            Enumeration<String> pEnum = parent.getKeys();
71
72            String nextElement;
73
74            private boolean findNext() {
75                if (nextElement != null) {
76                    return true;
77                }
78                while (pEnum.hasMoreElements()) {
79                    String next = pEnum.nextElement();
80                    if (!resources.containsKey(next)) {
81                        nextElement = next;
82                        return true;
83                    }
84                }
85                return false;
86            }
87
88            public boolean hasMoreElements() {
89                if (local.hasMoreElements()) {
90                    return true;
91                }
92                return findNext();
93            }
94
95            public String nextElement() {
96                if (local.hasMoreElements()) {
97                    return local.nextElement();
98                }
99                if (findNext()) {
100                    String result = nextElement;
101                    nextElement = null;
102                    return result;
103                }
104                // Cause an exception
105                return pEnum.nextElement();
106            }
107        };
108    }
109
110    /**
111     * Returns the named resource from this PropertyResourceBundle, or null if
112     * the resource is not found.
113     *
114     * @param key
115     *            the name of the resource
116     * @return the resource object
117     */
118    @Override
119    public Object handleGetObject(String key) {
120        return resources.get(key);
121    }
122}
123