xref: /AOO41X/main/linguistic/source/grammarchecker.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include "precompiled_linguistic.hxx"
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include <sal/config.h>
30*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
31*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
32*cdf0e10cSrcweir #include <com/sun/star/linguistic2/XGrammarChecker.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/i18n/XBreakIterator.hpp>
34*cdf0e10cSrcweir #include <cppuhelper/implbase4.hxx>
35*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
37*cdf0e10cSrcweir #include "linguistic/misc.hxx"
38*cdf0e10cSrcweir #include "defs.hxx"
39*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
40*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.h>
44*cdf0e10cSrcweir #include <com/sun/star/container/XContentEnumerationAccess.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/container/XEnumeration.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/linguistic2/XSupportedLocales.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/linguistic2/SingleGrammarError.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/linguistic2/GrammarCheckingResult.hpp>
49*cdf0e10cSrcweir #include "lngopt.hxx"
50*cdf0e10cSrcweir #include <comphelper/extract.hxx>
51*cdf0e10cSrcweir #include <unotools/processfactory.hxx>
52*cdf0e10cSrcweir #include <map>
53*cdf0e10cSrcweir #include <com/sun/star/text/TextMarkupType.hpp>
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #include "grammarchecker.hxx"
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir using namespace ::utl;
58*cdf0e10cSrcweir using namespace ::rtl;
59*cdf0e10cSrcweir using namespace ::com::sun::star;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir ////////////////////////////////////////////////////////////
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir GrammarChecker::GrammarChecker( /*uno::Reference< uno::XComponentContext > const & context*/ )
64*cdf0e10cSrcweir     /*m_xContext(context)*/
65*cdf0e10cSrcweir {}
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir GrammarChecker::~GrammarChecker()
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir sal_Bool SAL_CALL GrammarChecker::isSpellChecker() throw (uno::RuntimeException)
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
75*cdf0e10cSrcweir 	return sal_False;
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir sal_Bool SAL_CALL GrammarChecker::hasLocale( const lang::Locale & aLocale ) throw (uno::RuntimeException)
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
82*cdf0e10cSrcweir     (void) aLocale;
83*cdf0e10cSrcweir 	return sal_False;
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir uno::Sequence< lang::Locale > SAL_CALL GrammarChecker::getLocales(  ) throw (uno::RuntimeException)
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
89*cdf0e10cSrcweir     return uno::Sequence< lang::Locale >();
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir void SAL_CALL GrammarChecker::startDocument(sal_Int32 nDocId)
94*cdf0e10cSrcweir     throw (uno::RuntimeException, lang::IllegalArgumentException)
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
97*cdf0e10cSrcweir     (void) nDocId;
98*cdf0e10cSrcweir }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir void SAL_CALL GrammarChecker::startParagraph(sal_Int32 nDocId)
101*cdf0e10cSrcweir 	throw (uno::RuntimeException, lang::IllegalArgumentException)
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
104*cdf0e10cSrcweir     (void) nDocId;
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir void SAL_CALL GrammarChecker::endParagraph( sal_Int32 nDocId )
108*cdf0e10cSrcweir     throw (uno::RuntimeException, lang::IllegalArgumentException)
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
111*cdf0e10cSrcweir     (void) nDocId;
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir void SAL_CALL GrammarChecker::endDocument(sal_Int32 nDocId)
115*cdf0e10cSrcweir     throw (uno::RuntimeException, lang::IllegalArgumentException)
116*cdf0e10cSrcweir {
117*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
118*cdf0e10cSrcweir     (void) nDocId;
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir linguistic2::GrammarCheckingResult SAL_CALL GrammarChecker::doGrammarChecking(
122*cdf0e10cSrcweir         sal_Int32 nDocId,
123*cdf0e10cSrcweir         const rtl::OUString& rText,
124*cdf0e10cSrcweir         const lang::Locale& rLocale,
125*cdf0e10cSrcweir         sal_Int32 nStartOfSentencePos,
126*cdf0e10cSrcweir         sal_Int32 nSuggestedSentenceEndPos,
127*cdf0e10cSrcweir         const uno::Sequence< ::sal_Int32 >& rLanguagePortions,
128*cdf0e10cSrcweir         const uno::Sequence< lang::Locale >& rLanguagePortionsLocales )
129*cdf0e10cSrcweir     throw (lang::IllegalArgumentException, uno::RuntimeException)
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     (void) rLanguagePortions;
134*cdf0e10cSrcweir     (void) rLanguagePortionsLocales;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     linguistic2::GrammarCheckingResult  aRes;
137*cdf0e10cSrcweir     aRes.nDocumentId                = nDocId;
138*cdf0e10cSrcweir     aRes.aText                      = rText;
139*cdf0e10cSrcweir     aRes.aLocale                    = rLocale;
140*cdf0e10cSrcweir     aRes.nEndOfSentencePos          = nSuggestedSentenceEndPos;
141*cdf0e10cSrcweir     aRes.xGrammarChecker            = this;
142*cdf0e10cSrcweir     aRes.aGrammarErrors             = GrammarCheckingInDummy( nDocId, rText, rLocale, nStartOfSentencePos, nSuggestedSentenceEndPos );;
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     return aRes;
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir uno::Sequence< linguistic2::SingleGrammarError > GrammarChecker::GrammarCheckingInDummy(
148*cdf0e10cSrcweir 	sal_Int32 nDocId,
149*cdf0e10cSrcweir     const OUString & rFlatParaText,
150*cdf0e10cSrcweir     const lang::Locale & rLocale,
151*cdf0e10cSrcweir     sal_Int32 nStartOfSentencePos,
152*cdf0e10cSrcweir     sal_Int32 nSuggestedSentenceEndPos )
153*cdf0e10cSrcweir {
154*cdf0e10cSrcweir 	(void) nDocId;
155*cdf0e10cSrcweir     (void) rFlatParaText;
156*cdf0e10cSrcweir     (void) rLocale;
157*cdf0e10cSrcweir     (void) nStartOfSentencePos;
158*cdf0e10cSrcweir     (void) nSuggestedSentenceEndPos;
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir     typedef std::map< OUString, uno::Sequence<OUString> > Error_t;
162*cdf0e10cSrcweir 	Error_t aError;
163*cdf0e10cSrcweir     uno::Sequence< OUString > aSuggestion(1);
164*cdf0e10cSrcweir     OUString *pSeggestion = aSuggestion.getArray();
165*cdf0e10cSrcweir 	pSeggestion[0] = OUString::createFromAscii("Modified");
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     aError[OUString::createFromAscii("GrammarError")]  = aSuggestion;
168*cdf0e10cSrcweir     aError[OUString::createFromAscii("Grammar Error")] = aSuggestion;
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	typedef std::vector< linguistic2::SingleGrammarError> ErrorVector_t;
171*cdf0e10cSrcweir 	ErrorVector_t aErrorVector;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     OUString aText = rFlatParaText.copy( nStartOfSentencePos, nSuggestedSentenceEndPos - nStartOfSentencePos );
174*cdf0e10cSrcweir 	sal_Int32 nIndexOf = 0;
175*cdf0e10cSrcweir 	for(Error_t::const_iterator it = aError.begin(); it != aError.end(); ++it)
176*cdf0e10cSrcweir 	{
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir         while(nIndexOf >= 0)
179*cdf0e10cSrcweir 		{
180*cdf0e10cSrcweir             nIndexOf=aText.indexOf(it->first, nIndexOf);
181*cdf0e10cSrcweir 			if(nIndexOf > -1)
182*cdf0e10cSrcweir 			{
183*cdf0e10cSrcweir 				//error found
184*cdf0e10cSrcweir 				linguistic2::SingleGrammarError aErr;
185*cdf0e10cSrcweir                 aErr.nErrorStart        = nIndexOf + nStartOfSentencePos;
186*cdf0e10cSrcweir 				nIndexOf += it->first.getLength();
187*cdf0e10cSrcweir                 aErr.nErrorLength       = it->first.getLength();
188*cdf0e10cSrcweir                 aErr.nErrorType         = text::TextMarkupType::GRAMMAR;
189*cdf0e10cSrcweir                 aErr.nErrorLevel        = 0;
190*cdf0e10cSrcweir                 aErr.aShortComment      = OUString();
191*cdf0e10cSrcweir                 aErr.aFullComment       = OUString();
192*cdf0e10cSrcweir                 aErr.aNewLocale         = rLocale;
193*cdf0e10cSrcweir                 aErr.aSuggestions       = it->second;
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir                 aErrorVector.push_back( aErr );
196*cdf0e10cSrcweir 			}
197*cdf0e10cSrcweir 		}
198*cdf0e10cSrcweir 		nIndexOf = 0;
199*cdf0e10cSrcweir 	}
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	sal_Int32 nCount = aErrorVector.size();
202*cdf0e10cSrcweir 	uno::Sequence< linguistic2::SingleGrammarError > aErrors( nCount );
203*cdf0e10cSrcweir 	if( nCount > 0 )
204*cdf0e10cSrcweir 	{
205*cdf0e10cSrcweir 		linguistic2::SingleGrammarError* pErrors = aErrors.getArray();
206*cdf0e10cSrcweir 		for (sal_Int32 i=0; i < nCount; ++i)
207*cdf0e10cSrcweir 		{
208*cdf0e10cSrcweir 			pErrors[i] = aErrorVector[i];
209*cdf0e10cSrcweir 		}
210*cdf0e10cSrcweir 	}
211*cdf0e10cSrcweir 	return aErrors;
212*cdf0e10cSrcweir }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir sal_Bool SAL_CALL GrammarChecker::hasOptionsDialog(  ) throw (uno::RuntimeException)
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
218*cdf0e10cSrcweir     return sal_False;
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir void SAL_CALL GrammarChecker::runOptionsDialog()
222*cdf0e10cSrcweir     throw (uno::RuntimeException)
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir void SAL_CALL GrammarChecker::dispose(  ) throw (uno::RuntimeException)
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
230*cdf0e10cSrcweir }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir void SAL_CALL GrammarChecker::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
233*cdf0e10cSrcweir     throw (uno::RuntimeException)
234*cdf0e10cSrcweir {
235*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
236*cdf0e10cSrcweir     (void) xListener;
237*cdf0e10cSrcweir }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir void SAL_CALL GrammarChecker::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
240*cdf0e10cSrcweir     throw (uno::RuntimeException)
241*cdf0e10cSrcweir {
242*cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
243*cdf0e10cSrcweir     (void) xListener;
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir sal_Bool SAL_CALL GrammarChecker::supportsService( const OUString& ServiceName ) throw(uno::RuntimeException)
247*cdf0e10cSrcweir {
248*cdf0e10cSrcweir 	osl::Guard< osl::Mutex > aGuard(GetMutex());
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 	uno::Sequence< OUString > aSNL = getSupportedServiceNames();
251*cdf0e10cSrcweir 	const OUString * pArray = aSNL.getConstArray();
252*cdf0e10cSrcweir 	for( sal_Int32 i = 0; i < aSNL.getLength(); ++i )
253*cdf0e10cSrcweir 		if( pArray[i] == ServiceName )
254*cdf0e10cSrcweir 			return sal_True;
255*cdf0e10cSrcweir 	return sal_False;
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir uno::Sequence< OUString > GrammarChecker::getSupportedServiceNames_Static(  ) throw()
259*cdf0e10cSrcweir {
260*cdf0e10cSrcweir 	//osl::Guard< osl::Mutex > aGuard(GetMutex());
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 	uno::Sequence< OUString > aSNS( 1 );	// auch mehr als 1 Service moeglich
263*cdf0e10cSrcweir 	aSNS.getArray()[0] = A2OU( "com.sun.star.linguistic2.GrammarChecker" );//SN_LINGU_SERVCICE_MANAGER
264*cdf0e10cSrcweir 	return aSNS;
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir uno::Sequence< OUString > SAL_CALL GrammarChecker::getSupportedServiceNames(  ) throw(uno::RuntimeException)
268*cdf0e10cSrcweir {
269*cdf0e10cSrcweir 	osl::Guard< osl::Mutex > aGuard(GetMutex());
270*cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
271*cdf0e10cSrcweir }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir OUString SAL_CALL GrammarChecker::getImplementationName(  ) throw(uno::RuntimeException)
274*cdf0e10cSrcweir {
275*cdf0e10cSrcweir 	osl::Guard< osl::Mutex > aGuard(GetMutex());
276*cdf0e10cSrcweir 	return getImplementationName_Static();
277*cdf0e10cSrcweir }
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL GrammarChecker_CreateInstance(
280*cdf0e10cSrcweir         const uno::Reference< lang::XMultiServiceFactory > & /*rSMgr*/ )
281*cdf0e10cSrcweir     throw(uno::Exception)
282*cdf0e10cSrcweir {
283*cdf0e10cSrcweir     uno::Reference< uno::XInterface > xService = (cppu::OWeakObject*) new GrammarChecker;
284*cdf0e10cSrcweir 	return xService;
285*cdf0e10cSrcweir }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir void * SAL_CALL GrammarChecker_getFactory( const sal_Char * pImplName, lang::XMultiServiceFactory * pServiceManager, void * /*pRegistryKey*/ )
288*cdf0e10cSrcweir {
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir 	void * pRet = 0;
291*cdf0e10cSrcweir 	if ( !GrammarChecker::getImplementationName_Static().compareToAscii( pImplName ) )
292*cdf0e10cSrcweir 	{
293*cdf0e10cSrcweir         uno::Reference< lang::XSingleServiceFactory > xFactory =
294*cdf0e10cSrcweir 			cppu::createOneInstanceFactory(
295*cdf0e10cSrcweir 				pServiceManager,
296*cdf0e10cSrcweir 				GrammarChecker::getImplementationName_Static(),
297*cdf0e10cSrcweir 				GrammarChecker_CreateInstance,
298*cdf0e10cSrcweir 				GrammarChecker::getSupportedServiceNames_Static());
299*cdf0e10cSrcweir 		// acquire, because we return an interface pointer instead of a reference
300*cdf0e10cSrcweir 		xFactory->acquire();
301*cdf0e10cSrcweir 		pRet = xFactory.get();
302*cdf0e10cSrcweir 	}
303*cdf0e10cSrcweir 	return pRet;
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306