1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <sal/macros.h>
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <com/sun/star/container/ElementExistException.hpp>
23 #include <com/sun/star/container/XNameAccess.hpp>
24 #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 #include <com/sun/star/i18n/BreakIterator.hpp>
26 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
27 #include <com/sun/star/lang/XComponent.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/linguistic2/XSupportedLocales.hpp>
31 #include <com/sun/star/linguistic2/XProofreader.hpp>
32 #include <com/sun/star/linguistic2/XProofreadingIterator.hpp>
33 #include <com/sun/star/linguistic2/SingleProofreadingError.hpp>
34 #include <com/sun/star/linguistic2/ProofreadingResult.hpp>
35 #include <com/sun/star/linguistic2/LinguServiceEvent.hpp>
36 #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp>
37 #include <com/sun/star/text/TextMarkupType.hpp>
38 #include <com/sun/star/text/TextMarkupDescriptor.hpp>
39 #include <com/sun/star/text/XMultiTextMarkup.hpp>
40 #include <com/sun/star/text/XFlatParagraph.hpp>
41 #include <com/sun/star/text/XFlatParagraphIterator.hpp>
42 #include <com/sun/star/uno/XComponentContext.hpp>
44 #include <sal/config.h>
45 #include <sal/log.hxx>
46 #include <o3tl/safeint.hxx>
47 #include <osl/conditn.hxx>
48 #include <cppuhelper/supportsservice.hxx>
49 #include <cppuhelper/weak.hxx>
50 #include <i18nlangtag/languagetag.hxx>
51 #include <comphelper/processfactory.hxx>
52 #include <comphelper/propertysequence.hxx>
53 #include <tools/debug.hxx>
54 #include <comphelper/diagnose_ex.hxx>
58 #include <linguistic/misc.hxx>
60 #include "gciterator.hxx"
62 using namespace linguistic
;
63 using namespace ::com::sun::star
;
65 // white space list: obtained from the fonts.config.txt of a Linux system.
66 const sal_Unicode aWhiteSpaces
[] =
69 0x00a0, /* NO-BREAK SPACE */
70 0x00ad, /* SOFT HYPHEN */
71 0x115f, /* HANGUL CHOSEONG FILLER */
72 0x1160, /* HANGUL JUNGSEONG FILLER */
73 0x1680, /* OGHAM SPACE MARK */
76 0x2002, /* EN SPACE */
77 0x2003, /* EM SPACE */
78 0x2004, /* THREE-PER-EM SPACE */
79 0x2005, /* FOUR-PER-EM SPACE */
80 0x2006, /* SIX-PER-EM SPACE */
81 0x2007, /* FIGURE SPACE */
82 0x2008, /* PUNCTUATION SPACE */
83 0x2009, /* THIN SPACE */
84 0x200a, /* HAIR SPACE */
85 0x200b, /* ZERO WIDTH SPACE */
86 0x200c, /* ZERO WIDTH NON-JOINER */
87 0x200d, /* ZERO WIDTH JOINER */
88 0x200e, /* LEFT-TO-RIGHT MARK */
89 0x200f, /* RIGHT-TO-LEFT MARK */
90 0x2028, /* LINE SEPARATOR */
91 0x2029, /* PARAGRAPH SEPARATOR */
92 0x202a, /* LEFT-TO-RIGHT EMBEDDING */
93 0x202b, /* RIGHT-TO-LEFT EMBEDDING */
94 0x202c, /* POP DIRECTIONAL FORMATTING */
95 0x202d, /* LEFT-TO-RIGHT OVERRIDE */
96 0x202e, /* RIGHT-TO-LEFT OVERRIDE */
97 0x202f, /* NARROW NO-BREAK SPACE */
98 0x205f, /* MEDIUM MATHEMATICAL SPACE */
99 0x2060, /* WORD JOINER */
100 0x2061, /* FUNCTION APPLICATION */
101 0x2062, /* INVISIBLE TIMES */
102 0x2063, /* INVISIBLE SEPARATOR */
103 0x206A, /* INHIBIT SYMMETRIC SWAPPING */
104 0x206B, /* ACTIVATE SYMMETRIC SWAPPING */
105 0x206C, /* INHIBIT ARABIC FORM SHAPING */
106 0x206D, /* ACTIVATE ARABIC FORM SHAPING */
107 0x206E, /* NATIONAL DIGIT SHAPES */
108 0x206F, /* NOMINAL DIGIT SHAPES */
109 0x3000, /* IDEOGRAPHIC SPACE */
110 0x3164, /* HANGUL FILLER */
111 0xfeff, /* ZERO WIDTH NO-BREAK SPACE */
112 0xffa0, /* HALFWIDTH HANGUL FILLER */
113 0xfff9, /* INTERLINEAR ANNOTATION ANCHOR */
114 0xfffa, /* INTERLINEAR ANNOTATION SEPARATOR */
115 0xfffb /* INTERLINEAR ANNOTATION TERMINATOR */
118 // Information about reason for proofreading (ProofInfo)
119 const sal_Int32 PROOFINFO_GET_PROOFRESULT
= 1;
120 const sal_Int32 PROOFINFO_MARK_PARAGRAPH
= 2;
122 const int nWhiteSpaces
= SAL_N_ELEMENTS( aWhiteSpaces
);
124 static bool lcl_IsWhiteSpace( sal_Unicode cChar
)
127 for (int i
= 0; i
< nWhiteSpaces
&& !bFound
; ++i
)
129 if (cChar
== aWhiteSpaces
[i
])
135 static sal_Int32
lcl_SkipWhiteSpaces( const OUString
&rText
, sal_Int32 nStartPos
)
137 // note having nStartPos point right behind the string is OK since that one
138 // is a correct end-of-sentence position to be returned from a grammar checker...
140 const sal_Int32 nLen
= rText
.getLength();
141 bool bIllegalArgument
= false;
144 bIllegalArgument
= true;
147 if (nStartPos
> nLen
)
149 bIllegalArgument
= true;
152 if (bIllegalArgument
)
154 SAL_WARN( "linguistic", "lcl_SkipWhiteSpaces: illegal arguments" );
157 sal_Int32 nRes
= nStartPos
;
158 if (0 <= nStartPos
&& nStartPos
< nLen
)
160 const sal_Unicode
* const pEnd
= rText
.getStr() + nLen
;
161 const sal_Unicode
*pText
= rText
.getStr() + nStartPos
;
162 while (pText
!= pEnd
&& lcl_IsWhiteSpace(*pText
))
164 nRes
= pText
- rText
.getStr();
167 DBG_ASSERT( 0 <= nRes
&& nRes
<= nLen
, "lcl_SkipWhiteSpaces return value out of range" );
171 static sal_Int32
lcl_BacktraceWhiteSpaces( const OUString
&rText
, sal_Int32 nStartPos
)
173 // note: having nStartPos point right behind the string is OK since that one
174 // is a correct end-of-sentence position to be returned from a grammar checker...
176 const sal_Int32 nLen
= rText
.getLength();
177 bool bIllegalArgument
= false;
180 bIllegalArgument
= true;
183 if (nStartPos
> nLen
)
185 bIllegalArgument
= true;
188 if (bIllegalArgument
)
190 SAL_WARN( "linguistic", "lcl_BacktraceWhiteSpaces: illegal arguments" );
193 sal_Int32 nRes
= nStartPos
;
194 sal_Int32 nPosBefore
= nStartPos
- 1;
195 const sal_Unicode
*pStart
= rText
.getStr();
196 if (0 <= nPosBefore
&& nPosBefore
< nLen
&& lcl_IsWhiteSpace( pStart
[ nPosBefore
] ))
198 nStartPos
= nPosBefore
;
199 const sal_Unicode
*pText
= rText
.getStr() + nStartPos
;
200 while (pText
> pStart
&& lcl_IsWhiteSpace( *pText
))
202 // now add 1 since we want to point to the first char after the last char in the sentence...
203 nRes
= pText
- pStart
+ 1;
206 DBG_ASSERT( 0 <= nRes
&& nRes
<= nLen
, "lcl_BacktraceWhiteSpaces return value out of range" );
213 static void lcl_workerfunc (void * gci
)
215 osl_setThreadName("GrammarCheckingIterator");
217 static_cast<GrammarCheckingIterator
*>(gci
)->DequeueAndCheck();
222 static lang::Locale
lcl_GetPrimaryLanguageOfSentence(
223 const uno::Reference
< text::XFlatParagraph
>& xFlatPara
,
224 sal_Int32 nStartIndex
)
226 //get the language of the first word
227 return xFlatPara
->getLanguageOfText( nStartIndex
, 1 );
231 LngXStringKeyMap::LngXStringKeyMap() {}
233 void SAL_CALL
LngXStringKeyMap::insertValue(const OUString
& aKey
, const css::uno::Any
& aValue
)
235 std::map
<OUString
, css::uno::Any
>::const_iterator aIter
= maMap
.find(aKey
);
236 if (aIter
!= maMap
.end())
237 throw css::container::ElementExistException();
239 maMap
[aKey
] = aValue
;
242 css::uno::Any SAL_CALL
LngXStringKeyMap::getValue(const OUString
& aKey
)
244 std::map
<OUString
, css::uno::Any
>::const_iterator aIter
= maMap
.find(aKey
);
245 if (aIter
== maMap
.end())
246 throw css::container::NoSuchElementException();
248 return (*aIter
).second
;
251 sal_Bool SAL_CALL
LngXStringKeyMap::hasValue(const OUString
& aKey
)
253 return maMap
.find(aKey
) != maMap
.end();
256 ::sal_Int32 SAL_CALL
LngXStringKeyMap::getCount() { return maMap
.size(); }
258 OUString SAL_CALL
LngXStringKeyMap::getKeyByIndex(::sal_Int32 nIndex
)
260 if (nIndex
< 0 || o3tl::make_unsigned(nIndex
) >= maMap
.size())
261 throw css::lang::IndexOutOfBoundsException();
266 css::uno::Any SAL_CALL
LngXStringKeyMap::getValueByIndex(::sal_Int32 nIndex
)
268 if (nIndex
< 0 || o3tl::make_unsigned(nIndex
) >= maMap
.size())
269 throw css::lang::IndexOutOfBoundsException();
271 return css::uno::Any();
275 osl::Mutex
& GrammarCheckingIterator::MyMutex()
277 static osl::Mutex SINGLETON
;
281 GrammarCheckingIterator::GrammarCheckingIterator() :
283 m_bGCServicesChecked( false ),
284 m_nDocIdCounter( 0 ),
286 m_aEventListeners( MyMutex() ),
287 m_aNotifyListeners( MyMutex() )
292 GrammarCheckingIterator::~GrammarCheckingIterator()
297 void GrammarCheckingIterator::TerminateThread()
301 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
305 m_aWakeUpThread
.set();
309 osl_joinWithThread(t
);
310 osl_destroyThread(t
);
314 sal_Int32
GrammarCheckingIterator::NextDocId()
316 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
317 m_nDocIdCounter
+= 1;
318 return m_nDocIdCounter
;
322 OUString
GrammarCheckingIterator::GetOrCreateDocId(
323 const uno::Reference
< lang::XComponent
> &xComponent
)
325 // internal method; will always be called with locked mutex
330 if (m_aDocIdMap
.find( xComponent
.get() ) != m_aDocIdMap
.end())
332 // return already existing entry
333 aRes
= m_aDocIdMap
[ xComponent
.get() ];
335 else // add new entry
337 sal_Int32 nRes
= NextDocId();
338 aRes
= OUString::number( nRes
);
339 m_aDocIdMap
[ xComponent
.get() ] = aRes
;
340 xComponent
->addEventListener( this );
347 void GrammarCheckingIterator::AddEntry(
348 const uno::Reference
< text::XFlatParagraphIterator
>& xFlatParaIterator
,
349 const uno::Reference
< text::XFlatParagraph
>& xFlatPara
,
350 const OUString
& rDocId
,
351 sal_Int32 nStartIndex
,
354 // we may not need/have a xFlatParaIterator (e.g. if checkGrammarAtPos was called)
355 // but we always need a xFlatPara...
360 aNewFPEntry
.m_xParaIterator
= xFlatParaIterator
;
361 aNewFPEntry
.m_xPara
= xFlatPara
;
362 aNewFPEntry
.m_aDocId
= rDocId
;
363 aNewFPEntry
.m_nStartIndex
= nStartIndex
;
364 aNewFPEntry
.m_bAutomatic
= bAutomatic
;
366 // add new entry to the end of this queue
367 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
369 m_thread
= osl_createThread( lcl_workerfunc
, this );
370 m_aFPEntriesQueue
.push_back( aNewFPEntry
);
372 // wake up the thread in order to do grammar checking
373 m_aWakeUpThread
.set();
377 void GrammarCheckingIterator::ProcessResult(
378 const linguistic2::ProofreadingResult
&rRes
,
379 const uno::Reference
< text::XFlatParagraphIterator
> &rxFlatParagraphIterator
,
380 bool bIsAutomaticChecking
)
382 DBG_ASSERT( rRes
.xFlatParagraph
.is(), "xFlatParagraph is missing" );
383 //no guard necessary as no members are used
384 bool bContinueWithNextPara
= false;
385 if (!rRes
.xFlatParagraph
.is() || rRes
.xFlatParagraph
->isModified())
387 // if paragraph was modified/deleted meanwhile continue with the next one...
388 bContinueWithNextPara
= true;
390 else // paragraph is still unchanged...
392 // mark found errors...
394 sal_Int32 nTextLen
= rRes
.aText
.getLength();
395 bool bBoundariesOk
= 0 <= rRes
.nStartOfSentencePosition
&& rRes
.nStartOfSentencePosition
<= nTextLen
&&
396 0 <= rRes
.nBehindEndOfSentencePosition
&& rRes
.nBehindEndOfSentencePosition
<= nTextLen
&&
397 0 <= rRes
.nStartOfNextSentencePosition
&& rRes
.nStartOfNextSentencePosition
<= nTextLen
&&
398 rRes
.nStartOfSentencePosition
<= rRes
.nBehindEndOfSentencePosition
&&
399 rRes
.nBehindEndOfSentencePosition
<= rRes
.nStartOfNextSentencePosition
;
400 DBG_ASSERT( bBoundariesOk
, "inconsistent sentence boundaries" );
402 uno::Reference
< text::XMultiTextMarkup
> xMulti( rRes
.xFlatParagraph
, uno::UNO_QUERY
);
403 if (xMulti
.is()) // use new API for markups
407 // length = number of found errors + 1 sentence markup
408 sal_Int32 nErrors
= rRes
.aErrors
.getLength();
409 uno::Sequence
< text::TextMarkupDescriptor
> aDescriptors( nErrors
+ 1 );
410 text::TextMarkupDescriptor
* pDescriptors
= aDescriptors
.getArray();
412 // at pos 0 .. nErrors-1 -> all grammar errors
413 for (const linguistic2::SingleProofreadingError
&rError
: rRes
.aErrors
)
415 text::TextMarkupDescriptor
&rDesc
= *pDescriptors
++;
417 rDesc
.nType
= rError
.nErrorType
;
418 rDesc
.nOffset
= rError
.nErrorStart
;
419 rDesc
.nLength
= rError
.nErrorLength
;
421 // the proofreader may return SPELLING but right now our core
422 // does only handle PROOFREADING if the result is from the proofreader...
423 // (later on we may wish to color spelling errors found by the proofreader
424 // differently for example. But no special handling right now.
425 if (rDesc
.nType
== text::TextMarkupType::SPELLCHECK
)
426 rDesc
.nType
= text::TextMarkupType::PROOFREADING
;
428 uno::Reference
< container::XStringKeyMap
> xKeyMap(
429 new LngXStringKeyMap());
430 for( const beans::PropertyValue
& rProperty
: rError
.aProperties
)
432 if ( rProperty
.Name
== "LineColor" )
434 xKeyMap
->insertValue(rProperty
.Name
,
436 rDesc
.xMarkupInfoContainer
= xKeyMap
;
438 else if ( rProperty
.Name
== "LineType" )
440 xKeyMap
->insertValue(rProperty
.Name
,
442 rDesc
.xMarkupInfoContainer
= xKeyMap
;
447 // at pos nErrors -> sentence markup
448 // nSentenceLength: includes the white-spaces following the sentence end...
449 const sal_Int32 nSentenceLength
= rRes
.nStartOfNextSentencePosition
- rRes
.nStartOfSentencePosition
;
450 pDescriptors
->nType
= text::TextMarkupType::SENTENCE
;
451 pDescriptors
->nOffset
= rRes
.nStartOfSentencePosition
;
452 pDescriptors
->nLength
= nSentenceLength
;
454 xMulti
->commitMultiTextMarkup( aDescriptors
) ;
456 catch (lang::IllegalArgumentException
&)
458 TOOLS_WARN_EXCEPTION( "linguistic", "commitMultiTextMarkup" );
462 // other sentences left to be checked in this paragraph?
463 if (rRes
.nStartOfNextSentencePosition
< rRes
.aText
.getLength())
465 AddEntry( rxFlatParagraphIterator
, rRes
.xFlatParagraph
, rRes
.aDocumentIdentifier
, rRes
.nStartOfNextSentencePosition
, bIsAutomaticChecking
);
467 else // current paragraph finished
469 // set "already checked" flag for the current flat paragraph
470 if (rRes
.xFlatParagraph
.is())
471 rRes
.xFlatParagraph
->setChecked( text::TextMarkupType::PROOFREADING
, true );
473 bContinueWithNextPara
= true;
477 if (bContinueWithNextPara
)
479 // we need to continue with the next paragraph
480 if (rxFlatParagraphIterator
.is())
481 AddEntry(rxFlatParagraphIterator
, rxFlatParagraphIterator
->getNextPara(),
482 rRes
.aDocumentIdentifier
, 0, bIsAutomaticChecking
);
487 std::pair
<OUString
, std::optional
<OUString
>>
488 GrammarCheckingIterator::getServiceForLocale(const lang::Locale
& rLocale
) const
490 if (!rLocale
.Language
.isEmpty())
492 const OUString sBcp47
= LanguageTag::convertToBcp47(rLocale
, false);
493 GCImplNames_t::const_iterator
aLangIt(m_aGCImplNamesByLang
.find(sBcp47
));
494 if (aLangIt
!= m_aGCImplNamesByLang
.end())
495 return { aLangIt
->second
, {} };
497 for (const auto& sFallbackBcp47
: LanguageTag(rLocale
).getFallbackStrings(false))
499 aLangIt
= m_aGCImplNamesByLang
.find(sFallbackBcp47
);
500 if (aLangIt
!= m_aGCImplNamesByLang
.end())
501 return { aLangIt
->second
, sFallbackBcp47
};
509 uno::Reference
< linguistic2::XProofreader
> GrammarCheckingIterator::GetGrammarChecker(
510 lang::Locale
&rLocale
)
512 uno::Reference
< linguistic2::XProofreader
> xRes
;
514 // ---- THREAD SAFE START ----
515 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
517 // check supported locales for each grammarchecker if not already done
518 if (!m_bGCServicesChecked
)
520 GetConfiguredGCSvcs_Impl();
521 m_bGCServicesChecked
= true;
524 if (const auto& [aSvcImplName
, oFallbackBcp47
] = getServiceForLocale(rLocale
);
525 !aSvcImplName
.isEmpty()) // matching configured language found?
528 rLocale
= LanguageTag::convertToLocale(*oFallbackBcp47
, false);
529 GCReferences_t::const_iterator
aImplNameIt( m_aGCReferencesByService
.find( aSvcImplName
) );
530 if (aImplNameIt
!= m_aGCReferencesByService
.end()) // matching impl name found?
532 xRes
= aImplNameIt
->second
;
534 else // the service is to be instantiated here for the first time...
538 uno::Reference
< uno::XComponentContext
> xContext( comphelper::getProcessComponentContext() );
539 uno::Reference
< linguistic2::XProofreader
> xGC(
540 xContext
->getServiceManager()->createInstanceWithContext(aSvcImplName
, xContext
),
541 uno::UNO_QUERY_THROW
);
542 uno::Reference
< linguistic2::XSupportedLocales
> xSuppLoc( xGC
, uno::UNO_QUERY_THROW
);
544 if (xSuppLoc
->hasLocale( rLocale
))
546 m_aGCReferencesByService
[ aSvcImplName
] = xGC
;
549 uno::Reference
< linguistic2::XLinguServiceEventBroadcaster
> xBC( xGC
, uno::UNO_QUERY
);
551 xBC
->addLinguServiceEventListener( this );
555 SAL_WARN( "linguistic", "grammar checker does not support required locale" );
558 catch (uno::Exception
&)
560 SAL_WARN( "linguistic", "instantiating grammar checker failed" );
564 else // not found - quite normal
566 SAL_INFO("linguistic", "No grammar checker found for \""
567 << LanguageTag::convertToBcp47(rLocale
, false) << "\"");
569 // ---- THREAD SAFE END ----
574 static uno::Sequence
<beans::PropertyValue
>
575 lcl_makeProperties(uno::Reference
<text::XFlatParagraph
> const& xFlatPara
, sal_Int32 nProofInfo
)
577 uno::Reference
<beans::XPropertySet
> const xProps(
578 xFlatPara
, uno::UNO_QUERY_THROW
);
579 css::uno::Any
a (nProofInfo
);
580 return comphelper::InitPropertySequence({
581 { "FieldPositions", xProps
->getPropertyValue("FieldPositions") },
582 { "FootnotePositions", xProps
->getPropertyValue("FootnotePositions") },
583 { "SortedTextId", xProps
->getPropertyValue("SortedTextId") },
584 { "DocumentElementsCount", xProps
->getPropertyValue("DocumentElementsCount") },
589 void GrammarCheckingIterator::DequeueAndCheck()
593 // ---- THREAD SAFE START ----
594 bool bQueueEmpty
= false;
596 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
601 bQueueEmpty
= m_aFPEntriesQueue
.empty();
603 // ---- THREAD SAFE END ----
607 uno::Reference
< text::XFlatParagraphIterator
> xFPIterator
;
608 uno::Reference
< text::XFlatParagraph
> xFlatPara
;
609 FPEntry aFPEntryItem
;
611 // ---- THREAD SAFE START ----
613 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
614 aFPEntryItem
= m_aFPEntriesQueue
.front();
615 xFPIterator
= aFPEntryItem
.m_xParaIterator
;
616 xFlatPara
= aFPEntryItem
.m_xPara
;
617 m_aCurCheckedDocId
= aFPEntryItem
.m_aDocId
;
618 aCurDocId
= m_aCurCheckedDocId
;
620 m_aFPEntriesQueue
.pop_front();
622 // ---- THREAD SAFE END ----
624 if (xFlatPara
.is() && xFPIterator
.is())
628 OUString
aCurTxt( xFlatPara
->getText() );
629 lang::Locale aCurLocale
= lcl_GetPrimaryLanguageOfSentence( xFlatPara
, aFPEntryItem
.m_nStartIndex
);
631 const bool bModified
= xFlatPara
->isModified();
634 linguistic2::ProofreadingResult aRes
;
636 // ---- THREAD SAFE START ----
638 osl::ClearableMutexGuard
aGuard(MyMutex());
640 sal_Int32 nStartPos
= aFPEntryItem
.m_nStartIndex
;
641 sal_Int32 nSuggestedEnd
642 = GetSuggestedEndOfSentence(aCurTxt
, nStartPos
, aCurLocale
);
643 DBG_ASSERT((nSuggestedEnd
== 0 && aCurTxt
.isEmpty())
644 || nSuggestedEnd
> nStartPos
,
645 "nSuggestedEndOfSentencePos calculation failed?");
647 uno::Reference
<linguistic2::XProofreader
> xGC
=
648 GetGrammarChecker(aCurLocale
);
652 uno::Sequence
<beans::PropertyValue
> const aProps(
653 lcl_makeProperties(xFlatPara
, PROOFINFO_MARK_PARAGRAPH
));
654 aRes
= xGC
->doProofreading(aCurDocId
, aCurTxt
, aCurLocale
,
655 nStartPos
, nSuggestedEnd
, aProps
);
657 //!! work-around to prevent looping if the grammar checker
658 //!! failed to properly identify the sentence end
659 if (aRes
.nBehindEndOfSentencePosition
<= nStartPos
660 && aRes
.nBehindEndOfSentencePosition
!= nSuggestedEnd
)
664 "!! Grammarchecker failed to provide end of sentence !!");
665 aRes
.nBehindEndOfSentencePosition
= nSuggestedEnd
;
668 aRes
.xFlatParagraph
= xFlatPara
;
669 aRes
.nStartOfSentencePosition
= nStartPos
;
673 // no grammar checker -> no error
674 // but we need to provide the data below in order to continue with the next sentence
675 aRes
.aDocumentIdentifier
= aCurDocId
;
676 aRes
.xFlatParagraph
= xFlatPara
;
677 aRes
.aText
= aCurTxt
;
678 aRes
.aLocale
= aCurLocale
;
679 aRes
.nStartOfSentencePosition
= nStartPos
;
680 aRes
.nBehindEndOfSentencePosition
= nSuggestedEnd
;
682 aRes
.nStartOfNextSentencePosition
683 = lcl_SkipWhiteSpaces(aCurTxt
, aRes
.nBehindEndOfSentencePosition
);
684 aRes
.nBehindEndOfSentencePosition
= lcl_BacktraceWhiteSpaces(
685 aCurTxt
, aRes
.nStartOfNextSentencePosition
);
687 //guard has to be cleared as ProcessResult calls out of this class
689 // ---- THREAD SAFE END ----
690 ProcessResult( aRes
, xFPIterator
, aFPEntryItem
.m_bAutomatic
);
694 // the paragraph changed meanwhile... (and maybe is still edited)
695 // thus we simply continue to ask for the next to be checked.
696 uno::Reference
< text::XFlatParagraph
> xFlatParaNext( xFPIterator
->getNextPara() );
697 AddEntry( xFPIterator
, xFlatParaNext
, aCurDocId
, 0, aFPEntryItem
.m_bAutomatic
);
700 catch (css::uno::Exception
&)
702 TOOLS_WARN_EXCEPTION("linguistic", "GrammarCheckingIterator::DequeueAndCheck ignoring");
706 // ---- THREAD SAFE START ----
708 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
709 m_aCurCheckedDocId
.clear();
711 // ---- THREAD SAFE END ----
715 // ---- THREAD SAFE START ----
717 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
722 // Check queue state again
723 if (m_aFPEntriesQueue
.empty())
724 m_aWakeUpThread
.reset();
726 // ---- THREAD SAFE END ----
728 //if the queue is empty
729 // IMPORTANT: Don't call condition.wait() with locked
730 // mutex. Otherwise you would keep out other threads
731 // to add entries to the queue! A condition is thread-
733 m_aWakeUpThread
.wait();
739 void SAL_CALL
GrammarCheckingIterator::startProofreading(
740 const uno::Reference
< ::uno::XInterface
> & xDoc
,
741 const uno::Reference
< text::XFlatParagraphIteratorProvider
> & xIteratorProvider
)
743 // get paragraph to start checking with
744 const bool bAutomatic
= true;
745 uno::Reference
<text::XFlatParagraphIterator
> xFPIterator
= xIteratorProvider
->getFlatParagraphIterator(
746 text::TextMarkupType::PROOFREADING
, bAutomatic
);
747 uno::Reference
< text::XFlatParagraph
> xPara( xFPIterator
.is()? xFPIterator
->getFirstPara() : nullptr );
748 uno::Reference
< lang::XComponent
> xComponent( xDoc
, uno::UNO_QUERY
);
750 // ---- THREAD SAFE START ----
751 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
752 if (xPara
.is() && xComponent
.is())
754 OUString aDocId
= GetOrCreateDocId( xComponent
);
756 // create new entry and add it to queue
757 AddEntry( xFPIterator
, xPara
, aDocId
, 0, bAutomatic
);
759 // ---- THREAD SAFE END ----
763 linguistic2::ProofreadingResult SAL_CALL
GrammarCheckingIterator::checkSentenceAtPosition(
764 const uno::Reference
< uno::XInterface
>& xDoc
,
765 const uno::Reference
< text::XFlatParagraph
>& xFlatPara
,
766 const OUString
& rText
,
768 sal_Int32 nStartOfSentencePos
,
769 sal_Int32 nSuggestedEndOfSentencePos
,
770 sal_Int32 nErrorPosInPara
)
772 // for the context menu...
774 linguistic2::ProofreadingResult aRes
;
776 uno::Reference
< lang::XComponent
> xComponent( xDoc
, uno::UNO_QUERY
);
777 if (xFlatPara
.is() && xComponent
.is() &&
778 ( nErrorPosInPara
< 0 || nErrorPosInPara
< rText
.getLength()))
780 // iterate through paragraph until we find the sentence we are interested in
781 linguistic2::ProofreadingResult aTmpRes
;
782 sal_Int32 nStartPos
= nStartOfSentencePos
>= 0 ? nStartOfSentencePos
: 0;
787 lang::Locale aCurLocale
= lcl_GetPrimaryLanguageOfSentence( xFlatPara
, nStartPos
);
788 sal_Int32 nOldStartOfSentencePos
= nStartPos
;
789 uno::Reference
< linguistic2::XProofreader
> xGC
;
792 // ---- THREAD SAFE START ----
794 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
795 aDocId
= GetOrCreateDocId( xComponent
);
796 nSuggestedEndOfSentencePos
= GetSuggestedEndOfSentence( rText
, nStartPos
, aCurLocale
);
797 DBG_ASSERT( nSuggestedEndOfSentencePos
> nStartPos
, "nSuggestedEndOfSentencePos calculation failed?" );
799 xGC
= GetGrammarChecker( aCurLocale
);
801 // ---- THREAD SAFE START ----
802 sal_Int32 nEndPos
= -1;
805 uno::Sequence
<beans::PropertyValue
> const aProps(
806 lcl_makeProperties(xFlatPara
, PROOFINFO_GET_PROOFRESULT
));
807 aTmpRes
= xGC
->doProofreading( aDocId
, rText
,
808 aCurLocale
, nStartPos
, nSuggestedEndOfSentencePos
, aProps
);
810 //!! work-around to prevent looping if the grammar checker
811 //!! failed to properly identify the sentence end
812 if (aTmpRes
.nBehindEndOfSentencePosition
<= nStartPos
)
814 SAL_WARN( "linguistic", "!! Grammarchecker failed to provide end of sentence !!" );
815 aTmpRes
.nBehindEndOfSentencePosition
= nSuggestedEndOfSentencePos
;
818 aTmpRes
.xFlatParagraph
= xFlatPara
;
819 aTmpRes
.nStartOfSentencePosition
= nStartPos
;
820 nEndPos
= aTmpRes
.nBehindEndOfSentencePosition
;
822 if ((nErrorPosInPara
< 0 || nStartPos
<= nErrorPosInPara
) && nErrorPosInPara
< nEndPos
)
825 if (nEndPos
== -1) // no result from grammar checker
826 nEndPos
= nSuggestedEndOfSentencePos
;
827 nStartPos
= lcl_SkipWhiteSpaces( rText
, nEndPos
);
828 aTmpRes
.nBehindEndOfSentencePosition
= nEndPos
;
829 aTmpRes
.nStartOfNextSentencePosition
= nStartPos
;
830 aTmpRes
.nBehindEndOfSentencePosition
= lcl_BacktraceWhiteSpaces( rText
, aTmpRes
.nStartOfNextSentencePosition
);
832 // prevent endless loop by forcefully advancing if needs be...
833 if (nStartPos
<= nOldStartOfSentencePos
)
835 SAL_WARN( "linguistic", "end-of-sentence detection failed?" );
836 nStartPos
= nOldStartOfSentencePos
+ 1;
839 while (!bFound
&& nStartPos
< rText
.getLength());
841 if (bFound
&& !xFlatPara
->isModified())
849 sal_Int32
GrammarCheckingIterator::GetSuggestedEndOfSentence(
850 const OUString
&rText
,
851 sal_Int32 nSentenceStartPos
,
852 const lang::Locale
&rLocale
)
854 // internal method; will always be called with locked mutex
856 if (!m_xBreakIterator
.is())
858 uno::Reference
< uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
859 m_xBreakIterator
= i18n::BreakIterator::create(xContext
);
861 sal_Int32 nTextLen
= rText
.getLength();
862 sal_Int32
nEndPosition(0);
863 sal_Int32 nTmpStartPos
= nSentenceStartPos
;
866 sal_Int32
const nPrevEndPosition(nEndPosition
);
867 nEndPosition
= nTextLen
;
868 if (nTmpStartPos
< nTextLen
)
870 nEndPosition
= m_xBreakIterator
->endOfSentence( rText
, nTmpStartPos
, rLocale
);
871 if (nEndPosition
<= nPrevEndPosition
)
873 // fdo#68750 if there's no progress at all then presumably
874 // there's no end of sentence in this paragraph so just
875 // set the end position to end of paragraph
876 nEndPosition
= nTextLen
;
879 if (nEndPosition
< 0)
880 nEndPosition
= nTextLen
;
884 while (nEndPosition
<= nSentenceStartPos
&& nEndPosition
< nTextLen
);
885 if (nEndPosition
> nTextLen
)
886 nEndPosition
= nTextLen
;
891 void SAL_CALL
GrammarCheckingIterator::resetIgnoreRules( )
893 for (auto const& elem
: m_aGCReferencesByService
)
895 uno::Reference
< linguistic2::XProofreader
> xGC(elem
.second
);
897 xGC
->resetIgnoreRules();
902 sal_Bool SAL_CALL
GrammarCheckingIterator::isProofreading(
903 const uno::Reference
< uno::XInterface
>& xDoc
)
905 // ---- THREAD SAFE START ----
906 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
910 uno::Reference
< lang::XComponent
> xComponent( xDoc
, uno::UNO_QUERY
);
913 // if the component was already used in one of the two calls to check text
914 // i.e. in startGrammarChecking or checkGrammarAtPos it will be found in the
915 // m_aDocIdMap unless the document already disposed.
916 // If it is not found then it is not yet being checked (or requested to being checked)
917 const DocMap_t::const_iterator
aIt( m_aDocIdMap
.find( xComponent
.get() ) );
918 if (aIt
!= m_aDocIdMap
.end())
920 // check in document is checked automatically in the background...
921 OUString aDocId
= aIt
->second
;
922 if (!m_aCurCheckedDocId
.isEmpty() && m_aCurCheckedDocId
== aDocId
)
924 // an entry for that document was dequeued and is currently being checked.
929 // we need to check if there is an entry for that document in the queue...
930 // That is the document is going to be checked sooner or later.
932 sal_Int32 nSize
= m_aFPEntriesQueue
.size();
933 for (sal_Int32 i
= 0; i
< nSize
&& !bRes
; ++i
)
935 if (aDocId
== m_aFPEntriesQueue
[i
].m_aDocId
)
941 // ---- THREAD SAFE END ----
947 void SAL_CALL
GrammarCheckingIterator::processLinguServiceEvent(
948 const linguistic2::LinguServiceEvent
& rLngSvcEvent
)
950 if (rLngSvcEvent
.nEvent
!= linguistic2::LinguServiceEventFlags::PROOFREAD_AGAIN
)
955 uno::Reference
< uno::XInterface
> xThis( getXWeak() );
956 linguistic2::LinguServiceEvent
aEvent( xThis
, linguistic2::LinguServiceEventFlags::PROOFREAD_AGAIN
);
957 m_aNotifyListeners
.notifyEach(
958 &linguistic2::XLinguServiceEventListener::processLinguServiceEvent
,
961 catch (uno::RuntimeException
&)
965 catch (const ::uno::Exception
&)
968 TOOLS_WARN_EXCEPTION("linguistic", "processLinguServiceEvent");
973 sal_Bool SAL_CALL
GrammarCheckingIterator::addLinguServiceEventListener(
974 const uno::Reference
< linguistic2::XLinguServiceEventListener
>& xListener
)
978 m_aNotifyListeners
.addInterface( xListener
);
984 sal_Bool SAL_CALL
GrammarCheckingIterator::removeLinguServiceEventListener(
985 const uno::Reference
< linguistic2::XLinguServiceEventListener
>& xListener
)
989 m_aNotifyListeners
.removeInterface( xListener
);
995 void SAL_CALL
GrammarCheckingIterator::dispose()
997 lang::EventObject
aEvt( static_cast<linguistic2::XProofreadingIterator
*>(this) );
998 m_aEventListeners
.disposeAndClear( aEvt
);
1002 // ---- THREAD SAFE START ----
1004 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
1006 // release all UNO references
1008 m_xBreakIterator
.clear();
1010 // clear containers with UNO references AND have those references released
1011 GCReferences_t aTmpEmpty1
;
1012 DocMap_t aTmpEmpty2
;
1013 FPQueue_t aTmpEmpty3
;
1014 m_aGCReferencesByService
.swap( aTmpEmpty1
);
1015 m_aDocIdMap
.swap( aTmpEmpty2
);
1016 m_aFPEntriesQueue
.swap( aTmpEmpty3
);
1018 // ---- THREAD SAFE END ----
1022 void SAL_CALL
GrammarCheckingIterator::addEventListener(
1023 const uno::Reference
< lang::XEventListener
>& xListener
)
1027 m_aEventListeners
.addInterface( xListener
);
1032 void SAL_CALL
GrammarCheckingIterator::removeEventListener(
1033 const uno::Reference
< lang::XEventListener
>& xListener
)
1037 m_aEventListeners
.removeInterface( xListener
);
1042 void SAL_CALL
GrammarCheckingIterator::disposing( const lang::EventObject
&rSource
)
1044 // if the component (document) is disposing release all references
1045 //!! There is no need to remove entries from the queue that are from this document
1046 //!! since the respectives xFlatParagraphs should become invalid (isModified() == true)
1047 //!! and the call to xFlatParagraphIterator->getNextPara() will result in an empty reference.
1048 //!! And if an entry is currently checked by a grammar checker upon return the results
1049 //!! should be ignored.
1050 //!! Also GetOrCreateDocId will not use that very same Id again...
1051 //!! All of the above resulting in that we only have to get rid of the implementation pointer here.
1052 uno::Reference
< lang::XComponent
> xDoc( rSource
.Source
, uno::UNO_QUERY
);
1055 // ---- THREAD SAFE START ----
1056 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
1057 m_aDocIdMap
.erase( xDoc
.get() );
1058 // ---- THREAD SAFE END ----
1063 uno::Reference
< util::XChangesBatch
> const & GrammarCheckingIterator::GetUpdateAccess() const
1065 if (!m_xUpdateAccess
.is())
1069 // get configuration provider
1070 uno::Reference
< uno::XComponentContext
> xContext
= comphelper::getProcessComponentContext();
1071 uno::Reference
< lang::XMultiServiceFactory
> xConfigurationProvider
=
1072 configuration::theDefaultProvider::get( xContext
);
1074 // get configuration update access
1075 beans::PropertyValue aValue
;
1076 aValue
.Name
= "nodepath";
1077 aValue
.Value
<<= OUString("org.openoffice.Office.Linguistic/ServiceManager");
1078 uno::Sequence
< uno::Any
> aProps
{ uno::Any(aValue
) };
1079 m_xUpdateAccess
.set(
1080 xConfigurationProvider
->createInstanceWithArguments(
1081 "com.sun.star.configuration.ConfigurationUpdateAccess", aProps
),
1082 uno::UNO_QUERY_THROW
);
1084 catch (uno::Exception
&)
1089 return m_xUpdateAccess
;
1093 void GrammarCheckingIterator::GetConfiguredGCSvcs_Impl()
1095 GCImplNames_t aTmpGCImplNamesByLang
;
1099 // get node names (locale iso strings) for configured grammar checkers
1100 uno::Reference
< container::XNameAccess
> xNA( GetUpdateAccess(), uno::UNO_QUERY_THROW
);
1101 xNA
.set( xNA
->getByName( "GrammarCheckerList" ), uno::UNO_QUERY_THROW
);
1102 const uno::Sequence
< OUString
> aElementNames( xNA
->getElementNames() );
1104 for (const OUString
& rElementName
: aElementNames
)
1106 uno::Sequence
< OUString
> aImplNames
;
1107 uno::Any
aTmp( xNA
->getByName( rElementName
) );
1108 if (aTmp
>>= aImplNames
)
1110 if (aImplNames
.hasElements())
1112 // only the first entry is used, there should be only one grammar checker per language
1113 const OUString
aImplName( aImplNames
[0] );
1114 aTmpGCImplNamesByLang
[rElementName
] = aImplName
;
1119 SAL_WARN( "linguistic", "failed to get aImplNames. Wrong type?" );
1123 catch (uno::Exception
const &)
1125 TOOLS_WARN_EXCEPTION( "linguistic", "exception caught. Failed to get configured services" );
1129 // ---- THREAD SAFE START ----
1130 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
1131 m_aGCImplNamesByLang
.swap(aTmpGCImplNamesByLang
);
1132 // ---- THREAD SAFE END ----
1137 sal_Bool SAL_CALL
GrammarCheckingIterator::supportsService(
1138 const OUString
& rServiceName
)
1140 return cppu::supportsService(this, rServiceName
);
1144 OUString SAL_CALL
GrammarCheckingIterator::getImplementationName( )
1146 return "com.sun.star.lingu2.ProofreadingIterator";
1150 uno::Sequence
< OUString
> SAL_CALL
GrammarCheckingIterator::getSupportedServiceNames( )
1152 return { "com.sun.star.linguistic2.ProofreadingIterator" };
1156 void GrammarCheckingIterator::SetServiceList(
1157 const lang::Locale
&rLocale
,
1158 const uno::Sequence
< OUString
> &rSvcImplNames
)
1160 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
1162 OUString sBcp47
= LanguageTag::convertToBcp47(rLocale
, false);
1164 if (rSvcImplNames
.hasElements())
1165 aImplName
= rSvcImplNames
[0]; // there is only one grammar checker per language
1167 if (!LinguIsUnspecified(sBcp47
) && !sBcp47
.isEmpty())
1169 if (!aImplName
.isEmpty())
1170 m_aGCImplNamesByLang
[sBcp47
] = aImplName
;
1172 m_aGCImplNamesByLang
.erase(sBcp47
);
1177 uno::Sequence
< OUString
> GrammarCheckingIterator::GetServiceList(
1178 const lang::Locale
&rLocale
) const
1180 ::osl::Guard
< ::osl::Mutex
> aGuard( MyMutex() );
1182 const OUString aImplName
= getServiceForLocale(rLocale
).first
; // there is only one grammar checker per language
1184 if (!aImplName
.isEmpty())
1185 return { aImplName
};
1190 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
1191 linguistic_GrammarCheckingIterator_get_implementation(
1192 css::uno::XComponentContext
* , css::uno::Sequence
<css::uno::Any
> const&)
1194 return cppu::acquire(new GrammarCheckingIterator());
1199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */