Home | History | Annotate | Download | only in common
      1 /**
      2  *******************************************************************************
      3  * Copyright (C) 2001-2005, 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 
     30 LocaleKeyFactory::LocaleKeyFactory(int32_t coverage)
     31   : _name()
     32   , _coverage(coverage)
     33 {
     34 }
     35 
     36 LocaleKeyFactory::LocaleKeyFactory(int32_t coverage, const UnicodeString& name)
     37   : _name(name)
     38   , _coverage(coverage)
     39 {
     40 }
     41 
     42 LocaleKeyFactory::~LocaleKeyFactory() {
     43 }
     44 
     45 UObject*
     46 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
     59 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
     70 LocaleKeyFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const {
     71     const Hashtable* supported = getSupportedIDs(status);
     72     if (supported) {
     73         UBool visible = (_coverage & 0x1) == 0;
     74 
     75         const UHashElement* elem = NULL;
     76         int32_t pos = 0;
     77         while ((elem = supported->nextElement(pos)) != NULL) {
     78             const UnicodeString& id = *((const UnicodeString*)elem->key.pointer);
     79             if (!visible) {
     80                 result.remove(id);
     81             } else {
     82                 result.put(id, (void*)this, status); // this is dummy non-void marker used for set semantics
     83                 if (U_FAILURE(status)) {
     84                     break;
     85                 }
     86             }
     87         }
     88     }
     89 }
     90 
     91 UnicodeString&
     92 LocaleKeyFactory::getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const {
     93     if ((_coverage & 0x1) == 0) {
     94         //UErrorCode status = U_ZERO_ERROR;
     95         // assume if this is called on us, we support some fallback of this id
     96         // if (isSupportedID(id, status)) {
     97             Locale loc;
     98             LocaleUtility::initLocaleFromName(id, loc);
     99             return loc.getDisplayName(locale, result);
    100         // }
    101     }
    102     result.setToBogus();
    103     return result;
    104 }
    105 
    106 UObject*
    107 LocaleKeyFactory::handleCreate(const Locale& /* loc */,
    108                    int32_t /* kind */,
    109                    const ICUService* /* service */,
    110                    UErrorCode& /* status */) const {
    111     return NULL;
    112 }
    113 
    114 //UBool
    115 //LocaleKeyFactory::isSupportedID(const UnicodeString& id, UErrorCode& status) const {
    116 //    const Hashtable* ids = getSupportedIDs(status);
    117 //    return ids && ids->get(id);
    118 //}
    119 
    120 const Hashtable*
    121 LocaleKeyFactory::getSupportedIDs(UErrorCode& /* status */) const {
    122     return NULL;
    123 }
    124 
    125 #ifdef SERVICE_DEBUG
    126 UnicodeString&
    127 LocaleKeyFactory::debug(UnicodeString& result) const
    128 {
    129     debugClass(result);
    130     result.append(", name: ");
    131     result.append(_name);
    132     result.append(", coverage: ");
    133     result.append(_coverage);
    134     return result;
    135 }
    136 
    137 UnicodeString&
    138 LocaleKeyFactory::debugClass(UnicodeString& result) const
    139 {
    140   return result.append("LocaleKeyFactory");
    141 }
    142 #endif
    143 
    144 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKeyFactory)
    145 
    146 U_NAMESPACE_END
    147 
    148 /* !UCONFIG_NO_SERVICE */
    149 #endif
    150 
    151 
    152