001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.util;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022:
023: /**
024: * PropertyResourceBundle loads resources from an InputStream. All resources are
025: * Strings. The resources must be of the form <code>key=value</code>, one
026: * resource per line.
027: *
028: * @see ResourceBundle
029: * @see Properties
030: * @since 1.1
031: */
032: public class PropertyResourceBundle extends ResourceBundle {
033:
034: Properties resources;
035:
036: /**
037: * Constructs a new instance of PropertyResourceBundle and loads the
038: * properties file from the specified input stream.
039: *
040: * @param stream
041: * the input stream
042: * @throws IOException
043: */
044: public PropertyResourceBundle(InputStream stream)
045: throws IOException {
046: resources = new Properties();
047: resources.load(stream);
048: }
049:
050: @SuppressWarnings("unchecked")
051: private Enumeration<String> getLocalKeys() {
052: return (Enumeration<String>) resources.propertyNames();
053: }
054:
055: /**
056: * Answers the names of the resources contained in this
057: * PropertyResourceBundle.
058: *
059: * @return an Enumeration of the resource names
060: */
061: @Override
062: public Enumeration<String> getKeys() {
063: if (parent == null) {
064: return getLocalKeys();
065: }
066: return new Enumeration<String>() {
067: Enumeration<String> local = getLocalKeys();
068:
069: Enumeration<String> pEnum = parent.getKeys();
070:
071: String nextElement;
072:
073: private boolean findNext() {
074: if (nextElement != null) {
075: return true;
076: }
077: while (pEnum.hasMoreElements()) {
078: String next = pEnum.nextElement();
079: if (!resources.containsKey(next)) {
080: nextElement = next;
081: return true;
082: }
083: }
084: return false;
085: }
086:
087: public boolean hasMoreElements() {
088: if (local.hasMoreElements()) {
089: return true;
090: }
091: return findNext();
092: }
093:
094: public String nextElement() {
095: if (local.hasMoreElements()) {
096: return local.nextElement();
097: }
098: if (findNext()) {
099: String result = nextElement;
100: nextElement = null;
101: return result;
102: }
103: // Cause an exception
104: return pEnum.nextElement();
105: }
106: };
107: }
108:
109: /**
110: * Answers the named resource from this PropertyResourceBundle, or null if
111: * the resource is not found.
112: *
113: * @param key
114: * the name of the resource
115: * @return the resource object
116: */
117: @Override
118: public Object handleGetObject(String key) {
119: return resources.get(key);
120: }
121: }
|