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