| 1 | /* java.util.PropertyResourceBundle
|
|---|
| 2 | Copyright (C) 1998, 1999, 2001 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 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 gnu.java.util.DoubleEnumeration;
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * This class is a concrete <code>ResourceBundle</code> that gets it
|
|---|
| 45 | * resources from a property file. This implies that the resources are
|
|---|
| 46 | * strings. For more information about resource bundles see the class
|
|---|
| 47 | * <code>ResourceBundle</code>.
|
|---|
| 48 | *
|
|---|
| 49 | * You should not use this class directly, or subclass it, but you get
|
|---|
| 50 | * an object of this class automatically when you call
|
|---|
| 51 | * <code>ResourceBundle.getBundle()</code> and there is a properties
|
|---|
| 52 | * file.
|
|---|
| 53 | *
|
|---|
| 54 | * If there is also a class for this resource and the same locale, the
|
|---|
| 55 | * class does win.
|
|---|
| 56 | *
|
|---|
| 57 | * The properties file should have the name of the resource bundle,
|
|---|
| 58 | * appended with the locale (e.g. <code>_de</code) and the extension
|
|---|
| 59 | * <code>.properties</code>. The file should have the same format
|
|---|
| 60 | * as for <code>Properties.load()</code>
|
|---|
| 61 | *
|
|---|
| 62 | * XXX- move this to properties.
|
|---|
| 63 | * The file should have the following
|
|---|
| 64 | * format: An empty line or a line starting with <code>#</code> is
|
|---|
| 65 | * ignored. An backslash (<code>\</code>) at the end of the line
|
|---|
| 66 | * makes the line continueing on the next line. Otherwise, each line
|
|---|
| 67 | * describes a key/value pair. The chars up to the first whitespace,
|
|---|
| 68 | * = or : are the key. The key is followed by one or more
|
|---|
| 69 | * whitespaces, <code>=</code> or <code>:</code>. The rest of the
|
|---|
| 70 | * line is the resource belonging to the key. You can give unicode
|
|---|
| 71 | * characters with the <code>\\uxxxx</code> notation, where
|
|---|
| 72 | * <code>xxxx</code> is the hex encoding of the 16 bit unicode char
|
|---|
| 73 | * number.
|
|---|
| 74 | *
|
|---|
| 75 | * An example of a properties file for the german language is given
|
|---|
| 76 | * here. This extends the example given in ListResourceBundle.
|
|---|
| 77 | * Create a file MyResource_de.properties with the following contents
|
|---|
| 78 | * and put it in the CLASSPATH. (The char <code>\u00e4<char> is the
|
|---|
| 79 | * german ä)
|
|---|
| 80 | *
|
|---|
| 81 | * <pre>
|
|---|
| 82 | * s1=3
|
|---|
| 83 | * s2=MeineDisk
|
|---|
| 84 | * s3=3. M\u00e4rz 96
|
|---|
| 85 | * s4=Die Diskette ''{1}'' enth\u00e4lt {0} in {2}.
|
|---|
| 86 | * s5=0
|
|---|
| 87 | * s6=keine Dateien
|
|---|
| 88 | * s7=1
|
|---|
| 89 | * s8=eine Datei
|
|---|
| 90 | * s9=2
|
|---|
| 91 | * s10={0,number} Dateien
|
|---|
| 92 | * s11=Die Formatierung warf eine Exception: {0}
|
|---|
| 93 | * s12=FEHLER
|
|---|
| 94 | * s13=Ergebnis
|
|---|
| 95 | * s14=Dialog
|
|---|
| 96 | * s15=Auswahlkriterium
|
|---|
| 97 | * s16=1,3
|
|---|
| 98 | * </pre>
|
|---|
| 99 | *
|
|---|
| 100 | * @see ResourceBundle
|
|---|
| 101 | * @see ListResourceBundle
|
|---|
| 102 | * @see Properties#load()
|
|---|
| 103 | * @author Jochen Hoenicke */
|
|---|
| 104 | public class PropertyResourceBundle extends ResourceBundle
|
|---|
| 105 | {
|
|---|
| 106 | Properties properties;
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Creates a new property resource bundle.
|
|---|
| 110 | * @param stream An input stream, where the resources are read from.
|
|---|
| 111 | */
|
|---|
| 112 | public PropertyResourceBundle(java.io.InputStream stream)
|
|---|
| 113 | throws java.io.IOException
|
|---|
| 114 | {
|
|---|
| 115 | properties = new Properties();
|
|---|
| 116 | properties.load(stream);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | * Called by <code>getObject</code> when a resource is needed. This
|
|---|
| 121 | * returns the resource given by the key.
|
|---|
| 122 | * @param key The key of the resource.
|
|---|
| 123 | * @return The resource for the key or null if it doesn't exists.
|
|---|
| 124 | */
|
|---|
| 125 | public Object handleGetObject(String key)
|
|---|
| 126 | {
|
|---|
| 127 | return properties.getProperty(key);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * This method should return all keys for which a resource exists.
|
|---|
| 132 | * @return An enumeration of the keys.
|
|---|
| 133 | */
|
|---|
| 134 | public Enumeration getKeys()
|
|---|
| 135 | {
|
|---|
| 136 | // We must also return the keys of our parent.
|
|---|
| 137 | if (parent != null)
|
|---|
| 138 | {
|
|---|
| 139 | return new DoubleEnumeration(properties.propertyNames(),
|
|---|
| 140 | parent.getKeys());
|
|---|
| 141 | }
|
|---|
| 142 | return properties.propertyNames();
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|