1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_i18npool.hxx"
26
27 #include "transliterationImpl.hxx"
28 #include "servicename.hxx"
29
30 #include <com/sun/star/i18n/TransliterationType.hpp>
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
33 #include <com/sun/star/container/XEnumeration.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
36
37 #include <comphelper/processfactory.hxx>
38 #include <rtl/string.h>
39 #include <rtl/ustring.hxx>
40 #include <rtl/ustrbuf.hxx>
41
42 #include <algorithm>
43
44 #if OSL_DEBUG_LEVEL > 1
45 #include <stdio.h>
46 #endif
47
48 using namespace com::sun::star::uno;
49 using namespace com::sun::star::lang;
50 using namespace rtl;
51 using namespace com::sun::star::container;
52
53 namespace com { namespace sun { namespace star { namespace i18n {
54
55 #define ERROR RuntimeException()
56
57 #define TmItem1( name ) \
58 {TransliterationModules_##name, TransliterationModulesNew_##name, #name}
59
60 #define TmItem2( name ) \
61 {(TransliterationModules)0, TransliterationModulesNew_##name, #name}
62
63 // Ignore Module list
64 static struct TMlist {
65 TransliterationModules tm;
66 TransliterationModulesNew tmn;
67 const sal_Char *implName;
68 } TMlist[] = { // Modules ModulesNew
69 TmItem1 (IGNORE_CASE), // 0. (1<<8 256) (7)
70 TmItem1 (IGNORE_WIDTH), // 1. (1<<9 512) (8)
71 TmItem1 (IGNORE_KANA), // 2. (1<<10 1024) (9)
72 // No enum define for this trans. application has to use impl name to load it
73 // TmItem1 (IGNORE_CASE_SIMPLE), // (1<<11 1024) (66)
74
75 TmItem1 (ignoreTraditionalKanji_ja_JP), // 3. (1<<12 4096) (10)
76 TmItem1 (ignoreTraditionalKana_ja_JP), // 4. (1<<13 8192) (11)
77 TmItem1 (ignoreMinusSign_ja_JP), // 5. (1<<13 16384) (12)
78 TmItem1 (ignoreIterationMark_ja_JP), // 6. (1<<14 32768) (13)
79 TmItem1 (ignoreSeparator_ja_JP), // 7. (1<<15 65536) (14)
80 TmItem1 (ignoreSize_ja_JP), // 15. (1<<23 16777216) (22)
81 TmItem1 (ignoreMiddleDot_ja_JP), // 17. (1<<25 67108864) (24)
82 TmItem1 (ignoreSpace_ja_JP), // 18. (1<<26 134217728) (25)
83 TmItem1 (ignoreZiZu_ja_JP), // 8. (1<<16 131072) (15)
84 TmItem1 (ignoreBaFa_ja_JP), // 9. (1<<17 262144) (16)
85 TmItem1 (ignoreTiJi_ja_JP), // 10. (1<<18 524288) (17)
86 TmItem1 (ignoreHyuByu_ja_JP), // 11. (1<<19 1048576) (18)
87 TmItem1 (ignoreSeZe_ja_JP), // 12. (1<<20 2097152) (19)
88 TmItem1 (ignoreIandEfollowedByYa_ja_JP), // 13. (1<<21 4194304) (20)
89 TmItem1 (ignoreKiKuFollowedBySa_ja_JP), // 14. (1<<22 8388608) (21)
90 TmItem1 (ignoreProlongedSoundMark_ja_JP), // 16. (1<<24 33554432) (23)
91
92 TmItem1 (UPPERCASE_LOWERCASE), // 19. (1) (1)
93 TmItem1 (LOWERCASE_UPPERCASE), // 20. (2) (2)
94 TmItem1 (HALFWIDTH_FULLWIDTH), // 21. (3) (3)
95 TmItem1 (FULLWIDTH_HALFWIDTH), // 22. (4) (4)
96 TmItem1 (KATAKANA_HIRAGANA), // 23. (5) (5)
97 TmItem1 (HIRAGANA_KATAKANA), // 24. (6) (6)
98
99 TmItem1 (smallToLarge_ja_JP), // 25. (1<<27 268435456) (26)
100 TmItem1 (largeToSmall_ja_JP), // 26. (1<<28 536870912) (27)
101 TmItem2 (NumToTextLower_zh_CN), // 27. () (28)
102 TmItem2 (NumToTextUpper_zh_CN), // 28. () (29)
103 TmItem2 (NumToTextLower_zh_TW), // 29. () (30)
104 TmItem2 (NumToTextUpper_zh_TW), // 30. () (31)
105 TmItem2 (NumToTextFormalHangul_ko), // 31. () (32)
106 TmItem2 (NumToTextFormalLower_ko), // 32. () (33)
107 TmItem2 (NumToTextFormalUpper_ko), // 33. () (34)
108 TmItem2 (NumToTextInformalHangul_ko), // 34. () (35)
109 TmItem2 (NumToTextInformalLower_ko), // 35. () (36)
110 TmItem2 (NumToTextInformalUpper_ko), // 36. () (37)
111 TmItem2 (NumToCharLower_zh_CN), // 37. () (38)
112 TmItem2 (NumToCharUpper_zh_CN), // 38. () (39)
113 TmItem2 (NumToCharLower_zh_TW), // 39. () (40)
114 TmItem2 (NumToCharUpper_zh_TW), // 40. () (41)
115 TmItem2 (NumToCharHangul_ko), // 41. () (42)
116 TmItem2 (NumToCharLower_ko), // 42. () (43)
117 TmItem2 (NumToCharUpper_ko), // 43. () (44)
118 TmItem2 (NumToCharFullwidth), // 44. () (45)
119 TmItem2 (NumToCharKanjiShort_ja_JP), // 45. () (46)
120 TmItem2 (TextToNumLower_zh_CN), // 46. () (47)
121 TmItem2 (TextToNumUpper_zh_CN), // 47. () (48)
122 TmItem2 (TextToNumLower_zh_TW), // 48. () (49)
123 TmItem2 (TextToNumUpper_zh_TW), // 49. () (50)
124 TmItem2 (TextToNumFormalHangul_ko), // 50. () (51)
125 TmItem2 (TextToNumFormalLower_ko), // 51. () (52)
126 TmItem2 (TextToNumFormalUpper_ko), // 52. () (53)
127 TmItem2 (TextToNumInformalHangul_ko), // 53. () (54)
128 TmItem2 (TextToNumInformalLower_ko), // 54. () (55)
129 TmItem2 (TextToNumInformalUpper_ko), // 55. () (56)
130
131 TmItem2 (CharToNumLower_zh_CN), // 56. () (59)
132 TmItem2 (CharToNumUpper_zh_CN), // 57. () (60)
133 TmItem2 (CharToNumLower_zh_TW), // 58. () (61)
134 TmItem2 (CharToNumUpper_zh_TW), // 59. () (62)
135 TmItem2 (CharToNumHangul_ko), // 60. () (63)
136 TmItem2 (CharToNumLower_ko), // 61. () (64)
137 TmItem2 (CharToNumUpper_ko), // 62. () (65)
138
139 // no enum defined for these trans. application has to use impl name to load them
140 // TmItem2 (NumToCharArabic_Indic), // () (67)
141 // TmItem2 (NumToCharEstern_Arabic_Indic),// () (68)
142 // TmItem2 (NumToCharIndic), // () (69)
143 // TmItem2 (NumToCharThai), // () (70)
144 {(TransliterationModules)0, (TransliterationModulesNew)0, NULL}
145 };
146
147 // Constructor/Destructor
TransliterationImpl(const Reference<XMultiServiceFactory> & xMSF)148 TransliterationImpl::TransliterationImpl(const Reference <XMultiServiceFactory>& xMSF) : xSMgr(xMSF)
149 {
150 numCascade = 0;
151 caseignoreOnly = sal_True;
152
153 if ( xMSF.is() )
154 {
155 Reference < XInterface > xI=
156 xMSF->createInstance(OUString::createFromAscii("com.sun.star.i18n.LocaleData"));
157 if ( xI.is() ) {
158 Any x = xI->queryInterface( ::getCppuType( (const uno::Reference< i18n::XLocaleData >*)0) );
159 x >>= localedata;
160 }
161 }
162 }
163
~TransliterationImpl()164 TransliterationImpl::~TransliterationImpl()
165 {
166 localedata.clear();
167 clear();
168 }
169
170
171 // Methods
172 OUString SAL_CALL
getName()173 TransliterationImpl::getName()
174 {
175 if (numCascade == 1 && bodyCascade[0].is())
176 return bodyCascade[0]->getName();
177 if (numCascade < 1)
178 return ( OUString::createFromAscii("Not Loaded"));
179 throw ERROR;
180 }
181
182 sal_Int16 SAL_CALL
getType()183 TransliterationImpl::getType()
184 {
185 if (numCascade > 1)
186 return (TransliterationType::CASCADE|TransliterationType::IGNORE);
187 if (numCascade > 0 && bodyCascade[0].is())
188 return(bodyCascade[0]->getType());
189 throw ERROR;
190 }
191
192 void SAL_CALL
loadModule(TransliterationModules modType,const Locale & rLocale)193 TransliterationImpl::loadModule( TransliterationModules modType, const Locale& rLocale )
194 {
195 clear();
196 if (modType&TransliterationModules_IGNORE_MASK && modType&TransliterationModules_NON_IGNORE_MASK) {
197 throw ERROR;
198 } else if (modType&TransliterationModules_IGNORE_MASK) {
199 #define TransliterationModules_IGNORE_CASE_MASK (TransliterationModules_IGNORE_CASE | \
200 TransliterationModules_IGNORE_WIDTH | \
201 TransliterationModules_IGNORE_KANA)
202 sal_Int32 mask = ((modType&TransliterationModules_IGNORE_CASE_MASK) == modType) ?
203 TransliterationModules_IGNORE_CASE_MASK : TransliterationModules_IGNORE_MASK;
204 for (sal_Int16 i = 0; TMlist[i].tm & mask; i++) {
205 if (modType & TMlist[i].tm)
206 if (loadModuleByName(OUString::createFromAscii(TMlist[i].implName),
207 bodyCascade[numCascade], rLocale))
208 numCascade++;
209 }
210 } else if (modType&TransliterationModules_NON_IGNORE_MASK) {
211 for (sal_Int16 i = 0; TMlist[i].tm; i++) {
212 if (TMlist[i].tm == modType) {
213 if (loadModuleByName(OUString::createFromAscii(TMlist[i].implName), bodyCascade[numCascade], rLocale))
214 numCascade++;
215 break;
216 }
217 }
218 }
219 }
220
221 void SAL_CALL
loadModuleNew(const Sequence<TransliterationModulesNew> & modType,const Locale & rLocale)222 TransliterationImpl::loadModuleNew( const Sequence < TransliterationModulesNew > & modType, const Locale& rLocale )
223 {
224 clear();
225 sal_Int32 mask = 0, count = modType.getLength();
226 if (count > maxCascade)
227 throw ERROR; // could not handle more than maxCascade
228 for (sal_Int16 i = 0; i < count; i++) {
229 for (sal_Int16 j = 0; TMlist[j].tmn; j++) {
230 if (TMlist[j].tmn == modType[i]) {
231 if (mask == 0)
232 mask = TMlist[i].tm && (TMlist[i].tm&TransliterationModules_IGNORE_MASK) ?
233 TransliterationModules_IGNORE_MASK : TransliterationModules_NON_IGNORE_MASK;
234 else if (mask == TransliterationModules_IGNORE_MASK &&
235 (TMlist[i].tm&TransliterationModules_IGNORE_MASK) == 0)
236 throw ERROR; // could not mess up ignore trans. with non_ignore trans.
237 if (loadModuleByName(OUString::createFromAscii(TMlist[j].implName), bodyCascade[numCascade], rLocale))
238 numCascade++;
239 break;
240 }
241 }
242 }
243 }
244
245 void SAL_CALL
loadModuleByImplName(const OUString & implName,const Locale & rLocale)246 TransliterationImpl::loadModuleByImplName(const OUString& implName, const Locale& rLocale)
247 {
248 clear();
249 if (loadModuleByName(implName, bodyCascade[numCascade], rLocale))
250 numCascade++;
251 }
252
253
254 void SAL_CALL
loadModulesByImplNames(const Sequence<OUString> & implNameList,const Locale & rLocale)255 TransliterationImpl::loadModulesByImplNames(const Sequence< OUString >& implNameList, const Locale& rLocale )
256 {
257 if (implNameList.getLength() > maxCascade || implNameList.getLength() <= 0)
258 throw ERROR;
259
260 clear();
261 for (sal_Int32 i = 0; i < implNameList.getLength(); i++)
262 if (loadModuleByName(implNameList[i], bodyCascade[numCascade], rLocale))
263 numCascade++;
264 }
265
266
267 Sequence<OUString> SAL_CALL
getAvailableModules(const Locale & rLocale,sal_Int16 sType)268 TransliterationImpl::getAvailableModules( const Locale& rLocale, sal_Int16 sType )
269 {
270 const Sequence<OUString> &translist = localedata->getTransliterations(rLocale);
271 Sequence<OUString> r(translist.getLength());
272 Reference<XExtendedTransliteration> body;
273 sal_Int32 n = 0;
274 for (sal_Int32 i = 0; i < translist.getLength(); i++)
275 {
276 if (loadModuleByName(translist[i], body, rLocale)) {
277 if (body->getType() & sType)
278 r[n++] = translist[i];
279 body.clear();
280 }
281 }
282 r.realloc(n);
283 return (r);
284 }
285
286
287 OUString SAL_CALL
transliterate(const OUString & inStr,sal_Int32 startPos,sal_Int32 nCount,Sequence<sal_Int32> & offset)288 TransliterationImpl::transliterate( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
289 Sequence< sal_Int32 >& offset )
290 {
291 if (numCascade == 0)
292 return inStr;
293
294 if (offset.getLength() != nCount)
295 offset.realloc(nCount);
296 if (numCascade == 1)
297 {
298 if ( startPos == 0 && nCount == inStr.getLength() )
299 return bodyCascade[0]->transliterate( inStr, 0, nCount, offset);
300 else
301 {
302 OUString tmpStr = inStr.copy(startPos, nCount);
303 tmpStr = bodyCascade[0]->transliterate(tmpStr, 0, nCount, offset);
304 if ( startPos )
305 {
306 sal_Int32 * pArr = offset.getArray();
307 nCount = offset.getLength();
308 for (sal_Int32 j = 0; j < nCount; j++)
309 pArr[j] += startPos;
310 }
311 return tmpStr;
312 }
313 }
314 else
315 {
316 OUString tmpStr = inStr.copy(startPos, nCount);
317 sal_Int32 * pArr = offset.getArray();
318 for (sal_Int32 j = 0; j < nCount; j++)
319 pArr[j] = startPos + j;
320
321 sal_Int16 from = 0, to = 1, tmp;
322 Sequence<sal_Int32> off[2];
323
324 off[to] = offset;
325 off[from].realloc(nCount);
326 for (sal_Int32 i = 0; i < numCascade; i++) {
327 tmpStr = bodyCascade[i]->transliterate(tmpStr, 0, nCount, off[from]);
328
329 nCount = tmpStr.getLength();
330
331 tmp = from; from = to; to = tmp;
332 for (sal_Int32 j = 0; j < nCount; j++)
333 off[to][j] = off[from][off[to][j]];
334 }
335 offset = off[to];
336 return tmpStr;
337 }
338 }
339
340
341 //
342 OUString SAL_CALL
folding(const OUString & inStr,sal_Int32 startPos,sal_Int32 nCount,Sequence<sal_Int32> & offset)343 TransliterationImpl::folding( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
344 Sequence< sal_Int32 >& offset )
345 {
346 if (numCascade == 0)
347 return inStr;
348
349 if (offset.getLength() != nCount)
350 offset.realloc(nCount);
351 if (numCascade == 1)
352 {
353 if ( startPos == 0 && nCount == inStr.getLength() )
354 return bodyCascade[0]->folding( inStr, 0, nCount, offset);
355 else
356 {
357 OUString tmpStr = inStr.copy(startPos, nCount);
358 tmpStr = bodyCascade[0]->folding(tmpStr, 0, nCount, offset);
359 if ( startPos )
360 {
361 sal_Int32 * pArr = offset.getArray();
362 nCount = offset.getLength();
363 for (sal_Int32 j = 0; j < nCount; j++)
364 pArr[j] += startPos;
365 }
366 return tmpStr;
367 }
368 }
369 else
370 {
371 OUString tmpStr = inStr.copy(startPos, nCount);
372 sal_Int32 * pArr = offset.getArray();
373 for (sal_Int32 j = 0; j < nCount; j++)
374 pArr[j] = startPos + j;
375
376 sal_Int16 from = 0, to = 1, tmp;
377 Sequence<sal_Int32> off[2];
378
379 off[to] = offset;
380 for (sal_Int32 i = 0; i < numCascade; i++) {
381 tmpStr = bodyCascade[i]->folding(tmpStr, 0, nCount, off[from]);
382
383 nCount = tmpStr.getLength();
384
385 tmp = from; from = to; to = tmp;
386 for (sal_Int32 j = 0; j < nCount; j++)
387 off[to][j] = off[from][off[to][j]];
388 }
389 offset = off[to];
390 return tmpStr;
391 }
392 }
393
394 OUString SAL_CALL
transliterateString2String(const OUString & inStr,sal_Int32 startPos,sal_Int32 nCount)395 TransliterationImpl::transliterateString2String( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount )
396 {
397 if (numCascade == 0)
398 return inStr;
399 else if (numCascade == 1)
400 return bodyCascade[0]->transliterateString2String( inStr, startPos, nCount);
401 else {
402 OUString tmpStr = bodyCascade[0]->transliterateString2String(inStr, startPos, nCount);
403
404 for (sal_Int32 i = 1; i < numCascade; i++)
405 tmpStr = bodyCascade[i]->transliterateString2String(tmpStr, 0, tmpStr.getLength());
406 return tmpStr;
407 }
408 }
409
410 OUString SAL_CALL
transliterateChar2String(sal_Unicode inChar)411 TransliterationImpl::transliterateChar2String( sal_Unicode inChar )
412 {
413 if (numCascade == 0)
414 return OUString(&inChar, 1);
415 else if (numCascade == 1)
416 return bodyCascade[0]->transliterateChar2String( inChar);
417 else {
418 OUString tmpStr = bodyCascade[0]->transliterateChar2String(inChar);
419
420 for (sal_Int32 i = 1; i < numCascade; i++)
421 tmpStr = bodyCascade[i]->transliterateString2String(tmpStr, 0, tmpStr.getLength());
422 return tmpStr;
423 }
424 }
425
426 sal_Unicode SAL_CALL
transliterateChar2Char(sal_Unicode inChar)427 TransliterationImpl::transliterateChar2Char( sal_Unicode inChar )
428 {
429 sal_Unicode tmpChar = inChar;
430 for (sal_Int32 i = 0; i < numCascade; i++)
431 tmpChar = bodyCascade[i]->transliterateChar2Char(tmpChar);
432 return tmpChar;
433 }
434
435
436 sal_Bool SAL_CALL
equals(const OUString & str1,sal_Int32 pos1,sal_Int32 nCount1,sal_Int32 & nMatch1,const OUString & str2,sal_Int32 pos2,sal_Int32 nCount2,sal_Int32 & nMatch2)437 TransliterationImpl::equals(
438 const OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
439 const OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2)
440 {
441 // since this is an API function make it user fail safe
442 if ( nCount1 < 0 ) {
443 pos1 += nCount1;
444 nCount1 = -nCount1;
445 }
446 if ( nCount2 < 0 ) {
447 pos2 += nCount2;
448 nCount2 = -nCount2;
449 }
450 if ( !nCount1 || !nCount2 ||
451 pos1 >= str1.getLength() || pos2 >= str2.getLength() ||
452 pos1 < 0 || pos2 < 0 ) {
453 nMatch1 = nMatch2 = 0;
454 // two empty strings return true, else false
455 return !nCount1 && !nCount2 && pos1 == str1.getLength() && pos2 == str2.getLength();
456 }
457 if ( pos1 + nCount1 > str1.getLength() )
458 nCount1 = str1.getLength() - pos1;
459 if ( pos2 + nCount2 > str2.getLength() )
460 nCount2 = str2.getLength() - pos2;
461
462 if (caseignoreOnly && caseignore.is())
463 return caseignore->equals(str1, pos1, nCount1, nMatch1, str2, pos2, nCount2, nMatch2);
464
465 Sequence<sal_Int32> offset1, offset2;
466
467 OUString tmpStr1 = folding(str1, pos1, nCount1, offset1);
468 OUString tmpStr2 = folding(str2, pos2, nCount2, offset2);
469 // Length of offset1 and offset2 may still be 0 if there was no folding
470 // necessary!
471
472 const sal_Unicode *p1 = tmpStr1.getStr();
473 const sal_Unicode *p2 = tmpStr2.getStr();
474 sal_Int32 i, nLen = ::std::min( tmpStr1.getLength(), tmpStr2.getLength());
475 for (i = 0; i < nLen; ++i, ++p1, ++p2 ) {
476 if (*p1 != *p2) {
477 // return number of matched code points so far
478 nMatch1 = (i < offset1.getLength()) ? offset1[i] : i;
479 nMatch2 = (i < offset2.getLength()) ? offset2[i] : i;
480 return sal_False;
481 }
482 }
483 // i==nLen
484 if ( tmpStr1.getLength() != tmpStr2.getLength() ) {
485 // return number of matched code points so far
486 nMatch1 = (i <= offset1.getLength()) ? offset1[i-1] + 1 : i;
487 nMatch2 = (i <= offset2.getLength()) ? offset2[i-1] + 1 : i;
488 return sal_False;
489 } else {
490 nMatch1 = nCount1;
491 nMatch2 = nCount2;
492 return sal_True;
493 }
494 }
495
496 #define MaxOutput 2
497
498 Sequence< OUString > SAL_CALL
getRange(const Sequence<OUString> & inStrs,const sal_Int32 length,sal_Int16 _numCascade)499 TransliterationImpl::getRange(const Sequence< OUString > &inStrs,
500 const sal_Int32 length, sal_Int16 _numCascade)
501 {
502 if (_numCascade >= numCascade || ! bodyCascade[_numCascade].is())
503 return inStrs;
504
505 sal_Int32 j_tmp = 0;
506 Sequence< OUString > ostr(MaxOutput*length);
507 for (sal_Int32 j = 0; j < length; j+=2) {
508 const Sequence< OUString >& temp = bodyCascade[_numCascade]->transliterateRange(inStrs[j], inStrs[j+1]);
509
510 for ( sal_Int32 k = 0; k < temp.getLength(); k++) {
511 if ( j_tmp >= MaxOutput*length ) throw ERROR;
512 ostr[j_tmp++] = temp[k];
513 }
514 }
515 ostr.realloc(j_tmp);
516
517 return this->getRange(ostr, j_tmp, ++_numCascade);
518 }
519
520
521 Sequence< OUString > SAL_CALL
transliterateRange(const OUString & str1,const OUString & str2)522 TransliterationImpl::transliterateRange( const OUString& str1, const OUString& str2 )
523 {
524 if (numCascade == 1)
525 return bodyCascade[0]->transliterateRange(str1, str2);
526
527 Sequence< OUString > ostr(2);
528 ostr[0] = str1;
529 ostr[1] = str2;
530
531 return this->getRange(ostr, 2, 0);
532 }
533
534
535 sal_Int32 SAL_CALL
compareSubstring(const OUString & str1,sal_Int32 off1,sal_Int32 len1,const OUString & str2,sal_Int32 off2,sal_Int32 len2)536 TransliterationImpl::compareSubstring(
537 const OUString& str1, sal_Int32 off1, sal_Int32 len1,
538 const OUString& str2, sal_Int32 off2, sal_Int32 len2)
539 {
540 if (caseignoreOnly && caseignore.is())
541 return caseignore->compareSubstring(str1, off1, len1, str2, off2, len2);
542
543 Sequence <sal_Int32> offset;
544
545 OUString in_str1 = this->transliterate(str1, off1, len1, offset);
546 OUString in_str2 = this->transliterate(str2, off2, len2, offset);
547 const sal_Unicode* unistr1 = in_str1.getStr();
548 const sal_Unicode* unistr2 = in_str2.getStr();
549 sal_Int32 strlen1 = in_str1.getLength();
550 sal_Int32 strlen2 = in_str2.getLength();
551
552 while (strlen1 && strlen2) {
553 if (*unistr1 != *unistr2)
554 return *unistr1 > *unistr2 ? 1 : -1;
555
556 unistr1++; unistr2++; strlen1--; strlen2--;
557 }
558 return strlen1 == strlen2 ? 0 : (strlen1 > strlen2 ? 1 : -1);
559 }
560
561
562 sal_Int32 SAL_CALL
compareString(const OUString & str1,const OUString & str2)563 TransliterationImpl::compareString(const OUString& str1, const OUString& str2 )
564 {
565 if (caseignoreOnly && caseignore.is())
566 return caseignore->compareString(str1, str2);
567 else
568 return this->compareSubstring(str1, 0, str1.getLength(), str2, 0, str2.getLength());
569 }
570
571
572 void
clear()573 TransliterationImpl::clear()
574 {
575 for (sal_Int32 i = 0; i < numCascade; i++)
576 if (bodyCascade[i].is())
577 bodyCascade[i].clear();
578 numCascade = 0;
579 caseignore.clear();
580 caseignoreOnly = sal_True;
581 }
582
loadBody(OUString & implName,Reference<XExtendedTransliteration> & body)583 void TransliterationImpl::loadBody( OUString &implName, Reference<XExtendedTransliteration>& body )
584 {
585 Reference< XContentEnumerationAccess > xEnumAccess( xSMgr, UNO_QUERY );
586 Reference< XEnumeration > xEnum(xEnumAccess->createContentEnumeration(
587 OUString::createFromAscii(TRLT_SERVICELNAME_L10N)));
588 if (xEnum.is()) {
589 while (xEnum->hasMoreElements()) {
590 Any a = xEnum->nextElement();
591 Reference< XServiceInfo > xsInfo;
592 if (a >>= xsInfo) {
593 if (implName.equals(xsInfo->getImplementationName())) {
594 Reference< XSingleServiceFactory > xFactory;
595 if (a >>= xFactory) {
596 Reference< XInterface > xI = xFactory->createInstance();
597 if (xI.is()) {
598 a = xI->queryInterface(::getCppuType((
599 const Reference<XExtendedTransliteration>*)0));
600 a >>= body;
601 return;
602 }
603 }
604 }
605 }
606 }
607 }
608 throw ERROR;
609 }
610
611 sal_Bool SAL_CALL
loadModuleByName(const OUString & implName,Reference<XExtendedTransliteration> & body,const Locale & rLocale)612 TransliterationImpl::loadModuleByName( const OUString& implName,
613 Reference<XExtendedTransliteration>& body, const Locale& rLocale)
614 {
615 OUString cname = OUString::createFromAscii(TRLT_IMPLNAME_PREFIX) + implName;
616 loadBody(cname, body);
617 if (body.is()) {
618 body->loadModule((TransliterationModules)0, rLocale); // toUpper/toLoad need rLocale
619
620 // if the module is ignore case/kana/width, load caseignore for equals/compareString mothed
621 for (sal_Int16 i = 0; i < 3; i++) {
622 if (implName.compareToAscii(TMlist[i].implName) == 0) {
623 if (i == 0) // current module is caseignore
624 body->loadModule(TMlist[0].tm, rLocale); // caseingore need to setup module name
625 if (! caseignore.is()) {
626 OUString bname = OUString::createFromAscii(TRLT_IMPLNAME_PREFIX) +
627 OUString::createFromAscii(TMlist[0].implName);
628 loadBody(bname, caseignore);
629 }
630 if (caseignore.is())
631 caseignore->loadModule(TMlist[i].tm, rLocale);
632 return sal_True;
633 }
634 }
635 caseignoreOnly = sal_False; // has other module than just ignore case/kana/width
636 }
637 return body.is();
638 }
639
640 const sal_Char cTrans[] = "com.sun.star.i18n.Transliteration";
641
642 OUString SAL_CALL
getImplementationName()643 TransliterationImpl::getImplementationName()
644 {
645 return OUString::createFromAscii(cTrans);
646 }
647
648
649 sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)650 TransliterationImpl::supportsService(const OUString& rServiceName)
651 {
652 return !rServiceName.compareToAscii(cTrans);
653 }
654
655 Sequence< OUString > SAL_CALL
getSupportedServiceNames(void)656 TransliterationImpl::getSupportedServiceNames(void)
657 {
658 Sequence< OUString > aRet(1);
659 aRet[0] = OUString::createFromAscii(cTrans);
660 return aRet;
661 }
662
663 } } } }
664