2 **********************************************************************
3 * Copyright (c) 2001-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 08/10/2001 aliu Creation.
8 **********************************************************************
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_TRANSLITERATION
17 #include "unicode/uobject.h"
18 #include "unicode/translit.h"
28 //------------------------------------------------------------------
29 // TransliteratorAlias
30 //------------------------------------------------------------------
33 * A TransliteratorAlias object is returned by get() if the given ID
34 * actually translates into something else. The caller then invokes
35 * the create() method on the alias to create the actual
36 * transliterator, and deletes the alias.
38 * Why all the shenanigans? To prevent circular calls between
39 * the registry code and the transliterator code that deadlocks.
41 class TransliteratorAlias
: public UMemory
{
44 * Construct a simple alias (type == SIMPLE)
45 * @param aliasID the given id.
47 TransliteratorAlias(const UnicodeString
& aliasID
);
50 * Construct a compound RBT alias (type == COMPOUND)
52 TransliteratorAlias(const UnicodeString
& ID
, const UnicodeString
& idBlock
,
53 Transliterator
* adopted
, int32_t idSplitPoint
,
54 const UnicodeSet
* compoundFilter
);
57 * Construct a rules alias (type = RULES)
59 TransliteratorAlias(const UnicodeString
& theID
,
60 const UnicodeString
& rules
,
63 ~TransliteratorAlias();
66 * The whole point of create() is that the caller must invoke
67 * it when the registry mutex is NOT held, to prevent deadlock.
68 * It may only be called once.
70 * Note: Only call create() if isRuleBased() returns FALSE.
72 * This method must be called *outside* of the TransliteratorRegistry
75 Transliterator
* create(UParseError
&, UErrorCode
&);
78 * Return TRUE if this alias is rule-based. If so, the caller
79 * must call parse() on it, then call TransliteratorRegistry::reget().
81 UBool
isRuleBased() const;
84 * If isRuleBased() returns TRUE, then the caller must call this
85 * method, followed by TransliteratorRegistry::reget(). The latter
86 * method must be called inside the TransliteratorRegistry mutex.
88 * Note: Only call parse() if isRuleBased() returns TRUE.
90 * This method must be called *outside* of the TransliteratorRegistry
91 * mutex, because it can instantiate Transliterators embedded in
92 * the rules via the "&Latin-Arabic()" syntax.
94 void parse(TransliteratorParser
& parser
,
95 UParseError
& pe
, UErrorCode
& ec
) const;
98 // We actually come in three flavors:
100 // Here aliasID is the alias string. Everything else is
101 // null, zero, empty.
103 // Here ID is the ID, aliasID is the idBlock, trans is the
104 // contained RBT, and idSplitPoint is the offet in aliasID
105 // where the contained RBT goes. compoundFilter is the
106 // compound filter, and it is _not_ owned.
108 // Here ID is the ID, aliasID is the rules string.
109 // idSplitPoint is the UTransDirection.
111 UnicodeString aliasID
; // rename! holds rules for RULES type
112 Transliterator
* trans
; // owned
113 const UnicodeSet
* compoundFilter
; // alias
114 int32_t idSplitPoint
; // rename! holds UTransDirection for RULES type
115 enum { SIMPLE
, COMPOUND
, RULES
} type
;
117 TransliteratorAlias(const TransliteratorAlias
&other
); // forbid copying of this class
118 TransliteratorAlias
&operator=(const TransliteratorAlias
&other
); // forbid copying of this class
123 * A registry of system transliterators. This is the data structure
124 * that implements the mapping between transliterator IDs and the data
125 * or function pointers used to create the corresponding
126 * transliterators. There is one instance of the registry that is
127 * created statically.
129 * The registry consists of a dynamic component -- a hashtable -- and
130 * a static component -- locale resource bundles. The dynamic store
131 * is semantically overlaid on the static store, so the static mapping
132 * can be dynamically overridden.
134 * This is an internal class that is only used by Transliterator.
135 * Transliterator maintains one static instance of this class and
136 * delegates all registry-related operations to it.
140 class TransliteratorRegistry
: public UMemory
{
146 * @param status Output param set to success/failure code.
148 TransliteratorRegistry(UErrorCode
& status
);
151 * Nonvirtual destructor -- this class is not subclassable.
153 ~TransliteratorRegistry();
155 //------------------------------------------------------------------
157 //------------------------------------------------------------------
160 * Given a simple ID (forward direction, no inline filter, not
161 * compound) attempt to instantiate it from the registry. Return
164 * Return a non-NULL aliasReturn value if the ID points to an alias.
165 * We cannot instantiate it ourselves because the alias may contain
166 * filters or compounds, which we do not understand. Caller should
167 * make aliasReturn NULL before calling.
168 * @param ID the given ID
169 * @param aliasReturn output param to receive TransliteratorAlias;
170 * should be NULL on entry
171 * @param parseError Struct to recieve information on position
172 * of error if an error is encountered
173 * @param status Output param set to success/failure code.
175 Transliterator
* get(const UnicodeString
& ID
,
176 TransliteratorAlias
*& aliasReturn
,
180 * The caller must call this after calling get(), if [a] calling get()
181 * returns an alias, and [b] the alias is rule based. In that
182 * situation the caller must call alias->parse() to do the parsing
183 * OUTSIDE THE REGISTRY MUTEX, then call this method to retry
184 * instantiating the transliterator.
186 * Note: Another alias might be returned by this method.
188 * This method (like all public methods of this class) must be called
189 * from within the TransliteratorRegistry mutex.
191 * @param aliasReturn output param to receive TransliteratorAlias;
192 * should be NULL on entry
194 Transliterator
* reget(const UnicodeString
& ID
,
195 TransliteratorParser
& parser
,
196 TransliteratorAlias
*& aliasReturn
,
200 * Register a prototype (adopted). This adds an entry to the
201 * dynamic store, or replaces an existing entry. Any entry in the
202 * underlying static locale resource store is masked.
204 void put(Transliterator
* adoptedProto
,
208 * Register an ID and a factory function pointer. This adds an
209 * entry to the dynamic store, or replaces an existing entry. Any
210 * entry in the underlying static locale resource store is masked.
212 void put(const UnicodeString
& ID
,
213 Transliterator::Factory factory
,
214 Transliterator::Token context
,
218 * Register an ID and a resource name. This adds an entry to the
219 * dynamic store, or replaces an existing entry. Any entry in the
220 * underlying static locale resource store is masked.
222 void put(const UnicodeString
& ID
,
223 const UnicodeString
& resourceName
,
228 * Register an ID and an alias ID. This adds an entry to the
229 * dynamic store, or replaces an existing entry. Any entry in the
230 * underlying static locale resource store is masked.
232 void put(const UnicodeString
& ID
,
233 const UnicodeString
& alias
,
237 * Unregister an ID. This removes an entry from the dynamic store
238 * if there is one. The static locale resource store is
240 * @param ID the given ID.
242 void remove(const UnicodeString
& ID
);
244 //------------------------------------------------------------------
245 // Public ID and spec management
246 //------------------------------------------------------------------
249 * Return a StringEnumeration over the IDs currently registered
253 StringEnumeration
* getAvailableIDs() const;
256 * == OBSOLETE - remove in ICU 3.4 ==
257 * Return the number of IDs currently registered with the system.
258 * To retrieve the actual IDs, call getAvailableID(i) with
259 * i from 0 to countAvailableIDs() - 1.
260 * @return the number of IDs currently registered with the system.
263 int32_t countAvailableIDs(void) const;
266 * == OBSOLETE - remove in ICU 3.4 ==
267 * Return the index-th available ID. index must be between 0
268 * and countAvailableIDs() - 1, inclusive. If index is out of
269 * range, the result of getAvailableID(0) is returned.
270 * @param index the given index.
271 * @return the index-th available ID. index must be between 0
272 * and countAvailableIDs() - 1, inclusive. If index is out of
273 * range, the result of getAvailableID(0) is returned.
276 const UnicodeString
& getAvailableID(int32_t index
) const;
279 * Return the number of registered source specifiers.
280 * @return the number of registered source specifiers.
282 int32_t countAvailableSources(void) const;
285 * Return a registered source specifier.
286 * @param index which specifier to return, from 0 to n-1, where
287 * n = countAvailableSources()
288 * @param result fill-in paramter to receive the source specifier.
289 * If index is out of range, result will be empty.
290 * @return reference to result
292 UnicodeString
& getAvailableSource(int32_t index
,
293 UnicodeString
& result
) const;
296 * Return the number of registered target specifiers for a given
298 * @param source the given source specifier.
299 * @return the number of registered target specifiers for a given
302 int32_t countAvailableTargets(const UnicodeString
& source
) const;
305 * Return a registered target specifier for a given source.
306 * @param index which specifier to return, from 0 to n-1, where
307 * n = countAvailableTargets(source)
308 * @param source the source specifier
309 * @param result fill-in paramter to receive the target specifier.
310 * If source is invalid or if index is out of range, result will
312 * @return reference to result
314 UnicodeString
& getAvailableTarget(int32_t index
,
315 const UnicodeString
& source
,
316 UnicodeString
& result
) const;
319 * Return the number of registered variant specifiers for a given
320 * source-target pair. There is always at least one variant: If
321 * just source-target is registered, then the single variant
322 * NO_VARIANT is returned. If source-target/variant is registered
323 * then that variant is returned.
324 * @param source the source specifiers
325 * @param target the target specifiers
326 * @return the number of registered variant specifiers for a given
327 * source-target pair.
329 int32_t countAvailableVariants(const UnicodeString
& source
,
330 const UnicodeString
& target
) const;
333 * Return a registered variant specifier for a given source-target
334 * pair. If NO_VARIANT is one of the variants, then it will be
336 * @param index which specifier to return, from 0 to n-1, where
337 * n = countAvailableVariants(source, target)
338 * @param source the source specifier
339 * @param target the target specifier
340 * @param result fill-in paramter to receive the variant
341 * specifier. If source is invalid or if target is invalid or if
342 * index is out of range, result will be empty.
343 * @return reference to result
345 UnicodeString
& getAvailableVariant(int32_t index
,
346 const UnicodeString
& source
,
347 const UnicodeString
& target
,
348 UnicodeString
& result
) const;
352 //----------------------------------------------------------------
353 // Private implementation
354 //----------------------------------------------------------------
356 Entry
* find(const UnicodeString
& ID
);
358 Entry
* find(UnicodeString
& source
,
359 UnicodeString
& target
,
360 UnicodeString
& variant
);
362 Entry
* findInDynamicStore(const Spec
& src
,
364 const UnicodeString
& variant
) const;
366 Entry
* findInStaticStore(const Spec
& src
,
368 const UnicodeString
& variant
);
370 static Entry
* findInBundle(const Spec
& specToOpen
,
371 const Spec
& specToFind
,
372 const UnicodeString
& variant
,
373 UTransDirection direction
);
375 void registerEntry(const UnicodeString
& source
,
376 const UnicodeString
& target
,
377 const UnicodeString
& variant
,
381 void registerEntry(const UnicodeString
& ID
,
385 void registerEntry(const UnicodeString
& ID
,
386 const UnicodeString
& source
,
387 const UnicodeString
& target
,
388 const UnicodeString
& variant
,
392 void registerSTV(const UnicodeString
& source
,
393 const UnicodeString
& target
,
394 const UnicodeString
& variant
);
396 void removeSTV(const UnicodeString
& source
,
397 const UnicodeString
& target
,
398 const UnicodeString
& variant
);
400 Transliterator
* instantiateEntry(const UnicodeString
& ID
,
402 TransliteratorAlias
*& aliasReturn
,
406 * A StringEnumeration over the registered IDs in this object.
408 class Enumeration
: public StringEnumeration
{
410 Enumeration(const TransliteratorRegistry
& reg
);
411 virtual ~Enumeration();
412 virtual int32_t count(UErrorCode
& status
) const;
413 virtual const UnicodeString
* snext(UErrorCode
& status
);
414 virtual void reset(UErrorCode
& status
);
415 static UClassID U_EXPORT2
getStaticClassID();
416 virtual UClassID
getDynamicClassID() const;
419 const TransliteratorRegistry
& reg
;
421 friend class Enumeration
;
426 * Dynamic registry mapping full IDs to Entry objects. This
427 * contains both public and internal entities. The visibility is
428 * controlled by whether an entry is listed in availableIDs and
434 * DAG of visible IDs by spec. Hashtable: source => (Hashtable:
435 * target => (UVector: variant)) The UVector of variants is never
436 * empty. For a source-target with no variant, the special
437 * variant NO_VARIANT (the empty string) is stored in slot zero of
443 * Vector of public full IDs.
445 UVector availableIDs
;
447 TransliteratorRegistry(const TransliteratorRegistry
&other
); // forbid copying of this class
448 TransliteratorRegistry
&operator=(const TransliteratorRegistry
&other
); // forbid copying of this class
453 #endif /* #if !UCONFIG_NO_TRANSLITERATION */