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 <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
)
56 loadModuleIfNeeded( nLang
);
59 sRet
= xTrans
->transliterate( rStr
, nStart
, nLen
, *pOffset
);
61 sRet
= xTrans
->transliterateString2String( rStr
, nStart
, nLen
);
65 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
71 OUString
TransliterationWrapper::transliterate( const OUString
& rStr
,
72 sal_Int32 nStart
, sal_Int32 nLen
) const
74 OUString
sRet( rStr
);
79 sRet
= xTrans
->transliterateString2String( rStr
, nStart
, nLen
);
83 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
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
;
111 if( nType
== TransliterationFlags::SENTENCE_CASE
)
114 loadModuleByImplName("SENTENCE_CASE", nLang
);
116 else if( nType
== TransliterationFlags::TITLE_CASE
)
119 loadModuleByImplName("TITLE_CASE", nLang
);
121 else if( nType
== TransliterationFlags::TOGGLE_CASE
)
124 loadModuleByImplName("TOGGLE_CASE", nLang
);
128 if( aLanguageTag
.getLanguageType() != nLang
)
130 setLanguageLocaleImpl( nLang
);
132 bLoad
= needLanguageForTheMode();
139 void TransliterationWrapper::loadModuleImpl() const
142 const_cast<TransliterationWrapper
*>(this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM
);
147 xTrans
->loadModule( static_cast<TransliterationModules
>(nType
), aLanguageTag
.getLocale() );
149 catch ( const Exception
& )
151 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleImpl" );
157 void TransliterationWrapper::loadModuleByImplName(const OUString
& rModuleName
,
162 setLanguageLocaleImpl( nLang
);
163 css::lang::Locale
aLocale( aLanguageTag
.getLocale());
164 // Reset LanguageTag, so the next call to loadModuleIfNeeded() forces
166 aLanguageTag
.reset( LANGUAGE_DONTKNOW
);
168 xTrans
->loadModuleByImplName( rModuleName
, aLocale
);
170 catch ( const Exception
& )
172 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleByImplName" );
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
187 return xTrans
->equals( rStr1
, nPos1
, nCount1
, nMatch1
, rStr2
, nPos2
, nCount2
, nMatch2
);
189 catch ( const Exception
& )
191 TOOLS_WARN_EXCEPTION( "unotools.i18n", "equals" );
196 sal_Int32
TransliterationWrapper::compareString( const OUString
& rStr1
, const OUString
& rStr2
) const
203 return xTrans
->compareString( rStr1
, rStr2
);
205 catch (const Exception
&)
207 TOOLS_WARN_EXCEPTION( "unotools.i18n", "compareString" );
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
);
223 bool TransliterationWrapper::isMatch( const OUString
& rStr1
, const OUString
& rStr2
) const
225 sal_Int32
nMatch1(0), nMatch2(0);
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: */