xref: /aoo42x/main/linguistic/source/thesdsp.cxx (revision 3b8558fd)
1*3b8558fdSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*3b8558fdSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*3b8558fdSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*3b8558fdSAndrew Rist  * distributed with this work for additional information
6*3b8558fdSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*3b8558fdSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*3b8558fdSAndrew Rist  * "License"); you may not use this file except in compliance
9*3b8558fdSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*3b8558fdSAndrew Rist  *
11*3b8558fdSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*3b8558fdSAndrew Rist  *
13*3b8558fdSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*3b8558fdSAndrew Rist  * software distributed under the License is distributed on an
15*3b8558fdSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*3b8558fdSAndrew Rist  * KIND, either express or implied.  See the License for the
17*3b8558fdSAndrew Rist  * specific language governing permissions and limitations
18*3b8558fdSAndrew Rist  * under the License.
19*3b8558fdSAndrew Rist  *
20*3b8558fdSAndrew Rist  *************************************************************/
21*3b8558fdSAndrew Rist 
22*3b8558fdSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_linguistic.hxx"
26cdf0e10cSrcweir #include <i18npool/lang.h>
27cdf0e10cSrcweir #include <tools/debug.hxx>
28cdf0e10cSrcweir #include <svl/lngmisc.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <cppuhelper/factory.hxx>	// helper for factories
31cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
32cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
33cdf0e10cSrcweir #include <unotools/processfactory.hxx>
34cdf0e10cSrcweir #include <osl/mutex.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include "thesdsp.hxx"
37cdf0e10cSrcweir #include "linguistic/lngprops.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace utl;
40cdf0e10cSrcweir using namespace osl;
41cdf0e10cSrcweir using namespace rtl;
42cdf0e10cSrcweir using namespace com::sun::star;
43cdf0e10cSrcweir using namespace com::sun::star::beans;
44cdf0e10cSrcweir using namespace com::sun::star::lang;
45cdf0e10cSrcweir using namespace com::sun::star::uno;
46cdf0e10cSrcweir using namespace com::sun::star::linguistic2;
47cdf0e10cSrcweir using namespace linguistic;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
50cdf0e10cSrcweir 
SvcListHasLanguage(const Sequence<Reference<XThesaurus>> & rRefs,const Locale & rLocale)51cdf0e10cSrcweir static sal_Bool SvcListHasLanguage(
52cdf0e10cSrcweir         const Sequence< Reference< XThesaurus > > &rRefs,
53cdf0e10cSrcweir         const Locale &rLocale )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     sal_Bool bHasLanguage = sal_False;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     const Reference< XThesaurus > *pRef = rRefs.getConstArray();
58cdf0e10cSrcweir     sal_Int32 nLen = rRefs.getLength();
59cdf0e10cSrcweir     for (sal_Int32 k = 0;  k < nLen  &&  !bHasLanguage;  ++k)
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         if (pRef[k].is())
62cdf0e10cSrcweir             bHasLanguage = pRef[k]->hasLocale( rLocale );
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     return bHasLanguage;
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 
ThesaurusDispatcher()71cdf0e10cSrcweir ThesaurusDispatcher::ThesaurusDispatcher()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 
~ThesaurusDispatcher()76cdf0e10cSrcweir ThesaurusDispatcher::~ThesaurusDispatcher()
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	ClearSvcList();
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 
ClearSvcList()82cdf0e10cSrcweir void ThesaurusDispatcher::ClearSvcList()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir 	// release memory for each table entry
85cdf0e10cSrcweir     ThesSvcByLangMap_t aTmp;
86cdf0e10cSrcweir     aSvcMap.swap( aTmp );
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
90cdf0e10cSrcweir Sequence< Locale > SAL_CALL
getLocales()91cdf0e10cSrcweir 	ThesaurusDispatcher::getLocales()
92cdf0e10cSrcweir 		throw(RuntimeException)
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     Sequence< Locale > aLocales( static_cast< sal_Int32 >(aSvcMap.size()) );
97cdf0e10cSrcweir     Locale *pLocales = aLocales.getArray();
98cdf0e10cSrcweir     ThesSvcByLangMap_t::const_iterator aIt;
99cdf0e10cSrcweir     for (aIt = aSvcMap.begin();  aIt != aSvcMap.end();  ++aIt)
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         *pLocales++ = CreateLocale( aIt->first );
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir     return aLocales;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
107cdf0e10cSrcweir sal_Bool SAL_CALL
hasLocale(const Locale & rLocale)108cdf0e10cSrcweir     ThesaurusDispatcher::hasLocale( const Locale& rLocale )
109cdf0e10cSrcweir 		throw(RuntimeException)
110cdf0e10cSrcweir {
111cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
112cdf0e10cSrcweir     ThesSvcByLangMap_t::const_iterator aIt( aSvcMap.find( LocaleToLanguage( rLocale ) ) );
113cdf0e10cSrcweir     return aIt != aSvcMap.end();
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 
117cdf0e10cSrcweir Sequence< Reference< XMeaning > > SAL_CALL
queryMeanings(const OUString & rTerm,const Locale & rLocale,const PropertyValues & rProperties)118cdf0e10cSrcweir 	ThesaurusDispatcher::queryMeanings(
119cdf0e10cSrcweir 			const OUString& rTerm, const Locale& rLocale,
120cdf0e10cSrcweir 			const PropertyValues& rProperties )
121cdf0e10cSrcweir 		throw(IllegalArgumentException, RuntimeException)
122cdf0e10cSrcweir {
123cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	Sequence< Reference< XMeaning > >	aMeanings;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	sal_Int16 nLanguage = LocaleToLanguage( rLocale );
128cdf0e10cSrcweir 	if (nLanguage == LANGUAGE_NONE  || !rTerm.getLength())
129cdf0e10cSrcweir 		return aMeanings;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	// search for entry with that language
132cdf0e10cSrcweir     ThesSvcByLangMap_t::iterator    aIt( aSvcMap.find( nLanguage ) );
133cdf0e10cSrcweir     LangSvcEntries_Thes     *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : NULL;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	if (!pEntry)
136cdf0e10cSrcweir 	{
137cdf0e10cSrcweir #ifdef LINGU_EXCEPTIONS
138cdf0e10cSrcweir 		throw IllegalArgumentException();
139cdf0e10cSrcweir #endif
140cdf0e10cSrcweir 	}
141cdf0e10cSrcweir 	else
142cdf0e10cSrcweir 	{
143cdf0e10cSrcweir 		OUString aChkWord( rTerm );
144cdf0e10cSrcweir         aChkWord = aChkWord.replace( SVT_HARD_SPACE, ' ' );
145cdf0e10cSrcweir 		RemoveHyphens( aChkWord );
146cdf0e10cSrcweir 		if (IsIgnoreControlChars( rProperties, GetPropSet() ))
147cdf0e10cSrcweir 			RemoveControlChars( aChkWord );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 		sal_Int32 nLen = pEntry->aSvcRefs.getLength();
150cdf0e10cSrcweir 		DBG_ASSERT( nLen == pEntry->aSvcImplNames.getLength(),
151cdf0e10cSrcweir 				"lng : sequence length mismatch");
152cdf0e10cSrcweir         DBG_ASSERT( pEntry->nLastTriedSvcIndex < nLen,
153cdf0e10cSrcweir 				"lng : index out of range");
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 		sal_Int32 i = 0;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 		// try already instantiated services first
158cdf0e10cSrcweir 		{
159cdf0e10cSrcweir 			const Reference< XThesaurus > *pRef = pEntry->aSvcRefs.getConstArray();
160cdf0e10cSrcweir             while (i <= pEntry->nLastTriedSvcIndex
161cdf0e10cSrcweir 				   &&  aMeanings.getLength() == 0)
162cdf0e10cSrcweir 			{
163cdf0e10cSrcweir                 if (pRef[i].is()  &&  pRef[i]->hasLocale( rLocale ))
164cdf0e10cSrcweir 					aMeanings = pRef[i]->queryMeanings( aChkWord, rLocale, rProperties );
165cdf0e10cSrcweir 				++i;
166cdf0e10cSrcweir 			}
167cdf0e10cSrcweir 		}
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 		// if still no result instantiate new services and try those
170cdf0e10cSrcweir 		if (aMeanings.getLength() == 0
171cdf0e10cSrcweir             &&  pEntry->nLastTriedSvcIndex < nLen - 1)
172cdf0e10cSrcweir 		{
173cdf0e10cSrcweir 			const OUString *pImplNames = pEntry->aSvcImplNames.getConstArray();
174cdf0e10cSrcweir 			Reference< XThesaurus > *pRef = pEntry->aSvcRefs.getArray();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 			Reference< XMultiServiceFactory >  xMgr( getProcessServiceFactory() );
177cdf0e10cSrcweir 			if (xMgr.is())
178cdf0e10cSrcweir 			{
179cdf0e10cSrcweir 				// build service initialization argument
180cdf0e10cSrcweir 				Sequence< Any > aArgs(1);
181cdf0e10cSrcweir 				aArgs.getArray()[0] <<= GetPropSet();
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 				while (i < nLen  &&  aMeanings.getLength() == 0)
184cdf0e10cSrcweir 				{
185cdf0e10cSrcweir 					// create specific service via it's implementation name
186cdf0e10cSrcweir 					Reference< XThesaurus > xThes;
187cdf0e10cSrcweir 					try
188cdf0e10cSrcweir 					{
189cdf0e10cSrcweir 						xThes = Reference< XThesaurus >(
190cdf0e10cSrcweir 								xMgr->createInstanceWithArguments(
191cdf0e10cSrcweir 								pImplNames[i], aArgs ), UNO_QUERY );
192cdf0e10cSrcweir 					}
193cdf0e10cSrcweir 					catch (uno::Exception &)
194cdf0e10cSrcweir 					{
195cdf0e10cSrcweir                         DBG_ASSERT( 0, "createInstanceWithArguments failed" );
196cdf0e10cSrcweir 					}
197cdf0e10cSrcweir 					pRef[i] = xThes;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir                     if (xThes.is()  &&  xThes->hasLocale( rLocale ))
200cdf0e10cSrcweir 						aMeanings = xThes->queryMeanings( aChkWord, rLocale, rProperties );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir                     pEntry->nLastTriedSvcIndex = (sal_Int16) i;
203cdf0e10cSrcweir 					++i;
204cdf0e10cSrcweir 				}
205cdf0e10cSrcweir 
206cdf0e10cSrcweir                 // if language is not supported by any of the services
207cdf0e10cSrcweir                 // remove it from the list.
208cdf0e10cSrcweir                 if (i == nLen  &&  aMeanings.getLength() == 0)
209cdf0e10cSrcweir                 {
210cdf0e10cSrcweir                     if (!SvcListHasLanguage( pEntry->aSvcRefs, rLocale ))
211cdf0e10cSrcweir                         aSvcMap.erase( nLanguage );
212cdf0e10cSrcweir                 }
213cdf0e10cSrcweir 			}
214cdf0e10cSrcweir 		}
215cdf0e10cSrcweir 	}
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 	return aMeanings;
218cdf0e10cSrcweir }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 
SetServiceList(const Locale & rLocale,const Sequence<OUString> & rSvcImplNames)221cdf0e10cSrcweir void ThesaurusDispatcher::SetServiceList( const Locale &rLocale,
222cdf0e10cSrcweir 		const Sequence< OUString > &rSvcImplNames )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 	sal_Int16 nLanguage = LocaleToLanguage( rLocale );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     sal_Int32 nLen = rSvcImplNames.getLength();
229cdf0e10cSrcweir     if (0 == nLen)
230cdf0e10cSrcweir         // remove entry
231cdf0e10cSrcweir         aSvcMap.erase( nLanguage );
232cdf0e10cSrcweir     else
233cdf0e10cSrcweir     {
234cdf0e10cSrcweir         // modify/add entry
235cdf0e10cSrcweir         LangSvcEntries_Thes *pEntry = aSvcMap[ nLanguage ].get();
236cdf0e10cSrcweir         if (pEntry)
237cdf0e10cSrcweir         {
238cdf0e10cSrcweir             pEntry->Clear();
239cdf0e10cSrcweir             pEntry->aSvcImplNames = rSvcImplNames;
240cdf0e10cSrcweir             pEntry->aSvcRefs = Sequence< Reference < XThesaurus > >( nLen );
241cdf0e10cSrcweir         }
242cdf0e10cSrcweir         else
243cdf0e10cSrcweir         {
244cdf0e10cSrcweir             boost::shared_ptr< LangSvcEntries_Thes > pTmpEntry( new LangSvcEntries_Thes( rSvcImplNames ) );
245cdf0e10cSrcweir             pTmpEntry->aSvcRefs = Sequence< Reference < XThesaurus > >( nLen );
246cdf0e10cSrcweir             aSvcMap[ nLanguage ] = pTmpEntry;
247cdf0e10cSrcweir         }
248cdf0e10cSrcweir     }
249cdf0e10cSrcweir }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 
252cdf0e10cSrcweir Sequence< OUString >
GetServiceList(const Locale & rLocale) const253cdf0e10cSrcweir 	ThesaurusDispatcher::GetServiceList( const Locale &rLocale ) const
254cdf0e10cSrcweir {
255cdf0e10cSrcweir 	MutexGuard	aGuard( GetLinguMutex() );
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 	Sequence< OUString > aRes;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	// search for entry with that language and use data from that
260cdf0e10cSrcweir 	sal_Int16 nLanguage = LocaleToLanguage( rLocale );
261cdf0e10cSrcweir     ThesaurusDispatcher             *pThis = (ThesaurusDispatcher *) this;
262cdf0e10cSrcweir     const ThesSvcByLangMap_t::iterator  aIt( pThis->aSvcMap.find( nLanguage ) );
263cdf0e10cSrcweir     const LangSvcEntries_Thes       *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : NULL;
264cdf0e10cSrcweir 	if (pEntry)
265cdf0e10cSrcweir 		aRes = pEntry->aSvcImplNames;
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 	return aRes;
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 
GetDspType() const271cdf0e10cSrcweir LinguDispatcher::DspType ThesaurusDispatcher::GetDspType() const
272cdf0e10cSrcweir {
273cdf0e10cSrcweir 	return DSP_THES;
274cdf0e10cSrcweir }
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 
277cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
278cdf0e10cSrcweir 
279