001    /* PropertyResourceBundle -- a resource bundle built from a Property file
002       Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.util;
040    
041    import java.io.IOException;
042    import java.io.InputStream;
043    
044    /**
045     * This class is a concrete <code>ResourceBundle</code> that gets it
046     * resources from a property file. This implies that the resources are
047     * strings. For more information about resource bundles see the class
048     * <code>ResourceBundle</code>.
049     *
050     * You should not use this class directly, or subclass it, but you get
051     * an object of this class automatically when you call
052     * <code>ResourceBundle.getBundle()</code> and there is a properties
053     * file.
054     *
055     * If there is also a class for this resource and the same locale, the
056     * class will be chosen. The properties file should have the name of the
057     * resource bundle, appended with the locale (e.g. <code>_de</code> and the
058     * extension <code>.properties</code>. The file should have the same format
059     * as for <code>Properties.load()</code>
060     *
061     * An example of a properties file for the german language is given
062     * here. This extends the example given in ListResourceBundle.
063     * Create a file MyResource_de.properties with the following contents
064     * and put it in the CLASSPATH. (The char <code>\u00e4</code> is the
065     * german umlaut)
066     *
067     *
068    <pre>
069    s1=3
070    s2=MeineDisk
071    s3=3. M\u00e4rz 96
072    s4=Die Diskette ''{1}'' enth\u00e4lt {0} in {2}.
073    s5=0
074    s6=keine Dateien
075    s7=1
076    s8=eine Datei
077    s9=2
078    s10={0,number} Dateien
079    s11=Die Formatierung warf eine Exception: {0}
080    s12=FEHLER
081    s13=Ergebnis
082    s14=Dialog
083    s15=Auswahlkriterium
084    s16=1,3
085    </pre>
086     *
087     * @author Jochen Hoenicke
088     * @see ResourceBundle
089     * @see ListResourceBundle
090     * @see Properties#load(InputStream)
091     * @since 1.1
092     * @status updated to 1.4
093     */
094    public class PropertyResourceBundle extends ResourceBundle
095    {
096      /** The properties file this bundle is based on. */
097      private Properties properties;
098    
099      /**
100       * Creates a new property resource bundle.
101       *
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
105       */
106      public PropertyResourceBundle(InputStream stream) throws IOException
107      {
108        properties = new Properties();
109        properties.load(stream);
110      }
111    
112      /**
113       * Called by <code>getObject</code> when a resource is needed. This
114       * returns the resource given by the key.
115       *
116       * @param key the key of the resource
117       * @return the resource for the key, or null if it doesn't exist
118       */
119      public Object handleGetObject(String key)
120      {
121        return properties.getProperty(key);
122      }
123    
124      /**
125       * This method should return all keys for which a resource exists.
126       *
127       * @return an enumeration of the keys
128       */
129      public Enumeration<String> getKeys()
130      {
131        if (parent == null)
132          // FIXME: bogus cast.
133          return (Enumeration<String>) properties.propertyNames();
134        // We make a new Set that holds all the keys, then return an enumeration
135        // for that. This prevents modifications from ruining the enumeration,
136        // as well as ignoring duplicates.
137        Set<String> s = new HashSet<String>();
138        // FIXME: bogus cast.
139        Enumeration<String> e = (Enumeration<String>) properties.propertyNames();
140        while (e.hasMoreElements())
141          s.add(e.nextElement());
142        ResourceBundle bundle = parent;
143        // Eliminate tail recursion.
144        do
145          {
146            e = bundle.getKeys();
147            while (e.hasMoreElements())
148              s.add(e.nextElement());
149            bundle = bundle.parent;
150          }
151        while (bundle != null);
152        return Collections.enumeration(s);
153      }
154    } // class PropertyResourceBundle