1999-11-20 00:40:50 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (C) 1999, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 11/17/99 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/translit.h"
|
1999-11-20 00:40:50 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/hextouni.h"
|
|
|
|
#include "unicode/locid.h"
|
|
|
|
#include "unicode/msgfmt.h"
|
1999-11-20 00:40:50 +00:00
|
|
|
#include "mutex.h"
|
|
|
|
#include "rbt_data.h"
|
|
|
|
#include "rbt_pars.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/rep.h"
|
|
|
|
#include "unicode/resbund.h"
|
2000-03-28 22:12:14 +00:00
|
|
|
#include "hash.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/unifilt.h"
|
|
|
|
#include "unicode/unitohex.h"
|
2000-01-11 17:28:49 +00:00
|
|
|
#include "unicode/nultrans.h"
|
2000-01-11 19:48:48 +00:00
|
|
|
#include "unicode/putil.h"
|
2000-01-14 21:15:25 +00:00
|
|
|
#include "unicode/cpdtrans.h"
|
2000-01-18 01:55:52 +00:00
|
|
|
#include "unicode/jamohang.h"
|
|
|
|
#include "unicode/hangjamo.h"
|
2000-03-28 22:12:14 +00:00
|
|
|
#include <stdio.h>
|
2000-03-04 02:08:11 +00:00
|
|
|
const UChar Transliterator::ID_SEP = 0x002D; /*-*/
|
|
|
|
const UChar Transliterator::ID_DELIM = 0x003B; /*;*/
|
|
|
|
|
2000-12-18 22:13:29 +00:00
|
|
|
static Hashtable _cache;
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
|
|
|
* Dictionary of known transliterators. Keys are <code>String</code>
|
|
|
|
* names, values are one of the following:
|
|
|
|
*
|
|
|
|
* <ul><li><code>Transliterator</code> objects
|
|
|
|
*
|
|
|
|
* <li><code>RULE_BASED_PLACEHOLDER</code>, in which case the ID
|
2000-03-04 02:08:11 +00:00
|
|
|
* will have its first ID_SEP removed and be appended to
|
1999-11-20 00:40:50 +00:00
|
|
|
* RB_RULE_BASED_PREFIX to form a resource bundle name from which
|
|
|
|
* the RB_RULE key is looked up to obtain the rule.
|
|
|
|
*
|
|
|
|
* <li><code>REVERSE_RULE_BASED_PLACEHOLDER</code>. Like
|
|
|
|
* <code>RULE_BASED_PLACEHOLDER</code>, except the entity names in
|
|
|
|
* the ID are reversed, and the argument
|
1999-11-22 19:31:05 +00:00
|
|
|
* RuleBasedTransliterator::REVERSE is pased to the
|
1999-11-20 00:40:50 +00:00
|
|
|
* RuleBasedTransliterator constructor.
|
|
|
|
* </ul>
|
|
|
|
*/
|
2000-12-18 22:13:29 +00:00
|
|
|
Hashtable* Transliterator::cache = &_cache;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The mutex controlling access to the cache.
|
|
|
|
*/
|
|
|
|
UMTX Transliterator::cacheMutex = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When set to TRUE, the cache has been initialized. Any code must
|
|
|
|
* check this boolean before accessing the cache, and if the boolean
|
|
|
|
* is FALSE, it must call initializeCache(). We do this form of lazy
|
|
|
|
* evaluation for two reasons: (1) so we don't initialize if we don't
|
|
|
|
* have to (i.e., if no one is using Transliterator, but has included
|
|
|
|
* the code as part of a shared library, and (2) to avoid static
|
|
|
|
* intialization problems.
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool Transliterator::cacheInitialized = FALSE;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefix for resource bundle key for the display name for a
|
|
|
|
* transliterator. The ID is appended to this to form the key.
|
|
|
|
* The resource bundle value should be a String.
|
|
|
|
*/
|
2000-01-11 19:39:28 +00:00
|
|
|
const char* Transliterator::RB_DISPLAY_NAME_PREFIX = "%Translit%%";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefix for resource bundle key for the display name for a
|
|
|
|
* transliterator SCRIPT. The ID is appended to this to form the key.
|
|
|
|
* The resource bundle value should be a String.
|
|
|
|
*/
|
|
|
|
const char* Transliterator::RB_SCRIPT_DISPLAY_NAME_PREFIX = "%Translit%";
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resource bundle key for display name pattern.
|
|
|
|
* The resource bundle value should be a String forming a
|
|
|
|
* MessageFormat pattern, e.g.:
|
|
|
|
* "{0,choice,0#|1#{1} Transliterator|2#{1} to {2} Transliterator}".
|
|
|
|
*/
|
|
|
|
const char* Transliterator::RB_DISPLAY_NAME_PATTERN =
|
|
|
|
"TransliteratorNamePattern";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resource bundle key for the list of RuleBasedTransliterator IDs.
|
|
|
|
* The resource bundle value should be a String[] with each element
|
|
|
|
* being a valid ID. The ID will be appended to RB_RULE_BASED_PREFIX
|
|
|
|
* to obtain the class name in which the RB_RULE key will be sought.
|
|
|
|
*/
|
|
|
|
const char* Transliterator::RB_RULE_BASED_IDS =
|
|
|
|
"RuleBasedTransliteratorIDs";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resource bundle key for the RuleBasedTransliterator rule.
|
|
|
|
*/
|
|
|
|
const char* Transliterator::RB_RULE = "Rule";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor.
|
|
|
|
* @param theID the string identifier for this transliterator
|
|
|
|
* @param theFilter the filter. Any character for which
|
2001-02-28 22:05:16 +00:00
|
|
|
* <tt>filter.contains()</tt> returns <tt>FALSE</tt> will not be
|
1999-11-20 00:40:50 +00:00
|
|
|
* altered by this transliterator. If <tt>filter</tt> is
|
|
|
|
* <tt>null</tt> then no filtering is applied.
|
|
|
|
*/
|
|
|
|
Transliterator::Transliterator(const UnicodeString& theID,
|
|
|
|
UnicodeFilter* adoptedFilter) :
|
2000-01-18 18:27:27 +00:00
|
|
|
ID(theID), filter(adoptedFilter),
|
|
|
|
maximumContextLength(0) {}
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
Transliterator::~Transliterator() {
|
|
|
|
delete filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
Transliterator::Transliterator(const Transliterator& other) :
|
2000-01-18 18:27:27 +00:00
|
|
|
ID(other.ID), filter(0),
|
|
|
|
maximumContextLength(other.maximumContextLength) {
|
1999-11-20 00:40:50 +00:00
|
|
|
if (other.filter != 0) {
|
|
|
|
// We own the filter, so we must have our own copy
|
|
|
|
filter = other.filter->clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
Transliterator& Transliterator::operator=(const Transliterator& other) {
|
|
|
|
ID = other.ID;
|
2000-01-18 18:27:27 +00:00
|
|
|
maximumContextLength = other.maximumContextLength;
|
2000-06-30 23:26:07 +00:00
|
|
|
// MUST go through adoptFilter in case latter is overridden
|
|
|
|
adoptFilter((other.filter == 0) ? 0 : other.filter->clone());
|
1999-11-20 00:40:50 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-01-18 18:27:27 +00:00
|
|
|
/**
|
|
|
|
* Transliterates a segment of a string. <code>Transliterator</code> API.
|
|
|
|
* @param text the string to be transliterated
|
|
|
|
* @param start the beginning index, inclusive; <code>0 <= start
|
|
|
|
* <= limit</code>.
|
|
|
|
* @param limit the ending index, exclusive; <code>start <= limit
|
|
|
|
* <= text.length()</code>.
|
|
|
|
* @return the new limit index
|
|
|
|
*/
|
|
|
|
int32_t Transliterator::transliterate(Replaceable& text,
|
|
|
|
int32_t start, int32_t limit) const {
|
2000-01-21 03:42:27 +00:00
|
|
|
|
2000-08-11 22:54:31 +00:00
|
|
|
UTransPosition offsets;
|
|
|
|
offsets.contextStart= start;
|
|
|
|
offsets.contextLimit = limit;
|
|
|
|
offsets.start = start;
|
|
|
|
offsets.limit = limit;
|
2000-01-19 19:02:10 +00:00
|
|
|
handleTransliterate(text, offsets, FALSE);
|
2000-07-01 00:13:42 +00:00
|
|
|
return offsets.limit;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterates an entire string in place. Convenience method.
|
|
|
|
* @param text the string to be transliterated
|
|
|
|
*/
|
|
|
|
void Transliterator::transliterate(Replaceable& text) const {
|
|
|
|
transliterate(text, 0, text.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterates the portion of the text buffer that can be
|
|
|
|
* transliterated unambiguosly after new text has been inserted,
|
|
|
|
* typically as a result of a keyboard event. The new text in
|
|
|
|
* <code>insertion</code> will be inserted into <code>text</code>
|
2000-06-27 20:06:52 +00:00
|
|
|
* at <code>index.contextLimit</code>, advancing
|
|
|
|
* <code>index.contextLimit</code> by <code>insertion.length()</code>.
|
1999-11-20 00:40:50 +00:00
|
|
|
* Then the transliterator will try to transliterate characters of
|
2000-06-27 20:06:52 +00:00
|
|
|
* <code>text</code> between <code>index.start</code> and
|
|
|
|
* <code>index.contextLimit</code>. Characters before
|
|
|
|
* <code>index.start</code> will not be changed.
|
1999-11-20 00:40:50 +00:00
|
|
|
*
|
2000-01-19 19:02:10 +00:00
|
|
|
* <p>Upon return, values in <code>index</code> will be updated.
|
2000-06-27 20:06:52 +00:00
|
|
|
* <code>index.contextStart</code> will be advanced to the first
|
1999-11-20 00:40:50 +00:00
|
|
|
* character that future calls to this method will read.
|
2000-06-27 20:06:52 +00:00
|
|
|
* <code>index.start</code> and <code>index.contextLimit</code> will
|
1999-11-20 00:40:50 +00:00
|
|
|
* be adjusted to delimit the range of text that future calls to
|
|
|
|
* this method may change.
|
|
|
|
*
|
|
|
|
* <p>Typical usage of this method begins with an initial call
|
2000-06-27 20:06:52 +00:00
|
|
|
* with <code>index.contextStart</code> and <code>index.contextLimit</code>
|
1999-11-20 00:40:50 +00:00
|
|
|
* set to indicate the portion of <code>text</code> to be
|
2000-06-27 20:06:52 +00:00
|
|
|
* transliterated, and <code>index.start == index.contextStart</code>.
|
2000-01-19 19:02:10 +00:00
|
|
|
* Thereafter, <code>index</code> can be used without
|
1999-11-20 00:40:50 +00:00
|
|
|
* modification in future calls, provided that all changes to
|
|
|
|
* <code>text</code> are made via this method.
|
|
|
|
*
|
|
|
|
* <p>This method assumes that future calls may be made that will
|
|
|
|
* insert new text into the buffer. As a result, it only performs
|
|
|
|
* unambiguous transliterations. After the last call to this
|
|
|
|
* method, there may be untransliterated text that is waiting for
|
|
|
|
* more input to resolve an ambiguity. In order to perform these
|
|
|
|
* pending transliterations, clients should call {@link
|
|
|
|
* #finishKeyboardTransliteration} after the last call to this
|
|
|
|
* method has been made.
|
|
|
|
*
|
|
|
|
* @param text the buffer holding transliterated and untransliterated text
|
|
|
|
* @param index an array of three integers.
|
|
|
|
*
|
2000-06-27 20:06:52 +00:00
|
|
|
* <ul><li><code>index.contextStart</code>: the beginning index,
|
|
|
|
* inclusive; <code>0 <= index.contextStart <= index.contextLimit</code>.
|
1999-11-20 00:40:50 +00:00
|
|
|
*
|
2000-06-27 20:06:52 +00:00
|
|
|
* <li><code>index.contextLimit</code>: the ending index, exclusive;
|
|
|
|
* <code>index.contextStart <= index.contextLimit <= text.length()</code>.
|
1999-11-20 00:40:50 +00:00
|
|
|
* <code>insertion</code> is inserted at
|
2000-06-27 20:06:52 +00:00
|
|
|
* <code>index.contextLimit</code>.
|
1999-11-20 00:40:50 +00:00
|
|
|
*
|
2000-06-27 20:06:52 +00:00
|
|
|
* <li><code>index.start</code>: the next character to be
|
|
|
|
* considered for transliteration; <code>index.contextStart <=
|
|
|
|
* index.start <= index.contextLimit</code>. Characters before
|
|
|
|
* <code>index.start</code> will not be changed by future calls
|
1999-11-20 00:40:50 +00:00
|
|
|
* to this method.</ul>
|
|
|
|
*
|
|
|
|
* @param insertion text to be inserted and possibly
|
|
|
|
* transliterated into the translation buffer at
|
2000-06-27 20:06:52 +00:00
|
|
|
* <code>index.contextLimit</code>. If <code>null</code> then no text
|
1999-11-20 00:40:50 +00:00
|
|
|
* is inserted.
|
|
|
|
* @see #START
|
|
|
|
* @see #LIMIT
|
|
|
|
* @see #CURSOR
|
2000-01-18 18:27:27 +00:00
|
|
|
* @see #handleTransliterate
|
2000-01-19 19:02:10 +00:00
|
|
|
* @exception IllegalArgumentException if <code>index</code>
|
1999-11-20 00:40:50 +00:00
|
|
|
* is invalid
|
|
|
|
*/
|
2000-01-18 18:27:27 +00:00
|
|
|
void Transliterator::transliterate(Replaceable& text,
|
2000-06-27 19:00:38 +00:00
|
|
|
UTransPosition& index,
|
2000-01-18 18:27:27 +00:00
|
|
|
const UnicodeString& insertion,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
_transliterate(text, index, &insertion, status);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterates the portion of the text buffer that can be
|
|
|
|
* transliterated unambiguosly after a new character has been
|
|
|
|
* inserted, typically as a result of a keyboard event. This is a
|
|
|
|
* convenience method; see {@link
|
2000-01-18 18:27:27 +00:00
|
|
|
* #transliterate(Replaceable, int[], String)} for details.
|
1999-11-20 00:40:50 +00:00
|
|
|
* @param text the buffer holding transliterated and
|
|
|
|
* untransliterated text
|
|
|
|
* @param index an array of three integers. See {@link
|
2000-01-18 18:27:27 +00:00
|
|
|
* #transliterate(Replaceable, int[], String)}.
|
1999-11-20 00:40:50 +00:00
|
|
|
* @param insertion text to be inserted and possibly
|
|
|
|
* transliterated into the translation buffer at
|
2000-06-27 20:06:52 +00:00
|
|
|
* <code>index.contextLimit</code>.
|
2000-01-18 18:27:27 +00:00
|
|
|
* @see #transliterate(Replaceable, int[], String)
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-01-18 18:27:27 +00:00
|
|
|
void Transliterator::transliterate(Replaceable& text,
|
2000-06-27 19:00:38 +00:00
|
|
|
UTransPosition& index,
|
2000-01-18 18:27:27 +00:00
|
|
|
UChar insertion,
|
|
|
|
UErrorCode& status) const {
|
1999-11-20 00:40:50 +00:00
|
|
|
UnicodeString str(insertion);
|
2000-01-18 18:27:27 +00:00
|
|
|
_transliterate(text, index, &str, status);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterates the portion of the text buffer that can be
|
|
|
|
* transliterated unambiguosly. This is a convenience method; see
|
2000-01-18 18:27:27 +00:00
|
|
|
* {@link #transliterate(Replaceable, int[], String)} for
|
1999-11-20 00:40:50 +00:00
|
|
|
* details.
|
|
|
|
* @param text the buffer holding transliterated and
|
|
|
|
* untransliterated text
|
|
|
|
* @param index an array of three integers. See {@link
|
2000-01-18 18:27:27 +00:00
|
|
|
* #transliterate(Replaceable, int[], String)}.
|
|
|
|
* @see #transliterate(Replaceable, int[], String)
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-01-18 18:27:27 +00:00
|
|
|
void Transliterator::transliterate(Replaceable& text,
|
2000-06-27 19:00:38 +00:00
|
|
|
UTransPosition& index,
|
2000-01-18 18:27:27 +00:00
|
|
|
UErrorCode& status) const {
|
|
|
|
_transliterate(text, index, 0, status);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finishes any pending transliterations that were waiting for
|
|
|
|
* more characters. Clients should call this method as the last
|
|
|
|
* call after a sequence of one or more calls to
|
2000-01-18 18:27:27 +00:00
|
|
|
* <code>transliterate()</code>.
|
1999-11-20 00:40:50 +00:00
|
|
|
* @param text the buffer holding transliterated and
|
|
|
|
* untransliterated text.
|
|
|
|
* @param index the array of indices previously passed to {@link
|
2000-01-18 18:27:27 +00:00
|
|
|
* #transliterate}
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-01-18 18:27:27 +00:00
|
|
|
void Transliterator::finishTransliteration(Replaceable& text,
|
2000-06-27 19:00:38 +00:00
|
|
|
UTransPosition& index) const {
|
2001-02-28 22:05:16 +00:00
|
|
|
handleTransliterate(text, index, FALSE);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This internal method does keyboard transliteration. If the
|
|
|
|
* 'insertion' is non-null then we append it to 'text' before
|
|
|
|
* proceeding. This method calls through to the pure virtual
|
2000-01-18 18:27:27 +00:00
|
|
|
* framework method handleTransliterate() to do the actual
|
1999-11-20 00:40:50 +00:00
|
|
|
* work.
|
|
|
|
*/
|
2000-01-18 18:27:27 +00:00
|
|
|
void Transliterator::_transliterate(Replaceable& text,
|
2000-06-27 19:00:38 +00:00
|
|
|
UTransPosition& index,
|
2000-01-18 18:27:27 +00:00
|
|
|
const UnicodeString* insertion,
|
|
|
|
UErrorCode &status) const {
|
1999-11-20 00:40:50 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-06-27 20:06:52 +00:00
|
|
|
if (index.contextStart < 0 ||
|
|
|
|
index.contextLimit > text.length() ||
|
|
|
|
index.start < index.contextStart ||
|
|
|
|
index.start > index.contextLimit) {
|
1999-11-20 00:40:50 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-06-27 20:06:52 +00:00
|
|
|
int32_t originalStart = index.contextStart;
|
1999-11-20 00:40:50 +00:00
|
|
|
if (insertion != 0) {
|
2000-01-19 19:02:10 +00:00
|
|
|
text.handleReplaceBetween(index.limit, index.limit, *insertion);
|
|
|
|
index.limit += insertion->length();
|
2000-06-27 20:06:52 +00:00
|
|
|
index.contextLimit += insertion->length();
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
2000-01-19 19:02:10 +00:00
|
|
|
handleTransliterate(text, index, TRUE);
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2000-06-27 20:06:52 +00:00
|
|
|
index.contextStart = uprv_max(index.start - getMaximumContextLength(),
|
1999-11-20 00:40:50 +00:00
|
|
|
originalStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2000-01-18 18:27:27 +00:00
|
|
|
* Method for subclasses to use to set the maximum context length.
|
|
|
|
* @see #getMaximumContextLength
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2000-01-18 18:27:27 +00:00
|
|
|
void Transliterator::setMaximumContextLength(int32_t maxContextLength) {
|
|
|
|
maximumContextLength = maxContextLength;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a programmatic identifier for this transliterator.
|
|
|
|
* If this identifier is passed to <code>getInstance()</code>, it
|
|
|
|
* will return this object, if it has been registered.
|
|
|
|
* @see #registerInstance
|
|
|
|
* @see #getAvailableIDs
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
const UnicodeString& Transliterator::getID(void) const {
|
1999-11-20 00:40:50 +00:00
|
|
|
return ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a name for this transliterator that is appropriate for
|
|
|
|
* display to the user in the default locale. See {@link
|
|
|
|
* #getDisplayName(Locale)} for details.
|
|
|
|
*/
|
2000-01-11 19:39:28 +00:00
|
|
|
UnicodeString& Transliterator::getDisplayName(const UnicodeString& ID,
|
|
|
|
UnicodeString& result) {
|
|
|
|
return getDisplayName(ID, Locale::getDefault(), result);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a name for this transliterator that is appropriate for
|
|
|
|
* display to the user in the given locale. This name is taken
|
|
|
|
* from the locale resource data in the standard manner of the
|
|
|
|
* <code>java.text</code> package.
|
|
|
|
*
|
|
|
|
* <p>If no localized names exist in the system resource bundles,
|
|
|
|
* a name is synthesized using a localized
|
|
|
|
* <code>MessageFormat</code> pattern from the resource data. The
|
|
|
|
* arguments to this pattern are an integer followed by one or two
|
|
|
|
* strings. The integer is the number of strings, either 1 or 2.
|
|
|
|
* The strings are formed by splitting the ID for this
|
2000-03-04 02:08:11 +00:00
|
|
|
* transliterator at the first ID_SEP. If there is no ID_SEP, then the
|
1999-11-20 00:40:50 +00:00
|
|
|
* entire ID forms the only string.
|
|
|
|
* @param inLocale the Locale in which the display name should be
|
|
|
|
* localized.
|
|
|
|
* @see java.text.MessageFormat
|
|
|
|
*/
|
2000-01-11 19:39:28 +00:00
|
|
|
UnicodeString& Transliterator::getDisplayName(const UnicodeString& ID,
|
|
|
|
const Locale& inLocale,
|
|
|
|
UnicodeString& result) {
|
1999-11-20 00:40:50 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2000-05-15 18:39:17 +00:00
|
|
|
|
2000-04-15 21:23:28 +00:00
|
|
|
ResourceBundle bundle(u_getDataDirectory(), inLocale, status);
|
2000-05-15 18:39:17 +00:00
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
// Suspend checking status until later...
|
|
|
|
|
1999-12-01 21:27:44 +00:00
|
|
|
// build the char* key
|
|
|
|
char key[100];
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_strcpy(key, RB_DISPLAY_NAME_PREFIX);
|
|
|
|
int32_t length=uprv_strlen(RB_DISPLAY_NAME_PREFIX);
|
2000-01-11 19:39:28 +00:00
|
|
|
key[length + ID.extract(0, sizeof(key)-length-1, key+length, "")]=0;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
// Try to retrieve a UnicodeString* from the bundle. The result,
|
|
|
|
// if any, should NOT be deleted.
|
2000-05-15 18:39:17 +00:00
|
|
|
/*const UnicodeString* resString = bundle.getString(key, status);*/
|
|
|
|
UnicodeString resString = bundle.getStringEx(key, status);
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2000-05-15 18:39:17 +00:00
|
|
|
/*if (U_SUCCESS(status) && resString != 0) {*/
|
|
|
|
if (U_SUCCESS(status) && resString.length() != 0) {
|
|
|
|
/*return result = *resString; // [sic] assign & return*/
|
|
|
|
return result = resString; // [sic] assign & return
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have failed to get a name from the locale data. This is
|
|
|
|
// typical, since most transliterators will not have localized
|
|
|
|
// name data. The next step is to retrieve the MessageFormat
|
|
|
|
// pattern from the locale data and to use it to synthesize the
|
|
|
|
// name from the ID.
|
|
|
|
|
|
|
|
status = U_ZERO_ERROR;
|
2000-05-15 18:39:17 +00:00
|
|
|
/*resString = bundle.getString(RB_DISPLAY_NAME_PATTERN, status);*/
|
|
|
|
resString = bundle.getStringEx(RB_DISPLAY_NAME_PATTERN, status);
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2000-05-15 18:39:17 +00:00
|
|
|
/*if (U_SUCCESS(status) && resString != 0) {*/
|
|
|
|
if (U_SUCCESS(status) && resString.length() != 0) {
|
|
|
|
/*MessageFormat msg(*resString, inLocale, status);*/
|
|
|
|
MessageFormat msg(resString, inLocale, status);
|
1999-11-20 00:40:50 +00:00
|
|
|
// Suspend checking status until later...
|
|
|
|
|
|
|
|
// We pass either 2 or 3 Formattable objects to msg.
|
|
|
|
Formattable args[3];
|
2000-03-04 02:08:11 +00:00
|
|
|
int32_t i = ID.indexOf(ID_SEP);
|
1999-11-20 00:40:50 +00:00
|
|
|
int32_t nargs;
|
|
|
|
if (i < 0) {
|
|
|
|
args[0].setLong(1); // # of args to follow
|
|
|
|
args[1].setString(ID);
|
|
|
|
nargs = 2;
|
|
|
|
} else {
|
|
|
|
UnicodeString left, right;
|
|
|
|
ID.extractBetween(0, i, left);
|
|
|
|
ID.extractBetween(i+1, ID.length(), right);
|
|
|
|
args[0].setLong(2); // # of args to follow
|
|
|
|
args[1].setString(left);
|
|
|
|
args[2].setString(right);
|
|
|
|
nargs = 3;
|
|
|
|
}
|
2000-01-11 19:39:28 +00:00
|
|
|
|
|
|
|
// Use display names for the scripts, if they exist
|
|
|
|
UnicodeString s;
|
|
|
|
for (int j=1; j<=((i<0)?1:2); ++j) {
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
uprv_strcpy(key, RB_SCRIPT_DISPLAY_NAME_PREFIX);
|
|
|
|
length=uprv_strlen(RB_SCRIPT_DISPLAY_NAME_PREFIX);
|
|
|
|
args[j].getString(s);
|
|
|
|
key[length + s.extract(0, sizeof(key)-length-1, key+length, "")]=0;
|
|
|
|
|
2000-05-15 18:39:17 +00:00
|
|
|
/*resString = bundle.getString(key, status);*/
|
|
|
|
resString = bundle.getStringEx(key, status);
|
2000-01-11 19:39:28 +00:00
|
|
|
|
|
|
|
if (U_SUCCESS(status)) {
|
2000-05-15 18:39:17 +00:00
|
|
|
/*args[j] = *resString;*/
|
|
|
|
args[j] = resString;
|
2000-01-11 19:39:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status = U_ZERO_ERROR;
|
1999-11-20 00:40:50 +00:00
|
|
|
FieldPosition pos; // ignored by msg
|
|
|
|
msg.format(args, nargs, result, pos, status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should not reach this point unless there is something
|
|
|
|
// wrong with the build or the RB_DISPLAY_NAME_PATTERN has
|
|
|
|
// been deleted from the root RB_LOCALE_ELEMENTS resource.
|
|
|
|
result = ID;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the filter used by this transliterator, or <tt>null</tt>
|
|
|
|
* if this transliterator uses no filter. Caller musn't delete
|
|
|
|
* the result!
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
const UnicodeFilter* Transliterator::getFilter(void) const {
|
1999-11-20 00:40:50 +00:00
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
|
2000-02-05 00:24:24 +00:00
|
|
|
/**
|
|
|
|
* Returns the filter used by this transliterator, or
|
|
|
|
* <tt>NULL</tt> if this transliterator uses no filter. The
|
|
|
|
* caller must eventually delete the result. After this call,
|
|
|
|
* this transliterator's filter is set to <tt>NULL</tt>.
|
|
|
|
*/
|
|
|
|
UnicodeFilter* Transliterator::orphanFilter(void) {
|
|
|
|
UnicodeFilter *result = filter;
|
|
|
|
// MUST go through adoptFilter in case latter is overridden
|
|
|
|
adoptFilter(0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
|
|
|
* Changes the filter used by this transliterator. If the filter
|
|
|
|
* is set to <tt>null</tt> then no filtering will occur.
|
|
|
|
*
|
|
|
|
* <p>Callers must take care if a transliterator is in use by
|
|
|
|
* multiple threads. The filter should not be changed by one
|
|
|
|
* thread while another thread may be transliterating.
|
|
|
|
*/
|
|
|
|
void Transliterator::adoptFilter(UnicodeFilter* filterToAdopt) {
|
|
|
|
delete filter;
|
|
|
|
filter = filterToAdopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this transliterator's inverse. See the class
|
|
|
|
* documentation for details. This implementation simply inverts
|
|
|
|
* the two entities in the ID and attempts to retrieve the
|
|
|
|
* resulting transliterator. That is, if <code>getID()</code>
|
|
|
|
* returns "A-B", then this method will return the result of
|
|
|
|
* <code>getInstance("B-A")</code>, or <code>null</code> if that
|
|
|
|
* call fails.
|
|
|
|
*
|
|
|
|
* <p>This method does not take filtering into account. The
|
|
|
|
* returned transliterator will have no filter.
|
|
|
|
*
|
|
|
|
* <p>Subclasses with knowledge of their inverse may wish to
|
|
|
|
* override this method.
|
|
|
|
*
|
|
|
|
* @return a transliterator that is an inverse, not necessarily
|
|
|
|
* exact, of this transliterator, or <code>null</code> if no such
|
|
|
|
* transliterator is registered.
|
|
|
|
* @see #registerInstance
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
Transliterator* Transliterator::createInverse(void) const {
|
2000-06-27 19:00:38 +00:00
|
|
|
return Transliterator::createInstance(ID, UTRANS_REVERSE);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a <code>Transliterator</code> object given its ID.
|
|
|
|
* The ID must be either a system transliterator ID or a ID registered
|
|
|
|
* using <code>registerInstance()</code>.
|
|
|
|
*
|
|
|
|
* @param ID a valid ID, as enumerated by <code>getAvailableIDs()</code>
|
|
|
|
* @return A <code>Transliterator</code> object with the given ID
|
|
|
|
* @see #registerInstance
|
|
|
|
* @see #getAvailableIDs
|
|
|
|
* @see #getID
|
|
|
|
*/
|
2000-01-14 21:15:25 +00:00
|
|
|
Transliterator* Transliterator::createInstance(const UnicodeString& ID,
|
2000-06-27 19:00:38 +00:00
|
|
|
UTransDirection dir,
|
|
|
|
UParseError* parseError) {
|
2000-01-14 21:15:25 +00:00
|
|
|
Transliterator* t = 0;
|
2000-05-20 04:35:31 +00:00
|
|
|
if (ID.indexOf(ID_DELIM) >= 0) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
t = new CompoundTransliterator(ID, dir, 0, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete t;
|
|
|
|
t = 0;
|
2000-01-14 21:15:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2000-06-27 19:00:38 +00:00
|
|
|
if (dir == UTRANS_REVERSE) {
|
2000-05-20 04:35:31 +00:00
|
|
|
int32_t i = ID.indexOf(ID_SEP);
|
|
|
|
if (i >= 0) {
|
|
|
|
UnicodeString inverseID, right;
|
|
|
|
ID.extractBetween(i+1, ID.length(), inverseID);
|
|
|
|
ID.extractBetween(0, i, right);
|
|
|
|
inverseID.append(ID_SEP).append(right);
|
|
|
|
t = _createInstance(inverseID, parseError);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t = _createInstance(ID, parseError);
|
|
|
|
}
|
2000-01-14 21:15:25 +00:00
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a transliterator object given its ID. Unlike getInstance(),
|
|
|
|
* this method returns null if it cannot make use of the given ID.
|
|
|
|
*/
|
2000-03-18 01:42:45 +00:00
|
|
|
Transliterator* Transliterator::_createInstance(const UnicodeString& ID,
|
2000-06-27 19:00:38 +00:00
|
|
|
UParseError* parseError) {
|
1999-11-20 00:40:50 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
if (!cacheInitialized) {
|
|
|
|
initializeCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex lock(&cacheMutex);
|
|
|
|
|
2000-03-28 22:12:14 +00:00
|
|
|
CacheEntry* entry = (CacheEntry*) cache->get(ID);
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
TransliterationRuleData* data = 0;
|
|
|
|
|
|
|
|
if (entry == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->entryType == CacheEntry::RBT_DATA) {
|
|
|
|
data = entry->u.data;
|
|
|
|
// Fall through to construct transliterator from cached Data object.
|
|
|
|
} else if (entry->entryType == CacheEntry::PROTOTYPE) {
|
|
|
|
return entry->u.prototype->clone();
|
|
|
|
} else {
|
|
|
|
// At this point entry type must be either RULE_BASED_PLACEHOLDER
|
|
|
|
// or REVERSE_RULE_BASED_PLACEHOLDER.
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool isReverse =
|
1999-11-20 00:40:50 +00:00
|
|
|
(entry->entryType ==
|
|
|
|
CacheEntry::REVERSE_RULE_BASED_PLACEHOLDER);
|
|
|
|
|
|
|
|
// We use the file name, taken from another resource bundle
|
|
|
|
// 2-d array at static init time, as a locale language. We're
|
|
|
|
// just using the locale mechanism to map through to a file
|
|
|
|
// name; this in no way represents an actual locale.
|
2000-05-15 18:39:17 +00:00
|
|
|
|
2000-04-15 21:23:28 +00:00
|
|
|
char *ch;
|
|
|
|
ch = new char[entry->rbFile.size() + 1];
|
|
|
|
ch[entry->rbFile.extract(0, 0x7fffffff, ch, "")] = 0;
|
|
|
|
Locale fakeLocale(ch);
|
|
|
|
delete [] ch;
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2000-06-30 20:31:39 +00:00
|
|
|
ResourceBundle bundle((char *)0,
|
1999-11-20 00:40:50 +00:00
|
|
|
fakeLocale, status);
|
|
|
|
|
|
|
|
// Call RBT to parse the rules from the resource bundle
|
|
|
|
|
2000-05-15 18:39:17 +00:00
|
|
|
UnicodeString rules = bundle.getStringEx(RB_RULE, status);
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2000-06-30 23:26:07 +00:00
|
|
|
// If the status indicates a failure, then we don't have any
|
|
|
|
// rules -- there is probably an installation error. The list
|
|
|
|
// in the root locale should correspond to all the installed
|
|
|
|
// transliterators; if it lists something that's not
|
|
|
|
// installed, we'll get an error from ResourceBundle.
|
|
|
|
if (U_SUCCESS(status)) {
|
2000-05-15 18:39:17 +00:00
|
|
|
|
|
|
|
data = TransliterationRuleParser::parse(rules, isReverse
|
2000-06-27 19:00:38 +00:00
|
|
|
? UTRANS_REVERSE
|
|
|
|
: UTRANS_FORWARD,
|
2000-03-18 01:42:45 +00:00
|
|
|
parseError);
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
// Double check to see if someone has modified the entry
|
|
|
|
// since we last looked at it.
|
|
|
|
if (entry->entryType != CacheEntry::RBT_DATA) {
|
|
|
|
entry->entryType = CacheEntry::RBT_DATA;
|
|
|
|
entry->u.data = data;
|
|
|
|
} else {
|
|
|
|
// Oops! Another thread has updated this cache entry
|
|
|
|
// already to point to a data object. Discard the
|
|
|
|
// one we just created and use the one in the cache
|
|
|
|
// instead.
|
|
|
|
delete data;
|
|
|
|
data = entry->u.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data != 0) {
|
|
|
|
return new RuleBasedTransliterator(ID, data);
|
|
|
|
} else {
|
|
|
|
// We have a failure of some kind. Remove the ID from the
|
|
|
|
// cache so we don't keep trying. NOTE: This will throw off
|
|
|
|
// anyone who is, at the moment, trying to iterate over the
|
|
|
|
// available IDs. That's acceptable since we should never
|
|
|
|
// really get here except under installation, configuration,
|
|
|
|
// or unrecoverable run time memory failures.
|
|
|
|
_unregister(ID);
|
|
|
|
}
|
2000-01-21 03:42:27 +00:00
|
|
|
|
|
|
|
return 0;
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a instance <tt>obj</tt> of a subclass of
|
|
|
|
* <code>Transliterator</code> with the system. This object must
|
|
|
|
* implement the <tt>clone()</tt> method. When
|
|
|
|
* <tt>getInstance()</tt> is called with an ID string that is
|
|
|
|
* equal to <tt>obj.getID()</tt>, then <tt>obj.clone()</tt> is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* @param obj an instance of subclass of
|
|
|
|
* <code>Transliterator</code> that defines <tt>clone()</tt>
|
|
|
|
* @see #getInstance
|
|
|
|
* @see #unregister
|
|
|
|
*/
|
|
|
|
void Transliterator::registerInstance(Transliterator* adoptedPrototype,
|
|
|
|
UErrorCode &status) {
|
|
|
|
if (!cacheInitialized) {
|
|
|
|
initializeCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex lock(&cacheMutex);
|
|
|
|
_registerInstance(adoptedPrototype, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This internal method registers a prototype instance in the cache.
|
|
|
|
* The CALLER MUST MUTEX using cacheMutex before calling this method.
|
|
|
|
*/
|
|
|
|
void Transliterator::_registerInstance(Transliterator* adoptedPrototype,
|
|
|
|
UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-03-28 22:12:14 +00:00
|
|
|
/*int32_t hashCode = hash(adoptedPrototype->getID());*/
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
// This needs explaining: The string reference that getID returns
|
|
|
|
// is to the ID data member of Transliterator. As long as the
|
|
|
|
// Transliterator object exists, this reference is valid, and in
|
|
|
|
// fact we can take its address and store it in IDS. No problem
|
|
|
|
// there. The only thing we have to be sure of is that before we
|
|
|
|
// remove the prototype (via unregister()), we remove the ID
|
|
|
|
// entry.
|
|
|
|
cacheIDs.addElement((void*) &adoptedPrototype->getID());
|
|
|
|
|
2000-03-28 22:12:14 +00:00
|
|
|
CacheEntry* entry = (CacheEntry*) cache->get(adoptedPrototype->getID());
|
1999-11-20 00:40:50 +00:00
|
|
|
if (entry == 0) {
|
|
|
|
entry = new CacheEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->adoptPrototype(adoptedPrototype);
|
|
|
|
|
2000-03-28 22:12:14 +00:00
|
|
|
//uhash_putKey(cache, hashCode, entry, &status);
|
|
|
|
cache->put(adoptedPrototype->getID(), entry, status);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregisters a transliterator or class. This may be either
|
|
|
|
* a system transliterator or a user transliterator or class.
|
|
|
|
*
|
|
|
|
* @param ID the ID of the transliterator or class
|
|
|
|
* @see #registerInstance
|
|
|
|
*/
|
|
|
|
void Transliterator::unregister(const UnicodeString& ID) {
|
|
|
|
if (!cacheInitialized) {
|
|
|
|
initializeCache();
|
|
|
|
}
|
|
|
|
Mutex lock(&cacheMutex);
|
|
|
|
_unregister(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregisters a transliterator or class. Internal method.
|
|
|
|
* Prerequisites: The cache must be initialized, and the
|
|
|
|
* caller must own the cacheMutex.
|
|
|
|
*/
|
|
|
|
void Transliterator::_unregister(const UnicodeString& ID) {
|
|
|
|
cacheIDs.removeElement((void*) &ID);
|
2000-11-29 00:21:38 +00:00
|
|
|
//int32_t hc = hash(ID);
|
2000-03-28 22:12:14 +00:00
|
|
|
CacheEntry* entry = (CacheEntry*) cache->get(ID);
|
2000-11-29 00:21:38 +00:00
|
|
|
if (entry != 0) {
|
|
|
|
cache->remove(ID);
|
|
|
|
delete entry;
|
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Vector of registered IDs.
|
|
|
|
*/
|
|
|
|
UVector Transliterator::cacheIDs;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of IDs currently registered with the system.
|
|
|
|
* To retrieve the actual IDs, call getAvailableID(i) with
|
|
|
|
* i from 0 to countAvailableIDs() - 1.
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
int32_t Transliterator::countAvailableIDs(void) {
|
1999-11-20 00:40:50 +00:00
|
|
|
if (!cacheInitialized) {
|
|
|
|
initializeCache();
|
|
|
|
}
|
|
|
|
Mutex lock(&cacheMutex);
|
|
|
|
return cacheIDs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the index-th available ID. index must be between 0
|
|
|
|
* and countAvailableIDs() - 1, inclusive. If index is out of
|
|
|
|
* range, the result of getAvailableID(0) is returned.
|
|
|
|
*/
|
|
|
|
const UnicodeString& Transliterator::getAvailableID(int32_t index) {
|
|
|
|
if (index < 0 || index >= cacheIDs.size()) {
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
if (!cacheInitialized) {
|
|
|
|
initializeCache();
|
|
|
|
}
|
|
|
|
Mutex lock(&cacheMutex);
|
|
|
|
return *(const UnicodeString*) cacheIDs[index];
|
|
|
|
}
|
|
|
|
|
2000-01-18 18:27:27 +00:00
|
|
|
/**
|
|
|
|
* Method for subclasses to use to obtain a character in the given
|
|
|
|
* string, with filtering.
|
|
|
|
*/
|
|
|
|
UChar Transliterator::filteredCharAt(const Replaceable& text, int32_t i) const {
|
|
|
|
UChar c;
|
2000-11-29 00:21:38 +00:00
|
|
|
const UnicodeFilter* localFilter = getFilter();
|
|
|
|
return (localFilter == 0) ? text.charAt(i) :
|
|
|
|
(localFilter->contains(c = text.charAt(i)) ? c : (UChar)0xFFFE);
|
2000-01-18 18:27:27 +00:00
|
|
|
}
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
|
|
|
* Comparison function for UVector. Compares two UnicodeString
|
|
|
|
* objects given void* pointers to them.
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool Transliterator::compareIDs(void* a, void* b) {
|
1999-11-20 00:40:50 +00:00
|
|
|
const UnicodeString* aa = (const UnicodeString*) a;
|
|
|
|
const UnicodeString* bb = (const UnicodeString*) b;
|
|
|
|
return *aa == *bb;
|
|
|
|
}
|
|
|
|
|
1999-12-22 22:57:04 +00:00
|
|
|
void Transliterator::initializeCache(void) {
|
1999-11-20 00:40:50 +00:00
|
|
|
// Lock first, check init boolean second
|
|
|
|
Mutex lock(&cacheMutex);
|
|
|
|
if (cacheInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
// Before looking for the resource, construct our cache.
|
|
|
|
// That way if the resource is absent, we will at least
|
|
|
|
// have a valid cache object.
|
2000-12-18 22:13:29 +00:00
|
|
|
// cache = new Hashtable(status); // TODO: What if this call fails?
|
1999-11-20 00:40:50 +00:00
|
|
|
cacheIDs.setComparer(compareIDs);
|
|
|
|
|
2000-01-13 14:11:06 +00:00
|
|
|
/* The following code parses the index table located in
|
|
|
|
* icu/data/translit/index.txt. The index is an n x 3 table
|
1999-11-20 00:40:50 +00:00
|
|
|
* that looks like this:
|
|
|
|
*
|
|
|
|
* RuleBasedTransliteratorIDs {
|
|
|
|
* { "Latin-Arabic", "Arabic-Latin", "larabic" }
|
|
|
|
* { "KeyboardEscape-Latin1", "", "keyescl1" }
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
2000-05-15 18:39:17 +00:00
|
|
|
Locale indexLoc("translit_index");
|
2000-06-30 20:31:39 +00:00
|
|
|
|
|
|
|
ResourceBundle bundle((char *)0,
|
2000-05-15 18:39:17 +00:00
|
|
|
indexLoc, status);
|
2000-01-13 14:11:06 +00:00
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
int32_t rows, cols;
|
|
|
|
const UnicodeString** ruleBasedIDs =
|
|
|
|
bundle.get2dArray(RB_RULE_BASED_IDS, rows, cols, status);
|
|
|
|
|
|
|
|
if (U_SUCCESS(status) && (cols == 3)) {
|
|
|
|
for (int32_t i=0; i<rows; ++i) {
|
|
|
|
const UnicodeString* row = ruleBasedIDs[i];
|
|
|
|
for (int32_t col=0; col<2; ++col) {
|
|
|
|
|
|
|
|
if (row[col].length() > 0) {
|
|
|
|
CacheEntry* entry = new CacheEntry();
|
|
|
|
entry->entryType = (col == 0) ?
|
|
|
|
CacheEntry::RULE_BASED_PLACEHOLDER :
|
|
|
|
CacheEntry::REVERSE_RULE_BASED_PLACEHOLDER;
|
2000-05-15 18:39:17 +00:00
|
|
|
entry->rbFile = UnicodeString(row[2]);
|
2000-03-28 22:12:14 +00:00
|
|
|
//uhash_putKey(cache, hash(row[col]), entry, &status);
|
|
|
|
cache->put(row[col], entry, status);
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/* It's okay to take the address of the string
|
|
|
|
* from the resource bundle under the assumption
|
|
|
|
* that the RB is caching these, and that they
|
|
|
|
* stay around forever. If this changes, what we
|
|
|
|
* need to do is change the id vector so that it
|
|
|
|
* owns its strings and create a copy here.
|
|
|
|
*/
|
2000-05-15 18:39:17 +00:00
|
|
|
/*cacheIDs.addElement((void*) &row[col]);*/
|
|
|
|
cacheIDs.addElement((void*) new UnicodeString(row[col]));
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manually add prototypes that the system knows about to the
|
|
|
|
// cache. This is how new non-rule-based transliterators are
|
|
|
|
// added to the system.
|
|
|
|
|
|
|
|
status = U_ZERO_ERROR; // Reset status for following calls
|
|
|
|
_registerInstance(new HexToUnicodeTransliterator(), status);
|
|
|
|
_registerInstance(new UnicodeToHexTransliterator(), status);
|
2000-01-18 01:55:52 +00:00
|
|
|
_registerInstance(new JamoHangulTransliterator(), status);
|
|
|
|
_registerInstance(new HangulJamoTransliterator(), status);
|
2000-01-11 17:28:49 +00:00
|
|
|
_registerInstance(new NullTransliterator(), status);
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
cacheInitialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Transliterator::CacheEntry::CacheEntry() {
|
|
|
|
u.prototype = 0;
|
|
|
|
entryType = NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Transliterator::CacheEntry::~CacheEntry() {
|
|
|
|
if (entryType == PROTOTYPE) {
|
|
|
|
delete u.prototype;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transliterator::CacheEntry::adoptPrototype(Transliterator* adopted) {
|
|
|
|
if (entryType == PROTOTYPE) {
|
|
|
|
delete u.prototype;
|
|
|
|
}
|
|
|
|
entryType = PROTOTYPE;
|
|
|
|
u.prototype = adopted;
|
|
|
|
}
|