ICU-4726 Made Transliterator::filteredTransliterate() public, added Transliterator::registerAlias().

X-SVN-Rev: 18460
This commit is contained in:
Richard Gillam 2005-08-25 17:23:51 +00:00
parent 40a589829e
commit 98be212f0a
4 changed files with 130 additions and 9 deletions

View file

@ -1240,6 +1240,20 @@ void Transliterator::_registerInstance(Transliterator* adoptedPrototype) {
registry->put(adoptedPrototype, TRUE);
}
void U_EXPORT2 Transliterator::registerAlias(const UnicodeString& aliasID,
const UnicodeString& realID) {
umtx_init(&registryMutex);
Mutex lock(&registryMutex);
if (HAVE_REGISTRY) {
_registerAlias(aliasID, realID);
}
}
void Transliterator::_registerAlias(const UnicodeString& aliasID,
const UnicodeString& realID) {
registry->put(aliasID, realID, TRUE);
}
/**
* Unregisters a transliterator or class. This may be either
* a system transliterator or a user transliterator or class.

View file

@ -606,8 +606,8 @@ protected:
* [<code>pos.start</code>, <code>pos.limit</code>) without
* applying the filter. End user code should call <code>
* transliterate()</code> instead of this method. Subclass code
* should call <code>filteredTransliterate()</code> instead of
* this method.<p>
* and wrapping transliterators should call
* <code>filteredTransliterate()</code> instead of this method.<p>
*
* @param text the buffer holding transliterated and
* untransliterated text
@ -628,7 +628,8 @@ protected:
UTransPosition& pos,
UBool incremental) const = 0;
/**
public:
/**
* Transliterate a substring of text, as specified by index, taking filters
* into account. This method is for subclasses that need to delegate to
* another transliterator, such as CompoundTransliterator.
@ -643,9 +644,6 @@ protected:
UTransPosition& index,
UBool incremental) const;
friend class CompoundTransliterator; // for filteredTransliterate()
friend class AnyTransliterator; // for filteredTransliterate()
private:
/**
@ -982,7 +980,7 @@ public:
Token context);
/**
* Registers a instance <tt>obj</tt> of a subclass of
* Registers an instance <tt>obj</tt> of a subclass of
* <code>Transliterator</code> with the system. When
* <tt>createInstance()</tt> is called with an ID string that is
* equal to <tt>obj->getID()</tt>, then <tt>obj->clone()</tt> is
@ -1000,6 +998,22 @@ public:
*/
static void U_EXPORT2 registerInstance(Transliterator* adoptedObj);
/**
* Registers an ID string as an alias of another ID string.
* That is, after calling this function, <tt>createInstance(aliasID)</tt>
* will return the same thing as <tt>createInstance(realID)</tt>.
* This is generally used to create shorter, more mnemonic aliases
* for long compound IDs.
*
* @param aliasID The new ID being registered.
* @param realID The ID that the new ID is to be an alias for.
* This can be a compound ID and can include filters and should
* refer to transliterators that have already been registered with
* the framework, although this isn't checked.
*/
static void U_EXPORT2 registerAlias(const UnicodeString& aliasID,
const UnicodeString& realID);
protected:
/**
@ -1020,6 +1034,11 @@ protected:
*/
static void _registerInstance(Transliterator* adoptedObj);
/**
* @internal
*/
static void _registerAlias(const UnicodeString& aliasID, const UnicodeString& realID);
/**
* Register two targets as being inverses of one another. For
* example, calling registerSpecialInverse("NFC", "NFD", true) causes

View file

@ -185,6 +185,7 @@ TransliteratorTest::runIndexedTest(int32_t index, UBool exec,
TESTCASE(77,TestAlternateSyntax);
TESTCASE(78,TestBeginEnd);
TESTCASE(79,TestBeginEndToRules);
TESTCASE(80,TestRegisterAlias);
default: name = ""; break;
}
}
@ -4315,6 +4316,82 @@ void TransliteratorTest::TestBeginEndToRules() {
}
}
void TransliteratorTest::TestRegisterAlias() {
UnicodeString longID("Lower;[aeiou]Upper");
UnicodeString shortID("Any-CapVowels");
UnicodeString reallyShortID("CapVowels");
Transliterator::registerAlias(shortID, longID);
UErrorCode err = U_ZERO_ERROR;
Transliterator* t1 = Transliterator::createInstance(longID, UTRANS_FORWARD, err);
if (U_FAILURE(err)) {
errln("Failed to instantiate transliterator with long ID");
Transliterator::unregister(shortID);
return;
}
Transliterator* t2 = Transliterator::createInstance(reallyShortID, UTRANS_FORWARD, err);
if (U_FAILURE(err)) {
errln("Failed to instantiate transliterator with short ID");
delete t1;
Transliterator::unregister(shortID);
return;
}
if (t1->getID() != longID)
errln("Transliterator instantiated with long ID doesn't have long ID");
if (t2->getID() != reallyShortID)
errln("Transliterator instantiated with short ID doesn't have short ID");
UnicodeString rules1;
UnicodeString rules2;
t1->toRules(rules1, TRUE);
t2->toRules(rules2, TRUE);
if (rules1 != rules2)
errln("Alias transliterators aren't the same");
delete t1;
delete t2;
Transliterator::unregister(shortID);
t1 = Transliterator::createInstance(shortID, UTRANS_FORWARD, err);
if (U_SUCCESS(err)) {
errln("Instantiation with short ID succeeded after short ID was unregistered");
delete t1;
}
// try the same thing again, but this time with something other than
// an instance of CompoundTransliterator
UnicodeString realID("Latin-Greek");
UnicodeString fakeID("Latin-dlgkjdflkjdl");
Transliterator::registerAlias(fakeID, realID);
err = U_ZERO_ERROR;
t1 = Transliterator::createInstance(realID, UTRANS_FORWARD, err);
if (U_FAILURE(err)) {
errln("Failed to instantiate transliterator with real ID");
Transliterator::unregister(realID);
return;
}
t2 = Transliterator::createInstance(fakeID, UTRANS_FORWARD, err);
if (U_FAILURE(err)) {
errln("Failed to instantiate transliterator with fake ID");
delete t1;
Transliterator::unregister(realID);
return;
}
t1->toRules(rules1, TRUE);
t2->toRules(rules2, TRUE);
if (rules1 != rules2)
errln("Alias transliterators aren't the same");
delete t1;
delete t2;
Transliterator::unregister(fakeID);
}
//======================================================================
// Support methods
//======================================================================

View file

@ -344,9 +344,20 @@ private:
void TestAlternateSyntax(void);
void TestBeginEnd(void);
/**
* Tests the multiple-pass syntax
*/
void TestBeginEnd(void);
void TestBeginEndToRules(void);
/**
* Tests that toRules() works right with the multiple-pass syntax
*/
void TestBeginEndToRules(void);
/**
* Tests the registerAlias() function
*/
void TestRegisterAlias(void);
//======================================================================
// Support methods