Line data Source code
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 ((UChar)0x005f)
26 : #define AT_SIGN_CHAR ((UChar)64)
27 : #define PERIOD_CHAR ((UChar)46)
28 :
29 :
30 : U_NAMESPACE_BEGIN
31 :
32 0 : LocaleKeyFactory::LocaleKeyFactory(int32_t coverage)
33 : : _name()
34 0 : , _coverage(coverage)
35 : {
36 0 : }
37 :
38 0 : LocaleKeyFactory::LocaleKeyFactory(int32_t coverage, const UnicodeString& name)
39 : : _name(name)
40 0 : , _coverage(coverage)
41 : {
42 0 : }
43 :
44 0 : LocaleKeyFactory::~LocaleKeyFactory() {
45 0 : }
46 :
47 : UObject*
48 0 : LocaleKeyFactory::create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const {
49 0 : if (handlesKey(key, status)) {
50 0 : const LocaleKey& lkey = (const LocaleKey&)key;
51 0 : int32_t kind = lkey.kind();
52 0 : Locale loc;
53 0 : lkey.currentLocale(loc);
54 :
55 0 : return handleCreate(loc, kind, service, status);
56 : }
57 0 : return NULL;
58 : }
59 :
60 : UBool
61 0 : LocaleKeyFactory::handlesKey(const ICUServiceKey& key, UErrorCode& status) const {
62 0 : const Hashtable* supported = getSupportedIDs(status);
63 0 : if (supported) {
64 0 : UnicodeString id;
65 0 : key.currentID(id);
66 0 : return supported->get(id) != NULL;
67 : }
68 0 : return FALSE;
69 : }
70 :
71 : void
72 0 : LocaleKeyFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const {
73 0 : const Hashtable* supported = getSupportedIDs(status);
74 0 : if (supported) {
75 0 : UBool visible = (_coverage & 0x1) == 0;
76 0 : const UHashElement* elem = NULL;
77 0 : int32_t pos = UHASH_FIRST;
78 0 : while ((elem = supported->nextElement(pos)) != NULL) {
79 0 : const UnicodeString& id = *((const UnicodeString*)elem->key.pointer);
80 0 : if (!visible) {
81 0 : result.remove(id);
82 : } else {
83 0 : result.put(id, (void*)this, status); // this is dummy non-void marker used for set semantics
84 0 : if (U_FAILURE(status)) {
85 0 : break;
86 : }
87 : }
88 : }
89 : }
90 0 : }
91 :
92 : UnicodeString&
93 0 : LocaleKeyFactory::getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const {
94 0 : 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 0 : Locale loc;
99 0 : LocaleUtility::initLocaleFromName(id, loc);
100 0 : return loc.getDisplayName(locale, result);
101 : // }
102 : }
103 0 : result.setToBogus();
104 0 : return result;
105 : }
106 :
107 : UObject*
108 0 : LocaleKeyFactory::handleCreate(const Locale& /* loc */,
109 : int32_t /* kind */,
110 : const ICUService* /* service */,
111 : UErrorCode& /* status */) const {
112 0 : return NULL;
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 :
121 : const Hashtable*
122 0 : LocaleKeyFactory::getSupportedIDs(UErrorCode& /* status */) const {
123 0 : return NULL;
124 : }
125 :
126 : #ifdef SERVICE_DEBUG
127 : UnicodeString&
128 : LocaleKeyFactory::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 :
138 : UnicodeString&
139 : LocaleKeyFactory::debugClass(UnicodeString& result) const
140 : {
141 : return result.append((UnicodeString)"LocaleKeyFactory");
142 : }
143 : #endif
144 :
145 0 : UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKeyFactory)
146 :
147 : U_NAMESPACE_END
148 :
149 : /* !UCONFIG_NO_SERVICE */
150 : #endif
151 :
152 :
|