Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <com/sun/star/uno/Reference.h>
22 : #include <cppuhelper/factory.hxx> // helper for factories
23 : #include <com/sun/star/registry/XRegistryKey.hpp>
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <com/sun/star/linguistic2/LinguServiceManager.hpp>
26 : #include <i18npool/languagetag.hxx>
27 : #include <tools/debug.hxx>
28 : #include <comphelper/processfactory.hxx>
29 : #include <osl/mutex.hxx>
30 : #include <unotools/pathoptions.hxx>
31 : #include <unotools/lingucfg.hxx>
32 :
33 : #include <rtl/string.hxx>
34 : #include <rtl/ustrbuf.hxx>
35 : #include <rtl/textenc.h>
36 :
37 : #include "nthesimp.hxx"
38 : #include <linguistic/misc.hxx>
39 : #include <linguistic/lngprops.hxx>
40 : #include "nthesdta.hxx"
41 :
42 : #include <list>
43 : #include <set>
44 : #include <string.h>
45 :
46 : // values asigned to capitalization types
47 : #define CAPTYPE_UNKNOWN 0
48 : #define CAPTYPE_NOCAP 1
49 : #define CAPTYPE_INITCAP 2
50 : #define CAPTYPE_ALLCAP 3
51 : #define CAPTYPE_MIXED 4
52 :
53 : // XML-header to query SPELLML support
54 : #define SPELLML_SUPPORT "<?xml?>"
55 :
56 : using namespace osl;
57 : using namespace com::sun::star;
58 : using namespace com::sun::star::beans;
59 : using namespace com::sun::star::lang;
60 : using namespace com::sun::star::uno;
61 : using namespace com::sun::star::linguistic2;
62 : using namespace linguistic;
63 :
64 : using ::rtl::OUString;
65 : using ::rtl::OString;
66 : using ::rtl::OUStringToOString;
67 :
68 : ///////////////////////////////////////////////////////////////////////////
69 :
70 0 : static uno::Reference< XLinguServiceManager2 > GetLngSvcMgr_Impl()
71 : {
72 0 : uno::Reference< XComponentContext > xContext( comphelper::getProcessComponentContext() );
73 0 : uno::Reference< XLinguServiceManager2 > xRes = LinguServiceManager::create( xContext ) ;
74 0 : return xRes;
75 : }
76 :
77 1 : Thesaurus::Thesaurus() :
78 1 : aEvtListeners ( GetLinguMutex() )
79 : {
80 1 : bDisposing = false;
81 1 : pPropHelper = NULL;
82 1 : aThes = NULL;
83 1 : aCharSetInfo = NULL;
84 1 : aTEncs = NULL;
85 1 : aTLocs = NULL;
86 1 : aTNames = NULL;
87 1 : numthes = 0;
88 1 : }
89 :
90 :
91 3 : Thesaurus::~Thesaurus()
92 : {
93 1 : if (aThes)
94 : {
95 44 : for (int i = 0; i < numthes; i++)
96 : {
97 43 : if (aThes[i]) delete aThes[i];
98 43 : aThes[i] = NULL;
99 : }
100 1 : delete[] aThes;
101 : }
102 1 : aThes = NULL;
103 1 : if (aCharSetInfo)
104 : {
105 44 : for (int i = 0; i < numthes; i++)
106 : {
107 43 : if (aCharSetInfo[i]) delete aCharSetInfo[i];
108 43 : aCharSetInfo[i] = NULL;
109 : }
110 1 : delete[] aCharSetInfo;
111 : }
112 1 : aCharSetInfo = NULL;
113 1 : numthes = 0;
114 1 : if (aTEncs) delete[] aTEncs;
115 1 : aTEncs = NULL;
116 1 : if (aTLocs) delete[] aTLocs;
117 1 : aTLocs = NULL;
118 1 : if (aTNames) delete[] aTNames;
119 1 : aTNames = NULL;
120 :
121 1 : if (pPropHelper)
122 : {
123 0 : pPropHelper->RemoveAsPropListener();
124 0 : delete pPropHelper;
125 : }
126 2 : }
127 :
128 :
129 0 : PropertyHelper_Thesaurus& Thesaurus::GetPropHelper_Impl()
130 : {
131 0 : if (!pPropHelper)
132 : {
133 0 : Reference< XPropertySet > xPropSet( GetLinguProperties(), UNO_QUERY );
134 :
135 0 : pPropHelper = new PropertyHelper_Thesaurus( (XThesaurus *) this, xPropSet );
136 0 : pPropHelper->AddAsPropListener(); //! after a reference is established
137 : }
138 0 : return *pPropHelper;
139 : }
140 :
141 :
142 2 : Sequence< Locale > SAL_CALL Thesaurus::getLocales()
143 : throw(RuntimeException)
144 : {
145 2 : MutexGuard aGuard( GetLinguMutex() );
146 :
147 : // this routine should return the locales supported by the installed
148 : // dictionaries.
149 :
150 2 : if (!numthes)
151 : {
152 1 : SvtLinguConfig aLinguCfg;
153 :
154 : // get list of dictionaries-to-use
155 1 : std::list< SvtLinguConfigDictionaryEntry > aDics;
156 1 : uno::Sequence< rtl::OUString > aFormatList;
157 : aLinguCfg.GetSupportedDictionaryFormatsFor( A2OU("Thesauri"),
158 1 : A2OU("org.openoffice.lingu.new.Thesaurus"), aFormatList );
159 1 : sal_Int32 nLen = aFormatList.getLength();
160 2 : for (sal_Int32 i = 0; i < nLen; ++i)
161 : {
162 : std::vector< SvtLinguConfigDictionaryEntry > aTmpDic(
163 1 : aLinguCfg.GetActiveDictionariesByFormat( aFormatList[i] ) );
164 1 : aDics.insert( aDics.end(), aTmpDic.begin(), aTmpDic.end() );
165 1 : }
166 :
167 : //!! for compatibility with old dictionaries (the ones not using extensions
168 : //!! or new configuration entries, but still using the dictionary.lst file)
169 : //!! Get the list of old style spell checking dictionaries to use...
170 : std::vector< SvtLinguConfigDictionaryEntry > aOldStyleDics(
171 1 : GetOldStyleDics( "THES" ) );
172 :
173 : // to prefer dictionaries with configuration entries we will only
174 : // use those old style dictionaries that add a language that
175 : // is not yet supported by the list od new style dictionaries
176 1 : MergeNewStyleDicsAndOldStyleDics( aDics, aOldStyleDics );
177 :
178 1 : numthes = aDics.size();
179 1 : if (numthes)
180 : {
181 : // get supported locales from the dictionaries-to-use...
182 1 : sal_Int32 k = 0;
183 1 : std::set< rtl::OUString, lt_rtl_OUString > aLocaleNamesSet;
184 1 : std::list< SvtLinguConfigDictionaryEntry >::const_iterator aDictIt;
185 4 : for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt)
186 : {
187 3 : uno::Sequence< rtl::OUString > aLocaleNames( aDictIt->aLocaleNames );
188 3 : sal_Int32 nLen2 = aLocaleNames.getLength();
189 46 : for (k = 0; k < nLen2; ++k)
190 : {
191 43 : aLocaleNamesSet.insert( aLocaleNames[k] );
192 : }
193 3 : }
194 : // ... and add them to the resulting sequence
195 1 : aSuppLocales.realloc( aLocaleNamesSet.size() );
196 1 : std::set< rtl::OUString, lt_rtl_OUString >::const_iterator aItB;
197 1 : k = 0;
198 44 : for (aItB = aLocaleNamesSet.begin(); aItB != aLocaleNamesSet.end(); ++aItB)
199 : {
200 43 : Locale aTmp( LanguageTag( *aItB ).getLocale());
201 43 : aSuppLocales[k++] = aTmp;
202 43 : }
203 :
204 : //! For each dictionary and each locale we need a seperate entry.
205 : //! If this results in more than one dictionary per locale than (for now)
206 : //! it is undefined which dictionary gets used.
207 : //! In the future the implementation should support using several dictionaries
208 : //! for one locale.
209 1 : numthes = 0;
210 4 : for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt)
211 3 : numthes = numthes + aDictIt->aLocaleNames.getLength();
212 :
213 : // add dictionary information
214 1 : aThes = new MyThes* [numthes];
215 1 : aTEncs = new rtl_TextEncoding [numthes];
216 1 : aTLocs = new Locale [numthes];
217 1 : aTNames = new OUString [numthes];
218 1 : aCharSetInfo = new CharClass* [numthes];
219 :
220 1 : k = 0;
221 4 : for (aDictIt = aDics.begin(); aDictIt != aDics.end(); ++aDictIt)
222 : {
223 6 : if (aDictIt->aLocaleNames.getLength() > 0 &&
224 3 : aDictIt->aLocations.getLength() > 0)
225 : {
226 3 : uno::Sequence< rtl::OUString > aLocaleNames( aDictIt->aLocaleNames );
227 3 : sal_Int32 nLocales = aLocaleNames.getLength();
228 :
229 : // currently only one language per dictionary is supported in the actual implementation...
230 : // Thus here we work-around this by adding the same dictionary several times.
231 : // Once for each of it's supported locales.
232 46 : for (sal_Int32 i = 0; i < nLocales; ++i)
233 : {
234 43 : LanguageTag aLanguageTag( aDictIt->aLocaleNames[i] );
235 43 : aThes[k] = NULL;
236 43 : aTEncs[k] = RTL_TEXTENCODING_DONTKNOW;
237 43 : aTLocs[k] = aLanguageTag.getLocale();
238 43 : aCharSetInfo[k] = new CharClass( aLanguageTag );
239 : // also both files have to be in the same directory and the
240 : // file names must only differ in the extension (.aff/.dic).
241 : // Thus we use the first location only and strip the extension part.
242 43 : rtl::OUString aLocation = aDictIt->aLocations[0];
243 43 : sal_Int32 nPos = aLocation.lastIndexOf( '.' );
244 43 : aLocation = aLocation.copy( 0, nPos );
245 43 : aTNames[k] = aLocation;
246 :
247 43 : ++k;
248 46 : }
249 : }
250 : }
251 1 : DBG_ASSERT( k == numthes, "index mismatch?" );
252 : }
253 : else
254 : {
255 : /* no dictionary found so register no dictionaries */
256 0 : numthes = 0;
257 0 : aThes = NULL;
258 0 : aTEncs = NULL;
259 0 : aTLocs = NULL;
260 0 : aTNames = NULL;
261 0 : aCharSetInfo = NULL;
262 0 : aSuppLocales.realloc(0);
263 1 : }
264 : }
265 :
266 2 : return aSuppLocales;
267 : }
268 :
269 :
270 :
271 0 : sal_Bool SAL_CALL Thesaurus::hasLocale(const Locale& rLocale)
272 : throw(RuntimeException)
273 : {
274 0 : MutexGuard aGuard( GetLinguMutex() );
275 :
276 0 : sal_Bool bRes = sal_False;
277 0 : if (!aSuppLocales.getLength())
278 0 : getLocales();
279 0 : sal_Int32 nLen = aSuppLocales.getLength();
280 0 : for (sal_Int32 i = 0; i < nLen; ++i)
281 : {
282 0 : const Locale *pLocale = aSuppLocales.getConstArray();
283 0 : if (rLocale == pLocale[i])
284 : {
285 0 : bRes = sal_True;
286 0 : break;
287 : }
288 : }
289 0 : return bRes;
290 : }
291 :
292 :
293 0 : Sequence < Reference < ::com::sun::star::linguistic2::XMeaning > > SAL_CALL Thesaurus::queryMeanings(
294 : const OUString& qTerm, const Locale& rLocale,
295 : const PropertyValues& rProperties)
296 : throw(IllegalArgumentException, RuntimeException)
297 : {
298 0 : MutexGuard aGuard( GetLinguMutex() );
299 :
300 0 : uno::Sequence< Reference< XMeaning > > aMeanings( 1 );
301 0 : uno::Sequence< Reference< XMeaning > > noMeanings( 0 );
302 0 : uno::Reference< XLinguServiceManager2 > xLngSvcMgr( GetLngSvcMgr_Impl() );
303 0 : uno::Reference< XSpellChecker1 > xSpell;
304 :
305 0 : OUString rTerm(qTerm);
306 0 : OUString pTerm(qTerm);
307 0 : sal_uInt16 ct = CAPTYPE_UNKNOWN;
308 0 : sal_Int32 stem = 0;
309 0 : sal_Int32 stem2 = 0;
310 :
311 0 : sal_Int16 nLanguage = LanguageTag( rLocale ).getLanguageType();
312 :
313 0 : if (nLanguage == LANGUAGE_NONE || rTerm.isEmpty())
314 0 : return noMeanings;
315 :
316 0 : if (!hasLocale( rLocale ))
317 : #ifdef LINGU_EXCEPTIONS
318 : throw( IllegalArgumentException() );
319 : #else
320 0 : return noMeanings;
321 : #endif
322 :
323 0 : if (prevTerm == qTerm && prevLocale == nLanguage)
324 0 : return prevMeanings;
325 :
326 0 : mentry * pmean = NULL;
327 0 : sal_Int32 nmean = 0;
328 :
329 0 : PropertyHelper_Thesaurus &rHelper = GetPropHelper();
330 0 : rHelper.SetTmpPropVals( rProperties );
331 :
332 0 : MyThes * pTH = NULL;
333 0 : rtl_TextEncoding eEnc = RTL_TEXTENCODING_DONTKNOW;
334 0 : CharClass * pCC = NULL;
335 :
336 : // find the first thesaurus that matches the locale
337 0 : for (int i =0; i < numthes; i++)
338 : {
339 0 : if (rLocale == aTLocs[i])
340 : {
341 : // open up and intialize this thesaurus if need be
342 0 : if (!aThes[i])
343 : {
344 0 : OUString datpath = aTNames[i] + A2OU(".dat");
345 0 : OUString idxpath = aTNames[i] + A2OU(".idx");
346 0 : OUString ndat;
347 0 : OUString nidx;
348 0 : osl::FileBase::getSystemPathFromFileURL(datpath,ndat);
349 0 : osl::FileBase::getSystemPathFromFileURL(idxpath,nidx);
350 0 : OString aTmpidx(OU2ENC(nidx,osl_getThreadTextEncoding()));
351 0 : OString aTmpdat(OU2ENC(ndat,osl_getThreadTextEncoding()));
352 :
353 : #if defined(WNT)
354 : // workaround for Windows specifc problem that the
355 : // path length in calls to 'fopen' is limted to somewhat
356 : // about 120+ characters which will usually be exceed when
357 : // using dictionaries as extensions.
358 : aTmpidx = Win_GetShortPathName( nidx );
359 : aTmpdat = Win_GetShortPathName( ndat );
360 : #endif
361 :
362 0 : aThes[i] = new MyThes(aTmpidx.getStr(),aTmpdat.getStr());
363 0 : if (aThes[i])
364 0 : aTEncs[i] = getTextEncodingFromCharset(aThes[i]->get_th_encoding());
365 : }
366 0 : pTH = aThes[i];
367 0 : eEnc = aTEncs[i];
368 0 : pCC = aCharSetInfo[i];
369 :
370 0 : if (pTH)
371 0 : break;
372 : }
373 : }
374 :
375 : // we don't want to work with a default text encoding since following incorrect
376 : // results may occur only for specific text and thus may be hard to notice.
377 : // Thus better always make a clean exit here if the text encoding is in question.
378 : // Hopefully something not working at all will raise proper attention quickly. ;-)
379 : DBG_ASSERT( eEnc != RTL_TEXTENCODING_DONTKNOW, "failed to get text encoding! (maybe incorrect encoding string in file)" );
380 0 : if (eEnc == RTL_TEXTENCODING_DONTKNOW)
381 0 : return noMeanings;
382 :
383 0 : while (pTH)
384 : {
385 : // convert word to all lower case for searching
386 0 : if (!stem)
387 0 : ct = capitalType(rTerm, pCC);
388 0 : OUString nTerm(makeLowerCase(rTerm, pCC));
389 0 : OString aTmp( OU2ENC(nTerm, eEnc) );
390 0 : nmean = pTH->Lookup(aTmp.getStr(),aTmp.getLength(),&pmean);
391 :
392 0 : if (nmean)
393 0 : aMeanings.realloc( nmean );
394 :
395 0 : mentry * pe = pmean;
396 0 : OUString codeTerm = qTerm;
397 0 : Reference< XSpellAlternatives > xTmpRes2;
398 :
399 0 : if (stem)
400 : {
401 0 : xTmpRes2 = xSpell->spell( A2OU("<?xml?><query type='analyze'><word>") +
402 0 : pTerm + A2OU("</word></query>"), nLanguage, rProperties );
403 0 : if (xTmpRes2.is())
404 : {
405 0 : Sequence<OUString>seq = xTmpRes2->getAlternatives();
406 0 : if (seq.getLength() > 0)
407 : {
408 0 : codeTerm = seq[0];
409 0 : stem2 = 1;
410 0 : }
411 : }
412 : }
413 :
414 0 : for (int j = 0; j < nmean; j++)
415 : {
416 0 : int count = pe->count;
417 0 : if (count)
418 : {
419 0 : Sequence< OUString > aStr( count );
420 0 : OUString *pStr = aStr.getArray();
421 :
422 0 : for (int i=0; i < count; i++)
423 : {
424 0 : OUString sTerm(pe->psyns[i],strlen(pe->psyns[i]),eEnc );
425 0 : sal_Int32 catpos = sTerm.indexOf('(');
426 0 : sal_Int32 catpos2 = 0;
427 0 : OUString catst;
428 0 : OUString catst2;
429 0 : if (catpos > 2)
430 : {
431 : // remove category name for affixation and casing
432 0 : catst = A2OU(" ") + sTerm.copy(catpos);
433 0 : sTerm = sTerm.copy(0, catpos);
434 0 : sTerm = sTerm.trim();
435 : }
436 : // generate synonyms with affixes
437 0 : if (stem && stem2)
438 : {
439 0 : Reference< XSpellAlternatives > xTmpRes;
440 0 : xTmpRes = xSpell->spell( A2OU("<?xml?><query type='generate'><word>") +
441 0 : sTerm + A2OU("</word>") + codeTerm + A2OU("</query>"), nLanguage, rProperties );
442 0 : if (xTmpRes.is())
443 : {
444 0 : Sequence<OUString>seq = xTmpRes->getAlternatives();
445 0 : if (seq.getLength() > 0)
446 0 : sTerm = seq[0];
447 0 : }
448 : }
449 0 : if (catpos2)
450 0 : sTerm = catst2 + sTerm;
451 :
452 0 : sal_uInt16 ct1 = capitalType(sTerm, pCC);
453 0 : if (CAPTYPE_MIXED == ct1)
454 0 : ct = ct1;
455 0 : OUString cTerm;
456 0 : switch (ct)
457 : {
458 : case CAPTYPE_ALLCAP:
459 0 : cTerm = makeUpperCase(sTerm, pCC);
460 0 : break;
461 : case CAPTYPE_INITCAP:
462 0 : cTerm = makeInitCap(sTerm, pCC);
463 0 : break;
464 : default:
465 0 : cTerm = sTerm;
466 0 : break;
467 : }
468 0 : OUString aAlt( cTerm + catst);
469 0 : pStr[i] = aAlt;
470 0 : }
471 0 : Meaning * pMn = new Meaning(rTerm,nLanguage);
472 0 : OUString dTerm(pe->defn,strlen(pe->defn),eEnc );
473 0 : pMn->SetMeaning(dTerm);
474 0 : pMn->SetSynonyms(aStr);
475 0 : Reference<XMeaning>* pMeaning = aMeanings.getArray();
476 0 : pMeaning[j] = pMn;
477 : }
478 0 : pe++;
479 : }
480 0 : pTH->CleanUpAfterLookup(&pmean,nmean);
481 :
482 0 : if (nmean)
483 : {
484 0 : prevTerm = qTerm;
485 0 : prevMeanings = aMeanings;
486 0 : prevLocale = nLanguage;
487 0 : return aMeanings;
488 : }
489 :
490 0 : if (stem || !xLngSvcMgr.is())
491 0 : return noMeanings;
492 0 : stem = 1;
493 :
494 0 : xSpell = uno::Reference< XSpellChecker1 >( xLngSvcMgr->getSpellChecker(), UNO_QUERY );
495 0 : if (!xSpell.is() || !xSpell->isValid( A2OU(SPELLML_SUPPORT), nLanguage, rProperties ))
496 0 : return noMeanings;
497 0 : Reference< XSpellAlternatives > xTmpRes;
498 0 : xTmpRes = xSpell->spell( A2OU("<?xml?><query type='stem'><word>") +
499 0 : rTerm + A2OU("</word></query>"), nLanguage, rProperties );
500 0 : if (xTmpRes.is())
501 : {
502 0 : Sequence<OUString>seq = xTmpRes->getAlternatives();
503 0 : if (seq.getLength() > 0)
504 : {
505 0 : rTerm = seq[0]; // XXX Use only the first stem
506 0 : continue;
507 0 : }
508 : }
509 :
510 : // stem the last word of the synonym (for categories after affixation)
511 0 : rTerm = rTerm.trim();
512 0 : sal_Int32 pos = rTerm.lastIndexOf(' ');
513 0 : if (!pos)
514 0 : return noMeanings;
515 0 : xTmpRes = xSpell->spell( A2OU("<?xml?><query type='stem'><word>") +
516 0 : rTerm.copy(pos + 1) + A2OU("</word></query>"), nLanguage, rProperties );
517 0 : if (xTmpRes.is())
518 : {
519 0 : Sequence<OUString>seq = xTmpRes->getAlternatives();
520 0 : if (seq.getLength() > 0)
521 : {
522 0 : pTerm = rTerm.copy(pos + 1);
523 0 : rTerm = rTerm.copy(0, pos + 1) + seq[0];
524 : #if 0
525 : for (int i = 0; i < seq.getLength(); i++)
526 : {
527 : OString o = OUStringToOString(seq[i], RTL_TEXTENCODING_UTF8);
528 : fprintf(stderr, "%d: %s\n", i + 1, o.pData->buffer);
529 : }
530 : #endif
531 0 : continue;
532 0 : }
533 : }
534 : break;
535 0 : }
536 0 : return noMeanings;
537 : }
538 :
539 :
540 1 : Reference< XInterface > SAL_CALL Thesaurus_CreateInstance(
541 : const Reference< XMultiServiceFactory > & /*rSMgr*/ )
542 : throw(Exception)
543 : {
544 1 : Reference< XInterface > xService = (cppu::OWeakObject*) new Thesaurus;
545 1 : return xService;
546 : }
547 :
548 :
549 0 : OUString SAL_CALL Thesaurus::getServiceDisplayName( const Locale& /*rLocale*/ )
550 : throw(RuntimeException)
551 : {
552 0 : MutexGuard aGuard( GetLinguMutex() );
553 0 : return A2OU( "OpenOffice.org New Thesaurus" );
554 : }
555 :
556 :
557 0 : void SAL_CALL Thesaurus::initialize( const Sequence< Any >& rArguments )
558 : throw(Exception, RuntimeException)
559 : {
560 0 : MutexGuard aGuard( GetLinguMutex() );
561 :
562 0 : if (!pPropHelper)
563 : {
564 0 : sal_Int32 nLen = rArguments.getLength();
565 0 : if (1 == nLen)
566 : {
567 0 : Reference< XPropertySet > xPropSet;
568 0 : rArguments.getConstArray()[0] >>= xPropSet;
569 :
570 : //! Pointer allows for access of the non-UNO functions.
571 : //! And the reference to the UNO-functions while increasing
572 : //! the ref-count and will implicitly free the memory
573 : //! when the object is not longer used.
574 0 : pPropHelper = new PropertyHelper_Thesaurus( (XThesaurus *) this, xPropSet );
575 0 : pPropHelper->AddAsPropListener(); //! after a reference is established
576 : }
577 : else
578 : OSL_FAIL( "wrong number of arguments in sequence" );
579 0 : }
580 0 : }
581 :
582 :
583 :
584 0 : sal_uInt16 SAL_CALL Thesaurus::capitalType(const OUString& aTerm, CharClass * pCC)
585 : {
586 0 : sal_Int32 tlen = aTerm.getLength();
587 0 : if ((pCC) && (tlen))
588 : {
589 0 : String aStr(aTerm);
590 0 : sal_Int32 nc = 0;
591 0 : for (sal_uInt16 tindex = 0; tindex < tlen; tindex++)
592 : {
593 0 : if (pCC->getCharacterType(aStr,tindex) &
594 0 : ::com::sun::star::i18n::KCharacterType::UPPER) nc++;
595 : }
596 :
597 0 : if (nc == 0)
598 0 : return (sal_uInt16) CAPTYPE_NOCAP;
599 0 : if (nc == tlen)
600 0 : return (sal_uInt16) CAPTYPE_ALLCAP;
601 0 : if ((nc == 1) && (pCC->getCharacterType(aStr,0) &
602 : ::com::sun::star::i18n::KCharacterType::UPPER))
603 0 : return (sal_uInt16) CAPTYPE_INITCAP;
604 :
605 0 : return (sal_uInt16) CAPTYPE_MIXED;
606 : }
607 0 : return (sal_uInt16) CAPTYPE_UNKNOWN;
608 : }
609 :
610 :
611 :
612 0 : OUString SAL_CALL Thesaurus::makeLowerCase(const OUString& aTerm, CharClass * pCC)
613 : {
614 0 : if (pCC)
615 0 : return pCC->lowercase(aTerm);
616 0 : return aTerm;
617 : }
618 :
619 :
620 0 : OUString SAL_CALL Thesaurus::makeUpperCase(const OUString& aTerm, CharClass * pCC)
621 : {
622 0 : if (pCC)
623 0 : return pCC->uppercase(aTerm);
624 0 : return aTerm;
625 : }
626 :
627 :
628 0 : OUString SAL_CALL Thesaurus::makeInitCap(const OUString& aTerm, CharClass * pCC)
629 : {
630 0 : sal_Int32 tlen = aTerm.getLength();
631 0 : if ((pCC) && (tlen))
632 : {
633 0 : OUString bTemp = aTerm.copy(0,1);
634 0 : if (tlen > 1)
635 : {
636 : return ( pCC->uppercase(bTemp, 0, 1)
637 0 : + pCC->lowercase(aTerm,1,(tlen-1)) );
638 : }
639 :
640 0 : return pCC->uppercase(bTemp, 0, 1);
641 : }
642 0 : return aTerm;
643 : }
644 :
645 :
646 :
647 1 : void SAL_CALL Thesaurus::dispose()
648 : throw(RuntimeException)
649 : {
650 1 : MutexGuard aGuard( GetLinguMutex() );
651 :
652 1 : if (!bDisposing)
653 : {
654 1 : bDisposing = true;
655 1 : EventObject aEvtObj( (XThesaurus *) this );
656 1 : aEvtListeners.disposeAndClear( aEvtObj );
657 1 : if (pPropHelper)
658 : {
659 0 : pPropHelper->RemoveAsPropListener();
660 0 : delete pPropHelper;
661 0 : pPropHelper = NULL;
662 1 : }
663 1 : }
664 1 : }
665 :
666 :
667 0 : void SAL_CALL Thesaurus::addEventListener( const Reference< XEventListener >& rxListener )
668 : throw(RuntimeException)
669 : {
670 0 : MutexGuard aGuard( GetLinguMutex() );
671 :
672 0 : if (!bDisposing && rxListener.is())
673 0 : aEvtListeners.addInterface( rxListener );
674 0 : }
675 :
676 :
677 0 : void SAL_CALL Thesaurus::removeEventListener( const Reference< XEventListener >& rxListener )
678 : throw(RuntimeException)
679 : {
680 0 : MutexGuard aGuard( GetLinguMutex() );
681 :
682 0 : if (!bDisposing && rxListener.is())
683 0 : aEvtListeners.removeInterface( rxListener );
684 0 : }
685 :
686 :
687 : ///////////////////////////////////////////////////////////////////////////
688 : // Service specific part
689 : //
690 :
691 1 : OUString SAL_CALL Thesaurus::getImplementationName()
692 : throw(RuntimeException)
693 : {
694 1 : MutexGuard aGuard( GetLinguMutex() );
695 1 : return getImplementationName_Static();
696 : }
697 :
698 :
699 0 : sal_Bool SAL_CALL Thesaurus::supportsService( const OUString& ServiceName )
700 : throw(RuntimeException)
701 : {
702 0 : MutexGuard aGuard( GetLinguMutex() );
703 :
704 0 : Sequence< OUString > aSNL = getSupportedServiceNames();
705 0 : const OUString * pArray = aSNL.getConstArray();
706 0 : for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
707 0 : if( pArray[i] == ServiceName )
708 0 : return sal_True;
709 0 : return sal_False;
710 : }
711 :
712 :
713 0 : Sequence< OUString > SAL_CALL Thesaurus::getSupportedServiceNames()
714 : throw(RuntimeException)
715 : {
716 0 : MutexGuard aGuard( GetLinguMutex() );
717 0 : return getSupportedServiceNames_Static();
718 : }
719 :
720 :
721 1 : Sequence< OUString > Thesaurus::getSupportedServiceNames_Static()
722 : throw()
723 : {
724 1 : MutexGuard aGuard( GetLinguMutex() );
725 :
726 1 : Sequence< OUString > aSNS( 1 ); // auch mehr als 1 Service moeglich
727 1 : aSNS.getArray()[0] = A2OU( SN_THESAURUS );
728 1 : return aSNS;
729 : }
730 :
731 1 : void * SAL_CALL Thesaurus_getFactory( const sal_Char * pImplName,
732 : XMultiServiceFactory * pServiceManager, void * )
733 : {
734 1 : void * pRet = 0;
735 1 : if ( !Thesaurus::getImplementationName_Static().compareToAscii( pImplName ) )
736 : {
737 :
738 : Reference< XSingleServiceFactory > xFactory =
739 : cppu::createOneInstanceFactory(
740 : pServiceManager,
741 : Thesaurus::getImplementationName_Static(),
742 : Thesaurus_CreateInstance,
743 1 : Thesaurus::getSupportedServiceNames_Static());
744 : // acquire, because we return an interface pointer instead of a reference
745 1 : xFactory->acquire();
746 1 : pRet = xFactory.get();
747 : }
748 1 : return pRet;
749 : }
750 :
751 :
752 : ///////////////////////////////////////////////////////////////////////////
753 :
754 :
755 : #undef CAPTYPE_UNKNOWN
756 : #undef CAPTYPE_NOCAP
757 : #undef CAPTYPE_INITCAP
758 : #undef CAPTYPE_ALLCAP
759 : #undef CAPTYPE_MIXED
760 :
761 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|