servlkf.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /**
  4. *******************************************************************************
  5. * Copyright (C) 2001-2014, International Business Machines Corporation and *
  6. * others. All Rights Reserved. *
  7. *******************************************************************************
  8. *
  9. *******************************************************************************
  10. */
  11. #include "unicode/utypes.h"
  12. #if !UCONFIG_NO_SERVICE
  13. #include "unicode/resbund.h"
  14. #include "uresimp.h"
  15. #include "cmemory.h"
  16. #include "servloc.h"
  17. #include "ustrfmt.h"
  18. #include "uhash.h"
  19. #include "charstr.h"
  20. #include "ucln_cmn.h"
  21. #include "uassert.h"
  22. #define UNDERSCORE_CHAR ((char16_t)0x005f)
  23. #define AT_SIGN_CHAR ((char16_t)64)
  24. #define PERIOD_CHAR ((char16_t)46)
  25. U_NAMESPACE_BEGIN
  26. LocaleKeyFactory::LocaleKeyFactory(int32_t coverage)
  27. : _name()
  28. , _coverage(coverage)
  29. {
  30. }
  31. LocaleKeyFactory::LocaleKeyFactory(int32_t coverage, const UnicodeString& name)
  32. : _name(name)
  33. , _coverage(coverage)
  34. {
  35. }
  36. LocaleKeyFactory::~LocaleKeyFactory() {
  37. }
  38. UObject*
  39. LocaleKeyFactory::create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const {
  40. if (handlesKey(key, status)) {
  41. const LocaleKey& lkey = static_cast<const LocaleKey&>(key);
  42. int32_t kind = lkey.kind();
  43. Locale loc;
  44. lkey.currentLocale(loc);
  45. return handleCreate(loc, kind, service, status);
  46. }
  47. return nullptr;
  48. }
  49. UBool
  50. LocaleKeyFactory::handlesKey(const ICUServiceKey& key, UErrorCode& status) const {
  51. const Hashtable* supported = getSupportedIDs(status);
  52. if (supported) {
  53. UnicodeString id;
  54. key.currentID(id);
  55. return supported->get(id) != nullptr;
  56. }
  57. return false;
  58. }
  59. void
  60. LocaleKeyFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const {
  61. const Hashtable* supported = getSupportedIDs(status);
  62. if (supported) {
  63. UBool visible = (_coverage & 0x1) == 0;
  64. const UHashElement* elem = nullptr;
  65. int32_t pos = UHASH_FIRST;
  66. while ((elem = supported->nextElement(pos)) != nullptr) {
  67. const UnicodeString& id = *((const UnicodeString*)elem->key.pointer);
  68. if (!visible) {
  69. result.remove(id);
  70. } else {
  71. result.put(id, (void*)this, status); // this is dummy non-void marker used for set semantics
  72. if (U_FAILURE(status)) {
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. UnicodeString&
  80. LocaleKeyFactory::getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const {
  81. if ((_coverage & 0x1) == 0) {
  82. //UErrorCode status = U_ZERO_ERROR;
  83. // assume if this is called on us, we support some fallback of this id
  84. // if (isSupportedID(id, status)) {
  85. Locale loc;
  86. LocaleUtility::initLocaleFromName(id, loc);
  87. return loc.getDisplayName(locale, result);
  88. // }
  89. }
  90. result.setToBogus();
  91. return result;
  92. }
  93. UObject*
  94. LocaleKeyFactory::handleCreate(const Locale& /* loc */,
  95. int32_t /* kind */,
  96. const ICUService* /* service */,
  97. UErrorCode& /* status */) const {
  98. return nullptr;
  99. }
  100. //UBool
  101. //LocaleKeyFactory::isSupportedID(const UnicodeString& id, UErrorCode& status) const {
  102. // const Hashtable* ids = getSupportedIDs(status);
  103. // return ids && ids->get(id);
  104. //}
  105. const Hashtable*
  106. LocaleKeyFactory::getSupportedIDs(UErrorCode& /* status */) const {
  107. return nullptr;
  108. }
  109. #ifdef SERVICE_DEBUG
  110. UnicodeString&
  111. LocaleKeyFactory::debug(UnicodeString& result) const
  112. {
  113. debugClass(result);
  114. result.append((UnicodeString)", name: ");
  115. result.append(_name);
  116. result.append((UnicodeString)", coverage: ");
  117. result.append(_coverage);
  118. return result;
  119. }
  120. UnicodeString&
  121. LocaleKeyFactory::debugClass(UnicodeString& result) const
  122. {
  123. return result.append((UnicodeString)"LocaleKeyFactory");
  124. }
  125. #endif
  126. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKeyFactory)
  127. U_NAMESPACE_END
  128. /* !UCONFIG_NO_SERVICE */
  129. #endif