1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
55 loadModuleIfNeeded( nLang
);
58 sRet
= xTrans
->transliterate( rStr
, nStart
, nLen
, *pOffset
);
60 sRet
= xTrans
->transliterateString2String( rStr
, nStart
, nLen
);
64 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
70 OUString
TransliterationWrapper::transliterate( const OUString
& rStr
,
71 sal_Int32 nStart
, sal_Int32 nLen
) const
73 OUString
sRet( rStr
);
78 sRet
= xTrans
->transliterateString2String( rStr
, nStart
, nLen
);
82 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
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
;
110 if( nType
== TransliterationFlags::SENTENCE_CASE
)
113 loadModuleByImplName("SENTENCE_CASE", nLang
);
115 else if( nType
== TransliterationFlags::TITLE_CASE
)
118 loadModuleByImplName("TITLE_CASE", nLang
);
120 else if( nType
== TransliterationFlags::TOGGLE_CASE
)
123 loadModuleByImplName("TOGGLE_CASE", nLang
);
127 if( aLanguageTag
.getLanguageType() != nLang
)
129 setLanguageLocaleImpl( nLang
);
131 bLoad
= needLanguageForTheMode();
138 void TransliterationWrapper::loadModuleImpl() const
141 const_cast<TransliterationWrapper
*>(this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM
);
146 xTrans
->loadModule( static_cast<TransliterationModules
>(nType
), aLanguageTag
.getLocale() );
148 catch ( const Exception
& )
150 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleImpl" );
156 void TransliterationWrapper::loadModuleByImplName(const OUString
& rModuleName
,
161 setLanguageLocaleImpl( nLang
);
162 css::lang::Locale
aLocale( aLanguageTag
.getLocale());
163 // Reset LanguageTag, so the next call to loadModuleIfNeeded() forces
165 aLanguageTag
.reset( LANGUAGE_DONTKNOW
);
167 xTrans
->loadModuleByImplName( rModuleName
, aLocale
);
169 catch ( const Exception
& )
171 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleByImplName" );
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
186 return xTrans
->equals( rStr1
, nPos1
, nCount1
, nMatch1
, rStr2
, nPos2
, nCount2
, nMatch2
);
188 catch ( const Exception
& )
190 TOOLS_WARN_EXCEPTION( "unotools.i18n", "equals" );
195 sal_Int32
TransliterationWrapper::compareString( const OUString
& rStr1
, const OUString
& rStr2
) const
202 return xTrans
->compareString( rStr1
, rStr2
);
204 catch (const Exception
&)
206 TOOLS_WARN_EXCEPTION( "unotools.i18n", "compareString" );
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
);
222 bool TransliterationWrapper::isMatch( const OUString
& rStr1
, const OUString
& rStr2
) const
224 sal_Int32
nMatch1(0), nMatch2(0);
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: */