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
13#if !UCONFIG_NO_SERVICE
14
15#include "unicode/resbund.h"
16#include "uresimp.h"
17#include "cmemory.h"
18#include "servloc.h"
19#include "ustrfmt.h"
20#include "uhash.h"
21#include "charstr.h"
22#include "ucln_cmn.h"
23#include "uassert.h"
24
25#define UNDERSCORE_CHAR ((char16_t)0x005f)
26#define AT_SIGN_CHAR ((char16_t)64)
27#define PERIOD_CHAR ((char16_t)46)
28
29
30U_NAMESPACE_BEGIN
31
32LocaleKeyFactory::LocaleKeyFactory(int32_t coverage)
33 : _name()
34 , _coverage(coverage)
35{
36}
37
38LocaleKeyFactory::LocaleKeyFactory(int32_t coverage, const UnicodeString& name)
39 : _name(name)
40 , _coverage(coverage)
41{
42}
43
44LocaleKeyFactory::~LocaleKeyFactory() {
45}
46
47UObject*
48LocaleKeyFactory::create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const {
49 if (handlesKey(key, status)) {
50 const LocaleKey& lkey = static_cast<const LocaleKey&>(key);
51 int32_t kind = lkey.kind();
52 Locale loc;
53 lkey.currentLocale(loc);
54
55 return handleCreate(loc, kind, service, status);
56 }
57 return nullptr;
58}
59
60UBool
61LocaleKeyFactory::handlesKey(const ICUServiceKey& key, UErrorCode& status) const {
62 const Hashtable* supported = getSupportedIDs(status);
63 if (supported) {
64 UnicodeString id;
65 key.currentID(id);
66 return supported->get(id) != nullptr;
67 }
68 return false;
69}
70
71void
72LocaleKeyFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const {
73 const Hashtable* supported = getSupportedIDs(status);
74 if (supported) {
75 UBool visible = (_coverage & 0x1) == 0;
76 const UHashElement* elem = nullptr;
77 int32_t pos = UHASH_FIRST;
78 while ((elem = supported->nextElement(pos)) != nullptr) {
79 const UnicodeString& id = *((const UnicodeString*)elem->key.pointer);
80 if (!visible) {
81 result.remove(id);
82 } else {
83 result.put(id, (void*)this, status); // this is dummy non-void marker used for set semantics
84 if (U_FAILURE(status)) {
85 break;
86 }
87 }
88 }
89 }
90}
91
92UnicodeString&
93LocaleKeyFactory::getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const {
94 if ((_coverage & 0x1) == 0) {
95 //UErrorCode status = U_ZERO_ERROR;
96 // assume if this is called on us, we support some fallback of this id
97 // if (isSupportedID(id, status)) {
98 Locale loc;
99 LocaleUtility::initLocaleFromName(id, loc);
100 return loc.getDisplayName(locale, result);
101 // }
102 }
103 result.setToBogus();
104 return result;
105}
106
107UObject*
108LocaleKeyFactory::handleCreate(const Locale& /* loc */,
109 int32_t /* kind */,
110 const ICUService* /* service */,
111 UErrorCode& /* status */) const {
112 return nullptr;
113}
114
115//UBool
116//LocaleKeyFactory::isSupportedID(const UnicodeString& id, UErrorCode& status) const {
117// const Hashtable* ids = getSupportedIDs(status);
118// return ids && ids->get(id);
119//}
120
121const Hashtable*
122LocaleKeyFactory::getSupportedIDs(UErrorCode& /* status */) const {
123 return nullptr;
124}
125
126#ifdef SERVICE_DEBUG
127UnicodeString&
128LocaleKeyFactory::debug(UnicodeString& result) const
129{
130 debugClass(result);
131 result.append((UnicodeString)", name: ");
132 result.append(_name);
133 result.append((UnicodeString)", coverage: ");
134 result.append(_coverage);
135 return result;
136}
137
138UnicodeString&
139LocaleKeyFactory::debugClass(UnicodeString& result) const
140{
141 return result.append((UnicodeString)"LocaleKeyFactory");
142}
143#endif
144
145UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKeyFactory)
146
147U_NAMESPACE_END
148
149/* !UCONFIG_NO_SERVICE */
150#endif
151
152
153