2
|
1 |
/*
|
|
2 |
* Portions Copyright 1996-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
|
|
28 |
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
|
|
29 |
*
|
|
30 |
* The original version of this source code and documentation
|
|
31 |
* is copyrighted and owned by Taligent, Inc., a wholly-owned
|
|
32 |
* subsidiary of IBM. These materials are provided under terms
|
|
33 |
* of a License Agreement between Taligent and Sun. This technology
|
|
34 |
* is protected by multiple US and International patents.
|
|
35 |
*
|
|
36 |
* This notice and attribution to Taligent may not be removed.
|
|
37 |
* Taligent is a registered trademark of Taligent, Inc.
|
|
38 |
*
|
|
39 |
*/
|
|
40 |
|
|
41 |
package sun.util.resources;
|
|
42 |
|
|
43 |
import java.io.File;
|
|
44 |
import java.security.AccessController;
|
|
45 |
import java.security.PrivilegedAction;
|
|
46 |
import java.util.Iterator;
|
|
47 |
import java.util.List;
|
|
48 |
import java.util.Locale;
|
|
49 |
import java.util.ResourceBundle;
|
|
50 |
import java.util.StringTokenizer;
|
|
51 |
|
|
52 |
import sun.util.LocaleDataMetaInfo;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Provides information about and access to resource bundles in the
|
|
56 |
* sun.text.resources and sun.util.resources package.
|
|
57 |
*
|
|
58 |
* @author Asmus Freytag
|
|
59 |
* @author Mark Davis
|
|
60 |
*/
|
|
61 |
|
|
62 |
public class LocaleData {
|
|
63 |
|
|
64 |
private static final String localeDataJarName = "localedata.jar";
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Lazy load available locales.
|
|
68 |
*/
|
|
69 |
private static class AvailableLocales {
|
|
70 |
static final Locale[] localeList = createLocaleList();
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Returns a list of the installed locales. Currently, this simply returns
|
|
75 |
* the list of locales for which a sun.text.resources.FormatData bundle
|
|
76 |
* exists. This bundle family happens to be the one with the broadest
|
|
77 |
* locale coverage in the JRE.
|
|
78 |
*/
|
|
79 |
public static Locale[] getAvailableLocales() {
|
|
80 |
return AvailableLocales.localeList.clone();
|
|
81 |
}
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Gets a calendar data resource bundle, using privileges
|
|
85 |
* to allow accessing a sun.* package.
|
|
86 |
*/
|
|
87 |
public static ResourceBundle getCalendarData(Locale locale) {
|
|
88 |
return getBundle("sun.util.resources.CalendarData", locale);
|
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Gets a currency names resource bundle, using privileges
|
|
93 |
* to allow accessing a sun.* package.
|
|
94 |
*/
|
|
95 |
public static OpenListResourceBundle getCurrencyNames(Locale locale) {
|
|
96 |
return (OpenListResourceBundle)getBundle("sun.util.resources.CurrencyNames", locale);
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Gets a locale names resource bundle, using privileges
|
|
101 |
* to allow accessing a sun.* package.
|
|
102 |
*/
|
|
103 |
public static OpenListResourceBundle getLocaleNames(Locale locale) {
|
|
104 |
return (OpenListResourceBundle)getBundle("sun.util.resources.LocaleNames", locale);
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Gets a time zone names resource bundle, using privileges
|
|
109 |
* to allow accessing a sun.* package.
|
|
110 |
*/
|
|
111 |
public static OpenListResourceBundle getTimeZoneNames(Locale locale) {
|
|
112 |
return (OpenListResourceBundle)getBundle("sun.util.resources.TimeZoneNames", locale);
|
|
113 |
}
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Gets a collation data resource bundle, using privileges
|
|
117 |
* to allow accessing a sun.* package.
|
|
118 |
*/
|
|
119 |
public static ResourceBundle getCollationData(Locale locale) {
|
|
120 |
return getBundle("sun.text.resources.CollationData", locale);
|
|
121 |
}
|
|
122 |
|
|
123 |
/**
|
|
124 |
* Gets a date format data resource bundle, using privileges
|
|
125 |
* to allow accessing a sun.* package.
|
|
126 |
*/
|
|
127 |
public static ResourceBundle getDateFormatData(Locale locale) {
|
|
128 |
return getBundle("sun.text.resources.FormatData", locale);
|
|
129 |
}
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Gets a number format data resource bundle, using privileges
|
|
133 |
* to allow accessing a sun.* package.
|
|
134 |
*/
|
|
135 |
public static ResourceBundle getNumberFormatData(Locale locale) {
|
|
136 |
return getBundle("sun.text.resources.FormatData", locale);
|
|
137 |
}
|
|
138 |
|
|
139 |
private static ResourceBundle getBundle(final String baseName, final Locale locale) {
|
|
140 |
return (ResourceBundle) AccessController.doPrivileged(new PrivilegedAction() {
|
|
141 |
public Object run() {
|
|
142 |
return ResourceBundle.
|
|
143 |
getBundle(baseName, locale,
|
|
144 |
LocaleDataResourceBundleControl.getRBControlInstance());
|
|
145 |
}
|
|
146 |
});
|
|
147 |
}
|
|
148 |
|
|
149 |
static class LocaleDataResourceBundleControl extends ResourceBundle.Control {
|
|
150 |
/* Singlton instance of ResourceBundle.Control. */
|
|
151 |
private static LocaleDataResourceBundleControl rbControlInstance =
|
|
152 |
new LocaleDataResourceBundleControl();
|
|
153 |
|
|
154 |
public static LocaleDataResourceBundleControl getRBControlInstance() {
|
|
155 |
return rbControlInstance;
|
|
156 |
}
|
|
157 |
|
|
158 |
/*
|
|
159 |
* This method overrides the default implementation to search
|
|
160 |
* from a prebaked locale string list to determin the candidate
|
|
161 |
* locale list.
|
|
162 |
*
|
|
163 |
* @param baseName the resource bundle base name.
|
|
164 |
* locale the requested locale for the resource bundle.
|
|
165 |
* @returns a list of candidate locales to search from.
|
|
166 |
* @exception NullPointerException if baseName or locale is null.
|
|
167 |
*/
|
|
168 |
@Override
|
|
169 |
public List<Locale> getCandidateLocales(String baseName, Locale locale) {
|
|
170 |
List<Locale> candidates = super.getCandidateLocales(baseName, locale);
|
|
171 |
/* Get the locale string list from LocaleDataMetaInfo class. */
|
|
172 |
String localeString = LocaleDataMetaInfo.getSupportedLocaleString(baseName);
|
|
173 |
|
|
174 |
if (localeString.length() == 0) {
|
|
175 |
return candidates;
|
|
176 |
}
|
|
177 |
|
|
178 |
for (Iterator<Locale> l = candidates.iterator(); l.hasNext(); ) {
|
|
179 |
String lstr = l.next().toString();
|
|
180 |
/* Every locale string in the locale string list returned from
|
|
181 |
the above getSupportedLocaleString is enclosed
|
|
182 |
within two white spaces so that we could check some locale
|
|
183 |
such as "en".
|
|
184 |
*/
|
|
185 |
if (lstr.length() != 0 && localeString.indexOf(" " + lstr + " ") == -1) {
|
|
186 |
l.remove();
|
|
187 |
}
|
|
188 |
}
|
|
189 |
return candidates;
|
|
190 |
}
|
|
191 |
|
|
192 |
/*
|
|
193 |
* Overrides "getFallbackLocale" to return null so
|
|
194 |
* that the fallback locale will be null.
|
|
195 |
* @param baseName the resource bundle base name.
|
|
196 |
* locale the requested locale for the resource bundle.
|
|
197 |
* @return null for the fallback locale.
|
|
198 |
* @exception NullPointerException if baseName or locale is null.
|
|
199 |
*/
|
|
200 |
@Override
|
|
201 |
public Locale getFallbackLocale(String baseName, Locale locale) {
|
|
202 |
if (baseName == null || locale == null) {
|
|
203 |
throw new NullPointerException();
|
|
204 |
}
|
|
205 |
return null;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
/*
|
|
210 |
* Returns true if the non European resources jar file exists in jre
|
|
211 |
* extension directory.
|
|
212 |
* @returns true if the jar file is there. Otherwise, returns false.
|
|
213 |
*/
|
|
214 |
private static boolean isNonEuroLangSupported() {
|
|
215 |
final String sep = File.separator;
|
|
216 |
String localeDataJar =
|
|
217 |
java.security.AccessController.doPrivileged(
|
|
218 |
new sun.security.action.GetPropertyAction("java.home")) +
|
|
219 |
sep + "lib" + sep + "ext" + sep + localeDataJarName;
|
|
220 |
|
|
221 |
/* Peek at the installed extension directory to see if
|
|
222 |
localedata.jar is installed or not.
|
|
223 |
*/
|
|
224 |
final File f = new File(localeDataJar);
|
|
225 |
boolean isNonEuroResJarExist =
|
|
226 |
AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
|
|
227 |
public Boolean run() {
|
|
228 |
return Boolean.valueOf(f.exists());
|
|
229 |
}
|
|
230 |
}).booleanValue();
|
|
231 |
|
|
232 |
return isNonEuroResJarExist;
|
|
233 |
}
|
|
234 |
|
|
235 |
/*
|
|
236 |
* This method gets the locale string list from LocaleDataMetaInfo class and
|
|
237 |
* then contructs the Locale array based on the locale string returned above.
|
|
238 |
* @returns the Locale array for the supported locale of JRE.
|
|
239 |
*
|
|
240 |
*/
|
|
241 |
private static Locale[] createLocaleList() {
|
|
242 |
String supportedLocaleString = LocaleDataMetaInfo.
|
|
243 |
getSupportedLocaleString("sun.text.resources.FormatData");
|
|
244 |
|
|
245 |
if (supportedLocaleString.length() == 0) {
|
|
246 |
return null;
|
|
247 |
}
|
|
248 |
|
|
249 |
/* Look for "|" and construct a new locale string list. */
|
|
250 |
int barIndex = supportedLocaleString.indexOf("|");
|
|
251 |
StringTokenizer localeStringTokenizer = null;
|
|
252 |
if (isNonEuroLangSupported()) {
|
|
253 |
localeStringTokenizer = new
|
|
254 |
StringTokenizer(supportedLocaleString.substring(0, barIndex) +
|
|
255 |
supportedLocaleString.substring(barIndex + 1));
|
|
256 |
} else {
|
|
257 |
localeStringTokenizer = new
|
|
258 |
StringTokenizer(supportedLocaleString.substring(0, barIndex));
|
|
259 |
}
|
|
260 |
|
|
261 |
Locale[] locales = new Locale[localeStringTokenizer.countTokens()];
|
|
262 |
for (int i = 0; i < locales.length; i++) {
|
|
263 |
String currentToken = localeStringTokenizer.nextToken();
|
|
264 |
int p2 = 0;
|
|
265 |
int p1 = currentToken.indexOf('_');
|
|
266 |
String language = "";
|
|
267 |
String country = "";
|
|
268 |
String variant = "";
|
|
269 |
|
|
270 |
if (p1 == -1) {
|
|
271 |
language = currentToken;
|
|
272 |
} else {
|
|
273 |
language = currentToken.substring(0, p1);
|
|
274 |
p2 = currentToken.indexOf('_', p1 + 1);
|
|
275 |
if (p2 == -1) {
|
|
276 |
country = currentToken.substring(p1 + 1);
|
|
277 |
} else {
|
|
278 |
country = currentToken.substring(p1 + 1, p2);
|
|
279 |
if (p2 < currentToken.length()) {
|
|
280 |
variant = currentToken.substring(p2 + 1);
|
|
281 |
}
|
|
282 |
}
|
|
283 |
}
|
|
284 |
locales[i] = new Locale(language, country, variant);
|
|
285 |
}
|
|
286 |
return locales;
|
|
287 |
}
|
|
288 |
|
|
289 |
}
|