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    import java.io.Reader;
044    
045    /**
046     * This class is a concrete <code>ResourceBundle</code> that gets it
047     * resources from a property file. This implies that the resources are
048     * strings. For more information about resource bundles see the class
049     * <code>ResourceBundle</code>.
050     *
051     * You should not use this class directly, or subclass it, but you get
052     * an object of this class automatically when you call
053     * <code>ResourceBundle.getBundle()</code> and there is a properties
054     * file.
055     *
056     * If there is also a class for this resource and the same locale, the
057     * class will be chosen. The properties file should have the name of the
058     * resource bundle, appended with the locale (e.g. <code>_de</code> and the
059     * extension <code>.properties</code>. The file should have the same format
060     * as for <code>Properties.load()</code>
061     *
062     * An example of a properties file for the german language is given
063     * here. This extends the example given in ListResourceBundle.
064     * Create a file MyResource_de.properties with the following contents
065     * and put it in the CLASSPATH. (The char <code>\u00e4</code> is the
066     * german umlaut)
067     *
068     *
069    <pre>
070    s1=3
071    s2=MeineDisk
072    s3=3. M\u00e4rz 96
073    s4=Die Diskette ''{1}'' enth\u00e4lt {0} in {2}.
074    s5=0
075    s6=keine Dateien
076    s7=1
077    s8=eine Datei
078    s9=2
079    s10={0,number} Dateien
080    s11=Die Formatierung warf eine Exception: {0}
081    s12=FEHLER
082    s13=Ergebnis
083    s14=Dialog
084    s15=Auswahlkriterium
085    s16=1,3
086    </pre>
087     *
088     * @author Jochen Hoenicke
089     * @see ResourceBundle
090     * @see ListResourceBundle
091     * @see Properties#load(InputStream)
092     * @since 1.1
093     * @status updated to 1.4
094     */
095    public class PropertyResourceBundle extends ResourceBundle
096    {
097      /** The properties file this bundle is based on. */
098      private Properties properties;
099    
100      /**
101       * Creates a new property resource bundle.  The property file must
102       * be encoded using ISO-8859-1.
103       *
104       * @param stream an input stream, where the resources are read from
105       * @throws NullPointerException if stream is null
106       * @throws IOException if reading the stream fails
107       */
108      public PropertyResourceBundle(InputStream stream) throws IOException
109      {
110        properties = new Properties();
111        properties.load(stream);
112      }
113    
114      /**
115       * Creates a new property resource bundle.  The encoding of the property
116       * file is determined by the supplied {@link Reader} object.
117       *
118       * @param reader an input stream, where the resources are read from
119       * @throws NullPointerException if stream is null
120       * @throws IOException if reading the stream fails
121       * @since 1.6
122       */
123      public PropertyResourceBundle(Reader reader) throws IOException
124      {
125        properties = new Properties();
126        properties.load(reader);
127      }
128    
129      /**
130       * Called by <code>getObject</code> when a resource is needed. This
131       * returns the resource given by the key.
132       *
133       * @param key the key of the resource
134       * @return the resource for the key, or null if it doesn't exist
135       */
136      public Object handleGetObject(String key)
137      {
138        return properties.getProperty(key);
139      }
140    
141      /**
142       * This method should return all keys for which a resource exists.
143       *
144       * @return an enumeration of the keys
145       */
146      public Enumeration<String> getKeys()
147      {
148        if (parent == null)
149          // FIXME: bogus cast.
150          return (Enumeration<String>) properties.propertyNames();
151        // We make a new Set that holds all the keys, then return an enumeration
152        // for that. This prevents modifications from ruining the enumeration,
153        // as well as ignoring duplicates.
154        Set<String> s = new HashSet<String>();
155        // FIXME: bogus cast.
156        Enumeration<String> e = (Enumeration<String>) properties.propertyNames();
157        while (e.hasMoreElements())
158          s.add(e.nextElement());
159        ResourceBundle bundle = parent;
160        // Eliminate tail recursion.
161        do
162          {
163            e = bundle.getKeys();
164            while (e.hasMoreElements())
165              s.add(e.nextElement());
166            bundle = bundle.parent;
167          }
168        while (bundle != null);
169        return Collections.enumeration(s);
170      }
171    } // class PropertyResourceBundle