update credits
[LibreOffice.git] / unotools / source / i18n / transliterationwrapper.cxx
blob703c37e148cce2864c944129086be090218915bb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <unotools/transliterationwrapper.hxx>
22 #include <tools/debug.hxx>
23 #include <i18nlangtag/languagetag.hxx>
25 #include <com/sun/star/i18n/TransliterationModulesExtra.hpp>
26 #include <com/sun/star/i18n/Transliteration.hpp>
28 using namespace ::com::sun::star::lang;
29 using namespace ::com::sun::star::i18n;
30 using namespace ::com::sun::star::uno;
31 using namespace ::utl;
33 TransliterationWrapper::TransliterationWrapper(
34 const Reference< XComponentContext > & rxContext,
35 sal_uInt32 nTyp )
36 : xTrans( Transliteration::create(rxContext) ),
37 aLanguageTag( LANGUAGE_SYSTEM ), nType( nTyp ), bFirstCall( sal_True )
42 TransliterationWrapper::~TransliterationWrapper()
47 String TransliterationWrapper::transliterate(
48 const String& rStr, sal_uInt16 nLang,
49 xub_StrLen nStart, xub_StrLen nLen,
50 Sequence <sal_Int32>* pOffset )
52 String sRet;
53 if( xTrans.is() )
55 try
57 loadModuleIfNeeded( nLang );
59 if ( pOffset )
60 sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
61 else
62 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
64 catch( Exception& )
66 SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" );
69 return sRet;
73 String TransliterationWrapper::transliterate(
74 const String& rStr,
75 xub_StrLen nStart, xub_StrLen nLen,
76 Sequence <sal_Int32>* pOffset ) const
78 String sRet( rStr );
79 if( xTrans.is() )
81 try
83 if ( pOffset )
84 sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
85 else
86 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
88 catch( Exception& )
90 SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" );
93 return sRet;
96 sal_Bool TransliterationWrapper::needLanguageForTheMode() const
98 return TransliterationModules_UPPERCASE_LOWERCASE == nType ||
99 TransliterationModules_LOWERCASE_UPPERCASE == nType ||
100 TransliterationModules_IGNORE_CASE == nType ||
101 (sal_uInt32) TransliterationModulesExtra::SENTENCE_CASE == (sal_uInt32) nType ||
102 (sal_uInt32) TransliterationModulesExtra::TITLE_CASE == (sal_uInt32) nType ||
103 (sal_uInt32) TransliterationModulesExtra::TOGGLE_CASE == (sal_uInt32) nType;
107 void TransliterationWrapper::setLanguageLocaleImpl( sal_uInt16 nLang )
109 if( LANGUAGE_NONE == nLang )
110 nLang = LANGUAGE_SYSTEM;
111 aLanguageTag.reset( nLang);
115 void TransliterationWrapper::loadModuleIfNeeded( sal_uInt16 nLang )
117 sal_Bool bLoad = bFirstCall;
118 bFirstCall = sal_False;
120 if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::SENTENCE_CASE )
122 if( bLoad )
123 loadModuleByImplName(OUString("SENTENCE_CASE"), nLang);
125 else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TITLE_CASE )
127 if( bLoad )
128 loadModuleByImplName(OUString("TITLE_CASE"), nLang);
130 else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TOGGLE_CASE )
132 if( bLoad )
133 loadModuleByImplName(OUString("TOGGLE_CASE"), nLang);
135 else
137 if( aLanguageTag.getLanguageType() != nLang )
139 setLanguageLocaleImpl( nLang );
140 if( !bLoad )
141 bLoad = needLanguageForTheMode();
143 if( bLoad )
144 loadModuleImpl();
149 void TransliterationWrapper::loadModuleImpl() const
151 if ( bFirstCall )
152 ((TransliterationWrapper*)this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM );
156 if ( xTrans.is() )
157 xTrans->loadModule( (TransliterationModules)nType, aLanguageTag.getLocale() );
159 catch ( const Exception& e )
161 SAL_WARN( "unotools.i18n", "loadModuleImpl: Exception caught " << e.Message );
164 bFirstCall = sal_False;
168 void TransliterationWrapper::loadModuleByImplName(
169 const String& rModuleName, sal_uInt16 nLang )
173 setLanguageLocaleImpl( nLang );
174 com::sun::star::lang::Locale aLocale( aLanguageTag.getLocale());
175 // Reset LanguageTag, so the next call to loadModuleIfNeeded() forces
176 // new settings.
177 aLanguageTag.reset( LANGUAGE_DONTKNOW);
178 if ( xTrans.is() )
179 xTrans->loadModuleByImplName( rModuleName, aLocale );
181 catch ( const Exception& e )
183 SAL_WARN( "unotools.i18n", "loadModuleByImplName: Exception caught " << e.Message );
186 bFirstCall = sal_False;
190 sal_Bool TransliterationWrapper::equals(
191 const String& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1,
192 const String& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const
196 if( bFirstCall )
197 loadModuleImpl();
198 if ( xTrans.is() )
199 return xTrans->equals( rStr1, nPos1, nCount1, nMatch1, rStr2, nPos2, nCount2, nMatch2 );
201 catch ( const Exception& e )
203 SAL_WARN( "unotools.i18n", "equals: Exception caught " << e.Message );
205 return sal_False;
208 sal_Int32 TransliterationWrapper::compareString( const String& rStr1, const String& rStr2 ) const
212 if( bFirstCall )
213 loadModuleImpl();
214 if ( xTrans.is() )
215 return xTrans->compareString( rStr1, rStr2 );
217 catch (const Exception& e)
219 SAL_WARN( "unotools.i18n", "compareString: Exception caught " << e.Message );
221 return 0;
225 // --- helpers --------------------------------------------------------
227 sal_Bool TransliterationWrapper::isEqual( const String& rStr1, const String& rStr2 ) const
229 sal_Int32 nMatch1, nMatch2;
230 sal_Bool bMatch = equals(
231 rStr1, 0, rStr1.Len(), nMatch1,
232 rStr2, 0, rStr2.Len(), nMatch2 );
233 return bMatch;
237 sal_Bool TransliterationWrapper::isMatch( const String& rStr1, const String& rStr2 ) const
239 sal_Int32 nMatch1, nMatch2;
240 equals(
241 rStr1, 0, rStr1.Len(), nMatch1,
242 rStr2, 0, rStr2.Len(), nMatch2 );
243 return (nMatch1 <= nMatch2) && (nMatch1 == rStr1.Len());
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */