Bump version to 4.3-4
[LibreOffice.git] / i18npool / source / transliteration / transliterationImpl.cxx
blob20d22f0bb199814b83d7a4160984e97a6c1a38cc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "transliterationImpl.hxx"
22 #include "servicename.hxx"
24 #include <com/sun/star/i18n/LocaleData.hpp>
25 #include <com/sun/star/i18n/TransliterationType.hpp>
26 #include <com/sun/star/i18n/TransliterationModulesExtra.hpp>
27 #include <com/sun/star/lang/XComponent.hpp>
29 #include <comphelper/processfactory.hxx>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <rtl/instance.hxx>
32 #include <rtl/string.h>
33 #include <rtl/ustring.hxx>
34 #include <rtl/ustrbuf.hxx>
36 #include <algorithm>
38 #if OSL_DEBUG_LEVEL > 1
39 #include <stdio.h>
40 #endif
42 using namespace com::sun::star::uno;
43 using namespace com::sun::star::lang;
46 namespace com { namespace sun { namespace star { namespace i18n {
48 #define ERROR RuntimeException()
50 #define TmItem1( name ) \
51 {TransliterationModules_##name, TransliterationModulesNew_##name, #name}
53 #define TmItem2( name ) \
54 {(TransliterationModules)0, TransliterationModulesNew_##name, #name}
56 // Ignore Module list
57 static struct TMlist {
58 TransliterationModules tm;
59 TransliterationModulesNew tmn;
60 const sal_Char *implName;
61 } TMlist[] = { // Modules ModulesNew
62 TmItem1 (IGNORE_CASE), // 0. (1<<8 256) (7)
63 TmItem1 (IGNORE_WIDTH), // 1. (1<<9 512) (8)
64 TmItem1 (IGNORE_KANA), // 2. (1<<10 1024) (9)
65 // No enum define for this trans. application has to use impl name to load it
66 // TmItem1 (IGNORE_CASE_SIMPLE), // (1<<11 1024) (66)
68 TmItem1 (ignoreTraditionalKanji_ja_JP), // 3. (1<<12 4096) (10)
69 TmItem1 (ignoreTraditionalKana_ja_JP), // 4. (1<<13 8192) (11)
70 TmItem1 (ignoreMinusSign_ja_JP), // 5. (1<<13 16384) (12)
71 TmItem1 (ignoreIterationMark_ja_JP), // 6. (1<<14 32768) (13)
72 TmItem1 (ignoreSeparator_ja_JP), // 7. (1<<15 65536) (14)
73 TmItem1 (ignoreSize_ja_JP), // 15. (1<<23 16777216) (22)
74 TmItem1 (ignoreMiddleDot_ja_JP), // 17. (1<<25 67108864) (24)
75 TmItem1 (ignoreSpace_ja_JP), // 18. (1<<26 134217728) (25)
76 TmItem1 (ignoreZiZu_ja_JP), // 8. (1<<16 131072) (15)
77 TmItem1 (ignoreBaFa_ja_JP), // 9. (1<<17 262144) (16)
78 TmItem1 (ignoreTiJi_ja_JP), // 10. (1<<18 524288) (17)
79 TmItem1 (ignoreHyuByu_ja_JP), // 11. (1<<19 1048576) (18)
80 TmItem1 (ignoreSeZe_ja_JP), // 12. (1<<20 2097152) (19)
81 TmItem1 (ignoreIandEfollowedByYa_ja_JP), // 13. (1<<21 4194304) (20)
82 TmItem1 (ignoreKiKuFollowedBySa_ja_JP), // 14. (1<<22 8388608) (21)
83 TmItem1 (ignoreProlongedSoundMark_ja_JP), // 16. (1<<24 33554432) (23)
85 TmItem1 (UPPERCASE_LOWERCASE), // 19. (1) (1)
86 TmItem1 (LOWERCASE_UPPERCASE), // 20. (2) (2)
87 TmItem1 (HALFWIDTH_FULLWIDTH), // 21. (3) (3)
88 TmItem1 (FULLWIDTH_HALFWIDTH), // 22. (4) (4)
89 TmItem1 (KATAKANA_HIRAGANA), // 23. (5) (5)
90 TmItem1 (HIRAGANA_KATAKANA), // 24. (6) (6)
92 TmItem1 (smallToLarge_ja_JP), // 25. (1<<27 268435456) (26)
93 TmItem1 (largeToSmall_ja_JP), // 26. (1<<28 536870912) (27)
94 TmItem2 (NumToTextLower_zh_CN), // 27. () (28)
95 TmItem2 (NumToTextUpper_zh_CN), // 28. () (29)
96 TmItem2 (NumToTextLower_zh_TW), // 29. () (30)
97 TmItem2 (NumToTextUpper_zh_TW), // 30. () (31)
98 TmItem2 (NumToTextFormalHangul_ko), // 31. () (32)
99 TmItem2 (NumToTextFormalLower_ko), // 32. () (33)
100 TmItem2 (NumToTextFormalUpper_ko), // 33. () (34)
101 TmItem2 (NumToTextInformalHangul_ko), // 34. () (35)
102 TmItem2 (NumToTextInformalLower_ko), // 35. () (36)
103 TmItem2 (NumToTextInformalUpper_ko), // 36. () (37)
104 TmItem2 (NumToCharLower_zh_CN), // 37. () (38)
105 TmItem2 (NumToCharUpper_zh_CN), // 38. () (39)
106 TmItem2 (NumToCharLower_zh_TW), // 39. () (40)
107 TmItem2 (NumToCharUpper_zh_TW), // 40. () (41)
108 TmItem2 (NumToCharHangul_ko), // 41. () (42)
109 TmItem2 (NumToCharLower_ko), // 42. () (43)
110 TmItem2 (NumToCharUpper_ko), // 43. () (44)
111 TmItem2 (NumToCharFullwidth), // 44. () (45)
112 TmItem2 (NumToCharKanjiShort_ja_JP), // 45. () (46)
113 TmItem2 (TextToNumLower_zh_CN), // 46. () (47)
114 TmItem2 (TextToNumUpper_zh_CN), // 47. () (48)
115 TmItem2 (TextToNumLower_zh_TW), // 48. () (49)
116 TmItem2 (TextToNumUpper_zh_TW), // 49. () (50)
117 TmItem2 (TextToNumFormalHangul_ko), // 50. () (51)
118 TmItem2 (TextToNumFormalLower_ko), // 51. () (52)
119 TmItem2 (TextToNumFormalUpper_ko), // 52. () (53)
120 TmItem2 (TextToNumInformalHangul_ko), // 53. () (54)
121 TmItem2 (TextToNumInformalLower_ko), // 54. () (55)
122 TmItem2 (TextToNumInformalUpper_ko), // 55. () (56)
124 TmItem2 (CharToNumLower_zh_CN), // 56. () (59)
125 TmItem2 (CharToNumUpper_zh_CN), // 57. () (60)
126 TmItem2 (CharToNumLower_zh_TW), // 58. () (61)
127 TmItem2 (CharToNumUpper_zh_TW), // 59. () (62)
128 TmItem2 (CharToNumHangul_ko), // 60. () (63)
129 TmItem2 (CharToNumLower_ko), // 61. () (64)
130 TmItem2 (CharToNumUpper_ko), // 62. () (65)
132 // no enum defined for these trans. application has to use impl name to load them
133 // TmItem2 (NumToCharArabic_Indic), // () (67)
134 // TmItem2 (NumToCharEstern_Arabic_Indic),// () (68)
135 // TmItem2 (NumToCharIndic), // () (69)
136 // TmItem2 (NumToCharThai), // () (70)
137 {(TransliterationModules)0, (TransliterationModulesNew)0, NULL}
140 // Constructor/Destructor
141 TransliterationImpl::TransliterationImpl(const Reference <XComponentContext>& xContext) : mxContext(xContext)
143 numCascade = 0;
144 caseignoreOnly = true;
146 mxLocaledata.set(LocaleData::create(xContext));
149 TransliterationImpl::~TransliterationImpl()
151 mxLocaledata.clear();
152 clear();
156 // Methods
157 OUString SAL_CALL
158 TransliterationImpl::getName() throw(RuntimeException, std::exception)
160 if (numCascade == 1 && bodyCascade[0].is())
161 return bodyCascade[0]->getName();
162 if (numCascade < 1)
163 return ( OUString("Not Loaded"));
164 throw ERROR;
167 sal_Int16 SAL_CALL
168 TransliterationImpl::getType() throw(RuntimeException, std::exception)
170 if (numCascade > 1)
171 return (TransliterationType::CASCADE|TransliterationType::IGNORE);
172 if (numCascade > 0 && bodyCascade[0].is())
173 return(bodyCascade[0]->getType());
174 throw ERROR;
177 void SAL_CALL
178 TransliterationImpl::loadModule( TransliterationModules modType, const Locale& rLocale )
179 throw(RuntimeException, std::exception)
181 clear();
182 if (modType&TransliterationModules_IGNORE_MASK && modType&TransliterationModules_NON_IGNORE_MASK) {
183 throw ERROR;
184 } else if (modType&TransliterationModules_IGNORE_MASK) {
185 #define TransliterationModules_IGNORE_CASE_MASK (TransliterationModules_IGNORE_CASE | \
186 TransliterationModules_IGNORE_WIDTH | \
187 TransliterationModules_IGNORE_KANA)
188 sal_Int32 mask = ((modType&TransliterationModules_IGNORE_CASE_MASK) == modType) ?
189 TransliterationModules_IGNORE_CASE_MASK : TransliterationModules_IGNORE_MASK;
190 for (sal_Int16 i = 0; TMlist[i].tm & mask; i++) {
191 if (modType & TMlist[i].tm)
192 if (loadModuleByName(OUString::createFromAscii(TMlist[i].implName),
193 bodyCascade[numCascade], rLocale))
194 numCascade++;
196 // additional transliterations from TranslationModuleExtra (we cannot extend TransliterationModule)
197 if (modType & TransliterationModulesExtra::IGNORE_DIACRITICS_CTL)
199 if (loadModuleByName(OUString("ignoreDiacritics_CTL"), bodyCascade[numCascade], rLocale))
200 numCascade++;
202 if (modType & TransliterationModulesExtra::IGNORE_KASHIDA_CTL)
203 if (loadModuleByName(OUString("ignoreKashida_CTL"), bodyCascade[numCascade], rLocale))
204 numCascade++;
206 } else if (modType&TransliterationModules_NON_IGNORE_MASK) {
207 for (sal_Int16 i = 0; TMlist[i].tm; i++) {
208 if (TMlist[i].tm == modType) {
209 if (loadModuleByName(OUString::createFromAscii(TMlist[i].implName), bodyCascade[numCascade], rLocale))
210 numCascade++;
211 break;
217 void SAL_CALL
218 TransliterationImpl::loadModuleNew( const Sequence < TransliterationModulesNew > & modType, const Locale& rLocale )
219 throw(RuntimeException, std::exception)
221 clear();
222 sal_Int32 mask = 0, count = modType.getLength();
223 if (count > maxCascade)
224 throw ERROR; // could not handle more than maxCascade
225 for (sal_Int16 i = 0; i < count; i++) {
226 for (sal_Int16 j = 0; TMlist[j].tmn; j++) {
227 if (TMlist[j].tmn == modType[i]) {
228 if (mask == 0)
229 mask = TMlist[i].tm && (TMlist[i].tm&TransliterationModules_IGNORE_MASK) ?
230 TransliterationModules_IGNORE_MASK : TransliterationModules_NON_IGNORE_MASK;
231 else if (mask == (sal_Int32) TransliterationModules_IGNORE_MASK &&
232 (TMlist[i].tm&TransliterationModules_IGNORE_MASK) == 0)
233 throw ERROR; // could not mess up ignore trans. with non_ignore trans.
234 if (loadModuleByName(OUString::createFromAscii(TMlist[j].implName), bodyCascade[numCascade], rLocale))
235 numCascade++;
236 break;
242 void SAL_CALL
243 TransliterationImpl::loadModuleByImplName(const OUString& implName, const Locale& rLocale)
244 throw(RuntimeException, std::exception)
246 clear();
247 if (loadModuleByName(implName, bodyCascade[numCascade], rLocale))
248 numCascade++;
252 void SAL_CALL
253 TransliterationImpl::loadModulesByImplNames(const Sequence< OUString >& implNameList, const Locale& rLocale ) throw(RuntimeException, std::exception)
255 if (implNameList.getLength() > maxCascade || implNameList.getLength() <= 0)
256 throw ERROR;
258 clear();
259 for (sal_Int32 i = 0; i < implNameList.getLength(); i++)
260 if (loadModuleByName(implNameList[i], bodyCascade[numCascade], rLocale))
261 numCascade++;
265 Sequence<OUString> SAL_CALL
266 TransliterationImpl::getAvailableModules( const Locale& rLocale, sal_Int16 sType ) throw(RuntimeException, std::exception)
268 const Sequence<OUString> &translist = mxLocaledata->getTransliterations(rLocale);
269 Sequence<OUString> r(translist.getLength());
270 Reference<XExtendedTransliteration> body;
271 sal_Int32 n = 0;
272 for (sal_Int32 i = 0; i < translist.getLength(); i++)
274 if (loadModuleByName(translist[i], body, rLocale)) {
275 if (body->getType() & sType)
276 r[n++] = translist[i];
277 body.clear();
280 r.realloc(n);
281 return (r);
285 OUString SAL_CALL
286 TransliterationImpl::transliterate( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
287 Sequence< sal_Int32 >& offset ) throw(RuntimeException, std::exception)
289 if (numCascade == 0)
290 return inStr;
292 if (offset.getLength() != nCount)
293 offset.realloc(nCount);
294 if (numCascade == 1)
296 if ( startPos == 0 && nCount == inStr.getLength() )
297 return bodyCascade[0]->transliterate( inStr, 0, nCount, offset);
298 else
300 OUString tmpStr = inStr.copy(startPos, nCount);
301 tmpStr = bodyCascade[0]->transliterate(tmpStr, 0, nCount, offset);
302 if ( startPos )
304 sal_Int32 * pArr = offset.getArray();
305 nCount = offset.getLength();
306 for (sal_Int32 j = 0; j < nCount; j++)
307 pArr[j] += startPos;
309 return tmpStr;
312 else
314 OUString tmpStr = inStr.copy(startPos, nCount);
315 sal_Int32 * pArr = offset.getArray();
316 for (sal_Int32 j = 0; j < nCount; j++)
317 pArr[j] = startPos + j;
319 sal_Int16 from = 0, to = 1, tmp;
320 Sequence<sal_Int32> off[2];
322 off[to] = offset;
323 off[from].realloc(nCount);
324 for (sal_Int32 i = 0; i < numCascade; i++) {
325 tmpStr = bodyCascade[i]->transliterate(tmpStr, 0, nCount, off[from]);
327 nCount = tmpStr.getLength();
329 assert(off[from].getLength() == nCount);
330 tmp = from; from = to; to = tmp;
331 // tdf#89665: don't use operator[] to write - too slow!
332 // interestingly gcc 4.9 -Os won't even inline the const operator[]
333 sal_Int32 const*const pFrom(off[from].getConstArray());
334 sal_Int32 *const pTo(off[to].getArray());
335 for (sal_Int32 j = 0; j < nCount; j++)
337 assert(pTo[j] < off[from].getLength());
338 pTo[j] = pFrom[pTo[j]];
341 offset = off[to];
342 return tmpStr;
348 OUString SAL_CALL
349 TransliterationImpl::folding( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
350 Sequence< sal_Int32 >& offset ) throw(RuntimeException, std::exception)
352 if (numCascade == 0)
353 return inStr;
355 if (offset.getLength() != nCount)
356 offset.realloc(nCount);
357 if (numCascade == 1)
359 if ( startPos == 0 && nCount == inStr.getLength() )
360 return bodyCascade[0]->folding( inStr, 0, nCount, offset);
361 else
363 OUString tmpStr = inStr.copy(startPos, nCount);
364 tmpStr = bodyCascade[0]->folding(tmpStr, 0, nCount, offset);
365 if ( startPos )
367 sal_Int32 * pArr = offset.getArray();
368 nCount = offset.getLength();
369 for (sal_Int32 j = 0; j < nCount; j++)
370 pArr[j] += startPos;
372 return tmpStr;
375 else
377 OUString tmpStr = inStr.copy(startPos, nCount);
378 sal_Int32 * pArr = offset.getArray();
379 for (sal_Int32 j = 0; j < nCount; j++)
380 pArr[j] = startPos + j;
382 sal_Int16 from = 0, to = 1, tmp;
383 Sequence<sal_Int32> off[2];
385 off[to] = offset;
386 for (sal_Int32 i = 0; i < numCascade; i++) {
387 tmpStr = bodyCascade[i]->folding(tmpStr, 0, nCount, off[from]);
389 nCount = tmpStr.getLength();
391 tmp = from; from = to; to = tmp;
392 for (sal_Int32 j = 0; j < nCount; j++)
393 off[to][j] = off[from][off[to][j]];
395 offset = off[to];
396 return tmpStr;
400 OUString SAL_CALL
401 TransliterationImpl::transliterateString2String( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount ) throw(RuntimeException, std::exception)
403 if (numCascade == 0)
404 return inStr;
405 else if (numCascade == 1)
406 return bodyCascade[0]->transliterateString2String( inStr, startPos, nCount);
407 else {
408 OUString tmpStr = bodyCascade[0]->transliterateString2String(inStr, startPos, nCount);
410 for (sal_Int32 i = 1; i < numCascade; i++)
411 tmpStr = bodyCascade[i]->transliterateString2String(tmpStr, 0, tmpStr.getLength());
412 return tmpStr;
416 OUString SAL_CALL
417 TransliterationImpl::transliterateChar2String( sal_Unicode inChar ) throw(RuntimeException, std::exception)
419 if (numCascade == 0)
420 return OUString(&inChar, 1);
421 else if (numCascade == 1)
422 return bodyCascade[0]->transliterateChar2String( inChar);
423 else {
424 OUString tmpStr = bodyCascade[0]->transliterateChar2String(inChar);
426 for (sal_Int32 i = 1; i < numCascade; i++)
427 tmpStr = bodyCascade[i]->transliterateString2String(tmpStr, 0, tmpStr.getLength());
428 return tmpStr;
432 sal_Unicode SAL_CALL
433 TransliterationImpl::transliterateChar2Char( sal_Unicode inChar ) throw(MultipleCharsOutputException, RuntimeException, std::exception)
435 sal_Unicode tmpChar = inChar;
436 for (sal_Int32 i = 0; i < numCascade; i++)
437 tmpChar = bodyCascade[i]->transliterateChar2Char(tmpChar);
438 return tmpChar;
442 sal_Bool SAL_CALL
443 TransliterationImpl::equals(
444 const OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
445 const OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2)
446 throw(RuntimeException, std::exception)
448 // since this is an API function make it user fail safe
449 if ( nCount1 < 0 ) {
450 pos1 += nCount1;
451 nCount1 = -nCount1;
453 if ( nCount2 < 0 ) {
454 pos2 += nCount2;
455 nCount2 = -nCount2;
457 if ( !nCount1 || !nCount2 ||
458 pos1 >= str1.getLength() || pos2 >= str2.getLength() ||
459 pos1 < 0 || pos2 < 0 ) {
460 nMatch1 = nMatch2 = 0;
461 // two empty strings return true, else false
462 return !nCount1 && !nCount2 && pos1 == str1.getLength() && pos2 == str2.getLength();
464 if ( pos1 + nCount1 > str1.getLength() )
465 nCount1 = str1.getLength() - pos1;
466 if ( pos2 + nCount2 > str2.getLength() )
467 nCount2 = str2.getLength() - pos2;
469 if (caseignoreOnly && caseignore.is())
470 return caseignore->equals(str1, pos1, nCount1, nMatch1, str2, pos2, nCount2, nMatch2);
472 Sequence<sal_Int32> offset1, offset2;
474 OUString tmpStr1 = folding(str1, pos1, nCount1, offset1);
475 OUString tmpStr2 = folding(str2, pos2, nCount2, offset2);
476 // Length of offset1 and offset2 may still be 0 if there was no folding
477 // necessary!
479 const sal_Unicode *p1 = tmpStr1.getStr();
480 const sal_Unicode *p2 = tmpStr2.getStr();
481 sal_Int32 i, nLen = ::std::min( tmpStr1.getLength(), tmpStr2.getLength());
482 for (i = 0; i < nLen; ++i, ++p1, ++p2 ) {
483 if (*p1 != *p2) {
484 // return number of matched code points so far
485 nMatch1 = (i < offset1.getLength()) ? offset1[i] : i;
486 nMatch2 = (i < offset2.getLength()) ? offset2[i] : i;
487 return sal_False;
490 // i==nLen
491 if ( tmpStr1.getLength() != tmpStr2.getLength() ) {
492 // return number of matched code points so far
493 nMatch1 = (i <= offset1.getLength()) ? offset1[i-1] + 1 : i;
494 nMatch2 = (i <= offset2.getLength()) ? offset2[i-1] + 1 : i;
495 return sal_False;
496 } else {
497 nMatch1 = nCount1;
498 nMatch2 = nCount2;
499 return sal_True;
503 #define MaxOutput 2
505 Sequence< OUString > SAL_CALL
506 TransliterationImpl::getRange(const Sequence< OUString > &inStrs,
507 const sal_Int32 length, sal_Int16 _numCascade) throw(RuntimeException)
509 if (_numCascade >= numCascade || ! bodyCascade[_numCascade].is())
510 return inStrs;
512 sal_Int32 j_tmp = 0;
513 Sequence< OUString > ostr(MaxOutput*length);
514 for (sal_Int32 j = 0; j < length; j+=2) {
515 const Sequence< OUString >& temp = bodyCascade[_numCascade]->transliterateRange(inStrs[j], inStrs[j+1]);
517 for ( sal_Int32 k = 0; k < temp.getLength(); k++) {
518 if ( j_tmp >= MaxOutput*length ) throw ERROR;
519 ostr[j_tmp++] = temp[k];
522 ostr.realloc(j_tmp);
524 return this->getRange(ostr, j_tmp, ++_numCascade);
528 Sequence< OUString > SAL_CALL
529 TransliterationImpl::transliterateRange( const OUString& str1, const OUString& str2 )
530 throw(RuntimeException, std::exception)
532 if (numCascade == 1)
533 return bodyCascade[0]->transliterateRange(str1, str2);
535 Sequence< OUString > ostr(2);
536 ostr[0] = str1;
537 ostr[1] = str2;
539 return this->getRange(ostr, 2, 0);
543 sal_Int32 SAL_CALL
544 TransliterationImpl::compareSubstring(
545 const OUString& str1, sal_Int32 off1, sal_Int32 len1,
546 const OUString& str2, sal_Int32 off2, sal_Int32 len2)
547 throw(RuntimeException, std::exception)
549 if (caseignoreOnly && caseignore.is())
550 return caseignore->compareSubstring(str1, off1, len1, str2, off2, len2);
552 Sequence <sal_Int32> offset;
554 OUString in_str1 = this->transliterate(str1, off1, len1, offset);
555 OUString in_str2 = this->transliterate(str2, off2, len2, offset);
556 const sal_Unicode* unistr1 = in_str1.getStr();
557 const sal_Unicode* unistr2 = in_str2.getStr();
558 sal_Int32 strlen1 = in_str1.getLength();
559 sal_Int32 strlen2 = in_str2.getLength();
561 while (strlen1 && strlen2) {
562 if (*unistr1 != *unistr2)
563 return *unistr1 > *unistr2 ? 1 : -1;
565 unistr1++; unistr2++; strlen1--; strlen2--;
567 return strlen1 == strlen2 ? 0 : (strlen1 > strlen2 ? 1 : -1);
571 sal_Int32 SAL_CALL
572 TransliterationImpl::compareString(const OUString& str1, const OUString& str2 ) throw (RuntimeException, std::exception)
574 if (caseignoreOnly && caseignore.is())
575 return caseignore->compareString(str1, str2);
576 else
577 return this->compareSubstring(str1, 0, str1.getLength(), str2, 0, str2.getLength());
581 void
582 TransliterationImpl::clear()
584 for (sal_Int32 i = 0; i < numCascade; i++)
585 if (bodyCascade[i].is())
586 bodyCascade[i].clear();
587 numCascade = 0;
588 caseignore.clear();
589 caseignoreOnly = true;
592 namespace
594 /** structure to cache the last transliteration body used. */
595 struct TransBody
597 OUString Name;
598 ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XExtendedTransliteration > Body;
600 class theTransBodyMutex : public rtl::Static<osl::Mutex, theTransBodyMutex> {};
603 void TransliterationImpl::loadBody( OUString &implName, Reference<XExtendedTransliteration>& body )
604 throw (RuntimeException)
606 assert(!implName.isEmpty());
607 ::osl::MutexGuard guard(theTransBodyMutex::get());
608 static TransBody lastTransBody;
609 if (implName != lastTransBody.Name)
611 lastTransBody.Body.set(
612 mxContext->getServiceManager()->createInstanceWithContext(implName, mxContext), UNO_QUERY_THROW);
613 lastTransBody.Name = implName;
615 body = lastTransBody.Body;
618 bool SAL_CALL
619 TransliterationImpl::loadModuleByName( const OUString& implName,
620 Reference<XExtendedTransliteration>& body, const Locale& rLocale) throw(RuntimeException)
622 OUString cname = OUString(TRLT_IMPLNAME_PREFIX) + implName;
623 loadBody(cname, body);
624 if (body.is()) {
625 body->loadModule((TransliterationModules)0, rLocale); // toUpper/toLoad need rLocale
627 // if the module is ignore case/kana/width, load caseignore for equals/compareString mothed
628 for (sal_Int16 i = 0; i < 3; i++) {
629 if (implName.equalsAscii(TMlist[i].implName)) {
630 if (i == 0) // current module is caseignore
631 body->loadModule(TMlist[0].tm, rLocale); // caseingore need to setup module name
632 if (! caseignore.is()) {
633 OUString bname = OUString(TRLT_IMPLNAME_PREFIX) +
634 OUString::createFromAscii(TMlist[0].implName);
635 loadBody(bname, caseignore);
637 if (caseignore.is())
638 caseignore->loadModule(TMlist[i].tm, rLocale);
639 return true;
642 caseignoreOnly = false; // has other module than just ignore case/kana/width
644 return body.is();
647 OUString SAL_CALL
648 TransliterationImpl::getImplementationName() throw( RuntimeException, std::exception )
650 return OUString("com.sun.star.i18n.Transliteration");
653 sal_Bool SAL_CALL
654 TransliterationImpl::supportsService(const OUString& rServiceName) throw( RuntimeException, std::exception )
656 return cppu::supportsService(this, rServiceName);
659 Sequence< OUString > SAL_CALL
660 TransliterationImpl::getSupportedServiceNames(void) throw( RuntimeException, std::exception )
662 Sequence< OUString > aRet(1);
663 aRet[0] = OUString("com.sun.star.i18n.Transliteration");
664 return aRet;
667 } } } }
669 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
670 com_sun_star_i18n_Transliteration_get_implementation(
671 css::uno::XComponentContext *context,
672 css::uno::Sequence<css::uno::Any> const &)
674 return cppu::acquire(new com::sun::star::i18n::TransliterationImpl(context));
677 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */