2 *******************************************************************************
\r
3 * Copyright (C) 2001-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.impl;
\r
9 import java.util.Collections;
\r
10 import java.util.Locale;
\r
11 import java.util.Map;
\r
12 import java.util.Set;
\r
14 import com.ibm.icu.util.ULocale;
\r
16 public class ICULocaleService extends ICUService {
\r
17 private ULocale fallbackLocale;
\r
18 private String fallbackLocaleName;
\r
21 * Construct an ICULocaleService.
\r
23 public ICULocaleService() {
\r
27 * Construct an ICULocaleService with a name (useful for debugging).
\r
29 public ICULocaleService(String name) {
\r
34 * Convenience override for callers using locales. This calls
\r
35 * get(ULocale, int, ULocale[]) with KIND_ANY for kind and null for
\r
38 public Object get(ULocale locale) {
\r
39 return get(locale, LocaleKey.KIND_ANY, null);
\r
43 * Convenience override for callers using locales. This calls
\r
44 * get(ULocale, int, ULocale[]) with a null actualReturn.
\r
46 public Object get(ULocale locale, int kind) {
\r
47 return get(locale, kind, null);
\r
51 * Convenience override for callers using locales. This calls
\r
52 * get(ULocale, int, ULocale[]) with KIND_ANY for kind.
\r
54 public Object get(ULocale locale, ULocale[] actualReturn) {
\r
55 return get(locale, LocaleKey.KIND_ANY, actualReturn);
\r
59 * Convenience override for callers using locales. This uses
\r
60 * createKey(ULocale.toString(), kind) to create a key, calls getKey, and then
\r
61 * if actualReturn is not null, returns the actualResult from
\r
62 * getKey (stripping any prefix) into a ULocale.
\r
64 public Object get(ULocale locale, int kind, ULocale[] actualReturn) {
\r
65 Key key = createKey(locale, kind);
\r
66 if (actualReturn == null) {
\r
70 String[] temp = new String[1];
\r
71 Object result = getKey(key, temp);
\r
72 if (result != null) {
\r
73 int n = temp[0].indexOf("/");
\r
75 temp[0] = temp[0].substring(n+1);
\r
77 actualReturn[0] = new ULocale(temp[0]);
\r
83 * Convenience override for callers using locales. This calls
\r
84 * registerObject(Object, ULocale, int kind, boolean visible)
\r
85 * passing KIND_ANY for the kind, and true for the visibility.
\r
87 public Factory registerObject(Object obj, ULocale locale) {
\r
88 return registerObject(obj, locale, LocaleKey.KIND_ANY, true);
\r
92 * Convenience override for callers using locales. This calls
\r
93 * registerObject(Object, ULocale, int kind, boolean visible)
\r
94 * passing KIND_ANY for the kind.
\r
96 public Factory registerObject(Object obj, ULocale locale, boolean visible) {
\r
97 return registerObject(obj, locale, LocaleKey.KIND_ANY, visible);
\r
101 * Convenience function for callers using locales. This calls
\r
102 * registerObject(Object, ULocale, int kind, boolean visible)
\r
103 * passing true for the visibility.
\r
105 public Factory registerObject(Object obj, ULocale locale, int kind) {
\r
106 return registerObject(obj, locale, kind, true);
\r
110 * Convenience function for callers using locales. This instantiates
\r
111 * a SimpleLocaleKeyFactory, and registers the factory.
\r
113 public Factory registerObject(Object obj, ULocale locale, int kind, boolean visible) {
\r
114 Factory factory = new SimpleLocaleKeyFactory(obj, locale, kind, visible);
\r
115 return registerFactory(factory);
\r
119 * Convenience method for callers using locales. This returns the standard
\r
120 * Locale list, built from the Set of visible ids.
\r
122 public Locale[] getAvailableLocales() {
\r
123 // TODO make this wrap getAvailableULocales later
\r
124 Set<String> visIDs = getVisibleIDs();
\r
125 Locale[] locales = new Locale[visIDs.size()];
\r
127 for (String id : visIDs) {
\r
128 Locale loc = LocaleUtility.getLocaleFromName(id);
\r
129 locales[n++] = loc;
\r
135 * Convenience method for callers using locales. This returns the standard
\r
136 * ULocale list, built from the Set of visible ids.
\r
138 public ULocale[] getAvailableULocales() {
\r
139 Set<String> visIDs = getVisibleIDs();
\r
140 ULocale[] locales = new ULocale[visIDs.size()];
\r
142 for (String id : visIDs) {
\r
143 locales[n++] = new ULocale(id);
\r
149 * A subclass of Key that implements a locale fallback mechanism.
\r
150 * The first locale to search for is the locale provided by the
\r
151 * client, and the fallback locale to search for is the current
\r
152 * default locale. If a prefix is present, the currentDescriptor
\r
153 * includes it before the locale proper, separated by "/". This
\r
154 * is the default key instantiated by ICULocaleService.</p>
\r
156 * <p>Canonicalization adjusts the locale string so that the
\r
157 * section before the first understore is in lower case, and the rest
\r
158 * is in upper case, with no trailing underscores.</p>
\r
160 public static class LocaleKey extends ICUService.Key {
\r
162 private int varstart;
\r
163 private String primaryID;
\r
164 private String fallbackID;
\r
165 private String currentID;
\r
167 public static final int KIND_ANY = -1;
\r
170 * Create a LocaleKey with canonical primary and fallback IDs.
\r
172 public static LocaleKey createWithCanonicalFallback(String primaryID, String canonicalFallbackID) {
\r
173 return createWithCanonicalFallback(primaryID, canonicalFallbackID, KIND_ANY);
\r
177 * Create a LocaleKey with canonical primary and fallback IDs.
\r
179 public static LocaleKey createWithCanonicalFallback(String primaryID, String canonicalFallbackID, int kind) {
\r
180 if (primaryID == null) {
\r
183 String canonicalPrimaryID = ULocale.getName(primaryID);
\r
184 return new LocaleKey(primaryID, canonicalPrimaryID, canonicalFallbackID, kind);
\r
188 * Create a LocaleKey with canonical primary and fallback IDs.
\r
190 public static LocaleKey createWithCanonical(ULocale locale, String canonicalFallbackID, int kind) {
\r
191 if (locale == null) {
\r
194 String canonicalPrimaryID = locale.getName();
\r
195 return new LocaleKey(canonicalPrimaryID, canonicalPrimaryID, canonicalFallbackID, kind);
\r
199 * PrimaryID is the user's requested locale string,
\r
200 * canonicalPrimaryID is this string in canonical form,
\r
201 * fallbackID is the current default locale's string in
\r
204 protected LocaleKey(String primaryID, String canonicalPrimaryID, String canonicalFallbackID, int kind) {
\r
208 if (canonicalPrimaryID == null || canonicalPrimaryID.equalsIgnoreCase("root")) {
\r
209 this.primaryID = "";
\r
210 this.fallbackID = null;
\r
212 int idx = canonicalPrimaryID.indexOf('@');
\r
213 if (idx == 4 && canonicalPrimaryID.regionMatches(true, 0, "root", 0, 4)) {
\r
214 this.primaryID = canonicalPrimaryID.substring(4);
\r
216 this.fallbackID = null;
\r
218 this.primaryID = canonicalPrimaryID;
\r
219 this.varstart = idx;
\r
221 if (canonicalFallbackID == null || this.primaryID.equals(canonicalFallbackID)) {
\r
222 this.fallbackID = "";
\r
224 this.fallbackID = canonicalFallbackID;
\r
229 this.currentID = varstart == -1 ? this.primaryID : this.primaryID.substring(0, varstart);
\r
233 * Return the prefix associated with the kind, or null if the kind is KIND_ANY.
\r
235 public String prefix() {
\r
236 return kind == KIND_ANY ? null : Integer.toString(kind());
\r
240 * Return the kind code associated with this key.
\r
242 public int kind() {
\r
247 * Return the (canonical) original ID.
\r
249 public String canonicalID() {
\r
254 * Return the (canonical) current ID, or null if no current id.
\r
256 public String currentID() {
\r
261 * Return the (canonical) current descriptor, or null if no current id.
\r
262 * Includes the keywords, whereas the ID does not include keywords.
\r
264 public String currentDescriptor() {
\r
265 String result = currentID();
\r
266 if (result != null) {
\r
267 StringBuilder buf = new StringBuilder(); // default capacity 16 is usually good enough
\r
268 if (kind != KIND_ANY) {
\r
269 buf.append(prefix());
\r
272 buf.append(result);
\r
273 if (varstart != -1) {
\r
274 buf.append(primaryID.substring(varstart, primaryID.length()));
\r
276 result = buf.toString();
\r
282 * Convenience method to return the locale corresponding to the (canonical) original ID.
\r
284 public ULocale canonicalLocale() {
\r
285 return new ULocale(primaryID);
\r
289 * Convenience method to return the ulocale corresponding to the (canonical) currentID.
\r
291 public ULocale currentLocale() {
\r
292 if (varstart == -1) {
\r
293 return new ULocale(currentID);
\r
295 return new ULocale(currentID + primaryID.substring(varstart));
\r
300 * If the key has a fallback, modify the key and return true,
\r
301 * otherwise return false.</p>
\r
303 * <p>First falls back through the primary ID, then through
\r
304 * the fallbackID. The final fallback is "" (root)
\r
305 * unless the primary id was "" (root), in which case
\r
306 * there is no fallback.
\r
308 public boolean fallback() {
\r
309 int x = currentID.lastIndexOf('_');
\r
311 while (--x >= 0 && currentID.charAt(x) == '_') { // handle zh__PINYIN
\r
313 currentID = currentID.substring(0, x+1);
\r
316 if (fallbackID != null) {
\r
317 currentID = fallbackID;
\r
318 if (fallbackID.length() == 0) {
\r
330 * If a key created from id would eventually fallback to match the
\r
331 * canonical ID of this key, return true.
\r
333 public boolean isFallbackOf(String id) {
\r
334 return LocaleUtility.isFallbackOf(canonicalID(), id);
\r
339 * A subclass of Factory that uses LocaleKeys. If 'visible' the
\r
340 * factory reports its IDs.
\r
342 public static abstract class LocaleKeyFactory implements Factory {
\r
343 protected final String name;
\r
344 protected final boolean visible;
\r
346 public static final boolean VISIBLE = true;
\r
347 public static final boolean INVISIBLE = false;
\r
350 * Constructor used by subclasses.
\r
352 protected LocaleKeyFactory(boolean visible) {
\r
353 this.visible = visible;
\r
358 * Constructor used by subclasses.
\r
360 protected LocaleKeyFactory(boolean visible, String name) {
\r
361 this.visible = visible;
\r
366 * Implement superclass abstract method. This checks the currentID of
\r
367 * the key against the supported IDs, and passes the canonicalLocale and
\r
368 * kind off to handleCreate (which subclasses must implement).
\r
370 public Object create(Key key, ICUService service) {
\r
371 if (handlesKey(key)) {
\r
372 LocaleKey lkey = (LocaleKey)key;
\r
373 int kind = lkey.kind();
\r
375 ULocale uloc = lkey.currentLocale();
\r
376 return handleCreate(uloc, kind, service);
\r
378 // System.out.println("factory: " + this + " did not support id: " + key.currentID());
\r
379 // System.out.println("supported ids: " + getSupportedIDs());
\r
384 protected boolean handlesKey(Key key) {
\r
386 String id = key.currentID();
\r
387 Set<String> supported = getSupportedIDs();
\r
388 return supported.contains(id);
\r
394 * Override of superclass method.
\r
396 public void updateVisibleIDs(Map<String, Factory> result) {
\r
397 Set<String> cache = getSupportedIDs();
\r
398 for (String id : cache) {
\r
400 result.put(id, this);
\r
408 * Return a localized name for the locale represented by id.
\r
410 public String getDisplayName(String id, ULocale locale) {
\r
411 // assume if the user called this on us, we must have handled some fallback of this id
\r
412 // if (isSupportedID(id)) {
\r
413 if (locale == null) {
\r
416 ULocale loc = new ULocale(id);
\r
417 return loc.getDisplayName(locale);
\r
424 * Utility method used by create(Key, ICUService). Subclasses can
\r
425 * implement this instead of create.
\r
427 protected Object handleCreate(ULocale loc, int kind, ICUService service) {
\r
433 * Return true if this id is one the factory supports (visible or
\r
436 protected boolean isSupportedID(String id) {
\r
437 return getSupportedIDs().contains(id);
\r
441 * Return the set of ids that this factory supports (visible or
\r
442 * otherwise). This can be called often and might need to be
\r
443 * cached if it is expensive to create.
\r
445 protected Set<String> getSupportedIDs() {
\r
446 return Collections.emptySet();
\r
452 public String toString() {
\r
453 StringBuilder buf = new StringBuilder(super.toString());
\r
454 if (name != null) {
\r
455 buf.append(", name: ");
\r
458 buf.append(", visible: ");
\r
459 buf.append(visible);
\r
460 return buf.toString();
\r
465 * A LocaleKeyFactory that just returns a single object for a kind/locale.
\r
467 public static class SimpleLocaleKeyFactory extends LocaleKeyFactory {
\r
468 private final Object obj;
\r
469 private final String id;
\r
470 private final int kind;
\r
472 // TODO: remove when we no longer need this
\r
473 public SimpleLocaleKeyFactory(Object obj, ULocale locale, int kind, boolean visible) {
\r
474 this(obj, locale, kind, visible, null);
\r
477 public SimpleLocaleKeyFactory(Object obj, ULocale locale, int kind, boolean visible, String name) {
\r
478 super(visible, name);
\r
481 this.id = locale.getBaseName();
\r
486 * Returns the service object if kind/locale match. Service is not used.
\r
488 public Object create(Key key, ICUService service) {
\r
489 LocaleKey lkey = (LocaleKey)key;
\r
490 if (kind == LocaleKey.KIND_ANY || kind == lkey.kind()) {
\r
491 String keyID = lkey.currentID();
\r
492 if (id.equals(keyID)) {
\r
499 protected boolean isSupportedID(String idToCheck) {
\r
500 return this.id.equals(idToCheck);
\r
503 public void updateVisibleIDs(Map<String, Factory> result) {
\r
505 result.put(id, this);
\r
511 public String toString() {
\r
512 StringBuilder buf = new StringBuilder(super.toString());
\r
513 buf.append(", id: ");
\r
515 buf.append(", kind: ");
\r
517 return buf.toString();
\r
522 * A LocaleKeyFactory that creates a service based on the ICU locale data.
\r
523 * This is a base class for most ICU factories. Subclasses instantiate it
\r
524 * with a constructor that takes a bundle name, which determines the supported
\r
525 * IDs. Subclasses then override handleCreate to create the actual service
\r
526 * object. The default implementation returns a resource bundle.
\r
528 public static class ICUResourceBundleFactory extends LocaleKeyFactory {
\r
529 protected final String bundleName;
\r
532 * Convenience constructor that uses the main ICU bundle name.
\r
534 public ICUResourceBundleFactory() {
\r
535 this(ICUResourceBundle.ICU_BASE_NAME);
\r
539 * A service factory based on ICU resource data in resources
\r
540 * with the given name.
\r
542 public ICUResourceBundleFactory(String bundleName) {
\r
545 this.bundleName = bundleName;
\r
549 * Return the supported IDs. This is the set of all locale names for the bundleName.
\r
551 protected Set<String> getSupportedIDs() {
\r
552 return ICUResourceBundle.getFullLocaleNameSet(bundleName, loader());
\r
556 * Override of superclass method.
\r
558 public void updateVisibleIDs(Map<String, Factory> result) {
\r
559 Set<String> visibleIDs = ICUResourceBundle.getAvailableLocaleNameSet(bundleName, loader()); // only visible ids
\r
560 for (String id : visibleIDs) {
\r
561 result.put(id, this);
\r
566 * Create the service. The default implementation returns the resource bundle
\r
567 * for the locale, ignoring kind, and service.
\r
569 protected Object handleCreate(ULocale loc, int kind, ICUService service) {
\r
570 return ICUResourceBundle.getBundleInstance(bundleName, loc, loader());
\r
573 protected ClassLoader loader() {
\r
574 ClassLoader cl = getClass().getClassLoader();
\r
576 cl = Utility.getFallbackClassLoader();
\r
581 public String toString() {
\r
582 return super.toString() + ", bundle: " + bundleName;
\r
587 * Return the name of the current fallback locale. If it has changed since this was
\r
588 * last accessed, the service cache is cleared.
\r
590 public String validateFallbackLocale() {
\r
591 ULocale loc = ULocale.getDefault();
\r
592 if (loc != fallbackLocale) {
\r
593 synchronized (this) {
\r
594 if (loc != fallbackLocale) {
\r
595 fallbackLocale = loc;
\r
596 fallbackLocaleName = loc.getBaseName();
\r
597 clearServiceCache();
\r
601 return fallbackLocaleName;
\r
604 public Key createKey(String id) {
\r
605 return LocaleKey.createWithCanonicalFallback(id, validateFallbackLocale());
\r
608 public Key createKey(String id, int kind) {
\r
609 return LocaleKey.createWithCanonicalFallback(id, validateFallbackLocale(), kind);
\r
612 public Key createKey(ULocale l, int kind) {
\r
613 return LocaleKey.createWithCanonical(l, validateFallbackLocale(), kind);
\r