Source for java.util.PropertyResourceBundle

   1: /* PropertyResourceBundle -- a resource bundle built from a Property file
   2:    Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package java.util;
  40: 
  41: import java.io.IOException;
  42: import java.io.InputStream;
  43: import java.io.Reader;
  44: 
  45: /**
  46:  * This class is a concrete <code>ResourceBundle</code> that gets it
  47:  * resources from a property file. This implies that the resources are
  48:  * strings. For more information about resource bundles see the class
  49:  * <code>ResourceBundle</code>.
  50:  *
  51:  * You should not use this class directly, or subclass it, but you get
  52:  * an object of this class automatically when you call
  53:  * <code>ResourceBundle.getBundle()</code> and there is a properties
  54:  * file.
  55:  *
  56:  * If there is also a class for this resource and the same locale, the
  57:  * class will be chosen. The properties file should have the name of the
  58:  * resource bundle, appended with the locale (e.g. <code>_de</code> and the
  59:  * extension <code>.properties</code>. The file should have the same format
  60:  * as for <code>Properties.load()</code>
  61:  *
  62:  * An example of a properties file for the german language is given
  63:  * here. This extends the example given in ListResourceBundle.
  64:  * Create a file MyResource_de.properties with the following contents
  65:  * and put it in the CLASSPATH. (The char <code>\u00e4</code> is the
  66:  * german umlaut)
  67:  *
  68:  *
  69: <pre>
  70: s1=3
  71: s2=MeineDisk
  72: s3=3. M\u00e4rz 96
  73: s4=Die Diskette ''{1}'' enth\u00e4lt {0} in {2}.
  74: s5=0
  75: s6=keine Dateien
  76: s7=1
  77: s8=eine Datei
  78: s9=2
  79: s10={0,number} Dateien
  80: s11=Die Formatierung warf eine Exception: {0}
  81: s12=FEHLER
  82: s13=Ergebnis
  83: s14=Dialog
  84: s15=Auswahlkriterium
  85: s16=1,3
  86: </pre>
  87:  *
  88:  * @author Jochen Hoenicke
  89:  * @see ResourceBundle
  90:  * @see ListResourceBundle
  91:  * @see Properties#load(InputStream)
  92:  * @since 1.1
  93:  * @status updated to 1.4
  94:  */
  95: public class PropertyResourceBundle extends ResourceBundle
  96: {
  97:   /** The properties file this bundle is based on. */
  98:   private Properties properties;
  99: 
 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