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