1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/servlkf.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/** 1.5 + ******************************************************************************* 1.6 + * Copyright (C) 2001-2005, International Business Machines Corporation and * 1.7 + * others. All Rights Reserved. * 1.8 + ******************************************************************************* 1.9 + * 1.10 + ******************************************************************************* 1.11 + */ 1.12 +#include "unicode/utypes.h" 1.13 + 1.14 +#if !UCONFIG_NO_SERVICE 1.15 + 1.16 +#include "unicode/resbund.h" 1.17 +#include "uresimp.h" 1.18 +#include "cmemory.h" 1.19 +#include "servloc.h" 1.20 +#include "ustrfmt.h" 1.21 +#include "uhash.h" 1.22 +#include "charstr.h" 1.23 +#include "ucln_cmn.h" 1.24 +#include "uassert.h" 1.25 + 1.26 +#define UNDERSCORE_CHAR ((UChar)0x005f) 1.27 +#define AT_SIGN_CHAR ((UChar)64) 1.28 +#define PERIOD_CHAR ((UChar)46) 1.29 + 1.30 + 1.31 +U_NAMESPACE_BEGIN 1.32 + 1.33 +LocaleKeyFactory::LocaleKeyFactory(int32_t coverage) 1.34 + : _name() 1.35 + , _coverage(coverage) 1.36 +{ 1.37 +} 1.38 + 1.39 +LocaleKeyFactory::LocaleKeyFactory(int32_t coverage, const UnicodeString& name) 1.40 + : _name(name) 1.41 + , _coverage(coverage) 1.42 +{ 1.43 +} 1.44 + 1.45 +LocaleKeyFactory::~LocaleKeyFactory() { 1.46 +} 1.47 + 1.48 +UObject* 1.49 +LocaleKeyFactory::create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const { 1.50 + if (handlesKey(key, status)) { 1.51 + const LocaleKey& lkey = (const LocaleKey&)key; 1.52 + int32_t kind = lkey.kind(); 1.53 + Locale loc; 1.54 + lkey.currentLocale(loc); 1.55 + 1.56 + return handleCreate(loc, kind, service, status); 1.57 + } 1.58 + return NULL; 1.59 +} 1.60 + 1.61 +UBool 1.62 +LocaleKeyFactory::handlesKey(const ICUServiceKey& key, UErrorCode& status) const { 1.63 + const Hashtable* supported = getSupportedIDs(status); 1.64 + if (supported) { 1.65 + UnicodeString id; 1.66 + key.currentID(id); 1.67 + return supported->get(id) != NULL; 1.68 + } 1.69 + return FALSE; 1.70 +} 1.71 + 1.72 +void 1.73 +LocaleKeyFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const { 1.74 + const Hashtable* supported = getSupportedIDs(status); 1.75 + if (supported) { 1.76 + UBool visible = (_coverage & 0x1) == 0; 1.77 + 1.78 + const UHashElement* elem = NULL; 1.79 + int32_t pos = 0; 1.80 + while ((elem = supported->nextElement(pos)) != NULL) { 1.81 + const UnicodeString& id = *((const UnicodeString*)elem->key.pointer); 1.82 + if (!visible) { 1.83 + result.remove(id); 1.84 + } else { 1.85 + result.put(id, (void*)this, status); // this is dummy non-void marker used for set semantics 1.86 + if (U_FAILURE(status)) { 1.87 + break; 1.88 + } 1.89 + } 1.90 + } 1.91 + } 1.92 +} 1.93 + 1.94 +UnicodeString& 1.95 +LocaleKeyFactory::getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const { 1.96 + if ((_coverage & 0x1) == 0) { 1.97 + //UErrorCode status = U_ZERO_ERROR; 1.98 + // assume if this is called on us, we support some fallback of this id 1.99 + // if (isSupportedID(id, status)) { 1.100 + Locale loc; 1.101 + LocaleUtility::initLocaleFromName(id, loc); 1.102 + return loc.getDisplayName(locale, result); 1.103 + // } 1.104 + } 1.105 + result.setToBogus(); 1.106 + return result; 1.107 +} 1.108 + 1.109 +UObject* 1.110 +LocaleKeyFactory::handleCreate(const Locale& /* loc */, 1.111 + int32_t /* kind */, 1.112 + const ICUService* /* service */, 1.113 + UErrorCode& /* status */) const { 1.114 + return NULL; 1.115 +} 1.116 + 1.117 +//UBool 1.118 +//LocaleKeyFactory::isSupportedID(const UnicodeString& id, UErrorCode& status) const { 1.119 +// const Hashtable* ids = getSupportedIDs(status); 1.120 +// return ids && ids->get(id); 1.121 +//} 1.122 + 1.123 +const Hashtable* 1.124 +LocaleKeyFactory::getSupportedIDs(UErrorCode& /* status */) const { 1.125 + return NULL; 1.126 +} 1.127 + 1.128 +#ifdef SERVICE_DEBUG 1.129 +UnicodeString& 1.130 +LocaleKeyFactory::debug(UnicodeString& result) const 1.131 +{ 1.132 + debugClass(result); 1.133 + result.append(", name: "); 1.134 + result.append(_name); 1.135 + result.append(", coverage: "); 1.136 + result.append(_coverage); 1.137 + return result; 1.138 +} 1.139 + 1.140 +UnicodeString& 1.141 +LocaleKeyFactory::debugClass(UnicodeString& result) const 1.142 +{ 1.143 + return result.append("LocaleKeyFactory"); 1.144 +} 1.145 +#endif 1.146 + 1.147 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKeyFactory) 1.148 + 1.149 +U_NAMESPACE_END 1.150 + 1.151 +/* !UCONFIG_NO_SERVICE */ 1.152 +#endif 1.153 + 1.154 +