Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / i18n / transliterationwrapper.cxx
bloba8e4baedf90593d86a9b9d7d43630cb2d6971dbd
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 .
20 #include <sal/config.h>
22 #include <unotools/transliterationwrapper.hxx>
23 #include <i18nlangtag/languagetag.hxx>
24 #include <i18nutil/transliteration.hxx>
25 #include <comphelper/diagnose_ex.hxx>
27 #include <com/sun/star/i18n/Transliteration.hpp>
29 using namespace ::com::sun::star::lang;
30 using namespace ::com::sun::star::i18n;
31 using namespace ::com::sun::star::uno;
32 using namespace ::utl;
34 TransliterationWrapper::TransliterationWrapper(
35 const Reference< XComponentContext > & rxContext,
36 TransliterationFlags nTyp )
37 : xTrans( Transliteration::create(rxContext) ),
38 aLanguageTag( LANGUAGE_SYSTEM ), nType( nTyp ), bFirstCall( true )
42 TransliterationWrapper::~TransliterationWrapper()
46 OUString TransliterationWrapper::transliterate(const OUString& rStr, LanguageType nLang,
47 sal_Int32 nStart, sal_Int32 nLen,
48 Sequence <sal_Int32>* pOffset )
50 OUString sRet;
51 if( xTrans.is() )
53 try
55 loadModuleIfNeeded( nLang );
57 if ( pOffset )
58 sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
59 else
60 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
62 catch( Exception& )
64 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
67 return sRet;
70 OUString TransliterationWrapper::transliterate( const OUString& rStr,
71 sal_Int32 nStart, sal_Int32 nLen ) const
73 OUString sRet( rStr );
74 if( xTrans.is() )
76 try
78 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
80 catch( Exception& )
82 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
85 return sRet;
88 bool TransliterationWrapper::needLanguageForTheMode() const
90 return TransliterationFlags::UPPERCASE_LOWERCASE == nType ||
91 TransliterationFlags::LOWERCASE_UPPERCASE == nType ||
92 TransliterationFlags::IGNORE_CASE == nType ||
93 TransliterationFlags::SENTENCE_CASE == nType ||
94 TransliterationFlags::TITLE_CASE == nType ||
95 TransliterationFlags::TOGGLE_CASE == nType;
98 void TransliterationWrapper::setLanguageLocaleImpl( LanguageType nLang )
100 if( LANGUAGE_NONE == nLang )
101 nLang = LANGUAGE_SYSTEM;
102 aLanguageTag.reset( nLang);
105 void TransliterationWrapper::loadModuleIfNeeded( LanguageType nLang )
107 bool bLoad = bFirstCall;
108 bFirstCall = false;
110 if( nType == TransliterationFlags::SENTENCE_CASE )
112 if( bLoad )
113 loadModuleByImplName("SENTENCE_CASE", nLang);
115 else if( nType == TransliterationFlags::TITLE_CASE )
117 if( bLoad )
118 loadModuleByImplName("TITLE_CASE", nLang);
120 else if( nType == TransliterationFlags::TOGGLE_CASE )
122 if( bLoad )
123 loadModuleByImplName("TOGGLE_CASE", nLang);
125 else
127 if( aLanguageTag.getLanguageType() != nLang )
129 setLanguageLocaleImpl( nLang );
130 if( !bLoad )
131 bLoad = needLanguageForTheMode();
133 if( bLoad )
134 loadModuleImpl();
138 void TransliterationWrapper::loadModuleImpl() const
140 if ( bFirstCall )
141 const_cast<TransliterationWrapper*>(this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM );
145 if ( xTrans.is() )
146 xTrans->loadModule( static_cast<TransliterationModules>(nType), aLanguageTag.getLocale() );
148 catch ( const Exception& )
150 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleImpl" );
153 bFirstCall = false;
156 void TransliterationWrapper::loadModuleByImplName(const OUString& rModuleName,
157 LanguageType nLang )
161 setLanguageLocaleImpl( nLang );
162 css::lang::Locale aLocale( aLanguageTag.getLocale());
163 // Reset LanguageTag, so the next call to loadModuleIfNeeded() forces
164 // new settings.
165 aLanguageTag.reset( LANGUAGE_DONTKNOW);
166 if ( xTrans.is() )
167 xTrans->loadModuleByImplName( rModuleName, aLocale );
169 catch ( const Exception& )
171 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleByImplName" );
174 bFirstCall = false;
177 bool TransliterationWrapper::equals(
178 const OUString& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1,
179 const OUString& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const
183 if( bFirstCall )
184 loadModuleImpl();
185 if ( xTrans.is() )
186 return xTrans->equals( rStr1, nPos1, nCount1, nMatch1, rStr2, nPos2, nCount2, nMatch2 );
188 catch ( const Exception& )
190 TOOLS_WARN_EXCEPTION( "unotools.i18n", "equals" );
192 return false;
195 sal_Int32 TransliterationWrapper::compareString( const OUString& rStr1, const OUString& rStr2 ) const
199 if( bFirstCall )
200 loadModuleImpl();
201 if ( xTrans.is() )
202 return xTrans->compareString( rStr1, rStr2 );
204 catch (const Exception&)
206 TOOLS_WARN_EXCEPTION( "unotools.i18n", "compareString" );
208 return 0;
211 // --- helpers --------------------------------------------------------
213 bool TransliterationWrapper::isEqual( const OUString& rStr1, const OUString& rStr2 ) const
215 sal_Int32 nMatch1(0), nMatch2(0);
216 bool bMatch = equals(
217 rStr1, 0, rStr1.getLength(), nMatch1,
218 rStr2, 0, rStr2.getLength(), nMatch2 );
219 return bMatch;
222 bool TransliterationWrapper::isMatch( const OUString& rStr1, const OUString& rStr2 ) const
224 sal_Int32 nMatch1(0), nMatch2(0);
225 equals(
226 rStr1, 0, rStr1.getLength(), nMatch1,
227 rStr2, 0, rStr2.getLength(), nMatch2 );
228 return (nMatch1 <= nMatch2) && (nMatch1 == rStr1.getLength());
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */