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