nss: upgrade to release 3.73
[LibreOffice.git] / unotools / source / i18n / transliterationwrapper.cxx
blobff6f4933d88c317e2c4af0402840e9c619ebc3d6
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 <sal/log.hxx>
23 #include <unotools/transliterationwrapper.hxx>
24 #include <i18nlangtag/languagetag.hxx>
25 #include <i18nutil/transliteration.hxx>
26 #include <tools/diagnose_ex.h>
28 #include <com/sun/star/i18n/Transliteration.hpp>
30 using namespace ::com::sun::star::lang;
31 using namespace ::com::sun::star::i18n;
32 using namespace ::com::sun::star::uno;
33 using namespace ::utl;
35 TransliterationWrapper::TransliterationWrapper(
36 const Reference< XComponentContext > & rxContext,
37 TransliterationFlags nTyp )
38 : xTrans( Transliteration::create(rxContext) ),
39 aLanguageTag( LANGUAGE_SYSTEM ), nType( nTyp ), bFirstCall( true )
43 TransliterationWrapper::~TransliterationWrapper()
47 OUString TransliterationWrapper::transliterate(const OUString& rStr, LanguageType nLang,
48 sal_Int32 nStart, sal_Int32 nLen,
49 Sequence <sal_Int32>* pOffset )
51 OUString sRet;
52 if( xTrans.is() )
54 try
56 loadModuleIfNeeded( nLang );
58 if ( pOffset )
59 sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
60 else
61 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
63 catch( Exception& )
65 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
68 return sRet;
71 OUString TransliterationWrapper::transliterate( const OUString& rStr,
72 sal_Int32 nStart, sal_Int32 nLen ) const
74 OUString sRet( rStr );
75 if( xTrans.is() )
77 try
79 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
81 catch( Exception& )
83 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
86 return sRet;
89 bool TransliterationWrapper::needLanguageForTheMode() const
91 return TransliterationFlags::UPPERCASE_LOWERCASE == nType ||
92 TransliterationFlags::LOWERCASE_UPPERCASE == nType ||
93 TransliterationFlags::IGNORE_CASE == nType ||
94 TransliterationFlags::SENTENCE_CASE == nType ||
95 TransliterationFlags::TITLE_CASE == nType ||
96 TransliterationFlags::TOGGLE_CASE == nType;
99 void TransliterationWrapper::setLanguageLocaleImpl( LanguageType nLang )
101 if( LANGUAGE_NONE == nLang )
102 nLang = LANGUAGE_SYSTEM;
103 aLanguageTag.reset( nLang);
106 void TransliterationWrapper::loadModuleIfNeeded( LanguageType nLang )
108 bool bLoad = bFirstCall;
109 bFirstCall = false;
111 if( nType == TransliterationFlags::SENTENCE_CASE )
113 if( bLoad )
114 loadModuleByImplName("SENTENCE_CASE", nLang);
116 else if( nType == TransliterationFlags::TITLE_CASE )
118 if( bLoad )
119 loadModuleByImplName("TITLE_CASE", nLang);
121 else if( nType == TransliterationFlags::TOGGLE_CASE )
123 if( bLoad )
124 loadModuleByImplName("TOGGLE_CASE", nLang);
126 else
128 if( aLanguageTag.getLanguageType() != nLang )
130 setLanguageLocaleImpl( nLang );
131 if( !bLoad )
132 bLoad = needLanguageForTheMode();
134 if( bLoad )
135 loadModuleImpl();
139 void TransliterationWrapper::loadModuleImpl() const
141 if ( bFirstCall )
142 const_cast<TransliterationWrapper*>(this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM );
146 if ( xTrans.is() )
147 xTrans->loadModule( static_cast<TransliterationModules>(nType), aLanguageTag.getLocale() );
149 catch ( const Exception& )
151 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleImpl" );
154 bFirstCall = false;
157 void TransliterationWrapper::loadModuleByImplName(const OUString& rModuleName,
158 LanguageType nLang )
162 setLanguageLocaleImpl( nLang );
163 css::lang::Locale aLocale( aLanguageTag.getLocale());
164 // Reset LanguageTag, so the next call to loadModuleIfNeeded() forces
165 // new settings.
166 aLanguageTag.reset( LANGUAGE_DONTKNOW);
167 if ( xTrans.is() )
168 xTrans->loadModuleByImplName( rModuleName, aLocale );
170 catch ( const Exception& )
172 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleByImplName" );
175 bFirstCall = false;
178 bool TransliterationWrapper::equals(
179 const OUString& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1,
180 const OUString& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const
184 if( bFirstCall )
185 loadModuleImpl();
186 if ( xTrans.is() )
187 return xTrans->equals( rStr1, nPos1, nCount1, nMatch1, rStr2, nPos2, nCount2, nMatch2 );
189 catch ( const Exception& )
191 TOOLS_WARN_EXCEPTION( "unotools.i18n", "equals" );
193 return false;
196 sal_Int32 TransliterationWrapper::compareString( const OUString& rStr1, const OUString& rStr2 ) const
200 if( bFirstCall )
201 loadModuleImpl();
202 if ( xTrans.is() )
203 return xTrans->compareString( rStr1, rStr2 );
205 catch (const Exception&)
207 TOOLS_WARN_EXCEPTION( "unotools.i18n", "compareString" );
209 return 0;
212 // --- helpers --------------------------------------------------------
214 bool TransliterationWrapper::isEqual( const OUString& rStr1, const OUString& rStr2 ) const
216 sal_Int32 nMatch1(0), nMatch2(0);
217 bool bMatch = equals(
218 rStr1, 0, rStr1.getLength(), nMatch1,
219 rStr2, 0, rStr2.getLength(), nMatch2 );
220 return bMatch;
223 bool TransliterationWrapper::isMatch( const OUString& rStr1, const OUString& rStr2 ) const
225 sal_Int32 nMatch1(0), nMatch2(0);
226 equals(
227 rStr1, 0, rStr1.getLength(), nMatch1,
228 rStr2, 0, rStr2.getLength(), nMatch2 );
229 return (nMatch1 <= nMatch2) && (nMatch1 == rStr1.getLength());
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */