xref: /AOO41X/main/unotools/source/i18n/transliterationwrapper.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 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_unotools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <unotools/transliterationwrapper.hxx>
32*cdf0e10cSrcweir #include <tools/debug.hxx>
33*cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
34*cdf0e10cSrcweir #include <comphelper/componentfactory.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/i18n/TransliterationModulesExtra.hpp>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #define TRANSLIT_LIBRARYNAME "i18n"
42*cdf0e10cSrcweir #define TRANSLIT_SERVICENAME "com.sun.star.i18n.Transliteration"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
45*cdf0e10cSrcweir using namespace ::com::sun::star::i18n;
46*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
47*cdf0e10cSrcweir using namespace ::utl;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir TransliterationWrapper::TransliterationWrapper(
50*cdf0e10cSrcweir 					const Reference< XMultiServiceFactory > & xSF,
51*cdf0e10cSrcweir 					sal_uInt32 nTyp )
52*cdf0e10cSrcweir 	: xSMgr( xSF ), nType( nTyp ), nLanguage( 0 ), bFirstCall( sal_True )
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir 	if( xSMgr.is() )
55*cdf0e10cSrcweir 	{
56*cdf0e10cSrcweir 		try {
57*cdf0e10cSrcweir             xTrans = Reference< XExtendedTransliteration > (
58*cdf0e10cSrcweir                     xSMgr->createInstance( ::rtl::OUString(
59*cdf0e10cSrcweir                             RTL_CONSTASCII_USTRINGPARAM(
60*cdf0e10cSrcweir                                 TRANSLIT_SERVICENAME))), UNO_QUERY );
61*cdf0e10cSrcweir 		}
62*cdf0e10cSrcweir 		catch ( Exception&  )
63*cdf0e10cSrcweir 		{
64*cdf0e10cSrcweir 			DBG_ERRORFILE( "TransliterationWrapper: Exception caught!" );
65*cdf0e10cSrcweir 		}
66*cdf0e10cSrcweir 	}
67*cdf0e10cSrcweir 	else
68*cdf0e10cSrcweir 	{	// try to get an instance somehow
69*cdf0e10cSrcweir 		DBG_ERRORFILE( "TransliterationWrapper: no service manager, trying own" );
70*cdf0e10cSrcweir 		try
71*cdf0e10cSrcweir 		{
72*cdf0e10cSrcweir             Reference< XInterface > xI = ::comphelper::getComponentInstance(
73*cdf0e10cSrcweir                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( LLCF_LIBNAME(
74*cdf0e10cSrcweir                                 TRANSLIT_LIBRARYNAME ))),
75*cdf0e10cSrcweir                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
76*cdf0e10cSrcweir                             TRANSLIT_SERVICENAME)));
77*cdf0e10cSrcweir 			if ( xI.is() )
78*cdf0e10cSrcweir 			{
79*cdf0e10cSrcweir 				Any x = xI->queryInterface(
80*cdf0e10cSrcweir 					::getCppuType((const Reference< XExtendedTransliteration>*)0) );
81*cdf0e10cSrcweir 				x >>= xTrans ;
82*cdf0e10cSrcweir 			}
83*cdf0e10cSrcweir 		}
84*cdf0e10cSrcweir 		catch ( Exception&  )
85*cdf0e10cSrcweir 		{
86*cdf0e10cSrcweir 			DBG_ERRORFILE( "getComponentInstance: Exception caught!" );
87*cdf0e10cSrcweir 		}
88*cdf0e10cSrcweir 	}
89*cdf0e10cSrcweir 	DBG_ASSERT( xTrans.is(), "TransliterationWrapper: no Transliteraion available" );
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir TransliterationWrapper::~TransliterationWrapper()
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir String TransliterationWrapper::transliterate(
99*cdf0e10cSrcweir 								const String& rStr, sal_uInt16 nLang,
100*cdf0e10cSrcweir 								xub_StrLen nStart, xub_StrLen nLen,
101*cdf0e10cSrcweir 								Sequence <sal_Int32>* pOffset )
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir 	String sRet;
104*cdf0e10cSrcweir 	if( xTrans.is() )
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		try
107*cdf0e10cSrcweir 		{
108*cdf0e10cSrcweir             loadModuleIfNeeded( nLang );
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 			if ( pOffset )
111*cdf0e10cSrcweir 			    sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
112*cdf0e10cSrcweir             else
113*cdf0e10cSrcweir 			    sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
114*cdf0e10cSrcweir 		}
115*cdf0e10cSrcweir 		catch( Exception&  )
116*cdf0e10cSrcweir 		{
117*cdf0e10cSrcweir 			DBG_ERRORFILE( "transliterate: Exception caught!" );
118*cdf0e10cSrcweir 		}
119*cdf0e10cSrcweir 	}
120*cdf0e10cSrcweir 	return sRet;
121*cdf0e10cSrcweir }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir String TransliterationWrapper::transliterate(
125*cdf0e10cSrcweir 								const String& rStr,
126*cdf0e10cSrcweir 								xub_StrLen nStart, xub_StrLen nLen,
127*cdf0e10cSrcweir 								Sequence <sal_Int32>* pOffset ) const
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir 	String sRet( rStr );
130*cdf0e10cSrcweir 	if( xTrans.is() )
131*cdf0e10cSrcweir 	{
132*cdf0e10cSrcweir 		try
133*cdf0e10cSrcweir 		{
134*cdf0e10cSrcweir 			if ( pOffset )
135*cdf0e10cSrcweir 			    sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
136*cdf0e10cSrcweir             else
137*cdf0e10cSrcweir 			    sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
138*cdf0e10cSrcweir 		}
139*cdf0e10cSrcweir 		catch( Exception&  )
140*cdf0e10cSrcweir 		{
141*cdf0e10cSrcweir 			DBG_ERRORFILE( "transliterate: Exception caught!" );
142*cdf0e10cSrcweir 		}
143*cdf0e10cSrcweir 	}
144*cdf0e10cSrcweir 	return sRet;
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir sal_Bool TransliterationWrapper::needLanguageForTheMode() const
148*cdf0e10cSrcweir {
149*cdf0e10cSrcweir 	return TransliterationModules_UPPERCASE_LOWERCASE == nType ||
150*cdf0e10cSrcweir 		   TransliterationModules_LOWERCASE_UPPERCASE == nType ||
151*cdf0e10cSrcweir 		   TransliterationModules_IGNORE_CASE == nType ||
152*cdf0e10cSrcweir            (sal_uInt32) TransliterationModulesExtra::SENTENCE_CASE == (sal_uInt32) nType ||
153*cdf0e10cSrcweir            (sal_uInt32) TransliterationModulesExtra::TITLE_CASE    == (sal_uInt32) nType ||
154*cdf0e10cSrcweir            (sal_uInt32) TransliterationModulesExtra::TOGGLE_CASE   == (sal_uInt32) nType;
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir void TransliterationWrapper::setLanguageLocaleImpl( sal_uInt16 nLang )
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir     nLanguage = nLang;
161*cdf0e10cSrcweir     if( LANGUAGE_NONE == nLanguage )
162*cdf0e10cSrcweir         nLanguage = LANGUAGE_SYSTEM;
163*cdf0e10cSrcweir     MsLangId::convertLanguageToLocale( nLanguage, aLocale);
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir void TransliterationWrapper::loadModuleIfNeeded( sal_uInt16 nLang )
168*cdf0e10cSrcweir {
169*cdf0e10cSrcweir     sal_Bool bLoad = bFirstCall;
170*cdf0e10cSrcweir     bFirstCall = sal_False;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir     if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::SENTENCE_CASE )
173*cdf0e10cSrcweir     {
174*cdf0e10cSrcweir         if( bLoad )
175*cdf0e10cSrcweir             loadModuleByImplName(String::CreateFromAscii("SENTENCE_CASE"), nLang);
176*cdf0e10cSrcweir     }
177*cdf0e10cSrcweir     else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TITLE_CASE )
178*cdf0e10cSrcweir     {
179*cdf0e10cSrcweir         if( bLoad )
180*cdf0e10cSrcweir             loadModuleByImplName(String::CreateFromAscii("TITLE_CASE"), nLang);
181*cdf0e10cSrcweir     }
182*cdf0e10cSrcweir     else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TOGGLE_CASE )
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         if( bLoad )
185*cdf0e10cSrcweir             loadModuleByImplName(String::CreateFromAscii("TOGGLE_CASE"), nLang);
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir     else
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         if( nLanguage != nLang )
190*cdf0e10cSrcweir         {
191*cdf0e10cSrcweir             setLanguageLocaleImpl( nLang );
192*cdf0e10cSrcweir             if( !bLoad )
193*cdf0e10cSrcweir                 bLoad = needLanguageForTheMode();
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir         if( bLoad )
196*cdf0e10cSrcweir             loadModuleImpl();
197*cdf0e10cSrcweir     }
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir void TransliterationWrapper::loadModuleImpl() const
202*cdf0e10cSrcweir {
203*cdf0e10cSrcweir     if ( bFirstCall )
204*cdf0e10cSrcweir         ((TransliterationWrapper*)this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM );
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     try
207*cdf0e10cSrcweir     {
208*cdf0e10cSrcweir         if ( xTrans.is() )
209*cdf0e10cSrcweir             xTrans->loadModule( (TransliterationModules)nType, aLocale );
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir     catch ( Exception& e )
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir #ifdef DBG_UTIL
214*cdf0e10cSrcweir         ByteString aMsg( "loadModuleImpl: Exception caught\n" );
215*cdf0e10cSrcweir         aMsg += ByteString( String( e.Message ), RTL_TEXTENCODING_UTF8 );
216*cdf0e10cSrcweir         DBG_ERRORFILE( aMsg.GetBuffer() );
217*cdf0e10cSrcweir #else
218*cdf0e10cSrcweir         (void)e;
219*cdf0e10cSrcweir #endif
220*cdf0e10cSrcweir     }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir     bFirstCall = sal_False;
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir void TransliterationWrapper::loadModuleByImplName(
227*cdf0e10cSrcweir         const String& rModuleName, sal_uInt16 nLang )
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir     try
230*cdf0e10cSrcweir     {
231*cdf0e10cSrcweir         setLanguageLocaleImpl( nLang );
232*cdf0e10cSrcweir         // Reset LanguageType, so the next call to loadModuleIfNeeded() forces
233*cdf0e10cSrcweir         // new settings.
234*cdf0e10cSrcweir         nLanguage = LANGUAGE_DONTKNOW;
235*cdf0e10cSrcweir         if ( xTrans.is() )
236*cdf0e10cSrcweir             xTrans->loadModuleByImplName( rModuleName, aLocale );
237*cdf0e10cSrcweir     }
238*cdf0e10cSrcweir     catch ( Exception& e )
239*cdf0e10cSrcweir     {
240*cdf0e10cSrcweir #ifdef DBG_UTIL
241*cdf0e10cSrcweir         ByteString aMsg( "loadModuleByImplName: Exception caught\n" );
242*cdf0e10cSrcweir         aMsg += ByteString( String( e.Message ), RTL_TEXTENCODING_UTF8 );
243*cdf0e10cSrcweir         DBG_ERRORFILE( aMsg.GetBuffer() );
244*cdf0e10cSrcweir #else
245*cdf0e10cSrcweir         (void)e;
246*cdf0e10cSrcweir #endif
247*cdf0e10cSrcweir     }
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir     bFirstCall = sal_False;
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir sal_Bool TransliterationWrapper::equals(
254*cdf0e10cSrcweir     const String& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1,
255*cdf0e10cSrcweir     const String& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir     try
258*cdf0e10cSrcweir     {
259*cdf0e10cSrcweir         if( bFirstCall )
260*cdf0e10cSrcweir             loadModuleImpl();
261*cdf0e10cSrcweir         if ( xTrans.is() )
262*cdf0e10cSrcweir             return xTrans->equals( rStr1, nPos1, nCount1, nMatch1, rStr2, nPos2, nCount2, nMatch2 );
263*cdf0e10cSrcweir     }
264*cdf0e10cSrcweir 	catch ( Exception& e )
265*cdf0e10cSrcweir 	{
266*cdf0e10cSrcweir #ifdef DBG_UTIL
267*cdf0e10cSrcweir         ByteString aMsg( "equals: Exception caught\n" );
268*cdf0e10cSrcweir 		aMsg += ByteString( String( e.Message ), RTL_TEXTENCODING_UTF8 );
269*cdf0e10cSrcweir 		DBG_ERRORFILE( aMsg.GetBuffer() );
270*cdf0e10cSrcweir #else
271*cdf0e10cSrcweir         (void)e;
272*cdf0e10cSrcweir #endif
273*cdf0e10cSrcweir 	}
274*cdf0e10cSrcweir     return sal_False;
275*cdf0e10cSrcweir }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir sal_Int32 TransliterationWrapper::compareSubstring(
279*cdf0e10cSrcweir     const String& rStr1, sal_Int32 nOff1, sal_Int32 nLen1,
280*cdf0e10cSrcweir     const String& rStr2, sal_Int32 nOff2, sal_Int32 nLen2 ) const
281*cdf0e10cSrcweir {
282*cdf0e10cSrcweir     try
283*cdf0e10cSrcweir     {
284*cdf0e10cSrcweir         if( bFirstCall )
285*cdf0e10cSrcweir             loadModuleImpl();
286*cdf0e10cSrcweir         if ( xTrans.is() )
287*cdf0e10cSrcweir             return xTrans->compareSubstring( rStr1, nOff1, nLen1, rStr2, nOff2, nLen2 );
288*cdf0e10cSrcweir     }
289*cdf0e10cSrcweir 	catch ( Exception& e )
290*cdf0e10cSrcweir 	{
291*cdf0e10cSrcweir #ifdef DBG_UTIL
292*cdf0e10cSrcweir         ByteString aMsg( "compareSubstring: Exception caught\n" );
293*cdf0e10cSrcweir 		aMsg += ByteString( String( e.Message ), RTL_TEXTENCODING_UTF8 );
294*cdf0e10cSrcweir 		DBG_ERRORFILE( aMsg.GetBuffer() );
295*cdf0e10cSrcweir #else
296*cdf0e10cSrcweir         (void)e;
297*cdf0e10cSrcweir #endif
298*cdf0e10cSrcweir 	}
299*cdf0e10cSrcweir     return 0;
300*cdf0e10cSrcweir }
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir sal_Int32 TransliterationWrapper::compareString( const String& rStr1, const String& rStr2 ) const
304*cdf0e10cSrcweir {
305*cdf0e10cSrcweir     try
306*cdf0e10cSrcweir     {
307*cdf0e10cSrcweir         if( bFirstCall )
308*cdf0e10cSrcweir             loadModuleImpl();
309*cdf0e10cSrcweir         if ( xTrans.is() )
310*cdf0e10cSrcweir             return xTrans->compareString( rStr1, rStr2 );
311*cdf0e10cSrcweir     }
312*cdf0e10cSrcweir 	catch ( Exception& e )
313*cdf0e10cSrcweir 	{
314*cdf0e10cSrcweir #ifdef DBG_UTIL
315*cdf0e10cSrcweir         ByteString aMsg( "compareString: Exception caught\n" );
316*cdf0e10cSrcweir 		aMsg += ByteString( String( e.Message ), RTL_TEXTENCODING_UTF8 );
317*cdf0e10cSrcweir 		DBG_ERRORFILE( aMsg.GetBuffer() );
318*cdf0e10cSrcweir #else
319*cdf0e10cSrcweir         (void)e;
320*cdf0e10cSrcweir #endif
321*cdf0e10cSrcweir 	}
322*cdf0e10cSrcweir     return 0;
323*cdf0e10cSrcweir }
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir // --- helpers --------------------------------------------------------
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir sal_Bool TransliterationWrapper::isEqual( const String& rStr1, const String& rStr2 ) const
329*cdf0e10cSrcweir {
330*cdf0e10cSrcweir     sal_Int32 nMatch1, nMatch2;
331*cdf0e10cSrcweir     sal_Bool bMatch = equals(
332*cdf0e10cSrcweir         rStr1, 0, rStr1.Len(), nMatch1,
333*cdf0e10cSrcweir         rStr2, 0, rStr2.Len(), nMatch2 );
334*cdf0e10cSrcweir     return bMatch;
335*cdf0e10cSrcweir }
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir sal_Bool TransliterationWrapper::isMatch( const String& rStr1, const String& rStr2 ) const
339*cdf0e10cSrcweir {
340*cdf0e10cSrcweir     sal_Int32 nMatch1, nMatch2;
341*cdf0e10cSrcweir     equals(
342*cdf0e10cSrcweir         rStr1, 0, rStr1.Len(), nMatch1,
343*cdf0e10cSrcweir         rStr2, 0, rStr2.Len(), nMatch2 );
344*cdf0e10cSrcweir     return (nMatch1 <= nMatch2) && (nMatch1 == rStr1.Len());
345*cdf0e10cSrcweir }
346