1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <com/sun/star/uno/Reference.h>
#include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp>
#include <com/sun/star/linguistic2/SpellFailure.hpp>
#include <com/sun/star/registry/XRegistryKey.hpp>
#include <cppuhelper/factory.hxx>
#include <unotools/localedatawrapper.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/sequence.hxx>
#include <tools/debug.hxx>
#include <svl/lngmisc.hxx>
#include <osl/mutex.hxx>
#include <vector>
#include "spelldsp.hxx"
#include "linguistic/spelldta.hxx"
#include "lngsvcmgr.hxx"
#include "linguistic/lngprops.hxx"
using namespace osl;
using namespace com::sun::star;
using namespace com::sun::star::beans;
using namespace com::sun::star::lang;
using namespace com::sun::star::uno;
using namespace com::sun::star::linguistic2;
using namespace linguistic;
// ProposalList: list of proposals for misspelled words
// The order of strings in the array should be left unchanged because the
// spellchecker should have put the more likely suggestions at the top.
// New entries will be added to the end but duplicates are to be avoided.
// Removing entries is done by assigning the empty string.
// The sequence is constructed from all non empty strings in the original
// while maintaining the order.
class ProposalList
{
std::vector< OUString > aVec;
bool HasEntry( const OUString &rText ) const;
public:
ProposalList() {}
ProposalList(const ProposalList&) = delete;
ProposalList& operator=(const ProposalList&) = delete;
size_t Count() const;
void Prepend( const OUString &rText );
void Append( const OUString &rNew );
void Append( const std::vector< OUString > &rNew );
void Append( const Sequence< OUString > &rNew );
std::vector< OUString > GetVector() const;
};
bool ProposalList::HasEntry( const OUString &rText ) const
{
bool bFound = false;
size_t nCnt = aVec.size();
for (size_t i = 0; !bFound && i < nCnt; ++i)
{
if (aVec[i] == rText)
bFound = true;
}
return bFound;
}
void ProposalList::Prepend( const OUString &rText )
{
if (!HasEntry( rText ))
aVec.insert( aVec.begin(), rText );
}
void ProposalList::Append( const OUString &rText )
{
if (!HasEntry( rText ))
aVec.push_back( rText );
}
void ProposalList::Append( const std::vector< OUString > &rNew )
{
size_t nLen = rNew.size();
for ( size_t i = 0; i < nLen; ++i)
{
const OUString &rText = rNew[i];
if (!HasEntry( rText ))
Append( rText );
}
}
void ProposalList::Append( const Sequence< OUString > &rNew )
{
sal_Int32 nLen = rNew.getLength();
const OUString *pNew = rNew.getConstArray();
for (sal_Int32 i = 0; i < nLen; ++i)
{
const OUString &rText = pNew[i];
if (!HasEntry( rText ))
Append( rText );
}
}
size_t ProposalList::Count() const
{
// returns the number of non-empty strings in the vector
size_t nRes = 0;
size_t nLen = aVec.size();
for (size_t i = 0; i < nLen; ++i)
{
if (!aVec[i].isEmpty())
++nRes;
}
return nRes;
}
std::vector< OUString > ProposalList::GetVector() const
{
sal_Int32 nCount = Count();
sal_Int32 nIdx = 0;
std::vector< OUString > aRes( nCount );
sal_Int32 nLen = aVec.size();
for (sal_Int32 i = 0; i < nLen; ++i)
{
const OUString &rText = aVec[i];
DBG_ASSERT( nIdx < nCount, "index out of range" );
if (nIdx < nCount && !rText.isEmpty())
aRes[ nIdx++ ] = rText;
}
return aRes;
}
bool SvcListHasLanguage(
const LangSvcEntries_Spell &rEntry,
LanguageType nLanguage )
{
bool bHasLanguage = false;
Locale aTmpLocale;
const Reference< XSpellChecker > *pRef = rEntry.aSvcRefs .getConstArray();
sal_Int32 nLen = rEntry.aSvcRefs.getLength();
for (sal_Int32 k = 0; k < nLen && !bHasLanguage; ++k)
{
if (pRef[k].is())
{
if (aTmpLocale.Language.isEmpty())
aTmpLocale = LanguageTag::convertToLocale( nLanguage );
bHasLanguage = pRef[k]->hasLocale( aTmpLocale );
}
}
return bHasLanguage;
}
SpellCheckerDispatcher::SpellCheckerDispatcher( LngSvcMgr &rLngSvcMgr ) :
rMgr (rLngSvcMgr)
{
pCache = nullptr;
pCharClass = nullptr;
}
SpellCheckerDispatcher::~SpellCheckerDispatcher()
{
ClearSvcList();
delete pCache;
delete pCharClass;
}
void SpellCheckerDispatcher::ClearSvcList()
{
// release memory for each table entry
SpellSvcByLangMap_t aTmp;
aSvcMap.swap( aTmp );
}
Sequence< Locale > SAL_CALL SpellCheckerDispatcher::getLocales()
throw(RuntimeException, std::exception)
{
MutexGuard aGuard( GetLinguMutex() );
Sequence< Locale > aLocales( static_cast< sal_Int32 >(aSvcMap.size()) );
Locale *pLocales = aLocales.getArray();
SpellSvcByLangMap_t::const_iterator aIt;
for (aIt = aSvcMap.begin(); aIt != aSvcMap.end(); ++aIt)
{
*pLocales++ = LanguageTag::convertToLocale( aIt->first );
}
return aLocales;
}
sal_Bool SAL_CALL SpellCheckerDispatcher::hasLocale( const Locale& rLocale )
throw(RuntimeException, std::exception)
{
MutexGuard aGuard( GetLinguMutex() );
SpellSvcByLangMap_t::const_iterator aIt( aSvcMap.find( LinguLocaleToLanguage( rLocale ) ) );
return aIt != aSvcMap.end();
}
sal_Bool SAL_CALL
SpellCheckerDispatcher::isValid( const OUString& rWord, const Locale& rLocale,
const PropertyValues& rProperties )
throw(IllegalArgumentException, RuntimeException, std::exception)
{
MutexGuard aGuard( GetLinguMutex() );
return isValid_Impl( rWord, LinguLocaleToLanguage( rLocale ), rProperties );
}
Reference< XSpellAlternatives > SAL_CALL
SpellCheckerDispatcher::spell( const OUString& rWord, const Locale& rLocale,
const PropertyValues& rProperties )
throw(IllegalArgumentException, RuntimeException, std::exception)
{
MutexGuard aGuard( GetLinguMutex() );
return spell_Impl( rWord, LinguLocaleToLanguage( rLocale ), rProperties );
}
// returns the overall result of cross-checking with all user-dictionaries
// including the IgnoreAll list
static Reference< XDictionaryEntry > lcl_GetRulingDictionaryEntry(
const OUString &rWord,
LanguageType nLanguage )
{
Reference< XDictionaryEntry > xRes;
// the order of winning from top to bottom is:
// 1) IgnoreAll list will always win
// 2) Negative dictionaries will win over positive dictionaries
Reference< XDictionary > xIgnoreAll( GetIgnoreAllList() );
if (xIgnoreAll.is())
xRes = xIgnoreAll->getEntry( rWord );
if (!xRes.is())
{
Reference< XSearchableDictionaryList > xDList( GetDictionaryList() );
Reference< XDictionaryEntry > xNegEntry( SearchDicList( xDList,
rWord, nLanguage, false, true ) );
if (xNegEntry.is())
xRes = xNegEntry;
else
{
Reference< XDictionaryEntry > xPosEntry( SearchDicList( xDList,
rWord, nLanguage, true, true ) );
if (xPosEntry.is())
xRes = xPosEntry;
}
}
return xRes;
}
bool SpellCheckerDispatcher::isValid_Impl(
const OUString& rWord,
LanguageType nLanguage,
const PropertyValues& rProperties)
throw( RuntimeException, IllegalArgumentException, std::exception )
{
MutexGuard aGuard( GetLinguMutex() );
bool bRes = true;
if (LinguIsUnspecified( nLanguage) || rWord.isEmpty())
return bRes;
// search for entry with that language
SpellSvcByLangMap_t::iterator aIt( aSvcMap.find( nLanguage ) );
LangSvcEntries_Spell *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : nullptr;
if (pEntry)
{
OUString aChkWord( rWord );
Locale aLocale( LanguageTag::convertToLocale( nLanguage ) );
// replace typographical apostroph by ascii apostroph
OUString aSingleQuote( GetLocaleDataWrapper( nLanguage ).getQuotationMarkEnd() );
DBG_ASSERT( 1 == aSingleQuote.getLength(), "unexpectend length of quotation mark" );
if (!aSingleQuote.isEmpty())
aChkWord = aChkWord.replace( aSingleQuote[0], '\'' );
RemoveHyphens( aChkWord );
if (IsIgnoreControlChars( rProperties, GetPropSet() ))
RemoveControlChars( aChkWord );
sal_Int32 nLen = pEntry->aSvcRefs.getLength();
DBG_ASSERT( nLen == pEntry->aSvcImplNames.getLength(),
"lng : sequence length mismatch");
DBG_ASSERT( pEntry->nLastTriedSvcIndex < nLen,
"lng : index out of range");
sal_Int32 i = 0;
bool bTmpRes = true;
bool bTmpResValid = false;
// try already instantiated services first
{
const Reference< XSpellChecker > *pRef =
pEntry->aSvcRefs.getConstArray();
while (i <= pEntry->nLastTriedSvcIndex
&& (!bTmpResValid || !bTmpRes))
{
bTmpResValid = true;
if (pRef[i].is() && pRef[i]->hasLocale( aLocale ))
{
bTmpRes = GetCache().CheckWord( aChkWord, nLanguage );
if (!bTmpRes)
{
bTmpRes = pRef[i]->isValid( aChkWord, aLocale, rProperties );
// Add correct words to the cache.
// But not those that are correct only because of
// the temporary supplied settings.
if (bTmpRes && 0 == rProperties.getLength())
GetCache().AddWord( aChkWord, nLanguage );
}
}
else
bTmpResValid = false;
if (bTmpResValid)
bRes = bTmpRes;
++i;
}
}
// if still no result instantiate new services and try those
if ((!bTmpResValid || !bTmpRes)
&& pEntry->nLastTriedSvcIndex < nLen - 1)
{
const OUString *pImplNames = pEntry->aSvcImplNames.getConstArray();
Reference< XSpellChecker > *pRef = pEntry->aSvcRefs .getArray();
Reference< XComponentContext > xContext(
comphelper::getProcessComponentContext() );
// build service initialization argument
Sequence< Any > aArgs(2);
aArgs.getArray()[0] <<= GetPropSet();
while (i < nLen && (!bTmpResValid || !bTmpRes))
{
// create specific service via it's implementation name
Reference< XSpellChecker > xSpell;
try
{
xSpell.set( xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
pImplNames[i], aArgs, xContext ),
UNO_QUERY );
}
catch (uno::Exception &)
{
SAL_WARN( "linguistic", "createInstanceWithArguments failed" );
}
pRef [i] = xSpell;
Reference< XLinguServiceEventBroadcaster >
xBroadcaster( xSpell, UNO_QUERY );
if (xBroadcaster.is())
rMgr.AddLngSvcEvtBroadcaster( xBroadcaster );
bTmpResValid = true;
if (xSpell.is() && xSpell->hasLocale( aLocale ))
{
bTmpRes = GetCache().CheckWord( aChkWord, nLanguage );
if (!bTmpRes)
{
bTmpRes = xSpell->isValid( aChkWord, aLocale, rProperties );
// Add correct words to the cache.
// But not those that are correct only because of
// the temporary supplied settings.
if (bTmpRes && 0 == rProperties.getLength())
GetCache().AddWord( aChkWord, nLanguage );
}
}
else
bTmpResValid = false;
if (bTmpResValid)
bRes = bTmpRes;
pEntry->nLastTriedSvcIndex = (sal_Int16) i;
++i;
}
// if language is not supported by any of the services
// remove it from the list.
if (i == nLen)
{
if (!SvcListHasLanguage( *pEntry, nLanguage ))
aSvcMap.erase( nLanguage );
}
}
// cross-check against results from dictionaries which have precedence!
if (GetDicList().is() && IsUseDicList( rProperties, GetPropSet() ))
{
Reference< XDictionaryEntry > xTmp( lcl_GetRulingDictionaryEntry( aChkWord, nLanguage ) );
if (xTmp.is()) {
bRes = !xTmp->isNegative();
} else {
setCharClass(LanguageTag(nLanguage));
CapType ct = capitalType(aChkWord, pCharClass);
if (ct == CapType::INITCAP || ct == CapType::ALLCAP) {
Reference< XDictionaryEntry > xTmp2( lcl_GetRulingDictionaryEntry( makeLowerCase(aChkWord, pCharClass), nLanguage ) );
if (xTmp2.is()) {
bRes = !xTmp2->isNegative();
}
}
}
}
}
return bRes;
}
Reference< XSpellAlternatives > SpellCheckerDispatcher::spell_Impl(
const OUString& rWord,
LanguageType nLanguage,
const PropertyValues& rProperties )
throw(IllegalArgumentException, RuntimeException, std::exception)
{
MutexGuard aGuard( GetLinguMutex() );
Reference< XSpellAlternatives > xRes;
if (LinguIsUnspecified( nLanguage) || rWord.isEmpty())
return xRes;
// search for entry with that language
SpellSvcByLangMap_t::iterator aIt( aSvcMap.find( nLanguage ) );
LangSvcEntries_Spell *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : nullptr;
if (pEntry)
{
OUString aChkWord( rWord );
Locale aLocale( LanguageTag::convertToLocale( nLanguage ) );
// replace typographical apostroph by ascii apostroph
OUString aSingleQuote( GetLocaleDataWrapper( nLanguage ).getQuotationMarkEnd() );
DBG_ASSERT( 1 == aSingleQuote.getLength(), "unexpectend length of quotation mark" );
if (!aSingleQuote.isEmpty())
aChkWord = aChkWord.replace( aSingleQuote[0], '\'' );
RemoveHyphens( aChkWord );
if (IsIgnoreControlChars( rProperties, GetPropSet() ))
RemoveControlChars( aChkWord );
sal_Int32 nLen = pEntry->aSvcRefs.getLength();
DBG_ASSERT( nLen == pEntry->aSvcImplNames.getLength(),
"lng : sequence length mismatch");
DBG_ASSERT( pEntry->nLastTriedSvcIndex < nLen,
"lng : index out of range");
sal_Int32 i = 0;
Reference< XSpellAlternatives > xTmpRes;
bool bTmpResValid = false;
// try already instantiated services first
{
const Reference< XSpellChecker > *pRef = pEntry->aSvcRefs.getConstArray();
sal_Int32 nNumSugestions = -1;
while (i <= pEntry->nLastTriedSvcIndex
&& (!bTmpResValid || xTmpRes.is()) )
{
bTmpResValid = true;
if (pRef[i].is() && pRef[i]->hasLocale( aLocale ))
{
bool bOK = GetCache().CheckWord( aChkWord, nLanguage );
if (bOK)
xTmpRes = nullptr;
else
{
xTmpRes = pRef[i]->spell( aChkWord, aLocale, rProperties );
// Add correct words to the cache.
// But not those that are correct only because of
// the temporary supplied settings.
if (!xTmpRes.is() && 0 == rProperties.getLength())
GetCache().AddWord( aChkWord, nLanguage );
}
}
else
bTmpResValid = false;
// return first found result if the word is not known by any checker.
// But if that result has no suggestions use the first one that does
// provide suggestions for the misspelled word.
if (!xRes.is() && bTmpResValid)
{
xRes = xTmpRes;
nNumSugestions = 0;
if (xRes.is())
nNumSugestions = xRes->getAlternatives().getLength();
}
sal_Int32 nTmpNumSugestions = 0;
if (xTmpRes.is() && bTmpResValid)
nTmpNumSugestions = xTmpRes->getAlternatives().getLength();
if (xRes.is() && nNumSugestions == 0 && nTmpNumSugestions > 0)
{
xRes = xTmpRes;
nNumSugestions = nTmpNumSugestions;
}
++i;
}
}
// if still no result instantiate new services and try those
if ((!bTmpResValid || xTmpRes.is())
&& pEntry->nLastTriedSvcIndex < nLen - 1)
{
const OUString *pImplNames = pEntry->aSvcImplNames.getConstArray();
Reference< XSpellChecker > *pRef = pEntry->aSvcRefs .getArray();
Reference< XComponentContext > xContext(
comphelper::getProcessComponentContext() );
// build service initialization argument
Sequence< Any > aArgs(2);
aArgs.getArray()[0] <<= GetPropSet();
sal_Int32 nNumSugestions = -1;
while (i < nLen && (!bTmpResValid || xTmpRes.is()))
{
// create specific service via it's implementation name
Reference< XSpellChecker > xSpell;
try
{
xSpell.set( xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
pImplNames[i], aArgs, xContext ),
UNO_QUERY );
}
catch (uno::Exception &)
{
SAL_WARN( "linguistic", "createInstanceWithArguments failed" );
}
pRef [i] = xSpell;
Reference< XLinguServiceEventBroadcaster >
xBroadcaster( xSpell, UNO_QUERY );
if (xBroadcaster.is())
rMgr.AddLngSvcEvtBroadcaster( xBroadcaster );
bTmpResValid = true;
if (xSpell.is() && xSpell->hasLocale( aLocale ))
{
bool bOK = GetCache().CheckWord( aChkWord, nLanguage );
if (bOK)
xTmpRes = nullptr;
else
{
xTmpRes = xSpell->spell( aChkWord, aLocale, rProperties );
// Add correct words to the cache.
// But not those that are correct only because of
// the temporary supplied settings.
if (!xTmpRes.is() && 0 == rProperties.getLength())
GetCache().AddWord( aChkWord, nLanguage );
}
}
else
bTmpResValid = false;
// return first found result if the word is not known by any checker.
// But if that result has no suggestions use the first one that does
// provide suggestions for the misspelled word.
if (!xRes.is() && bTmpResValid)
{
xRes = xTmpRes;
nNumSugestions = 0;
if (xRes.is())
nNumSugestions = xRes->getAlternatives().getLength();
}
sal_Int32 nTmpNumSugestions = 0;
if (xTmpRes.is() && bTmpResValid)
nTmpNumSugestions = xTmpRes->getAlternatives().getLength();
if (xRes.is() && nNumSugestions == 0 && nTmpNumSugestions > 0)
{
xRes = xTmpRes;
nNumSugestions = nTmpNumSugestions;
}
pEntry->nLastTriedSvcIndex = (sal_Int16) i;
++i;
}
// if language is not supported by any of the services
// remove it from the list.
if (i == nLen)
{
if (!SvcListHasLanguage( *pEntry, nLanguage ))
aSvcMap.erase( nLanguage );
}
}
// if word is finally found to be correct
// clear previously remembered alternatives
if (bTmpResValid && !xTmpRes.is())
xRes = nullptr;
// list of proposals found (to be checked against entries of
// negative dictionaries)
ProposalList aProposalList;
sal_Int16 eFailureType = -1; // no failure
if (xRes.is())
{
aProposalList.Append( xRes->getAlternatives() );
eFailureType = xRes->getFailureType();
}
Reference< XSearchableDictionaryList > xDList;
if (GetDicList().is() && IsUseDicList( rProperties, GetPropSet() ))
xDList = GetDicList();
// cross-check against results from user-dictionaries which have precedence!
if (xDList.is())
{
Reference< XDictionaryEntry > xTmp( lcl_GetRulingDictionaryEntry( aChkWord, nLanguage ) );
if (xTmp.is())
{
if (xTmp->isNegative()) // negative entry found
{
eFailureType = SpellFailure::IS_NEGATIVE_WORD;
// replacement text to be added to suggestions, if not empty
OUString aAddRplcTxt( xTmp->getReplacementText() );
// replacement text must not be in negative dictionary itself
if (!aAddRplcTxt.isEmpty() &&
!SearchDicList( xDList, aAddRplcTxt, nLanguage, false, true ).is())
{
aProposalList.Prepend( aAddRplcTxt );
}
}
else // positive entry found
{
xRes = nullptr;
eFailureType = -1; // no failure
}
}
else
{
setCharClass(LanguageTag(nLanguage));
CapType ct = capitalType(aChkWord, pCharClass);
if (ct == CapType::INITCAP || ct == CapType::ALLCAP)
{
Reference< XDictionaryEntry > xTmp2( lcl_GetRulingDictionaryEntry( makeLowerCase(aChkWord, pCharClass), nLanguage ) );
if (xTmp2.is())
{
if (xTmp2->isNegative()) // negative entry found
{
eFailureType = SpellFailure::IS_NEGATIVE_WORD;
// replacement text to be added to suggestions, if not empty
OUString aAddRplcTxt( xTmp2->getReplacementText() );
// replacement text must not be in negative dictionary itself
if (!aAddRplcTxt.isEmpty() &&
!SearchDicList( xDList, aAddRplcTxt, nLanguage, false, true ).is())
{
switch ( ct )
{
case CapType::INITCAP:
aProposalList.Prepend( pCharClass->titlecase(aAddRplcTxt) );
break;
case CapType::ALLCAP:
aProposalList.Prepend( pCharClass->uppercase(aAddRplcTxt) );
break;
default:
/* can't happen because of if ct == above */
break;
}
}
}
else // positive entry found
{
xRes = nullptr;
eFailureType = -1; // no failure
}
}
}
}
}
if (eFailureType != -1) // word misspelled or found in negative user-dictionary
{
// search suitable user-dictionaries for suggestions that are
// similar to the misspelled word
std::vector< OUString > aDicListProps; // list of proposals from user-dictionaries
SearchSimilarText( aChkWord, nLanguage, xDList, aDicListProps );
aProposalList.Append( aDicListProps );
std::vector< OUString > aProposals = aProposalList.GetVector();
// remove entries listed in negative dictionaries
// (we don't want to display suggestions that will be regarded as misspelled later on)
if (xDList.is())
SeqRemoveNegEntries( aProposals, xDList, nLanguage );
uno::Reference< linguistic2::XSetSpellAlternatives > xSetAlt( xRes, uno::UNO_QUERY );
if (xSetAlt.is())
{
xSetAlt->setAlternatives( comphelper::containerToSequence(aProposals) );
xSetAlt->setFailureType( eFailureType );
}
else
{
if (xRes.is())
{
SAL_WARN( "linguistic", "XSetSpellAlternatives not implemented!" );
}
else if (!aProposals.empty())
{
// no xRes but Proposals found from the user-dictionaries.
// Thus we need to create an xRes...
xRes = new linguistic::SpellAlternatives( rWord, nLanguage,
SpellFailure::IS_NEGATIVE_WORD, comphelper::containerToSequence(aProposals) );
}
}
}
}
return xRes;
}
uno::Sequence< sal_Int16 > SAL_CALL SpellCheckerDispatcher::getLanguages( )
throw (uno::RuntimeException, std::exception)
{
MutexGuard aGuard( GetLinguMutex() );
uno::Sequence< Locale > aTmp( getLocales() );
uno::Sequence< sal_Int16 > aRes( LocaleSeqToLangSeq( aTmp ) );
return aRes;
}
sal_Bool SAL_CALL SpellCheckerDispatcher::hasLanguage(
sal_Int16 nLanguage )
throw (uno::RuntimeException, std::exception)
{
MutexGuard aGuard( GetLinguMutex() );
return hasLocale( LanguageTag::convertToLocale( nLanguage) );
}
sal_Bool SAL_CALL SpellCheckerDispatcher::isValid(
const OUString& rWord,
sal_Int16 nLanguage,
const uno::Sequence< beans::PropertyValue >& rProperties )
throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
{
MutexGuard aGuard( GetLinguMutex() );
return isValid( rWord, LanguageTag::convertToLocale( nLanguage ), rProperties);
}
uno::Reference< linguistic2::XSpellAlternatives > SAL_CALL SpellCheckerDispatcher::spell(
const OUString& rWord,
sal_Int16 nLanguage,
const uno::Sequence< beans::PropertyValue >& rProperties )
throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
{
MutexGuard aGuard( GetLinguMutex() );
return spell( rWord, LanguageTag::convertToLocale( nLanguage), rProperties);
}
void SpellCheckerDispatcher::SetServiceList( const Locale &rLocale,
const Sequence< OUString > &rSvcImplNames )
{
MutexGuard aGuard( GetLinguMutex() );
if (pCache)
pCache->Flush(); // new services may spell differently...
sal_Int16 nLanguage = LinguLocaleToLanguage( rLocale );
sal_Int32 nLen = rSvcImplNames.getLength();
if (0 == nLen)
// remove entry
aSvcMap.erase( nLanguage );
else
{
// modify/add entry
LangSvcEntries_Spell *pEntry = aSvcMap[ nLanguage ].get();
if (pEntry)
{
pEntry->Clear();
pEntry->aSvcImplNames = rSvcImplNames;
pEntry->aSvcRefs = Sequence< Reference < XSpellChecker > > ( nLen );
}
else
{
std::shared_ptr< LangSvcEntries_Spell > pTmpEntry( new LangSvcEntries_Spell( rSvcImplNames ) );
pTmpEntry->aSvcRefs = Sequence< Reference < XSpellChecker > >( nLen );
aSvcMap[ nLanguage ] = pTmpEntry;
}
}
}
Sequence< OUString >
SpellCheckerDispatcher::GetServiceList( const Locale &rLocale ) const
{
MutexGuard aGuard( GetLinguMutex() );
Sequence< OUString > aRes;
// search for entry with that language and use data from that
sal_Int16 nLanguage = LinguLocaleToLanguage( rLocale );
const SpellSvcByLangMap_t::const_iterator aIt( aSvcMap.find( nLanguage ) );
const LangSvcEntries_Spell *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : nullptr;
if (pEntry)
aRes = pEntry->aSvcImplNames;
return aRes;
}
void SpellCheckerDispatcher::FlushSpellCache()
{
if (pCache)
pCache->Flush();
}
void SpellCheckerDispatcher::setCharClass(const LanguageTag& rLanguageTag)
{
if (!pCharClass)
pCharClass = new CharClass(rLanguageTag);
pCharClass->setLanguageTag(rLanguageTag);
}
OUString SAL_CALL SpellCheckerDispatcher::makeLowerCase(const OUString& aTerm, CharClass * pCC)
{
if (pCC)
return pCC->lowercase(aTerm);
return aTerm;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|