PropertyResourceBundle.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* PropertyResourceBundle -- a resource bundle built from a Property file
  2. Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.io.Reader;
  35. /**
  36. * This class is a concrete <code>ResourceBundle</code> that gets it
  37. * resources from a property file. This implies that the resources are
  38. * strings. For more information about resource bundles see the class
  39. * <code>ResourceBundle</code>.
  40. *
  41. * You should not use this class directly, or subclass it, but you get
  42. * an object of this class automatically when you call
  43. * <code>ResourceBundle.getBundle()</code> and there is a properties
  44. * file.
  45. *
  46. * If there is also a class for this resource and the same locale, the
  47. * class will be chosen. The properties file should have the name of the
  48. * resource bundle, appended with the locale (e.g. <code>_de</code> and the
  49. * extension <code>.properties</code>. The file should have the same format
  50. * as for <code>Properties.load()</code>
  51. *
  52. * An example of a properties file for the german language is given
  53. * here. This extends the example given in ListResourceBundle.
  54. * Create a file MyResource_de.properties with the following contents
  55. * and put it in the CLASSPATH. (The char <code>\u00e4</code> is the
  56. * german umlaut)
  57. *
  58. *
  59. <pre>
  60. s1=3
  61. s2=MeineDisk
  62. s3=3. M\u00e4rz 96
  63. s4=Die Diskette ''{1}'' enth\u00e4lt {0} in {2}.
  64. s5=0
  65. s6=keine Dateien
  66. s7=1
  67. s8=eine Datei
  68. s9=2
  69. s10={0,number} Dateien
  70. s11=Die Formatierung warf eine Exception: {0}
  71. s12=FEHLER
  72. s13=Ergebnis
  73. s14=Dialog
  74. s15=Auswahlkriterium
  75. s16=1,3
  76. </pre>
  77. *
  78. * @author Jochen Hoenicke
  79. * @see ResourceBundle
  80. * @see ListResourceBundle
  81. * @see Properties#load(InputStream)
  82. * @since 1.1
  83. * @status updated to 1.4
  84. */
  85. public class PropertyResourceBundle extends ResourceBundle
  86. {
  87. /** The properties file this bundle is based on. */
  88. private Properties properties;
  89. /**
  90. * Creates a new property resource bundle. The property file must
  91. * be encoded using ISO-8859-1.
  92. *
  93. * @param stream an input stream, where the resources are read from
  94. * @throws NullPointerException if stream is null
  95. * @throws IOException if reading the stream fails
  96. */
  97. public PropertyResourceBundle(InputStream stream) throws IOException
  98. {
  99. properties = new Properties();
  100. properties.load(stream);
  101. }
  102. /**
  103. * Creates a new property resource bundle. The encoding of the property
  104. * file is determined by the supplied {@link Reader} object.
  105. *
  106. * @param reader an input stream, where the resources are read from
  107. * @throws NullPointerException if stream is null
  108. * @throws IOException if reading the stream fails
  109. * @since 1.6
  110. */
  111. public PropertyResourceBundle(Reader reader) throws IOException
  112. {
  113. properties = new Properties();
  114. properties.load(reader);
  115. }
  116. /**
  117. * Called by <code>getObject</code> when a resource is needed. This
  118. * returns the resource given by the key.
  119. *
  120. * @param key the key of the resource
  121. * @return the resource for the key, or null if it doesn't exist
  122. */
  123. public Object handleGetObject(String key)
  124. {
  125. return properties.getProperty(key);
  126. }
  127. /**
  128. * This method should return all keys for which a resource exists.
  129. *
  130. * @return an enumeration of the keys
  131. */
  132. public Enumeration<String> getKeys()
  133. {
  134. if (parent == null)
  135. // FIXME: bogus cast.
  136. return (Enumeration<String>) properties.propertyNames();
  137. // We make a new Set that holds all the keys, then return an enumeration
  138. // for that. This prevents modifications from ruining the enumeration,
  139. // as well as ignoring duplicates.
  140. Set<String> s = new HashSet<String>();
  141. // FIXME: bogus cast.
  142. Enumeration<String> e = (Enumeration<String>) properties.propertyNames();
  143. while (e.hasMoreElements())
  144. s.add(e.nextElement());
  145. ResourceBundle bundle = parent;
  146. // Eliminate tail recursion.
  147. do
  148. {
  149. e = bundle.getKeys();
  150. while (e.hasMoreElements())
  151. s.add(e.nextElement());
  152. bundle = bundle.parent;
  153. }
  154. while (bundle != null);
  155. return Collections.enumeration(s);
  156. }
  157. } // class PropertyResourceBundle