2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / util / PropertyResourceBundle.java
blobd873e134e627a84d6ddf46921fb2b6b7418736e9
1 /* PropertyResourceBundle -- a resource bundle built from a Property file
2 Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.util;
41 import java.io.IOException;
42 import java.io.InputStream;
44 /**
45 * This class is a concrete <code>ResourceBundle</code> that gets it
46 * resources from a property file. This implies that the resources are
47 * strings. For more information about resource bundles see the class
48 * <code>ResourceBundle</code>.
50 * You should not use this class directly, or subclass it, but you get
51 * an object of this class automatically when you call
52 * <code>ResourceBundle.getBundle()</code> and there is a properties
53 * file.
55 * If there is also a class for this resource and the same locale, the
56 * class will be chosen. The properties file should have the name of the
57 * resource bundle, appended with the locale (e.g. <code>_de</code) and the
58 * extension <code>.properties</code>. The file should have the same format
59 * as for <code>Properties.load()</code>
61 * An example of a properties file for the german language is given
62 * here. This extends the example given in ListResourceBundle.
63 * Create a file MyResource_de.properties with the following contents
64 * and put it in the CLASSPATH. (The char <code>\u00e4</code> is the
65 * german umlaut)
68 <pre>
69 s1=3
70 s2=MeineDisk
71 s3=3. M\u00e4rz 96
72 s4=Die Diskette ''{1}'' enth\u00e4lt {0} in {2}.
73 s5=0
74 s6=keine Dateien
75 s7=1
76 s8=eine Datei
77 s9=2
78 s10={0,number} Dateien
79 s11=Die Formatierung warf eine Exception: {0}
80 s12=FEHLER
81 s13=Ergebnis
82 s14=Dialog
83 s15=Auswahlkriterium
84 s16=1,3
85 </pre>
87 * @author Jochen Hoenicke
88 * @see ResourceBundle
89 * @see ListResourceBundle
90 * @see Properties#load()
91 * @since 1.1
92 * @status updated to 1.4
94 public class PropertyResourceBundle extends ResourceBundle
96 /** The properties file this bundle is based on. */
97 private Properties properties;
99 /**
100 * Creates a new property resource bundle.
102 * @param stream an input stream, where the resources are read from
103 * @throws NullPointerException if stream is null
104 * @throws IOException if reading the stream fails
106 public PropertyResourceBundle(InputStream stream) throws IOException
108 properties = new Properties();
109 properties.load(stream);
113 * Called by <code>getObject</code> when a resource is needed. This
114 * returns the resource given by the key.
116 * @param key the key of the resource
117 * @return the resource for the key, or null if it doesn't exist
119 public Object handleGetObject(String key)
121 return properties.getProperty(key);
125 * This method should return all keys for which a resource exists.
127 * @return an enumeration of the keys
129 public Enumeration getKeys()
131 if (parent == null)
132 return properties.propertyNames();
133 // We make a new Set that holds all the keys, then return an enumeration
134 // for that. This prevents modifications from ruining the enumeration,
135 // as well as ignoring duplicates.
136 Set s = new HashSet();
137 Enumeration e = properties.propertyNames();
138 while (e.hasMoreElements())
139 s.add(e.nextElement());
140 ResourceBundle bundle = parent;
141 // Eliminate tail recursion.
144 e = bundle.getKeys();
145 while (e.hasMoreElements())
146 s.add(e.nextElement());
147 bundle = bundle.parent;
149 while (bundle != null);
150 return Collections.enumeration(s);
152 } // class PropertyResourceBundle