1 /**
2 *******************************************************************************
3 * Copyright (C) 2001-2014, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 *
7 *******************************************************************************
8 */
9 #include "unicode/utypes.h"
10
11 #if !UCONFIG_NO_SERVICE
12
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
23 #define UNDERSCORE_CHAR ((UChar)0x005f)
24 #define AT_SIGN_CHAR ((UChar)64)
25 #define PERIOD_CHAR ((UChar)46)
26
27
28 U_NAMESPACE_BEGIN
29
LocaleKeyFactory(int32_t coverage)30 LocaleKeyFactory::LocaleKeyFactory(int32_t coverage)
31 : _name()
32 , _coverage(coverage)
33 {
34 }
35
LocaleKeyFactory(int32_t coverage,const UnicodeString & name)36 LocaleKeyFactory::LocaleKeyFactory(int32_t coverage, const UnicodeString& name)
37 : _name(name)
38 , _coverage(coverage)
39 {
40 }
41
~LocaleKeyFactory()42 LocaleKeyFactory::~LocaleKeyFactory() {
43 }
44
45 UObject*
create(const ICUServiceKey & key,const ICUService * service,UErrorCode & status) const46 LocaleKeyFactory::create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const {
47 if (handlesKey(key, status)) {
48 const LocaleKey& lkey = (const LocaleKey&)key;
49 int32_t kind = lkey.kind();
50 Locale loc;
51 lkey.currentLocale(loc);
52
53 return handleCreate(loc, kind, service, status);
54 }
55 return NULL;
56 }
57
58 UBool
handlesKey(const ICUServiceKey & key,UErrorCode & status) const59 LocaleKeyFactory::handlesKey(const ICUServiceKey& key, UErrorCode& status) const {
60 const Hashtable* supported = getSupportedIDs(status);
61 if (supported) {
62 UnicodeString id;
63 key.currentID(id);
64 return supported->get(id) != NULL;
65 }
66 return FALSE;
67 }
68
69 void
updateVisibleIDs(Hashtable & result,UErrorCode & status) const70 LocaleKeyFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const {
71 const Hashtable* supported = getSupportedIDs(status);
72 if (supported) {
73 UBool visible = (_coverage & 0x1) == 0;
74 const UHashElement* elem = NULL;
75 int32_t pos = UHASH_FIRST;
76 while ((elem = supported->nextElement(pos)) != NULL) {
77 const UnicodeString& id = *((const UnicodeString*)elem->key.pointer);
78 if (!visible) {
79 result.remove(id);
80 } else {
81 result.put(id, (void*)this, status); // this is dummy non-void marker used for set semantics
82 if (U_FAILURE(status)) {
83 break;
84 }
85 }
86 }
87 }
88 }
89
90 UnicodeString&
getDisplayName(const UnicodeString & id,const Locale & locale,UnicodeString & result) const91 LocaleKeyFactory::getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const {
92 if ((_coverage & 0x1) == 0) {
93 //UErrorCode status = U_ZERO_ERROR;
94 // assume if this is called on us, we support some fallback of this id
95 // if (isSupportedID(id, status)) {
96 Locale loc;
97 LocaleUtility::initLocaleFromName(id, loc);
98 return loc.getDisplayName(locale, result);
99 // }
100 }
101 result.setToBogus();
102 return result;
103 }
104
105 UObject*
handleCreate(const Locale &,int32_t,const ICUService *,UErrorCode &) const106 LocaleKeyFactory::handleCreate(const Locale& /* loc */,
107 int32_t /* kind */,
108 const ICUService* /* service */,
109 UErrorCode& /* status */) const {
110 return NULL;
111 }
112
113 //UBool
114 //LocaleKeyFactory::isSupportedID(const UnicodeString& id, UErrorCode& status) const {
115 // const Hashtable* ids = getSupportedIDs(status);
116 // return ids && ids->get(id);
117 //}
118
119 const Hashtable*
getSupportedIDs(UErrorCode &) const120 LocaleKeyFactory::getSupportedIDs(UErrorCode& /* status */) const {
121 return NULL;
122 }
123
124 #ifdef SERVICE_DEBUG
125 UnicodeString&
debug(UnicodeString & result) const126 LocaleKeyFactory::debug(UnicodeString& result) const
127 {
128 debugClass(result);
129 result.append((UnicodeString)", name: ");
130 result.append(_name);
131 result.append((UnicodeString)", coverage: ");
132 result.append(_coverage);
133 return result;
134 }
135
136 UnicodeString&
debugClass(UnicodeString & result) const137 LocaleKeyFactory::debugClass(UnicodeString& result) const
138 {
139 return result.append((UnicodeString)"LocaleKeyFactory");
140 }
141 #endif
142
143 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKeyFactory)
144
145 U_NAMESPACE_END
146
147 /* !UCONFIG_NO_SERVICE */
148 #endif
149
150
151