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 .
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
,
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
)
57 loadModuleIfNeeded( nLang
);
60 sRet
= xTrans
->transliterate( rStr
, nStart
, nLen
, *pOffset
);
62 sRet
= xTrans
->transliterateString2String( rStr
, nStart
, nLen
);
66 SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" );
73 String
TransliterationWrapper::transliterate(
75 xub_StrLen nStart
, xub_StrLen nLen
,
76 Sequence
<sal_Int32
>* pOffset
) const
84 sRet
= xTrans
->transliterate( rStr
, nStart
, nLen
, *pOffset
);
86 sRet
= xTrans
->transliterateString2String( rStr
, nStart
, nLen
);
90 SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" );
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
)
123 loadModuleByImplName(OUString("SENTENCE_CASE"), nLang
);
125 else if( static_cast< sal_Int32
>(nType
) == TransliterationModulesExtra::TITLE_CASE
)
128 loadModuleByImplName(OUString("TITLE_CASE"), nLang
);
130 else if( static_cast< sal_Int32
>(nType
) == TransliterationModulesExtra::TOGGLE_CASE
)
133 loadModuleByImplName(OUString("TOGGLE_CASE"), nLang
);
137 if( aLanguageTag
.getLanguageType() != nLang
)
139 setLanguageLocaleImpl( nLang
);
141 bLoad
= needLanguageForTheMode();
149 void TransliterationWrapper::loadModuleImpl() const
152 ((TransliterationWrapper
*)this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM
);
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
177 aLanguageTag
.reset( LANGUAGE_DONTKNOW
);
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
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
);
208 sal_Int32
TransliterationWrapper::compareString( const String
& rStr1
, const String
& rStr2
) const
215 return xTrans
->compareString( rStr1
, rStr2
);
217 catch (const Exception
& e
)
219 SAL_WARN( "unotools.i18n", "compareString: Exception caught " << e
.Message
);
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
);
237 sal_Bool
TransliterationWrapper::isMatch( const String
& rStr1
, const String
& rStr2
) const
239 sal_Int32 nMatch1
, nMatch2
;
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: */