sl@0: /** sl@0: ******************************************************************************* sl@0: * Copyright (C) 2001-2005, International Business Machines Corporation and * sl@0: * others. All Rights Reserved. * sl@0: ******************************************************************************* sl@0: * sl@0: ******************************************************************************* sl@0: */ sl@0: #ifndef ICULSERV_H sl@0: #define ICULSERV_H sl@0: sl@0: #include "unicode/utypes.h" sl@0: sl@0: #if UCONFIG_NO_SERVICE sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: /* sl@0: * Allow the declaration of APIs with pointers to ICUService sl@0: * even when service is removed from the build. sl@0: */ sl@0: class ICULocaleService; sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: #else sl@0: sl@0: #include "unicode/unistr.h" sl@0: #include "unicode/locid.h" sl@0: #include "unicode/strenum.h" sl@0: sl@0: #include "hash.h" sl@0: #include "uvector.h" sl@0: sl@0: #include "serv.h" sl@0: #include "locutil.h" sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: class ICULocaleService; sl@0: sl@0: class LocaleKey; sl@0: class LocaleKeyFactory; sl@0: class SimpleLocaleKeyFactory; sl@0: class ServiceListener; sl@0: sl@0: /* sl@0: ****************************************************************** sl@0: */ sl@0: sl@0: /** sl@0: * A subclass of Key that implements a locale fallback mechanism. sl@0: * The first locale to search for is the locale provided by the sl@0: * client, and the fallback locale to search for is the current sl@0: * default locale. If a prefix is present, the currentDescriptor sl@0: * includes it before the locale proper, separated by "/". This sl@0: * is the default key instantiated by ICULocaleService.
sl@0: * sl@0: *Canonicalization adjusts the locale string so that the sl@0: * section before the first understore is in lower case, and the rest sl@0: * is in upper case, with no trailing underscores.
sl@0: */ sl@0: sl@0: class U_COMMON_API LocaleKey : public ICUServiceKey { sl@0: private: sl@0: int32_t _kind; sl@0: UnicodeString _primaryID; sl@0: UnicodeString _fallbackID; sl@0: UnicodeString _currentID; sl@0: sl@0: public: sl@0: enum { sl@0: KIND_ANY = -1 sl@0: }; sl@0: sl@0: /** sl@0: * Create a LocaleKey with canonical primary and fallback IDs. sl@0: */ sl@0: static LocaleKey* createWithCanonicalFallback(const UnicodeString* primaryID, sl@0: const UnicodeString* canonicalFallbackID, sl@0: UErrorCode& status); sl@0: sl@0: /** sl@0: * Create a LocaleKey with canonical primary and fallback IDs. sl@0: */ sl@0: static LocaleKey* createWithCanonicalFallback(const UnicodeString* primaryID, sl@0: const UnicodeString* canonicalFallbackID, sl@0: int32_t kind, sl@0: UErrorCode& status); sl@0: sl@0: protected: sl@0: /** sl@0: * PrimaryID is the user's requested locale string, sl@0: * canonicalPrimaryID is this string in canonical form, sl@0: * fallbackID is the current default locale's string in sl@0: * canonical form. sl@0: */ sl@0: LocaleKey(const UnicodeString& primaryID, sl@0: const UnicodeString& canonicalPrimaryID, sl@0: const UnicodeString* canonicalFallbackID, sl@0: int32_t kind); sl@0: sl@0: public: sl@0: /** sl@0: * Append the prefix associated with the kind, or nothing if the kind is KIND_ANY. sl@0: */ sl@0: virtual UnicodeString& prefix(UnicodeString& result) const; sl@0: sl@0: /** sl@0: * Return the kind code associated with this key. sl@0: */ sl@0: virtual int32_t kind() const; sl@0: sl@0: /** sl@0: * Return the canonicalID. sl@0: */ sl@0: virtual UnicodeString& canonicalID(UnicodeString& result) const; sl@0: sl@0: /** sl@0: * Return the currentID. sl@0: */ sl@0: virtual UnicodeString& currentID(UnicodeString& result) const; sl@0: sl@0: /** sl@0: * Return the (canonical) current descriptor, or null if no current id. sl@0: */ sl@0: virtual UnicodeString& currentDescriptor(UnicodeString& result) const; sl@0: sl@0: /** sl@0: * Convenience method to return the locale corresponding to the (canonical) original ID. sl@0: */ sl@0: virtual Locale& canonicalLocale(Locale& result) const; sl@0: sl@0: /** sl@0: * Convenience method to return the locale corresponding to the (canonical) current ID. sl@0: */ sl@0: virtual Locale& currentLocale(Locale& result) const; sl@0: sl@0: /** sl@0: * If the key has a fallback, modify the key and return true, sl@0: * otherwise return false. sl@0: * sl@0: *First falls back through the primary ID, then through sl@0: * the fallbackID. The final fallback is the empty string, sl@0: * unless the primary id was the empty string, in which case sl@0: * there is no fallback. sl@0: */ sl@0: virtual UBool fallback(); sl@0: sl@0: /** sl@0: * Return true if a key created from id matches, or would eventually sl@0: * fallback to match, the canonical ID of this key. sl@0: */ sl@0: virtual UBool isFallbackOf(const UnicodeString& id) const; sl@0: sl@0: public: sl@0: /** sl@0: * UObject boilerplate. sl@0: */ sl@0: static UClassID U_EXPORT2 getStaticClassID(); sl@0: sl@0: virtual UClassID getDynamicClassID() const; sl@0: sl@0: /** sl@0: * Destructor. sl@0: */ sl@0: virtual ~LocaleKey(); sl@0: sl@0: #ifdef SERVICE_DEBUG sl@0: public: sl@0: virtual UnicodeString& debug(UnicodeString& result) const; sl@0: virtual UnicodeString& debugClass(UnicodeString& result) const; sl@0: #endif sl@0: sl@0: }; sl@0: sl@0: /* sl@0: ****************************************************************** sl@0: */ sl@0: sl@0: /** sl@0: * A subclass of ICUServiceFactory that uses LocaleKeys, and is able to sl@0: * 'cover' more specific locales with more general locales that it sl@0: * supports. sl@0: * sl@0: *
Coverage may be either of the values VISIBLE or INVISIBLE. sl@0: * sl@0: *
'Visible' indicates that the specific locale(s) supported by sl@0: * the factory are registered in getSupportedIDs, 'Invisible' sl@0: * indicates that they are not. sl@0: * sl@0: *
Localization of visible ids is handled sl@0: * by the handling factory, regardless of kind. sl@0: */ sl@0: class U_COMMON_API LocaleKeyFactory : public ICUServiceFactory { sl@0: protected: sl@0: const UnicodeString _name; sl@0: const int32_t _coverage; sl@0: sl@0: public: sl@0: enum { sl@0: /** sl@0: * Coverage value indicating that the factory makes sl@0: * its locales visible, and does not cover more specific sl@0: * locales. sl@0: */ sl@0: VISIBLE = 0, sl@0: sl@0: /** sl@0: * Coverage value indicating that the factory does not make sl@0: * its locales visible, and does not cover more specific sl@0: * locales. sl@0: */ sl@0: INVISIBLE = 1 sl@0: }; sl@0: sl@0: /** sl@0: * Destructor. sl@0: */ sl@0: virtual ~LocaleKeyFactory(); sl@0: sl@0: protected: sl@0: /** sl@0: * Constructor used by subclasses. sl@0: */ sl@0: LocaleKeyFactory(int32_t coverage); sl@0: sl@0: /** sl@0: * Constructor used by subclasses. sl@0: */ sl@0: LocaleKeyFactory(int32_t coverage, const UnicodeString& name); sl@0: sl@0: /** sl@0: * Implement superclass abstract method. This checks the currentID of sl@0: * the key against the supported IDs, and passes the canonicalLocale and sl@0: * kind off to handleCreate (which subclasses must implement). sl@0: */ sl@0: public: sl@0: virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const; sl@0: sl@0: protected: sl@0: virtual UBool handlesKey(const ICUServiceKey& key, UErrorCode& status) const; sl@0: sl@0: public: sl@0: /** sl@0: * Override of superclass method. This adjusts the result based sl@0: * on the coverage rule for this factory. sl@0: */ sl@0: virtual void updateVisibleIDs(Hashtable& result, UErrorCode& status) const; sl@0: sl@0: /** sl@0: * Return a localized name for the locale represented by id. sl@0: */ sl@0: virtual UnicodeString& getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const; sl@0: sl@0: protected: sl@0: /** sl@0: * Utility method used by create(ICUServiceKey, ICUService). Subclasses can implement sl@0: * this instead of create. The default returns NULL. sl@0: */ sl@0: virtual UObject* handleCreate(const Locale& loc, int32_t kind, const ICUService* service, UErrorCode& status) const; sl@0: sl@0: /** sl@0: * Return true if this id is one the factory supports (visible or sl@0: * otherwise). sl@0: */ sl@0: // virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const; sl@0: sl@0: /** sl@0: * Return the set of ids that this factory supports (visible or sl@0: * otherwise). This can be called often and might need to be sl@0: * cached if it is expensive to create. sl@0: */ sl@0: virtual const Hashtable* getSupportedIDs(UErrorCode& status) const; sl@0: sl@0: public: sl@0: /** sl@0: * UObject boilerplate. sl@0: */ sl@0: static UClassID U_EXPORT2 getStaticClassID(); sl@0: sl@0: virtual UClassID getDynamicClassID() const; sl@0: sl@0: #ifdef SERVICE_DEBUG sl@0: public: sl@0: virtual UnicodeString& debug(UnicodeString& result) const; sl@0: virtual UnicodeString& debugClass(UnicodeString& result) const; sl@0: #endif sl@0: sl@0: }; sl@0: sl@0: /* sl@0: ****************************************************************** sl@0: */ sl@0: sl@0: /** sl@0: * A LocaleKeyFactory that just returns a single object for a kind/locale. sl@0: */ sl@0: sl@0: class U_COMMON_API SimpleLocaleKeyFactory : public LocaleKeyFactory { sl@0: private: sl@0: UObject* _obj; sl@0: UnicodeString _id; sl@0: const int32_t _kind; sl@0: sl@0: public: sl@0: SimpleLocaleKeyFactory(UObject* objToAdopt, sl@0: const UnicodeString& locale, sl@0: int32_t kind, sl@0: int32_t coverage); sl@0: sl@0: SimpleLocaleKeyFactory(UObject* objToAdopt, sl@0: const Locale& locale, sl@0: int32_t kind, sl@0: int32_t coverage); sl@0: sl@0: /** sl@0: * Destructor. sl@0: */ sl@0: virtual ~SimpleLocaleKeyFactory(); sl@0: sl@0: /** sl@0: * Override of superclass method. Returns the service object if kind/locale match. Service is not used. sl@0: */ sl@0: virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const; sl@0: sl@0: /** sl@0: * Override of superclass method. This adjusts the result based sl@0: * on the coverage rule for this factory. sl@0: */ sl@0: virtual void updateVisibleIDs(Hashtable& result, UErrorCode& status) const; sl@0: sl@0: protected: sl@0: /** sl@0: * Return true if this id is equal to the locale name. sl@0: */ sl@0: //virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const; sl@0: sl@0: sl@0: public: sl@0: /** sl@0: * UObject boilerplate. sl@0: */ sl@0: static UClassID U_EXPORT2 getStaticClassID(); sl@0: sl@0: virtual UClassID getDynamicClassID() const; sl@0: sl@0: #ifdef SERVICE_DEBUG sl@0: public: sl@0: virtual UnicodeString& debug(UnicodeString& result) const; sl@0: virtual UnicodeString& debugClass(UnicodeString& result) const; sl@0: #endif sl@0: sl@0: }; sl@0: sl@0: /* sl@0: ****************************************************************** sl@0: */ sl@0: sl@0: /** sl@0: * A LocaleKeyFactory that creates a service based on the ICU locale data. sl@0: * This is a base class for most ICU factories. Subclasses instantiate it sl@0: * with a constructor that takes a bundle name, which determines the supported sl@0: * IDs. Subclasses then override handleCreate to create the actual service sl@0: * object. The default implementation returns a resource bundle. sl@0: */ sl@0: class U_COMMON_API ICUResourceBundleFactory : public LocaleKeyFactory sl@0: { sl@0: protected: sl@0: UnicodeString _bundleName; sl@0: sl@0: public: sl@0: /** sl@0: * Convenience constructor that uses the main ICU bundle name. sl@0: */ sl@0: ICUResourceBundleFactory(); sl@0: sl@0: /** sl@0: * A service factory based on ICU resource data in resources with sl@0: * the given name. This should be a 'path' that can be passed to sl@0: * ures_openAvailableLocales, such as U_ICUDATA or U_ICUDATA_COLL. sl@0: * The empty string is equivalent to U_ICUDATA. sl@0: */ sl@0: ICUResourceBundleFactory(const UnicodeString& bundleName); sl@0: sl@0: /** sl@0: * Destructor sl@0: */ sl@0: virtual ~ICUResourceBundleFactory(); sl@0: sl@0: protected: sl@0: /** sl@0: * Return the supported IDs. This is the set of all locale names in ICULocaleData. sl@0: */ sl@0: virtual const Hashtable* getSupportedIDs(UErrorCode& status) const; sl@0: sl@0: /** sl@0: * Create the service. The default implementation returns the resource bundle sl@0: * for the locale, ignoring kind, and service. sl@0: */ sl@0: virtual UObject* handleCreate(const Locale& loc, int32_t kind, const ICUService* service, UErrorCode& status) const; sl@0: sl@0: public: sl@0: /** sl@0: * UObject boilerplate. sl@0: */ sl@0: static UClassID U_EXPORT2 getStaticClassID(); sl@0: virtual UClassID getDynamicClassID() const; sl@0: sl@0: sl@0: #ifdef SERVICE_DEBUG sl@0: public: sl@0: virtual UnicodeString& debug(UnicodeString& result) const; sl@0: virtual UnicodeString& debugClass(UnicodeString& result) const; sl@0: #endif sl@0: sl@0: }; sl@0: sl@0: /* sl@0: ****************************************************************** sl@0: */ sl@0: sl@0: class U_COMMON_API ICULocaleService : public ICUService sl@0: { sl@0: private: sl@0: Locale fallbackLocale; sl@0: UnicodeString fallbackLocaleName; sl@0: UMTX llock; sl@0: sl@0: public: sl@0: /** sl@0: * Construct an ICULocaleService. sl@0: */ sl@0: ICULocaleService(); sl@0: sl@0: /** sl@0: * Construct an ICULocaleService with a name (useful for debugging). sl@0: */ sl@0: ICULocaleService(const UnicodeString& name); sl@0: sl@0: /** sl@0: * Destructor. sl@0: */ sl@0: virtual ~ICULocaleService(); sl@0: sl@0: #if 0 sl@0: // redeclare because of overload resolution rules? sl@0: // no, causes ambiguities since both UnicodeString and Locale have constructors that take a const char* sl@0: // need some compiler flag to remove warnings sl@0: UObject* get(const UnicodeString& descriptor, UErrorCode& status) const { sl@0: return ICUService::get(descriptor, status); sl@0: } sl@0: sl@0: UObject* get(const UnicodeString& descriptor, UnicodeString* actualReturn, UErrorCode& status) const { sl@0: return ICUService::get(descriptor, actualReturn, status); sl@0: } sl@0: #endif sl@0: sl@0: /** sl@0: * Convenience override for callers using locales. This calls sl@0: * get(Locale, int, Locale[]) with KIND_ANY for kind and null for sl@0: * actualReturn. sl@0: */ sl@0: UObject* get(const Locale& locale, UErrorCode& status) const; sl@0: sl@0: /** sl@0: * Convenience override for callers using locales. This calls sl@0: * get(Locale, int, Locale[]) with a null actualReturn. sl@0: */ sl@0: UObject* get(const Locale& locale, int32_t kind, UErrorCode& status) const; sl@0: sl@0: /** sl@0: * Convenience override for callers using locales. This calls sl@0: * get(Locale, String, Locale[]) with a null kind. sl@0: */ sl@0: UObject* get(const Locale& locale, Locale* actualReturn, UErrorCode& status) const; sl@0: sl@0: /** sl@0: * Convenience override for callers using locales. This uses sl@0: * createKey(Locale.toString(), kind) to create a key, calls getKey, and then sl@0: * if actualReturn is not null, returns the actualResult from sl@0: * getKey (stripping any prefix) into a Locale. sl@0: */ sl@0: UObject* get(const Locale& locale, int32_t kind, Locale* actualReturn, UErrorCode& status) const; sl@0: sl@0: /** sl@0: * Convenience override for callers using locales. This calls sl@0: * registerObject(Object, Locale, int32_t kind, int coverage) sl@0: * passing KIND_ANY for the kind, and VISIBLE for the coverage. sl@0: */ sl@0: virtual URegistryKey registerInstance(UObject* objToAdopt, const Locale& locale, UErrorCode& status); sl@0: sl@0: /** sl@0: * Convenience function for callers using locales. This calls sl@0: * registerObject(Object, Locale, int kind, int coverage) sl@0: * passing VISIBLE for the coverage. sl@0: */ sl@0: virtual URegistryKey registerInstance(UObject* objToAdopt, const Locale& locale, int32_t kind, UErrorCode& status); sl@0: sl@0: /** sl@0: * Convenience function for callers using locales. This instantiates sl@0: * a SimpleLocaleKeyFactory, and registers the factory. sl@0: */ sl@0: virtual URegistryKey registerInstance(UObject* objToAdopt, const Locale& locale, int32_t kind, int32_t coverage, UErrorCode& status); sl@0: sl@0: sl@0: /** sl@0: * (Stop compiler from complaining about hidden overrides.) sl@0: * Since both UnicodeString and Locale have constructors that take const char*, adding a public sl@0: * method that takes UnicodeString causes ambiguity at call sites that use const char*. sl@0: * We really need a flag that is understood by all compilers that will suppress the warning about sl@0: * hidden overrides. sl@0: */ sl@0: virtual URegistryKey registerInstance(UObject* objToAdopt, const UnicodeString& locale, UBool visible, UErrorCode& status); sl@0: sl@0: /** sl@0: * Convenience method for callers using locales. This returns the standard sl@0: * service ID enumeration. sl@0: */ sl@0: virtual StringEnumeration* getAvailableLocales(void) const; sl@0: sl@0: protected: sl@0: sl@0: /** sl@0: * Return the name of the current fallback locale. If it has changed since this was sl@0: * last accessed, the service cache is cleared. sl@0: */ sl@0: const UnicodeString& validateFallbackLocale() const; sl@0: sl@0: /** sl@0: * Override superclass createKey method. sl@0: */ sl@0: virtual ICUServiceKey* createKey(const UnicodeString* id, UErrorCode& status) const; sl@0: sl@0: /** sl@0: * Additional createKey that takes a kind. sl@0: */ sl@0: virtual ICUServiceKey* createKey(const UnicodeString* id, int32_t kind, UErrorCode& status) const; sl@0: sl@0: friend class ServiceEnumeration; sl@0: }; sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: /* UCONFIG_NO_SERVICE */ sl@0: #endif sl@0: sl@0: /* ICULSERV_H */ sl@0: #endif sl@0: