home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / util / PropertyResourceBundle.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.2 KB  |  156 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)PropertyResourceBundle.java    1.15 98/08/11
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996-1998 Sun Microsystems, Inc.
  8.  * All Rights Reserved.
  9.  *
  10.  * The original version of this source code and documentation
  11.  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  12.  * subsidiary of IBM. These materials are provided under terms
  13.  * of a License Agreement between Taligent and Sun. This technology
  14.  * is protected by multiple US and International patents.
  15.  *
  16.  * This notice and attribution to Taligent may not be removed.
  17.  * Taligent is a registered trademark of Taligent, Inc.
  18.  *
  19.  * Permission to use, copy, modify, and distribute this software
  20.  * and its documentation for NON-COMMERCIAL purposes and without
  21.  * fee is hereby granted provided that this copyright notice
  22.  * appears in all copies. Please refer to the file "copyright.html"
  23.  * for further important copyright and licensing information.
  24.  *
  25.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  26.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  27.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  28.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  29.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  30.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  31.  *
  32.  */
  33. package java.util;
  34.  
  35. import java.util.Properties;
  36. import java.io.InputStream;
  37. import java.io.IOException;
  38.  
  39. /**
  40.  * <code>PropertyResourceBundle</code> is a concrete subclass of
  41.  * <code>ResourceBundle</code> that manages resources for a locale
  42.  * using a set of static strings from a property file. See
  43.  * <code>ResourceBundle</code> for more information about resource
  44.  * bundles in general.
  45.  *
  46.  * <p>
  47.  * Unlike other types of resource bundle, you don't subclass
  48.  * <code>PropertyResourceBundle</code>.  Instead, you supply properties
  49.  * files containing the resource data.  <code>ResourceBundle.getBundle()</code>
  50.  * will automatically look for the appropriate properties file and create a
  51.  * <code>PropertyResourceBundle</code> that refers to it.  The resource
  52.  * bundle name that you pass to <code>ResourceBundle.getBundle()</code> is
  53.  * the file name of the properties file, not the class name of the object that
  54.  * is returned.
  55.  *
  56.  * <p>
  57.  * For example, if you say <code>ResourceBundle.getBundle("MyResources",
  58.  * new Locale("fr", "FR"));</code> the resource bundle lookup mechanism
  59.  * will search the class path for a file called
  60.  * <code>MyResources_fr_FR.properties</code>.
  61.  *
  62.  * <p>
  63.  * If a real class and a properties file with a particular name both exist,
  64.  * the class wins; the properties file will only be used if there is no class
  65.  * with the desired name.
  66.  *
  67.  * <p>
  68.  * In the following example, the keys are of the form "s1"... The actual
  69.  * keys are entirely up to your choice, so long as they are the same as
  70.  * the keys you use in your program to retrieve the objects from the bundle.
  71.  * Keys are case-sensitive.
  72.  * <blockquote>
  73.  * <pre>
  74.  * s1=3
  75.  * s2=MeinDisk
  76.  * s3=3 Mar 96
  77.  * s4=Der disk '{1}' a {0} a {2}.
  78.  * s5=0
  79.  * s6=keine Datein
  80.  * s7=1
  81.  * s8=ein Datei
  82.  * s9=2
  83.  * s10={0}|3 Datein
  84.  * s11=Der Format worf ein Exception: {0}
  85.  * s12=ERROR
  86.  * s14=Resulte
  87.  * s13=Dialogue
  88.  * s15=Pattern
  89.  * s16=1,3
  90.  * </pre>
  91.  * </blockquote>
  92.  *
  93.  * @see ResourceBundle
  94.  * @see ListResourceBundle
  95.  */
  96. public class PropertyResourceBundle extends ResourceBundle {
  97.     /**
  98.      * Creates a property resource
  99.      * @param stream property file to read from.
  100.      */
  101.     public PropertyResourceBundle (InputStream stream) throws IOException {
  102.         lookup.load(stream);
  103.     }
  104.  
  105.     /**
  106.      * Override of ResourceBundle, same semantics
  107.      */
  108.     public Object handleGetObject(String key) {
  109.         Object obj = lookup.get(key);
  110.         return obj; // once serialization is in place, you can do non-strings
  111.     }
  112.  
  113.     /**
  114.      * Implementation of ResourceBundle.getKeys.
  115.      */
  116.     public Enumeration getKeys() {
  117.         Enumeration result = null;
  118.         if (parent != null) {
  119.             final Enumeration myKeys = lookup.keys();
  120.             final Enumeration parentKeys = parent.getKeys();
  121.  
  122.             result = new Enumeration() {
  123.                 public boolean hasMoreElements() {
  124.                     if (temp == null)
  125.                         nextElement();
  126.                     return temp != null;
  127.                 }
  128.  
  129.                 public Object nextElement() {
  130.                     Object returnVal = temp;
  131.                     if (myKeys.hasMoreElements())
  132.                         temp = myKeys.nextElement();
  133.                     else {
  134.                         temp = null;
  135.                         while (temp == null && parentKeys.hasMoreElements()) {
  136.                             temp = parentKeys.nextElement();
  137.                             if (lookup.containsKey(temp))
  138.                                 temp = null;
  139.                         }
  140.                     }
  141.                     return returnVal;
  142.                 }
  143.  
  144.                 Object temp = null;
  145.             };
  146.         } else {
  147.             result = lookup.keys();
  148.         }
  149.         return result;
  150.     }
  151.  
  152.     // ==================privates====================
  153.  
  154.     private Properties lookup = new Properties();
  155. }
  156.