1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: grammarchecker.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "precompiled_linguistic.hxx"
32 #include <sal/config.h>
33 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <cppuhelper/implbase1.hxx>
35 #include <com/sun/star/linguistic2/XGrammarChecker.hpp>
36 #include <com/sun/star/i18n/XBreakIterator.hpp>
37 #include <vcl/unohelp.hxx>
38 #include <cppuhelper/implbase4.hxx>
39 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <cppuhelper/factory.hxx>
44 #include <com/sun/star/registry/XRegistryKey.hpp>
45 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47 #include <cppuhelper/interfacecontainer.h>
48 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
49 #include <com/sun/star/container/XEnumeration.hpp>
50 #include <com/sun/star/linguistic2/XSupportedLocales.hpp>
51 #include <com/sun/star/linguistic2/SingleGrammarError.hpp>
52 #include <com/sun/star/linguistic2/GrammarCheckingResult.hpp>
54 #include <cppuhelper/extract.hxx>
55 #include <unotools/processfactory.hxx>
57 #include <com/sun/star/text/TextMarkupType.hpp>
59 #include "grammarchecker.hxx"
61 using namespace ::utl
;
62 using namespace ::rtl
;
63 using namespace ::com::sun::star
;
65 ////////////////////////////////////////////////////////////
67 GrammarChecker::GrammarChecker( /*uno::Reference< uno::XComponentContext > const & context*/ )
68 /*m_xContext(context)*/
71 GrammarChecker::~GrammarChecker()
76 sal_Bool SAL_CALL
GrammarChecker::isSpellChecker() throw (uno::RuntimeException
)
78 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
83 sal_Bool SAL_CALL
GrammarChecker::hasLocale( const lang::Locale
& aLocale
) throw (uno::RuntimeException
)
85 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
90 uno::Sequence
< lang::Locale
> SAL_CALL
GrammarChecker::getLocales( ) throw (uno::RuntimeException
)
92 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
93 return uno::Sequence
< lang::Locale
>();
97 void SAL_CALL
GrammarChecker::startDocument(sal_Int32 nDocId
)
98 throw (uno::RuntimeException
, lang::IllegalArgumentException
)
100 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
104 void SAL_CALL
GrammarChecker::startParagraph(sal_Int32 nDocId
)
105 throw (uno::RuntimeException
, lang::IllegalArgumentException
)
107 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
111 void SAL_CALL
GrammarChecker::endParagraph( sal_Int32 nDocId
)
112 throw (uno::RuntimeException
, lang::IllegalArgumentException
)
114 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
118 void SAL_CALL
GrammarChecker::endDocument(sal_Int32 nDocId
)
119 throw (uno::RuntimeException
, lang::IllegalArgumentException
)
121 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
125 linguistic2::GrammarCheckingResult SAL_CALL
GrammarChecker::doGrammarChecking(
127 const rtl::OUString
& rText
,
128 const lang::Locale
& rLocale
,
129 sal_Int32 nStartOfSentencePos
,
130 sal_Int32 nSuggestedSentenceEndPos
,
131 const uno::Sequence
< ::sal_Int32
>& rLanguagePortions
,
132 const uno::Sequence
< lang::Locale
>& rLanguagePortionsLocales
)
133 throw (lang::IllegalArgumentException
, uno::RuntimeException
)
135 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
137 (void) rLanguagePortions
;
138 (void) rLanguagePortionsLocales
;
140 linguistic2::GrammarCheckingResult aRes
;
141 aRes
.nDocumentId
= nDocId
;
143 aRes
.aLocale
= rLocale
;
144 aRes
.nEndOfSentencePos
= nSuggestedSentenceEndPos
;
145 aRes
.xGrammarChecker
= this;
146 aRes
.aGrammarErrors
= GrammarCheckingInDummy( nDocId
, rText
, rLocale
, nStartOfSentencePos
, nSuggestedSentenceEndPos
);;
151 uno::Sequence
< linguistic2::SingleGrammarError
> GrammarChecker::GrammarCheckingInDummy(
153 const OUString
& rFlatParaText
,
154 const lang::Locale
& rLocale
,
155 sal_Int32 nStartOfSentencePos
,
156 sal_Int32 nSuggestedSentenceEndPos
)
159 (void) rFlatParaText
;
161 (void) nStartOfSentencePos
;
162 (void) nSuggestedSentenceEndPos
;
165 typedef std::map
< OUString
, uno::Sequence
<OUString
> > Error_t
;
167 uno::Sequence
< OUString
> aSuggestion(1);
168 OUString
*pSeggestion
= aSuggestion
.getArray();
169 pSeggestion
[0] = OUString::createFromAscii("Modified");
171 aError
[OUString::createFromAscii("GrammarError")] = aSuggestion
;
172 aError
[OUString::createFromAscii("Grammar Error")] = aSuggestion
;
174 typedef std::vector
< linguistic2::SingleGrammarError
> ErrorVector_t
;
175 ErrorVector_t aErrorVector
;
177 OUString aText
= rFlatParaText
.copy( nStartOfSentencePos
, nSuggestedSentenceEndPos
- nStartOfSentencePos
);
178 sal_Int32 nIndexOf
= 0;
179 for(Error_t::const_iterator it
= aError
.begin(); it
!= aError
.end(); ++it
)
184 nIndexOf
=aText
.indexOf(it
->first
, nIndexOf
);
188 linguistic2::SingleGrammarError aErr
;
189 aErr
.nErrorStart
= nIndexOf
+ nStartOfSentencePos
;
190 nIndexOf
+= it
->first
.getLength();
191 aErr
.nErrorLength
= it
->first
.getLength();
192 aErr
.nErrorType
= text::TextMarkupType::GRAMMAR
;
193 aErr
.nErrorLevel
= 0;
194 aErr
.aShortComment
= OUString();
195 aErr
.aFullComment
= OUString();
196 aErr
.aNewLocale
= rLocale
;
197 aErr
.aSuggestions
= it
->second
;
199 aErrorVector
.push_back( aErr
);
205 sal_Int32 nCount
= aErrorVector
.size();
206 uno::Sequence
< linguistic2::SingleGrammarError
> aErrors( nCount
);
209 linguistic2::SingleGrammarError
* pErrors
= aErrors
.getArray();
210 for (sal_Int32 i
=0; i
< nCount
; ++i
)
212 pErrors
[i
] = aErrorVector
[i
];
219 sal_Bool SAL_CALL
GrammarChecker::hasOptionsDialog( ) throw (uno::RuntimeException
)
221 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
225 void SAL_CALL
GrammarChecker::runOptionsDialog()
226 throw (uno::RuntimeException
)
228 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
231 void SAL_CALL
GrammarChecker::dispose( ) throw (uno::RuntimeException
)
233 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
236 void SAL_CALL
GrammarChecker::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
237 throw (uno::RuntimeException
)
239 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
243 void SAL_CALL
GrammarChecker::removeEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
244 throw (uno::RuntimeException
)
246 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
250 sal_Bool SAL_CALL
GrammarChecker::supportsService( const OUString
& ServiceName
) throw(uno::RuntimeException
)
252 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
254 uno::Sequence
< OUString
> aSNL
= getSupportedServiceNames();
255 const OUString
* pArray
= aSNL
.getConstArray();
256 for( INT32 i
= 0; i
< aSNL
.getLength(); ++i
)
257 if( pArray
[i
] == ServiceName
)
262 uno::Sequence
< OUString
> GrammarChecker::getSupportedServiceNames_Static( ) throw()
264 //osl::Guard< osl::Mutex > aGuard(GetMutex());
266 uno::Sequence
< OUString
> aSNS( 1 ); // auch mehr als 1 Service moeglich
267 aSNS
.getArray()[0] = A2OU( "com.sun.star.linguistic2.GrammarChecker" );//SN_LINGU_SERVCICE_MANAGER
271 uno::Sequence
< OUString
> SAL_CALL
GrammarChecker::getSupportedServiceNames( ) throw(uno::RuntimeException
)
273 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
274 return getSupportedServiceNames_Static();
277 OUString SAL_CALL
GrammarChecker::getImplementationName( ) throw(uno::RuntimeException
)
279 osl::Guard
< osl::Mutex
> aGuard(GetMutex());
280 return getImplementationName_Static();
283 sal_Bool SAL_CALL
GrammarChecker_writeInfo( void * /*pServiceManager*/, registry::XRegistryKey
* pRegistryKey
)
288 aImpl
+= GrammarChecker::getImplementationName_Static().getStr();
289 aImpl
.AppendAscii( "/UNO/SERVICES" );
290 uno::Reference
< registry::XRegistryKey
> xNewKey
=
291 pRegistryKey
->createKey( aImpl
);
292 uno::Sequence
< OUString
> aServices
= GrammarChecker::getSupportedServiceNames_Static();
293 for( INT32 i
= 0; i
< aServices
.getLength(); ++i
)
294 xNewKey
->createKey( aServices
.getConstArray()[i
] );
298 catch(uno::Exception
&)
304 uno::Reference
< uno::XInterface
> SAL_CALL
GrammarChecker_CreateInstance(
305 const uno::Reference
< lang::XMultiServiceFactory
> & /*rSMgr*/ )
306 throw(uno::Exception
)
308 uno::Reference
< uno::XInterface
> xService
= (cppu::OWeakObject
*) new GrammarChecker
;
312 void * SAL_CALL
GrammarChecker_getFactory( const sal_Char
* pImplName
, lang::XMultiServiceFactory
* pServiceManager
, void * /*pRegistryKey*/ )
316 if ( !GrammarChecker::getImplementationName_Static().compareToAscii( pImplName
) )
318 uno::Reference
< lang::XSingleServiceFactory
> xFactory
=
319 cppu::createOneInstanceFactory(
321 GrammarChecker::getImplementationName_Static(),
322 GrammarChecker_CreateInstance
,
323 GrammarChecker::getSupportedServiceNames_Static());
324 // acquire, because we return an interface pointer instead of a reference
326 pRet
= xFactory
.get();