KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > OverlayBundle


1 /**
2  *******************************************************************************
3  * Copyright (C) 2001-2004, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.util;
8
9 import java.util.*;
10
11 /**
12  * A ResourceBundle that overlays one hierarchy atop another. This is
13  * best explained by example. Suppose one wants to use the
14  * resource hiararchy (in JDK 1.2 and 1.3, but not 1.4) at
15  * "java.text.resources.LocaleElements", but one wants to use
16  * a modified version of the "NumberPatterns" resource in the
17  * fr_FR locale. One way to do this is to add special case code
18  * to the lookup operation to check for fr_FR and the key
19  * "NumberPatterns", and in that case, load up custom data. However,
20  * this becomes unwieldy and places some information about the
21  * effective resource hierarchy into the code.
22  *
23  * The OverlayBundle solves this problem by layering another
24  * hierarchy, e.g, "com.acme.resources.LocaleElements", on top of a
25  * base hierarchy. When a resource is requested, it is first sought
26  * in the overlay hierarchy, and if not found there, it is sought in
27  * the base hierarchy. Multiple overlays are supported, but in
28  * practice one is usually sufficient.
29  *
30  * The OverlayBundle also addresses the problem of country-oriented
31  * data. To specify the default data for a language, one just sets
32  * the language resource bundle data. However, specifying the default
33  * data for a country using the standard ResourceBundle mechanism is
34  * impossible. The OverlayBundle recognizes "wildcard" locales with
35  * the special language code "xx". When looking up data for a locale
36  * with a non-empty country, if an exact locale match cannot be found,
37  * the OverlayBundle looks for data in the locale xx_YY, where YY is
38  * the country being sought. This effectively adds another entry in
39  * the fallback sequence for a locale aa_BB: aa_BB, xx_BB, aa, root.
40  * Wildcard locales are not implemented for the base hierarchy, only
41  * for overlays.
42  *
43  * The OverlayBundle is implemented as an array of n ResourceBundle
44  * base names. The base names are searched from 0 to n-1. Base name
45  * n-1 is special; it is the base hierarchy. This should be a
46  * well-populated hierarchy with most of the default data, typically,
47  * the icu or sun core hierarchies. The base hierarchy is
48  * treated differently from the overlays above it. It does not get
49  * wildcard resolution, and the getKeys() framework method is
50  * delegated to the base hierarchy bundle.
51  *
52  * Usage: Instantiate an OverlayBundle directly (not via a factory
53  * method as in ResourceBundle). Instead of specifying a single base
54  * name, pass it an array of 2 or more base names. After that, use it
55  * exactly as you would use ResourceBundle.
56  *
57  * @see java.util.ResourceBundle
58  * @author Alan Liu
59  * @internal
60  * @deprecated ICU 2.4. This class may be removed or modified.
61  */

62 // prepare to deprecate in next release
63
///CLOVER:OFF
64
public class OverlayBundle extends ResourceBundle {
65
66     /**
67      * The array of base names, with the length-1 entry being the base
68      * hierarchy, typically "sun.text.resources.LocaleElements".
69      */

70     private String JavaDoc[] baseNames;
71
72     /**
73      * The requested locale.
74      */

75     private Locale locale;
76
77     /**
78      * Loaded bundles. These will be null until they are loaded on
79      * demand.
80      */

81     private ResourceBundle[] bundles;
82
83     /**
84      * Construct an overlay bundle given a sequence of base names and
85      * a locale.
86      * @internal
87      * @deprecated ICU 2.4. This class may be removed or modified.
88      */

89     public OverlayBundle(String JavaDoc[] baseNames,
90                          Locale locale) {
91         this.baseNames = baseNames;
92         this.locale = locale;
93         bundles = new ResourceBundle[baseNames.length];
94     }
95
96     /**
97      * ResourceBundle framework method. Delegates to
98      * bundles[i].getObject().
99      * @internal
100      * @deprecated ICU 2.4. This class may be removed or modified.
101      */

102    protected Object JavaDoc handleGetObject(String JavaDoc key)
103         throws MissingResourceException {
104
105         Object JavaDoc o = null;
106
107         for (int i=0; i<bundles.length; ++i) {
108             load(i);
109             try {
110                 o = bundles[i].getObject(key);
111             } catch (MissingResourceException e) {
112                 if (i == bundles.length-1) {
113                     throw e;
114                 }
115             }
116             if (o != null) {
117                 break;
118             }
119         }
120
121         return o;
122     }
123
124     /**
125      * ResourceBundle framework method. Delegates to
126      * bundles[bundles.length-1].getKeys().
127      * @internal
128      * @deprecated ICU 2.4. This class may be removed or modified.
129      */

130     public Enumeration getKeys() {
131         // Return the enumeration of the last bundle, which is the base
132
// of our hierarchy stack.
133
int i = bundles.length - 1;
134         load(i);
135         return bundles[i].getKeys();
136     }
137
138     /**
139      * Load the i-th bundle and implement wildcard resolution.
140      */

141     private void load(int i)
142         throws MissingResourceException {
143
144         if (bundles[i] == null) {
145             boolean tryWildcard = false;
146             try {
147                 bundles[i] = ResourceBundle.getBundle(baseNames[i], locale);
148                 if (bundles[i].getLocale().equals(locale)) {
149                     return;
150                 }
151                 if (locale.getCountry().length() != 0 && i != bundles.length-1) {
152                     tryWildcard = true;
153                 }
154             } catch (MissingResourceException e) {
155                 if (i == bundles.length-1) {
156                     throw e;
157                 }
158                 tryWildcard = true;
159             }
160             if (tryWildcard) {
161                 Locale wildcard = new Locale("xx", locale.getCountry(),
162                                              locale.getVariant());
163                 try {
164                     bundles[i] = ResourceBundle.getBundle(baseNames[i], wildcard);
165                 } catch (MissingResourceException e) {
166                     if (bundles[i] == null) {
167                         throw e;
168                     }
169                 }
170             }
171         }
172     }
173 }
174
Popular Tags