1999-11-20 00:40:50 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2001-03-21 20:31:13 +00:00
|
|
|
* Copyright (C) 1999-2001, International Business Machines
|
1999-11-20 00:40:50 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 11/17/99 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "unicode/putil.h"
|
|
|
|
#include "unicode/translit.h"
|
2001-03-31 01:21:38 +00:00
|
|
|
#include "unicode/cpdtrans.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "unicode/nultrans.h"
|
2001-03-31 01:21:38 +00:00
|
|
|
#include "unicode/locid.h"
|
|
|
|
#include "unicode/msgfmt.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "unicode/rep.h"
|
|
|
|
#include "unicode/resbund.h"
|
|
|
|
#include "unicode/unifilt.h"
|
|
|
|
#include "unicode/unifltlg.h"
|
|
|
|
#include "unicode/uniset.h"
|
|
|
|
#include "unicode/uscript.h"
|
|
|
|
#include "rbt_data.h"
|
|
|
|
#include "rbt_pars.h"
|
|
|
|
#include "transreg.h"
|
2001-11-16 23:51:15 +00:00
|
|
|
#include "name2uni.h"
|
|
|
|
#include "nortrans.h"
|
|
|
|
#include "remtrans.h"
|
|
|
|
#include "titletrn.h"
|
|
|
|
#include "tolowtrn.h"
|
|
|
|
#include "toupptrn.h"
|
|
|
|
#include "uni2name.h"
|
2001-11-21 07:02:15 +00:00
|
|
|
#include "esctrn.h"
|
|
|
|
#include "unesctrn.h"
|
2002-02-07 01:07:55 +00:00
|
|
|
#include "tridpars.h"
|
2002-06-07 20:24:40 +00:00
|
|
|
#include "anytrans.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "ucln_in.h"
|
|
|
|
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
2001-03-28 23:23:23 +00:00
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
static const UChar TARGET_SEP = 0x002D; /*-*/
|
2001-07-17 23:55:42 +00:00
|
|
|
static const UChar ID_DELIM = 0x003B; /*;*/
|
2001-08-01 17:37:08 +00:00
|
|
|
static const UChar VARIANT_SEP = 0x002F; // '/'
|
2001-11-15 23:40:02 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2001-10-11 23:54:55 +00:00
|
|
|
static const char RB_DISPLAY_NAME_PREFIX[] = "%Translit%%";
|
2000-01-11 19:39:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2001-10-11 23:54:55 +00:00
|
|
|
static const char 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}".
|
|
|
|
*/
|
2001-10-11 23:54:55 +00:00
|
|
|
static const char RB_DISPLAY_NAME_PATTERN[] = "TransliteratorNamePattern";
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2001-10-11 23:54:55 +00:00
|
|
|
static const char RB_RULE_BASED_IDS[] = "RuleBasedTransliteratorIDs";
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
/**
|
2002-02-07 01:07:55 +00:00
|
|
|
* The mutex controlling access to registry object.
|
2001-10-08 23:26:58 +00:00
|
|
|
*/
|
|
|
|
static UMTX registryMutex = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* System transliterator registry; non-null when initialized.
|
|
|
|
*/
|
|
|
|
static TransliteratorRegistry* registry = 0;
|
|
|
|
|
2001-10-25 00:03:50 +00:00
|
|
|
// Empty string
|
|
|
|
static const UChar EMPTY[] = {0}; //""
|
|
|
|
|
2001-10-12 05:54:15 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2001-06-12 17:10:10 +00:00
|
|
|
/**
|
|
|
|
* Class identifier for subclasses of Transliterator that do not
|
|
|
|
* define their class (anonymous subclasses).
|
|
|
|
*/
|
2001-10-11 23:54:55 +00:00
|
|
|
const char Transliterator::fgClassID = 0; // Value is irrelevant
|
2001-06-12 17:10:10 +00:00
|
|
|
|
2001-11-19 20:53:06 +00:00
|
|
|
/**
|
|
|
|
* Return TRUE if the given UTransPosition is valid for text of
|
|
|
|
* the given length.
|
|
|
|
*/
|
|
|
|
inline UBool positionIsValid(UTransPosition& index, int32_t len) {
|
|
|
|
return !(index.contextStart < 0 ||
|
|
|
|
index.start < index.contextStart ||
|
|
|
|
index.limit < index.start ||
|
|
|
|
index.contextLimit < index.limit ||
|
|
|
|
len < index.contextLimit);
|
|
|
|
}
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
|
|
|
* 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) :
|
2002-07-02 23:58:34 +00:00
|
|
|
UObject(), ID(theID), filter(adoptedFilter),
|
2000-01-18 18:27:27 +00:00
|
|
|
maximumContextLength(0) {}
|
1999-11-20 00:40:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
Transliterator::~Transliterator() {
|
|
|
|
delete filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
Transliterator::Transliterator(const Transliterator& other) :
|
2002-07-02 23:58:34 +00:00
|
|
|
UObject(other), ID(other.ID), filter(0),
|
2000-01-18 18:27:27 +00:00
|
|
|
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
|
2001-07-27 00:18:53 +00:00
|
|
|
filter = (UnicodeFilter*) other.filter->clone();
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
Transliterator& Transliterator::operator=(const Transliterator& other) {
|
|
|
|
ID = other.ID;
|
2000-01-18 18:27:27 +00:00
|
|
|
maximumContextLength = other.maximumContextLength;
|
2001-07-27 00:18:53 +00:00
|
|
|
adoptFilter((other.filter == 0) ? 0 : (UnicodeFilter*) 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>.
|
2001-11-19 19:27:51 +00:00
|
|
|
* @return the new limit index, or -1
|
2000-01-18 18:27:27 +00:00
|
|
|
*/
|
|
|
|
int32_t Transliterator::transliterate(Replaceable& text,
|
|
|
|
int32_t start, int32_t limit) const {
|
2001-11-19 19:27:51 +00:00
|
|
|
if (start < 0 ||
|
|
|
|
limit < start ||
|
|
|
|
text.length() < limit) {
|
|
|
|
return -1;
|
|
|
|
}
|
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;
|
2001-11-29 01:01:31 +00:00
|
|
|
filteredTransliterate(text, offsets, FALSE, TRUE);
|
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,
|
2001-07-25 19:11:02 +00:00
|
|
|
UChar32 insertion,
|
2000-01-18 18:27:27 +00:00
|
|
|
UErrorCode& status) const {
|
1999-11-20 00:40:50 +00:00
|
|
|
UnicodeString str(insertion);
|
2001-10-03 16:34:53 +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-11-19 20:53:06 +00:00
|
|
|
if (!positionIsValid(index, text.length())) {
|
2001-11-19 19:27:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-11-29 01:01:31 +00:00
|
|
|
filteredTransliterate(text, index, FALSE, TRUE);
|
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;
|
|
|
|
}
|
|
|
|
|
2001-11-19 20:53:06 +00:00
|
|
|
if (!positionIsValid(index, text.length())) {
|
1999-11-20 00:40:50 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-08-23 01:06:08 +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
|
|
|
}
|
|
|
|
|
2001-10-03 16:34:53 +00:00
|
|
|
if (index.limit > 0 &&
|
|
|
|
UTF_IS_LEAD(text.charAt(index.limit - 1))) {
|
|
|
|
// Oops, there is a dangling lead surrogate in the buffer.
|
|
|
|
// This will break most transliterators, since they will
|
2001-10-24 13:34:11 +00:00
|
|
|
// assume it is part of a pair. Don't transliterate until
|
2001-10-03 16:34:53 +00:00
|
|
|
// more text comes in.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-11-29 01:01:31 +00:00
|
|
|
filteredTransliterate(text, index, TRUE, TRUE);
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2001-07-27 00:18:53 +00:00
|
|
|
#if 0
|
2001-11-19 20:53:06 +00:00
|
|
|
// TODO
|
2001-07-27 00:18:53 +00:00
|
|
|
// I CAN'T DO what I'm attempting below now that the Kleene star
|
|
|
|
// operator is supported. For example, in the rule
|
|
|
|
|
|
|
|
// ([:Lu:]+) { x } > $1;
|
|
|
|
|
|
|
|
// what is the maximum context length? getMaximumContextLength()
|
|
|
|
// will return 1, but this is just the length of the ante context
|
|
|
|
// part of the pattern string -- 1 character, which is a standin
|
|
|
|
// for a Quantifier, which contains a StringMatcher, which
|
|
|
|
// contains a UnicodeSet.
|
|
|
|
|
|
|
|
// There is a complicated way to make this work again, and that's
|
|
|
|
// to add a "maximum left context" protocol into the
|
|
|
|
// UnicodeMatcher hierarchy. At present I'm not convinced this is
|
|
|
|
// worth it.
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2001-07-25 19:11:02 +00:00
|
|
|
// The purpose of the code below is to keep the context small
|
|
|
|
// while doing incremental transliteration. When part of the left
|
|
|
|
// context (between contextStart and start) is no longer needed,
|
|
|
|
// we try to advance contextStart past that portion. We use the
|
|
|
|
// maximum context length to do so.
|
|
|
|
int32_t newCS = index.start;
|
|
|
|
int32_t n = getMaximumContextLength();
|
|
|
|
while (newCS > originalStart && n-- > 0) {
|
|
|
|
--newCS;
|
|
|
|
newCS -= UTF_CHAR_LENGTH(text.char32At(newCS)) - 1;
|
|
|
|
}
|
|
|
|
index.contextStart = uprv_max(newCS, originalStart);
|
2001-07-27 00:18:53 +00:00
|
|
|
#endif
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
2001-07-17 23:36:41 +00:00
|
|
|
/**
|
|
|
|
* This method breaks up the input text into runs of unfiltered
|
|
|
|
* characters. It passes each such run to
|
|
|
|
* <subclass>.handleTransliterate(). Subclasses that can handle the
|
|
|
|
* filter logic more efficiently themselves may override this method.
|
|
|
|
*
|
|
|
|
* All transliteration calls in this class go through this method.
|
|
|
|
*/
|
|
|
|
void Transliterator::filteredTransliterate(Replaceable& text,
|
|
|
|
UTransPosition& index,
|
2001-11-29 01:01:31 +00:00
|
|
|
UBool incremental,
|
|
|
|
UBool rollback) const {
|
|
|
|
// Short circuit path for transliterators with no filter in
|
|
|
|
// non-incremental mode.
|
|
|
|
if (filter == 0 && !rollback) {
|
2001-07-17 23:36:41 +00:00
|
|
|
handleTransliterate(text, index, incremental);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-11-29 01:01:31 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// This method processes text in two groupings:
|
|
|
|
//
|
|
|
|
// RUNS -- A run is a contiguous group of characters which are contained
|
2001-11-30 01:26:49 +00:00
|
|
|
// in the filter for this transliterator (filter.contains(ch) == TRUE).
|
2001-11-29 01:01:31 +00:00
|
|
|
// Text outside of runs may appear as context but it is not modified.
|
|
|
|
// The start and limit Position values are narrowed to each run.
|
|
|
|
//
|
|
|
|
// PASSES (incremental only) -- To make incremental mode work correctly,
|
|
|
|
// each run is broken up into n passes, where n is the length (in code
|
|
|
|
// points) of the run. Each pass contains the first n characters. If a
|
|
|
|
// pass is completely transliterated, it is committed, and further passes
|
|
|
|
// include characters after the committed text. If a pass is blocked,
|
|
|
|
// and does not transliterate completely, then this method rolls back
|
|
|
|
// the changes made during the pass, extends the pass by one code point,
|
|
|
|
// and tries again.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2001-07-17 23:36:41 +00:00
|
|
|
// globalLimit is the limit value for the entire operation. We
|
|
|
|
// set index.limit to the end of each unfiltered run before
|
|
|
|
// calling handleTransliterate(), so we need to maintain the real
|
|
|
|
// value of index.limit here. After each transliteration, we
|
|
|
|
// update globalLimit for insertions or deletions that have
|
|
|
|
// happened.
|
|
|
|
int32_t globalLimit = index.limit;
|
2001-11-29 01:01:31 +00:00
|
|
|
|
|
|
|
// If there is a non-null filter, then break the input text up. Say the
|
|
|
|
// input text has the form:
|
2001-07-17 23:36:41 +00:00
|
|
|
// xxxabcxxdefxx
|
2001-11-29 01:01:31 +00:00
|
|
|
// where 'x' represents a filtered character (filter.contains('x') ==
|
|
|
|
// false). Then we break this up into:
|
2001-07-17 23:36:41 +00:00
|
|
|
// xxxabc xxdef xx
|
|
|
|
// Each pass through the loop consumes a run of filtered
|
|
|
|
// characters (which are ignored) and a subsequent run of
|
2001-11-29 01:01:31 +00:00
|
|
|
// unfiltered characters (which are transliterated).
|
|
|
|
|
2001-10-25 00:03:50 +00:00
|
|
|
for (;;) {
|
2001-07-17 23:36:41 +00:00
|
|
|
|
2001-11-29 01:01:31 +00:00
|
|
|
if (filter != NULL) {
|
|
|
|
// Narrow the range to be transliterated to the first segment
|
|
|
|
// of unfiltered characters at or after index.start.
|
2001-07-31 22:38:52 +00:00
|
|
|
|
2001-11-29 01:01:31 +00:00
|
|
|
// Advance past filtered chars
|
|
|
|
UChar32 c;
|
|
|
|
while (index.start < globalLimit &&
|
|
|
|
!filter->contains(c=text.char32At(index.start))) {
|
|
|
|
index.start += UTF_CHAR_LENGTH(c);
|
|
|
|
}
|
2001-07-17 23:36:41 +00:00
|
|
|
|
2001-11-29 01:01:31 +00:00
|
|
|
// Find the end of this run of unfiltered chars
|
|
|
|
index.limit = index.start;
|
|
|
|
while (index.limit < globalLimit &&
|
|
|
|
filter->contains(c=text.char32At(index.limit))) {
|
|
|
|
index.limit += UTF_CHAR_LENGTH(c);
|
|
|
|
}
|
2001-07-17 23:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if the unfiltered run is empty. This only
|
|
|
|
// happens at the end of the string when all the remaining
|
|
|
|
// characters are filtered.
|
|
|
|
if (index.limit == index.start) {
|
|
|
|
// assert(index.start == globalLimit);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-11-29 01:01:31 +00:00
|
|
|
// Is this run incremental? If there is additional
|
2001-10-25 00:03:50 +00:00
|
|
|
// filtered text (if limit < globalLimit) then we pass in
|
|
|
|
// an incremental value of FALSE to force the subclass to
|
2001-11-29 01:01:31 +00:00
|
|
|
// complete the transliteration for this run.
|
|
|
|
UBool isIncrementalRun =
|
|
|
|
(index.limit < globalLimit ? FALSE : incremental);
|
2001-10-25 00:03:50 +00:00
|
|
|
|
2001-11-29 01:01:31 +00:00
|
|
|
int32_t delta;
|
|
|
|
|
2001-10-25 00:03:50 +00:00
|
|
|
// Implement rollback. To understand the need for rollback,
|
|
|
|
// consider the following transliterator:
|
|
|
|
//
|
|
|
|
// "t" is "a > A;"
|
|
|
|
// "u" is "A > b;"
|
|
|
|
// "v" is a compound of "t; NFD; u" with a filter [:Ll:]
|
|
|
|
//
|
|
|
|
// Now apply "c" to the input text "a". The result is "b". But if
|
|
|
|
// the transliteration is done incrementally, then the NFD holds
|
|
|
|
// things up after "t" has already transformed "a" to "A". When
|
|
|
|
// finishTransliterate() is called, "A" is _not_ processed because
|
|
|
|
// it gets excluded by the [:Ll:] filter, and the end result is "A"
|
|
|
|
// -- incorrect. The problem is that the filter is applied to a
|
|
|
|
// partially-transliterated result, when we only want it to apply to
|
|
|
|
// input text. Although this example hinges on a compound
|
|
|
|
// transliterator containing NFD and a specific filter, it can
|
|
|
|
// actually happen with any transliterator which may do a partial
|
|
|
|
// transformation in incremental mode into characters outside its
|
|
|
|
// filter.
|
|
|
|
//
|
2001-11-29 01:01:31 +00:00
|
|
|
// To handle this, when in incremental mode we supply characters to
|
|
|
|
// handleTransliterate() in several passes. Each pass adds one more
|
|
|
|
// input character to the input text. That is, for input "ABCD", we
|
|
|
|
// first try "A", then "AB", then "ABC", and finally "ABCD". If at
|
|
|
|
// any point we block (upon return, start < limit) then we roll
|
|
|
|
// back. If at any point we complete the run (upon return start ==
|
|
|
|
// limit) then we commit that run.
|
|
|
|
|
|
|
|
if (rollback && isIncrementalRun) {
|
|
|
|
|
|
|
|
int32_t runStart = index.start;
|
|
|
|
int32_t runLimit = index.limit;
|
|
|
|
int32_t runLength = runLimit - runStart;
|
|
|
|
|
2001-10-25 00:03:50 +00:00
|
|
|
// Make a rollback copy at the end of the string
|
2001-11-29 01:01:31 +00:00
|
|
|
int32_t rollbackOrigin = text.length();
|
|
|
|
text.copy(runStart, runLimit, rollbackOrigin);
|
|
|
|
|
|
|
|
// Variables reflecting the commitment of completely
|
|
|
|
// transliterated text. passStart is the runStart, advanced
|
|
|
|
// past committed text. rollbackStart is the rollbackOrigin,
|
|
|
|
// advanced past rollback text that corresponds to committed
|
|
|
|
// text.
|
|
|
|
int32_t passStart = runStart;
|
|
|
|
int32_t rollbackStart = rollbackOrigin;
|
|
|
|
|
|
|
|
// The limit for each pass; we advance by one code point with
|
|
|
|
// each iteration.
|
|
|
|
int32_t passLimit = index.start;
|
|
|
|
|
|
|
|
// Total length, in 16-bit code units, of uncommitted text.
|
|
|
|
// This is the length to be rolled back.
|
|
|
|
int32_t uncommittedLength = 0;
|
|
|
|
|
|
|
|
// Total delta (change in length) for all passes
|
|
|
|
int32_t totalDelta = 0;
|
|
|
|
|
|
|
|
// PASS MAIN LOOP -- Start with a single character, and extend
|
|
|
|
// the text by one character at a time. Roll back partial
|
|
|
|
// transliterations and commit complete transliterations.
|
|
|
|
for (;;) {
|
|
|
|
// Length of additional code point, either one or two
|
|
|
|
int32_t charLength =
|
|
|
|
UTF_CHAR_LENGTH(text.char32At(passLimit));
|
|
|
|
passLimit += charLength;
|
|
|
|
if (passLimit > runLimit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uncommittedLength += charLength;
|
|
|
|
|
|
|
|
index.limit = passLimit;
|
|
|
|
|
|
|
|
// Delegate to subclass for actual transliteration. Upon
|
|
|
|
// return, start will be updated to point after the
|
|
|
|
// transliterated text, and limit and contextLimit will be
|
|
|
|
// adjusted for length changes.
|
2001-11-30 01:26:49 +00:00
|
|
|
handleTransliterate(text, index, TRUE);
|
2001-11-29 01:01:31 +00:00
|
|
|
|
|
|
|
delta = index.limit - passLimit; // change in length
|
|
|
|
|
|
|
|
// We failed to completely transliterate this pass.
|
|
|
|
// Roll back the text. Indices remain unchanged; reset
|
|
|
|
// them where necessary.
|
|
|
|
if (index.start != index.limit) {
|
|
|
|
// Find the rollbackStart, adjusted for length changes
|
|
|
|
// and the deletion of partially transliterated text.
|
|
|
|
int32_t rs = rollbackStart + delta - (index.limit - passStart);
|
|
|
|
|
|
|
|
// Delete the partially transliterated text
|
|
|
|
text.handleReplaceBetween(passStart, index.limit, EMPTY);
|
|
|
|
|
|
|
|
// Copy the rollback text back
|
|
|
|
text.copy(rs, rs + uncommittedLength, passStart);
|
|
|
|
|
|
|
|
// Restore indices to their original values
|
|
|
|
index.start = passStart;
|
|
|
|
index.limit = passLimit;
|
|
|
|
index.contextLimit -= delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We did completely transliterate this pass. Update the
|
|
|
|
// commit indices to record how far we got. Adjust indices
|
|
|
|
// for length change.
|
|
|
|
else {
|
|
|
|
// Move the pass indices past the committed text.
|
|
|
|
passStart = passLimit = index.start;
|
|
|
|
|
|
|
|
// Adjust the rollbackStart for length changes and move
|
|
|
|
// it past the committed text. All characters we've
|
|
|
|
// processed to this point are committed now, so zero
|
|
|
|
// out the uncommittedLength.
|
|
|
|
rollbackStart += delta + uncommittedLength;
|
|
|
|
uncommittedLength = 0;
|
|
|
|
|
|
|
|
// Adjust indices for length changes.
|
|
|
|
runLimit += delta;
|
|
|
|
totalDelta += delta;
|
|
|
|
}
|
2001-10-25 00:03:50 +00:00
|
|
|
}
|
2001-11-29 01:01:31 +00:00
|
|
|
|
|
|
|
// Adjust overall limit and rollbackOrigin for insertions and
|
|
|
|
// deletions. Don't need to worry about contextLimit because
|
|
|
|
// handleTransliterate() maintains that.
|
|
|
|
rollbackOrigin += totalDelta;
|
|
|
|
globalLimit += totalDelta;
|
|
|
|
|
|
|
|
// Delete the rollback copy
|
|
|
|
text.handleReplaceBetween(rollbackOrigin, rollbackOrigin + runLength, EMPTY);
|
2001-11-30 01:24:25 +00:00
|
|
|
|
|
|
|
// Move start past committed text
|
|
|
|
index.start = passStart;
|
2001-10-25 00:03:50 +00:00
|
|
|
}
|
2001-11-29 01:01:31 +00:00
|
|
|
|
|
|
|
else {
|
|
|
|
// Delegate to subclass for actual transliteration.
|
|
|
|
int32_t limit = index.limit;
|
|
|
|
handleTransliterate(text, index, isIncrementalRun);
|
|
|
|
delta = index.limit - limit; // change in length
|
|
|
|
|
2001-11-30 01:24:25 +00:00
|
|
|
// In a properly written transliterator, start == limit after
|
|
|
|
// handleTransliterate() returns when incremental is false.
|
|
|
|
// Catch cases where the subclass doesn't do this, and throw
|
|
|
|
// an exception. (Just pinning start to limit is a bad idea,
|
|
|
|
// because what's probably happening is that the subclass
|
|
|
|
// isn't transliterating all the way to the end, and it should
|
|
|
|
// in non-incremental mode.)
|
|
|
|
if (!incremental && index.start != index.limit) {
|
|
|
|
// We can't throw an exception, so just fudge things
|
|
|
|
index.start = index.limit;
|
|
|
|
}
|
|
|
|
|
2001-11-29 01:01:31 +00:00
|
|
|
// Adjust overall limit for insertions/deletions. Don't need
|
|
|
|
// to worry about contextLimit because handleTransliterate()
|
|
|
|
// maintains that.
|
|
|
|
globalLimit += delta;
|
|
|
|
}
|
|
|
|
|
2001-11-30 01:24:25 +00:00
|
|
|
if (filter == NULL || isIncrementalRun) {
|
2001-11-21 19:55:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-11-29 01:01:31 +00:00
|
|
|
|
2001-10-25 00:03:50 +00:00
|
|
|
// If we did completely transliterate this
|
2001-11-29 01:01:31 +00:00
|
|
|
// run, then repeat with the next unfiltered run.
|
2001-10-25 00:03:50 +00:00
|
|
|
}
|
2001-07-17 23:36:41 +00:00
|
|
|
|
|
|
|
// Start is valid where it is. Limit needs to be put back where
|
|
|
|
// it was, modulo adjustments for deletions/insertions.
|
2001-11-29 01:01:31 +00:00
|
|
|
index.limit = globalLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transliterator::filteredTransliterate(Replaceable& text,
|
|
|
|
UTransPosition& index,
|
|
|
|
UBool incremental) const {
|
|
|
|
filteredTransliterate(text, index, incremental, FALSE);
|
2001-07-17 23:36:41 +00:00
|
|
|
}
|
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
/**
|
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
|
2002-02-07 01:07:55 +00:00
|
|
|
* transliterator at the first TARGET_SEP. If there is no TARGET_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
|
|
|
|
*/
|
2001-11-29 23:49:44 +00:00
|
|
|
UnicodeString& Transliterator::getDisplayName(const UnicodeString& id,
|
2000-01-11 19:39:28 +00:00
|
|
|
const Locale& inLocale,
|
|
|
|
UnicodeString& result) {
|
2001-11-29 23:49:44 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
|
|
|
}
|
|
|
|
|
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...
|
|
|
|
|
2001-11-29 23:49:44 +00:00
|
|
|
result.truncate(0);
|
|
|
|
|
|
|
|
// Normalize the ID
|
|
|
|
UnicodeString source, target, variant;
|
2002-02-07 01:07:55 +00:00
|
|
|
UBool sawSource;
|
|
|
|
TransliteratorIDParser::IDtoSTV(id, source, target, variant, sawSource);
|
2001-11-29 23:49:44 +00:00
|
|
|
if (target.length() < 1) {
|
|
|
|
// No target; malformed id
|
|
|
|
return result;
|
|
|
|
}
|
2002-02-07 01:07:55 +00:00
|
|
|
if (variant.length() > 0) { // Change "Foo" to "/Foo"
|
|
|
|
variant.insert(0, VARIANT_SEP);
|
|
|
|
}
|
2001-11-29 23:49:44 +00:00
|
|
|
UnicodeString ID(source);
|
2002-02-07 01:07:55 +00:00
|
|
|
ID.append(TARGET_SEP).append(target).append(variant);
|
2001-11-29 23:49:44 +00:00
|
|
|
|
1999-12-01 21:27:44 +00:00
|
|
|
// build the char* key
|
2001-09-18 23:53:27 +00:00
|
|
|
char key[200];
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_strcpy(key, RB_DISPLAY_NAME_PREFIX);
|
2001-03-28 23:23:23 +00:00
|
|
|
int32_t length=(int32_t)uprv_strlen(RB_DISPLAY_NAME_PREFIX);
|
2001-09-18 23:53:27 +00:00
|
|
|
ID.extract(0, (int32_t)(sizeof(key)-length), key+length, "");
|
1999-11-20 00:40:50 +00:00
|
|
|
|
2001-08-15 19:06:40 +00:00
|
|
|
// Try to retrieve a UnicodeString from the bundle.
|
2000-05-15 18:39:17 +00:00
|
|
|
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.length() != 0) {
|
|
|
|
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.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.length() != 0) {
|
|
|
|
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];
|
|
|
|
int32_t nargs;
|
2001-11-29 23:49:44 +00:00
|
|
|
args[0].setLong(2); // # of args to follow
|
|
|
|
args[1].setString(source);
|
|
|
|
args[2].setString(target);
|
|
|
|
nargs = 3;
|
2000-01-11 19:39:28 +00:00
|
|
|
|
|
|
|
// Use display names for the scripts, if they exist
|
|
|
|
UnicodeString s;
|
2001-03-28 23:23:23 +00:00
|
|
|
length=(int32_t)uprv_strlen(RB_SCRIPT_DISPLAY_NAME_PREFIX);
|
2001-11-29 23:49:44 +00:00
|
|
|
for (int j=1; j<=2; ++j) {
|
2000-01-11 19:39:28 +00:00
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
uprv_strcpy(key, RB_SCRIPT_DISPLAY_NAME_PREFIX);
|
|
|
|
args[j].getString(s);
|
2001-09-18 23:53:27 +00:00
|
|
|
s.extract(0, sizeof(key)-length-1, key+length, "");
|
2000-01-11 19:39:28 +00:00
|
|
|
|
2000-05-15 18:39:17 +00:00
|
|
|
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;
|
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)) {
|
2001-11-29 23:49:44 +00:00
|
|
|
result.append(variant);
|
1999-11-20 00:40:50 +00:00
|
|
|
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;
|
2001-10-24 13:34:11 +00:00
|
|
|
filter = NULL;
|
2000-02-05 00:24:24 +00:00
|
|
|
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
|
|
|
|
*/
|
2001-08-31 03:23:39 +00:00
|
|
|
Transliterator* Transliterator::createInverse(UErrorCode& status) const {
|
|
|
|
UParseError parseError;
|
|
|
|
return Transliterator::createInstance(ID, UTRANS_REVERSE,parseError,status);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
2002-02-20 00:01:31 +00:00
|
|
|
Transliterator* Transliterator::createInstance(const UnicodeString& ID,
|
|
|
|
UTransDirection dir,
|
|
|
|
UErrorCode& status) {
|
|
|
|
UParseError parseError;
|
|
|
|
return createInstance(ID, dir, parseError, status);
|
|
|
|
}
|
|
|
|
|
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,
|
2001-08-31 03:23:39 +00:00
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& status) {
|
2001-07-13 21:09:41 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-29 23:49:44 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
|
|
|
}
|
2001-07-13 21:09:41 +00:00
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
UnicodeString canonID;
|
2001-08-23 01:06:08 +00:00
|
|
|
UVector list(status);
|
2001-07-13 21:09:41 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2002-02-07 01:07:55 +00:00
|
|
|
return NULL;
|
2001-07-13 21:09:41 +00:00
|
|
|
}
|
2001-07-16 20:48:46 +00:00
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
UnicodeSet* globalFilter;
|
2002-03-01 21:56:40 +00:00
|
|
|
// TODO add code for parseError...currently unused, but
|
|
|
|
// later may be used by parsing code...
|
2002-02-07 01:07:55 +00:00
|
|
|
if (!TransliteratorIDParser::parseCompoundID(ID, dir, canonID, list, globalFilter)) {
|
|
|
|
status = U_INVALID_ID;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TransliteratorIDParser::instantiateList(list, NULL, -1, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert(list.size() > 0);
|
|
|
|
Transliterator* t = NULL;
|
2001-07-13 21:09:41 +00:00
|
|
|
switch (list.size()) {
|
|
|
|
case 1:
|
2001-07-16 20:48:46 +00:00
|
|
|
t = (Transliterator*) list.elementAt(0);
|
|
|
|
break;
|
2001-07-13 21:09:41 +00:00
|
|
|
default:
|
2002-03-01 21:56:40 +00:00
|
|
|
t = new CompoundTransliterator(list, parseError, status);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (t == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-02-07 01:07:55 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete t;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-07-16 20:48:46 +00:00
|
|
|
break;
|
2001-07-13 21:09:41 +00:00
|
|
|
}
|
2002-02-07 01:07:55 +00:00
|
|
|
t->setID(canonID);
|
|
|
|
if (globalFilter != NULL) {
|
|
|
|
t->adoptFilter(globalFilter);
|
2001-10-10 19:29:45 +00:00
|
|
|
}
|
2001-07-16 20:48:46 +00:00
|
|
|
return t;
|
2001-07-13 21:09:41 +00:00
|
|
|
}
|
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
/**
|
|
|
|
* Create a transliterator from a basic ID. This is an ID
|
|
|
|
* containing only the forward direction source, target, and
|
|
|
|
* variant.
|
|
|
|
* @param id a basic ID of the form S-T or S-T/V.
|
|
|
|
* @return a newly created Transliterator or null if the ID is
|
|
|
|
* invalid.
|
|
|
|
*/
|
|
|
|
Transliterator* Transliterator::createBasicInstance(const UnicodeString& id,
|
|
|
|
const UnicodeString* canon) {
|
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
|
|
|
}
|
|
|
|
UParseError pe;
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
TransliteratorAlias* alias = 0;
|
|
|
|
Transliterator* t = 0;
|
|
|
|
{
|
|
|
|
Mutex lock(®istryMutex);
|
|
|
|
t = registry->get(id, alias, pe, ec);
|
|
|
|
}
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
delete t;
|
|
|
|
delete alias;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alias != 0) {
|
|
|
|
// assert(t==0);
|
|
|
|
// Instantiate an alias
|
|
|
|
t = alias->create(pe, ec);
|
|
|
|
delete alias;
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
delete t;
|
|
|
|
t = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t != NULL && canon != NULL) {
|
|
|
|
t->setID(*canon);
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2001-07-13 21:09:41 +00:00
|
|
|
/**
|
|
|
|
* Returns a <code>Transliterator</code> object constructed from
|
|
|
|
* the given rule string. This will be a RuleBasedTransliterator,
|
|
|
|
* if the rule string contains only rules, or a
|
|
|
|
* CompoundTransliterator, if it contains ID blocks, or a
|
|
|
|
* NullTransliterator, if it contains ID blocks which parse as
|
|
|
|
* empty for the given direction.
|
|
|
|
*/
|
|
|
|
Transliterator* Transliterator::createFromRules(const UnicodeString& ID,
|
|
|
|
const UnicodeString& rules,
|
|
|
|
UTransDirection dir,
|
2001-08-31 03:23:39 +00:00
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& status) {
|
2001-10-10 19:29:45 +00:00
|
|
|
Transliterator* t = NULL;
|
2001-07-13 21:09:41 +00:00
|
|
|
|
2001-10-10 19:29:45 +00:00
|
|
|
TransliteratorParser parser;
|
|
|
|
parser.parse(rules, dir, parseError, status);
|
2001-07-13 21:09:41 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-08-15 19:06:40 +00:00
|
|
|
// NOTE: The logic here matches that in TransliteratorRegistry.
|
2001-10-10 19:29:45 +00:00
|
|
|
if (parser.idBlock.length() == 0) {
|
|
|
|
if (parser.data == NULL) {
|
2001-07-13 21:09:41 +00:00
|
|
|
// No idBlock, no data -- this is just an
|
|
|
|
// alias for Null
|
2001-10-10 19:29:45 +00:00
|
|
|
t = new NullTransliterator();
|
2001-07-13 21:09:41 +00:00
|
|
|
} else {
|
|
|
|
// No idBlock, data != 0 -- this is an
|
|
|
|
// ordinary RBT_DATA.
|
2001-10-10 19:29:45 +00:00
|
|
|
t = new RuleBasedTransliterator(ID, parser.orphanData(), TRUE); // TRUE == adopt data object
|
2000-01-14 21:15:25 +00:00
|
|
|
}
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (t == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2000-01-14 21:15:25 +00:00
|
|
|
} else {
|
2001-10-10 19:29:45 +00:00
|
|
|
if (parser.data == NULL) {
|
2001-10-24 13:34:11 +00:00
|
|
|
// idBlock, no data -- this is an alias. The ID has
|
|
|
|
// been munged from reverse into forward mode, if
|
|
|
|
// necessary, so instantiate the ID in the forward
|
|
|
|
// direction.
|
|
|
|
t = createInstance(parser.idBlock, UTRANS_FORWARD, parseError, status);
|
2001-10-10 19:29:45 +00:00
|
|
|
if (t != NULL) {
|
2001-07-13 21:09:41 +00:00
|
|
|
t->setID(ID);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// idBlock and data -- this is a compound
|
|
|
|
// RBT
|
|
|
|
UnicodeString id("_", "");
|
2001-10-10 19:29:45 +00:00
|
|
|
t = new RuleBasedTransliterator(id, parser.orphanData(), TRUE); // TRUE == adopt data object
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (t == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-10-10 19:29:45 +00:00
|
|
|
t = new CompoundTransliterator(ID, parser.idBlock, parser.idSplitPoint,
|
2002-02-20 00:01:31 +00:00
|
|
|
t, status);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (t == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-06-20 22:30:52 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2001-07-13 21:09:41 +00:00
|
|
|
delete t;
|
|
|
|
t = 0;
|
2001-03-31 01:21:38 +00:00
|
|
|
}
|
2001-10-10 19:29:45 +00:00
|
|
|
if (parser.compoundFilter != NULL) {
|
|
|
|
t->adoptFilter(parser.orphanCompoundFilter());
|
|
|
|
}
|
2001-07-13 21:09:41 +00:00
|
|
|
return t;
|
2001-06-20 22:30:52 +00:00
|
|
|
}
|
2001-07-13 21:09:41 +00:00
|
|
|
}
|
2001-10-10 19:29:45 +00:00
|
|
|
|
|
|
|
return t;
|
2001-07-13 21:09:41 +00:00
|
|
|
}
|
2001-06-20 22:30:52 +00:00
|
|
|
|
2001-07-13 21:09:41 +00:00
|
|
|
UnicodeString& Transliterator::toRules(UnicodeString& rulesSource,
|
|
|
|
UBool escapeUnprintable) const {
|
|
|
|
// The base class implementation of toRules munges the ID into
|
|
|
|
// the correct format. That is: foo => ::foo
|
2001-11-16 21:49:10 +00:00
|
|
|
if (escapeUnprintable) {
|
|
|
|
rulesSource.truncate(0);
|
|
|
|
UnicodeString id = getID();
|
|
|
|
for (int32_t i=0; i<id.length();) {
|
|
|
|
UChar32 c = id.char32At(i);
|
2001-12-03 20:19:08 +00:00
|
|
|
if (!ICU_Utility::escapeUnprintable(rulesSource, c)) {
|
2001-11-16 21:49:10 +00:00
|
|
|
rulesSource.append(c);
|
|
|
|
}
|
|
|
|
i += UTF_CHAR_LENGTH(c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rulesSource = getID();
|
|
|
|
}
|
2001-07-13 21:09:41 +00:00
|
|
|
// KEEP in sync with rbt_pars
|
2002-01-22 00:27:49 +00:00
|
|
|
rulesSource.insert(0, UNICODE_STRING_SIMPLE("::"));
|
2001-07-17 18:13:51 +00:00
|
|
|
rulesSource.append(ID_DELIM);
|
2001-07-13 21:09:41 +00:00
|
|
|
return rulesSource;
|
|
|
|
}
|
|
|
|
|
2002-06-28 21:13:54 +00:00
|
|
|
UnicodeSet& Transliterator::getSourceSet(UnicodeSet& result) const {
|
|
|
|
handleGetSourceSet(result);
|
|
|
|
if (filter != NULL) {
|
2002-06-29 09:31:05 +00:00
|
|
|
UnicodeSet* filterSet;
|
|
|
|
UBool deleteFilterSet = FALSE;
|
|
|
|
// Most, but not all filters will be UnicodeSets. Optimize for
|
|
|
|
// the high-runner case.
|
|
|
|
if (filter->getDynamicClassID() == UnicodeSet::getStaticClassID()) {
|
|
|
|
filterSet = (UnicodeSet*) filter;
|
|
|
|
} else {
|
|
|
|
filterSet = new UnicodeSet();
|
|
|
|
deleteFilterSet = TRUE;
|
|
|
|
filter->addMatchSetTo(*filterSet);
|
|
|
|
}
|
|
|
|
result.retainAll(*filterSet);
|
|
|
|
if (deleteFilterSet) {
|
|
|
|
delete filterSet;
|
|
|
|
}
|
2002-06-28 21:13:54 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transliterator::handleGetSourceSet(UnicodeSet& result) const {
|
|
|
|
result.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeSet& Transliterator::getTargetSet(UnicodeSet& result) const {
|
|
|
|
return result.clear();
|
|
|
|
}
|
|
|
|
|
2001-07-03 23:44:37 +00:00
|
|
|
// For public consumption
|
2001-07-03 20:01:56 +00:00
|
|
|
void Transliterator::registerFactory(const UnicodeString& id,
|
2001-10-17 17:29:34 +00:00
|
|
|
Transliterator::Factory factory,
|
|
|
|
Transliterator::Token context) {
|
2001-08-15 19:06:40 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
2001-07-03 20:01:56 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
2001-10-17 17:29:34 +00:00
|
|
|
_registerFactory(id, factory, context);
|
2001-07-03 23:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// To be called only by Transliterator subclasses that are called
|
2001-08-15 19:06:40 +00:00
|
|
|
// to register themselves by initializeRegistry().
|
2001-07-03 23:44:37 +00:00
|
|
|
void Transliterator::_registerFactory(const UnicodeString& id,
|
2001-10-17 17:29:34 +00:00
|
|
|
Transliterator::Factory factory,
|
|
|
|
Transliterator::Token context) {
|
|
|
|
registry->put(id, factory, context, TRUE);
|
2001-07-03 20:01:56 +00:00
|
|
|
}
|
|
|
|
|
2001-11-15 23:40:02 +00:00
|
|
|
// To be called only by Transliterator subclasses that are called
|
|
|
|
// to register themselves by initializeRegistry().
|
2001-11-17 21:04:27 +00:00
|
|
|
void Transliterator::_registerSpecialInverse(const UnicodeString& target,
|
|
|
|
const UnicodeString& inverseTarget,
|
|
|
|
UBool bidirectional) {
|
2002-02-07 01:07:55 +00:00
|
|
|
TransliteratorIDParser::registerSpecialInverse(target, inverseTarget, bidirectional);
|
2001-11-15 23:40:02 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2001-08-15 19:06:40 +00:00
|
|
|
void Transliterator::registerInstance(Transliterator* adoptedPrototype) {
|
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
2002-06-11 22:34:20 +00:00
|
|
|
_registerInstance(adoptedPrototype);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transliterator::_registerInstance(Transliterator* adoptedPrototype) {
|
2001-08-15 19:06:40 +00:00
|
|
|
registry->put(adoptedPrototype, TRUE);
|
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
|
2001-07-03 20:01:56 +00:00
|
|
|
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
|
|
|
void Transliterator::unregister(const UnicodeString& ID) {
|
2001-08-15 19:06:40 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
2000-11-29 00:21:38 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
|
|
|
registry->remove(ID);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2001-08-15 19:06:40 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
|
|
|
return registry->countAvailableIDs();
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2001-08-15 19:06:40 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
|
|
|
return registry->getAvailableID(index);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
2001-08-01 17:37:08 +00:00
|
|
|
int32_t Transliterator::countAvailableSources(void) {
|
2001-08-15 19:06:40 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
2002-06-11 22:52:32 +00:00
|
|
|
return _countAvailableSources();
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& Transliterator::getAvailableSource(int32_t index,
|
|
|
|
UnicodeString& result) {
|
2001-08-15 19:06:40 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
2002-06-11 22:52:32 +00:00
|
|
|
return _getAvailableSource(index, result);
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Transliterator::countAvailableTargets(const UnicodeString& source) {
|
2001-08-15 19:06:40 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
2002-06-11 22:52:32 +00:00
|
|
|
return _countAvailableTargets(source);
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& Transliterator::getAvailableTarget(int32_t index,
|
|
|
|
const UnicodeString& source,
|
|
|
|
UnicodeString& result) {
|
2001-08-15 19:06:40 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
2002-06-11 22:52:32 +00:00
|
|
|
return _getAvailableTarget(index, source, result);
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Transliterator::countAvailableVariants(const UnicodeString& source,
|
|
|
|
const UnicodeString& target) {
|
2001-08-15 19:06:40 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
2002-06-11 22:52:32 +00:00
|
|
|
return _countAvailableVariants(source, target);
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& Transliterator::getAvailableVariant(int32_t index,
|
|
|
|
const UnicodeString& source,
|
|
|
|
const UnicodeString& target,
|
|
|
|
UnicodeString& result) {
|
2001-08-15 19:06:40 +00:00
|
|
|
if (registry == 0) {
|
|
|
|
initializeRegistry();
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
Mutex lock(®istryMutex);
|
2002-06-11 22:52:32 +00:00
|
|
|
return _getAvailableVariant(index, source, target, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Transliterator::_countAvailableSources(void) {
|
|
|
|
return registry->countAvailableSources();
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& Transliterator::_getAvailableSource(int32_t index,
|
|
|
|
UnicodeString& result) {
|
|
|
|
return registry->getAvailableSource(index, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Transliterator::_countAvailableTargets(const UnicodeString& source) {
|
|
|
|
return registry->countAvailableTargets(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& Transliterator::_getAvailableTarget(int32_t index,
|
|
|
|
const UnicodeString& source,
|
|
|
|
UnicodeString& result) {
|
|
|
|
return registry->getAvailableTarget(index, source, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Transliterator::_countAvailableVariants(const UnicodeString& source,
|
|
|
|
const UnicodeString& target) {
|
|
|
|
return registry->countAvailableVariants(source, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& Transliterator::_getAvailableVariant(int32_t index,
|
|
|
|
const UnicodeString& source,
|
|
|
|
const UnicodeString& target,
|
|
|
|
UnicodeString& result) {
|
2001-08-15 19:06:40 +00:00
|
|
|
return registry->getAvailableVariant(index, source, target, result);
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
|
|
|
|
2001-10-24 13:34:11 +00:00
|
|
|
#ifdef U_USE_DEPRECATED_TRANSLITERATOR_API
|
|
|
|
|
2000-01-18 18:27:27 +00:00
|
|
|
/**
|
|
|
|
* Method for subclasses to use to obtain a character in the given
|
|
|
|
* string, with filtering.
|
2001-07-31 22:43:47 +00:00
|
|
|
* @deprecated the new architecture provides filtering at the top
|
|
|
|
* level. This method will be removed Dec 31 2001.
|
2000-01-18 18:27:27 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-10-24 13:34:11 +00:00
|
|
|
#endif
|
|
|
|
|
2001-08-15 19:06:40 +00:00
|
|
|
void Transliterator::initializeRegistry(void) {
|
|
|
|
// Lock first, check registry pointer second
|
|
|
|
Mutex lock(®istryMutex);
|
|
|
|
if (registry != 0) {
|
|
|
|
// We were blocked by another thread in initializeRegistry()
|
|
|
|
return;
|
2001-08-01 17:37:08 +00:00
|
|
|
}
|
|
|
|
|
2001-08-23 01:06:08 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
registry = new TransliteratorRegistry(status);
|
|
|
|
if (registry == 0 || U_FAILURE(status)) {
|
2001-08-15 19:06:40 +00:00
|
|
|
return; // out of memory, no recovery
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2001-07-16 20:48:46 +00:00
|
|
|
|
2000-01-13 14:11:06 +00:00
|
|
|
/* The following code parses the index table located in
|
2001-03-03 00:49:45 +00:00
|
|
|
* icu/data/translit_index.txt. The index is an n x 4 table
|
|
|
|
* that follows this format:
|
|
|
|
*
|
|
|
|
* <id>:file:<resource>:<direction>
|
|
|
|
* <id>:internal:<resource>:<direction>
|
|
|
|
* <id>:alias:<getInstanceArg>:
|
|
|
|
*
|
|
|
|
* <id> is the ID of the system transliterator being defined. These
|
|
|
|
* are public IDs enumerated by Transliterator.getAvailableIDs(),
|
|
|
|
* unless the second field is "internal".
|
|
|
|
*
|
|
|
|
* <resource> is a ResourceReader resource name. Currently these refer
|
|
|
|
* to file names under com/ibm/text/resources. This string is passed
|
|
|
|
* directly to ResourceReader, together with <encoding>.
|
|
|
|
*
|
|
|
|
* <direction> is either "FORWARD" or "REVERSE".
|
|
|
|
*
|
|
|
|
* <getInstanceArg> is a string to be passed directly to
|
|
|
|
* Transliterator.getInstance(). The returned Transliterator object
|
|
|
|
* then has its ID changed to <id> and is returned.
|
1999-11-20 00:40:50 +00:00
|
|
|
*
|
2001-03-03 00:49:45 +00:00
|
|
|
* The extra blank field on "alias" lines is to make the array square.
|
1999-11-20 00:40:50 +00:00
|
|
|
*/
|
2001-10-11 23:54:55 +00:00
|
|
|
static const char translit_index[] = "translit_index";
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2001-10-02 23:53:04 +00:00
|
|
|
UResourceBundle *bundle, *transIDs, *colBund;
|
2001-10-11 23:54:55 +00:00
|
|
|
bundle = ures_openDirect(0, translit_index, &status);
|
2001-10-02 23:53:04 +00:00
|
|
|
transIDs = ures_getByKey(bundle, RB_RULE_BASED_IDS, 0, &status);
|
2001-03-05 22:36:29 +00:00
|
|
|
|
|
|
|
int32_t row, maxRows;
|
|
|
|
if (U_SUCCESS(status)) {
|
2001-10-02 23:53:04 +00:00
|
|
|
maxRows = ures_getSize(transIDs);
|
2001-03-05 22:36:29 +00:00
|
|
|
for (row = 0; row < maxRows; row++) {
|
2001-10-02 23:53:04 +00:00
|
|
|
colBund = ures_getByIndex(transIDs, row, 0, &status);
|
2001-03-05 22:36:29 +00:00
|
|
|
|
2001-10-02 23:53:04 +00:00
|
|
|
if (U_SUCCESS(status) && ures_getSize(colBund) == 4) {
|
|
|
|
UnicodeString id = ures_getUnicodeStringByIndex(colBund, 0, &status);
|
|
|
|
UChar type = ures_getUnicodeStringByIndex(colBund, 1, &status).charAt(0);
|
|
|
|
UnicodeString resString = ures_getUnicodeStringByIndex(colBund, 2, &status);
|
2001-03-05 22:36:29 +00:00
|
|
|
|
|
|
|
if (U_SUCCESS(status)) {
|
2001-08-15 19:06:40 +00:00
|
|
|
switch (type) {
|
|
|
|
case 0x66: // 'f'
|
|
|
|
case 0x69: // 'i'
|
|
|
|
// 'file' or 'internal';
|
|
|
|
// row[2]=resource, row[3]=direction
|
|
|
|
{
|
|
|
|
UBool visible = (type == 0x0066 /*f*/);
|
|
|
|
UTransDirection dir =
|
2001-10-02 23:53:04 +00:00
|
|
|
(ures_getUnicodeStringByIndex(colBund, 3, &status).charAt(0) ==
|
2001-08-15 19:06:40 +00:00
|
|
|
0x0046 /*F*/) ?
|
|
|
|
UTRANS_FORWARD : UTRANS_REVERSE;
|
|
|
|
registry->put(id, resString, dir, visible);
|
2001-03-05 22:36:29 +00:00
|
|
|
}
|
2001-08-15 19:06:40 +00:00
|
|
|
break;
|
|
|
|
case 0x61: // 'a'
|
2001-03-05 22:36:29 +00:00
|
|
|
// 'alias'; row[2]=createInstance argument
|
2001-08-15 19:06:40 +00:00
|
|
|
registry->put(id, resString, TRUE);
|
|
|
|
break;
|
2001-03-22 23:25:26 +00:00
|
|
|
}
|
2001-03-05 22:36:29 +00:00
|
|
|
}
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
2001-10-02 23:53:04 +00:00
|
|
|
|
|
|
|
ures_close(colBund);
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-02 23:53:04 +00:00
|
|
|
|
|
|
|
ures_close(transIDs);
|
|
|
|
ures_close(bundle);
|
|
|
|
|
2001-11-17 21:04:27 +00:00
|
|
|
_registerSpecialInverse(NullTransliterator::SHORT_ID,
|
|
|
|
NullTransliterator::SHORT_ID, FALSE);
|
2001-11-15 23:40:02 +00:00
|
|
|
|
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.
|
|
|
|
|
2001-08-15 19:06:40 +00:00
|
|
|
registry->put(new NullTransliterator(), TRUE);
|
|
|
|
registry->put(new LowercaseTransliterator(), TRUE);
|
|
|
|
registry->put(new UppercaseTransliterator(), TRUE);
|
|
|
|
registry->put(new TitlecaseTransliterator(), TRUE);
|
2001-11-17 21:04:27 +00:00
|
|
|
_registerSpecialInverse("Upper", "Lower", TRUE);
|
|
|
|
_registerSpecialInverse("Title", "Lower", FALSE);
|
2001-08-15 19:06:40 +00:00
|
|
|
registry->put(new UnicodeNameTransliterator(), TRUE);
|
|
|
|
registry->put(new NameUnicodeTransliterator(), TRUE);
|
2001-11-29 18:03:51 +00:00
|
|
|
RemoveTransliterator::registerIDs();
|
2001-11-21 07:02:15 +00:00
|
|
|
EscapeTransliterator::registerIDs();
|
|
|
|
UnescapeTransliterator::registerIDs();
|
2001-07-03 23:44:37 +00:00
|
|
|
NormalizationTransliterator::registerIDs();
|
2002-06-07 20:24:40 +00:00
|
|
|
AnyTransliterator::registerIDs();
|
2001-09-21 00:19:53 +00:00
|
|
|
ucln_i18n_registerCleanup();
|
1999-11-20 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
2001-10-12 05:54:15 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2001-09-19 16:50:06 +00:00
|
|
|
// Defined in ucln_in.h:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release all static memory held by transliterator. This will
|
|
|
|
* necessarily invalidate any rule-based transliterators held by the
|
|
|
|
* user, because RBTs hold pointers to common data objects.
|
2001-09-21 00:19:53 +00:00
|
|
|
*/
|
|
|
|
U_CFUNC UBool transliterator_cleanup(void) {
|
2001-11-13 23:47:11 +00:00
|
|
|
TitlecaseTransliterator::cleanup();
|
2001-12-03 20:50:50 +00:00
|
|
|
NormalizationTransliterator::cleanup();
|
2002-02-07 01:07:55 +00:00
|
|
|
TransliteratorIDParser::cleanup();
|
2001-11-16 03:57:48 +00:00
|
|
|
if (registry) {
|
|
|
|
delete registry;
|
|
|
|
registry = NULL;
|
|
|
|
}
|
2001-09-21 00:19:53 +00:00
|
|
|
umtx_destroy(®istryMutex);
|
|
|
|
return TRUE;
|
2001-09-19 16:50:06 +00:00
|
|
|
}
|
|
|
|
|
2001-08-15 19:06:40 +00:00
|
|
|
//eof
|