1 7 package com.ibm.icu.impl; 8 9 import java.util.Collections ; 10 import java.util.Iterator ; 11 import java.util.Locale ; 12 import java.util.Map ; 13 import java.util.Set ; 14 15 import com.ibm.icu.util.ULocale; 16 17 public class ICULocaleService extends ICUService { 18 private ULocale fallbackLocale; 19 private String fallbackLocaleName; 20 21 24 public ICULocaleService() { 25 } 26 27 30 public ICULocaleService(String name) { 31 super(name); 32 } 33 34 39 public Object get(ULocale locale) { 40 return get(locale, LocaleKey.KIND_ANY, null); 41 } 42 43 47 public Object get(ULocale locale, int kind) { 48 return get(locale, kind, null); 49 } 50 51 55 public Object get(ULocale locale, ULocale[] actualReturn) { 56 return get(locale, LocaleKey.KIND_ANY, actualReturn); 57 } 58 59 65 public Object get(ULocale locale, int kind, ULocale[] actualReturn) { 66 Key key = createKey(locale, kind); 67 if (actualReturn == null) { 68 return getKey(key); 69 } 70 71 String [] temp = new String [1]; 72 Object result = getKey(key, temp); 73 if (result != null) { 74 int n = temp[0].indexOf("/"); 75 if (n >= 0) { 76 temp[0] = temp[0].substring(n+1); 77 } 78 actualReturn[0] = new ULocale(temp[0]); 79 } 80 return result; 81 } 82 83 88 public Factory registerObject(Object obj, ULocale locale) { 89 return registerObject(obj, locale, LocaleKey.KIND_ANY, true); 90 } 91 92 97 public Factory registerObject(Object obj, ULocale locale, boolean visible) { 98 return registerObject(obj, locale, LocaleKey.KIND_ANY, visible); 99 } 100 101 106 public Factory registerObject(Object obj, ULocale locale, int kind) { 107 return registerObject(obj, locale, kind, true); 108 } 109 110 114 public Factory registerObject(Object obj, ULocale locale, int kind, boolean visible) { 115 Factory factory = new SimpleLocaleKeyFactory(obj, locale, kind, visible); 116 return registerFactory(factory); 117 } 118 119 123 public Locale [] getAvailableLocales() { 124 Set visIDs = getVisibleIDs(); 126 Iterator iter = visIDs.iterator(); 127 Locale [] locales = new Locale [visIDs.size()]; 128 int n = 0; 129 while (iter.hasNext()) { 130 Locale loc = LocaleUtility.getLocaleFromName((String )iter.next()); 131 locales[n++] = loc; 132 } 133 return locales; 134 } 135 136 140 public ULocale[] getAvailableULocales() { 141 Set visIDs = getVisibleIDs(); 142 Iterator iter = visIDs.iterator(); 143 ULocale[] locales = new ULocale[visIDs.size()]; 144 int n = 0; 145 while (iter.hasNext()) { 146 locales[n++] = new ULocale((String )iter.next()); 147 } 148 return locales; 149 } 150 151 163 public static class LocaleKey extends ICUService.Key { 164 private int kind; 165 private int varstart; 166 private String primaryID; 167 private String fallbackID; 168 private String currentID; 169 170 public static final int KIND_ANY = -1; 171 172 175 public static LocaleKey createWithCanonicalFallback(String primaryID, String canonicalFallbackID) { 176 return createWithCanonicalFallback(primaryID, canonicalFallbackID, KIND_ANY); 177 } 178 179 182 public static LocaleKey createWithCanonicalFallback(String primaryID, String canonicalFallbackID, int kind) { 183 if (primaryID == null) { 184 return null; 185 } 186 if (primaryID.length() == 0) { 187 primaryID = "root"; 188 } 189 String canonicalPrimaryID = ULocale.getName(primaryID); 190 return new LocaleKey(primaryID, canonicalPrimaryID, canonicalFallbackID, kind); 191 } 192 193 196 public static LocaleKey createWithCanonical(ULocale locale, String canonicalFallbackID, int kind) { 197 if (locale == null) { 198 return null; 199 } 200 String canonicalPrimaryID = locale.getName(); 201 return new LocaleKey(canonicalPrimaryID, canonicalPrimaryID, canonicalFallbackID, kind); 202 } 203 204 210 protected LocaleKey(String primaryID, String canonicalPrimaryID, String canonicalFallbackID, int kind) { 211 super(primaryID); 212 213 this.kind = kind; 214 if (canonicalPrimaryID == null) { 215 this.primaryID = ""; 216 } else { 217 this.primaryID = canonicalPrimaryID; 218 this.varstart = this.primaryID.indexOf('@'); 219 } 220 if (this.primaryID == "") { 221 this.fallbackID = null; 222 } else { 223 if (canonicalFallbackID == null || this.primaryID.equals(canonicalFallbackID)) { 224 this.fallbackID = ""; 225 } else { 226 this.fallbackID = canonicalFallbackID; 227 } 228 } 229 230 this.currentID = varstart == -1 ? this.primaryID : this.primaryID.substring(0, varstart); 231 } 232 233 236 public String prefix() { 237 return kind == KIND_ANY ? null : Integer.toString(kind()); 238 } 239 240 243 public int kind() { 244 return kind; 245 } 246 247 250 public String canonicalID() { 251 return primaryID; 252 } 253 254 257 public String currentID() { 258 return currentID; 259 } 260 261 265 public String currentDescriptor() { 266 String result = currentID(); 267 if (result != null) { 268 StringBuffer buf = new StringBuffer (); if (kind != KIND_ANY) { 270 buf.append(prefix()); 271 } 272 buf.append('/'); 273 buf.append(result); 274 if (varstart != -1) { 275 buf.append(primaryID.substring(varstart, primaryID.length())); 276 } 277 result = buf.toString(); 278 } 279 return result; 280 } 281 282 285 public ULocale canonicalLocale() { 286 return new ULocale(primaryID); 287 } 288 289 292 public ULocale currentLocale() { 293 if (varstart == -1) { 294 return new ULocale(currentID); 295 } else { 296 return new ULocale(currentID + primaryID.substring(varstart)); 297 } 298 } 299 300 309 public boolean fallback() { 310 int x = currentID.lastIndexOf('_'); 311 if (x != -1) { 312 while (--x >= 0 && currentID.charAt(x) == '_') { } 314 currentID = currentID.substring(0, x+1); 315 return true; 316 } 317 if (fallbackID != null) { 318 if (fallbackID.length() == 0) { 319 currentID = "root"; 320 fallbackID = null; 321 } else { 322 currentID = fallbackID; 323 fallbackID = ""; 324 } 325 return true; 326 } 327 currentID = null; 328 return false; 329 } 330 331 335 public boolean isFallbackOf(String id) { 336 return LocaleUtility.isFallbackOf(canonicalID(), id); 337 } 338 } 339 340 344 public static abstract class LocaleKeyFactory implements Factory { 345 protected final String name; 346 protected final boolean visible; 347 348 public static final boolean VISIBLE = true; 349 public static final boolean INVISIBLE = false; 350 351 354 protected LocaleKeyFactory(boolean visible) { 355 this.visible = visible; 356 this.name = null; 357 } 358 359 362 protected LocaleKeyFactory(boolean visible, String name) { 363 this.visible = visible; 364 this.name = name; 365 } 366 367 372 public Object create(Key key, ICUService service) { 373 if (handlesKey(key)) { 374 LocaleKey lkey = (LocaleKey)key; 375 int kind = lkey.kind(); 376 377 ULocale uloc = lkey.currentLocale(); 378 return handleCreate(uloc, kind, service); 379 } else { 380 } 383 return null; 384 } 385 386 protected boolean handlesKey(Key key) { 387 if (key != null) { 388 String id = key.currentID(); 389 Set supported = getSupportedIDs(); 390 return supported.contains(id); 391 } 392 return false; 393 } 394 395 398 public void updateVisibleIDs(Map result) { 399 Set cache = getSupportedIDs(); 400 Iterator iter = cache.iterator(); 401 while (iter.hasNext()) { 402 String id = (String )iter.next(); 403 if (visible) { 404 result.put(id, this); 405 } else { 406 result.remove(id); 407 } 408 } 409 } 410 411 414 public String getDisplayName(String id, ULocale locale) { 415 if (locale == null) { 418 return id; 419 } 420 ULocale loc = new ULocale(id); 421 return loc.getDisplayName(locale); 422 } 425 426 431 protected Object handleCreate(ULocale loc, int kind, ICUService service) { 432 return null; 433 } 434 436 440 protected boolean isSupportedID(String id) { 441 return getSupportedIDs().contains(id); 442 } 443 444 449 protected Set getSupportedIDs() { 450 return Collections.EMPTY_SET; 451 } 452 453 456 public String toString() { 457 StringBuffer buf = new StringBuffer (super.toString()); 458 if (name != null) { 459 buf.append(", name: "); 460 buf.append(name); 461 } 462 buf.append(", visible: "); 463 buf.append(visible); 464 return buf.toString(); 465 } 466 } 467 468 471 public static class SimpleLocaleKeyFactory extends LocaleKeyFactory { 472 private final Object obj; 473 private final String id; 474 private final int kind; 475 476 public SimpleLocaleKeyFactory(Object obj, ULocale locale, int kind, boolean visible) { 478 this(obj, locale, kind, visible, null); 479 } 480 481 public SimpleLocaleKeyFactory(Object obj, ULocale locale, int kind, boolean visible, String name) { 482 super(visible, name); 483 484 this.obj = obj; 485 this.id = locale.getBaseName(); 486 this.kind = kind; 487 } 488 489 492 public Object create(Key key, ICUService service) { 493 LocaleKey lkey = (LocaleKey)key; 494 if (kind == LocaleKey.KIND_ANY || kind == lkey.kind()) { 495 String keyID = lkey.currentID(); 496 if (id.equals(keyID)) { 497 return obj; 498 } 499 } 500 return null; 501 } 502 503 protected boolean isSupportedID(String id) { 504 return this.id.equals(id); 505 } 506 507 public void updateVisibleIDs(Map result) { 508 if (visible) { 509 result.put(id, this); 510 } else { 511 result.remove(id); 512 } 513 } 514 515 public String toString() { 516 StringBuffer buf = new StringBuffer (super.toString()); 517 buf.append(", id: "); 518 buf.append(id); 519 buf.append(", kind: "); 520 buf.append(kind); 521 return buf.toString(); 522 } 523 } 524 525 532 public static class ICUResourceBundleFactory extends LocaleKeyFactory { 533 protected final String bundleName; 534 535 538 public ICUResourceBundleFactory() { 539 this(ICUResourceBundle.ICU_BASE_NAME); 540 } 541 542 546 public ICUResourceBundleFactory(String bundleName) { 547 super(true); 548 549 this.bundleName = bundleName; 550 } 551 552 555 protected Set getSupportedIDs() { 556 return ICUResourceBundle.getFullLocaleNameSet(bundleName); 558 } 559 560 563 public void updateVisibleIDs(Map result) { 564 Set visibleIDs = ICUResourceBundle.getAvailableLocaleNameSet(bundleName); Iterator iter = visibleIDs.iterator(); 566 while (iter.hasNext()) { 567 String id = (String )iter.next(); 568 result.put(id, this); 569 } 570 } 571 572 576 protected Object handleCreate(ULocale loc, int kind, ICUService service) { 577 return ICUResourceBundle.getBundleInstance(bundleName, loc); 578 } 579 580 public String toString() { 581 return super.toString() + ", bundle: " + bundleName; 582 } 583 } 584 585 589 public String validateFallbackLocale() { 590 ULocale loc = ULocale.getDefault(); 591 if (loc != fallbackLocale) { 592 synchronized (this) { 593 if (loc != fallbackLocale) { 594 fallbackLocale = loc; 595 fallbackLocaleName = loc.getBaseName(); 596 clearServiceCache(); 597 } 598 } 599 } 600 return fallbackLocaleName; 601 } 602 603 public Key createKey(String id) { 604 return LocaleKey.createWithCanonicalFallback(id, validateFallbackLocale()); 605 } 606 607 public Key createKey(String id, int kind) { 608 return LocaleKey.createWithCanonicalFallback(id, validateFallbackLocale(), kind); 609 } 610 611 public Key createKey(ULocale l, int kind) { 612 return LocaleKey.createWithCanonical(l, validateFallbackLocale(), kind); 613 } 614 } 615 | Popular Tags |