KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > util > ParentChildResourceBundle


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.util;
10
11 import java.util.Enumeration JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Locale JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16
17 /**
18  * This resource bundle takes two resource bundle to make one :
19  * - The locale of this resource bundle is given by the child.
20  * - For a given key present in the child and the parent, the child
21  * value overrides the parent value.
22  *
23  * The locale used for the bundle is the child locale.
24  *
25  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
26  * @author <a HREF="mailto:theute@jboss.org">Thomas Heute</a>
27  * @version $Revision: 1.3 $
28  */

29 public class ParentChildResourceBundle extends ResourceBundle JavaDoc
30 {
31
32    private Locale JavaDoc locale;
33    private Map JavaDoc values;
34
35    /**
36     * Construct a new resource bundle whose content is based on the child
37     * and parent content.
38     *
39     * @param parent the parent eventually null
40     * @param child the child
41     * @throws IllegalArgumentException if the child is null
42     */

43    public ParentChildResourceBundle(ResourceBundle JavaDoc parent, ResourceBundle JavaDoc child) throws IllegalArgumentException JavaDoc
44    {
45       // Arg check
46
if (child == null)
47       {
48          throw new IllegalArgumentException JavaDoc("Child cannot be null");
49       }
50       this.locale = child.getLocale();
51       this.values = new HashMap JavaDoc();
52
53       // Set the parent content
54
if (parent != null)
55       {
56          for (Enumeration JavaDoc e = parent.getKeys();e.hasMoreElements();)
57          {
58             String JavaDoc key = (String JavaDoc)e.nextElement();
59             Object JavaDoc value = parent.getObject(key);
60             values.put(key, value);
61          }
62       }
63
64       // Set the child content
65
for (Enumeration JavaDoc e = child.getKeys();e.hasMoreElements();)
66       {
67          String JavaDoc key = (String JavaDoc)e.nextElement();
68          Object JavaDoc value = child.getObject(key);
69          values.put(key, value);
70       }
71    }
72
73    public Locale JavaDoc getLocale()
74    {
75       return locale;
76    }
77
78    protected Object JavaDoc handleGetObject(String JavaDoc key)
79    {
80       return values.get(key);
81    }
82
83    public Enumeration JavaDoc getKeys()
84    {
85       return Tools.toEnumeration(values.keySet().iterator());
86    }
87 }
88
Popular Tags