Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : :
21 : : #include <unotools/transliterationwrapper.hxx>
22 : : #include <tools/debug.hxx>
23 : : #include <i18npool/mslangid.hxx>
24 : :
25 : : #include "instance.hxx"
26 : : #include <com/sun/star/i18n/TransliterationModulesExtra.hpp>
27 : :
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;
32 : :
33 : 304 : TransliterationWrapper::TransliterationWrapper(
34 : : const Reference< XMultiServiceFactory > & xSF,
35 : : sal_uInt32 nTyp )
36 : 304 : : xSMgr( xSF ), nType( nTyp ), nLanguage( 0 ), bFirstCall( sal_True )
37 : : {
38 : : xTrans = Reference< XExtendedTransliteration > (
39 : : intl_createInstance( xSMgr, "com.sun.star.i18n.Transliteration",
40 [ + - ][ + - ]: 304 : "TransliterationWrapper" ), UNO_QUERY );
[ + - ]
41 : : DBG_ASSERT( xTrans.is(), "TransliterationWrapper: no Transliteraion available" );
42 : 304 : }
43 : :
44 : :
45 : 210 : TransliterationWrapper::~TransliterationWrapper()
46 : : {
47 : 210 : }
48 : :
49 : :
50 : 57 : String TransliterationWrapper::transliterate(
51 : : const String& rStr, sal_uInt16 nLang,
52 : : xub_StrLen nStart, xub_StrLen nLen,
53 : : Sequence <sal_Int32>* pOffset )
54 : : {
55 : 57 : String sRet;
56 [ + - ]: 57 : if( xTrans.is() )
57 : : {
58 : : try
59 : : {
60 [ + - ]: 57 : loadModuleIfNeeded( nLang );
61 : :
62 [ + - ]: 57 : if ( pOffset )
63 [ + - ][ + - ]: 57 : sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
[ + - ][ + - ]
64 : : else
65 [ # # ][ # # ]: 0 : sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
[ # # ][ # # ]
[ # # ]
66 : : }
67 [ # # ]: 0 : catch( Exception& )
68 : : {
69 : : SAL_WARN( "unotools", "transliterate: Exception caught!" );
70 : : }
71 : : }
72 : 57 : return sRet;
73 : : }
74 : :
75 : :
76 : 0 : String TransliterationWrapper::transliterate(
77 : : const String& rStr,
78 : : xub_StrLen nStart, xub_StrLen nLen,
79 : : Sequence <sal_Int32>* pOffset ) const
80 : : {
81 : 0 : String sRet( rStr );
82 [ # # ]: 0 : if( xTrans.is() )
83 : : {
84 : : try
85 : : {
86 [ # # ]: 0 : if ( pOffset )
87 [ # # ][ # # ]: 0 : sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
[ # # ][ # # ]
88 : : else
89 [ # # ][ # # ]: 0 : sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
[ # # ][ # # ]
[ # # ]
90 : : }
91 [ # # ]: 0 : catch( Exception& )
92 : : {
93 : : SAL_WARN( "unotools", "transliterate: Exception caught!" );
94 : : }
95 : : }
96 : 0 : return sRet;
97 : : }
98 : :
99 : 22 : sal_Bool TransliterationWrapper::needLanguageForTheMode() const
100 : : {
101 : : return TransliterationModules_UPPERCASE_LOWERCASE == nType ||
102 : : TransliterationModules_LOWERCASE_UPPERCASE == nType ||
103 : : TransliterationModules_IGNORE_CASE == nType ||
104 : : (sal_uInt32) TransliterationModulesExtra::SENTENCE_CASE == (sal_uInt32) nType ||
105 : : (sal_uInt32) TransliterationModulesExtra::TITLE_CASE == (sal_uInt32) nType ||
106 [ + - ][ + - ]: 22 : (sal_uInt32) TransliterationModulesExtra::TOGGLE_CASE == (sal_uInt32) nType;
[ - + ][ # # ]
[ # # ][ # # ]
107 : : }
108 : :
109 : :
110 : 232 : void TransliterationWrapper::setLanguageLocaleImpl( sal_uInt16 nLang )
111 : : {
112 : 232 : nLanguage = nLang;
113 [ - + ]: 232 : if( LANGUAGE_NONE == nLanguage )
114 : 0 : nLanguage = LANGUAGE_SYSTEM;
115 : 232 : MsLangId::convertLanguageToLocale( nLanguage, aLocale);
116 : 232 : }
117 : :
118 : :
119 : 373 : void TransliterationWrapper::loadModuleIfNeeded( sal_uInt16 nLang )
120 : : {
121 : 373 : sal_Bool bLoad = bFirstCall;
122 : 373 : bFirstCall = sal_False;
123 : :
124 [ - + ]: 373 : if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::SENTENCE_CASE )
125 : : {
126 [ # # ]: 0 : if( bLoad )
127 [ # # ][ # # ]: 0 : loadModuleByImplName(rtl::OUString("SENTENCE_CASE"), nLang);
[ # # ]
128 : : }
129 [ - + ]: 373 : else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TITLE_CASE )
130 : : {
131 [ # # ]: 0 : if( bLoad )
132 [ # # ][ # # ]: 0 : loadModuleByImplName(rtl::OUString("TITLE_CASE"), nLang);
[ # # ]
133 : : }
134 [ - + ]: 373 : else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TOGGLE_CASE )
135 : : {
136 [ # # ]: 0 : if( bLoad )
137 [ # # ][ # # ]: 0 : loadModuleByImplName(rtl::OUString("TOGGLE_CASE"), nLang);
[ # # ]
138 : : }
139 : : else
140 : : {
141 [ + + ]: 373 : if( nLanguage != nLang )
142 : : {
143 : 232 : setLanguageLocaleImpl( nLang );
144 [ + + ]: 232 : if( !bLoad )
145 : 22 : bLoad = needLanguageForTheMode();
146 : : }
147 [ + + ]: 373 : if( bLoad )
148 : 320 : loadModuleImpl();
149 : : }
150 : 373 : }
151 : :
152 : :
153 : 320 : void TransliterationWrapper::loadModuleImpl() const
154 : : {
155 [ - + ]: 320 : if ( bFirstCall )
156 : 0 : ((TransliterationWrapper*)this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM );
157 : :
158 : : try
159 : : {
160 [ + - ]: 320 : if ( xTrans.is() )
161 [ + - ][ + - ]: 320 : xTrans->loadModule( (TransliterationModules)nType, aLocale );
162 : : }
163 : 0 : catch ( const Exception& e )
164 : : {
165 : : SAL_WARN( "unotools.i18n", "loadModuleImpl: Exception caught " << e.Message );
166 : : }
167 : :
168 : 320 : bFirstCall = sal_False;
169 [ # # ]: 320 : }
170 : :
171 : :
172 : 0 : void TransliterationWrapper::loadModuleByImplName(
173 : : const String& rModuleName, sal_uInt16 nLang )
174 : : {
175 : : try
176 : : {
177 [ # # ]: 0 : setLanguageLocaleImpl( nLang );
178 : : // Reset LanguageType, so the next call to loadModuleIfNeeded() forces
179 : : // new settings.
180 : 0 : nLanguage = LANGUAGE_DONTKNOW;
181 [ # # ]: 0 : if ( xTrans.is() )
182 [ # # ][ # # ]: 0 : xTrans->loadModuleByImplName( rModuleName, aLocale );
[ # # ][ # # ]
183 : : }
184 : 0 : catch ( const Exception& e )
185 : : {
186 : : SAL_WARN( "unotools.i18n", "loadModuleByImplName: Exception caught " << e.Message );
187 : : }
188 : :
189 : 0 : bFirstCall = sal_False;
190 : 0 : }
191 : :
192 : :
193 : 8219 : sal_Bool TransliterationWrapper::equals(
194 : : const String& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1,
195 : : const String& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const
196 : : {
197 : : try
198 : : {
199 [ - + ]: 8219 : if( bFirstCall )
200 [ # # ]: 0 : loadModuleImpl();
201 [ + - ]: 8219 : if ( xTrans.is() )
202 [ + - ][ + - ]: 8219 : return xTrans->equals( rStr1, nPos1, nCount1, nMatch1, rStr2, nPos2, nCount2, nMatch2 );
[ + - ][ + - ]
[ # # ]
203 : : }
204 : 0 : catch ( const Exception& e )
205 : : {
206 : : SAL_WARN( "unotools.i18n", "equals: Exception caught " << e.Message );
207 : : }
208 : 8219 : return sal_False;
209 : : }
210 : :
211 : 143 : sal_Int32 TransliterationWrapper::compareString( const String& rStr1, const String& rStr2 ) const
212 : : {
213 : : try
214 : : {
215 [ - + ]: 143 : if( bFirstCall )
216 [ # # ]: 0 : loadModuleImpl();
217 [ + - ]: 143 : if ( xTrans.is() )
218 [ + - ][ + - ]: 143 : return xTrans->compareString( rStr1, rStr2 );
[ + - ][ + - ]
[ # # ]
219 : : }
220 : 0 : catch (const Exception& e)
221 : : {
222 : : SAL_WARN( "unotools.i18n", "compareString: Exception caught " << e.Message );
223 : : }
224 : 143 : return 0;
225 : : }
226 : :
227 : :
228 : : // --- helpers --------------------------------------------------------
229 : :
230 : 7119 : sal_Bool TransliterationWrapper::isEqual( const String& rStr1, const String& rStr2 ) const
231 : : {
232 : : sal_Int32 nMatch1, nMatch2;
233 : : sal_Bool bMatch = equals(
234 : 7119 : rStr1, 0, rStr1.Len(), nMatch1,
235 [ + - ]: 14238 : rStr2, 0, rStr2.Len(), nMatch2 );
236 : 7119 : return bMatch;
237 : : }
238 : :
239 : :
240 : 1100 : sal_Bool TransliterationWrapper::isMatch( const String& rStr1, const String& rStr2 ) const
241 : : {
242 : : sal_Int32 nMatch1, nMatch2;
243 : : equals(
244 : 1100 : rStr1, 0, rStr1.Len(), nMatch1,
245 [ + - ]: 2200 : rStr2, 0, rStr2.Len(), nMatch2 );
246 [ + - ][ + + ]: 1100 : return (nMatch1 <= nMatch2) && (nMatch1 == rStr1.Len());
247 : : }
248 : :
249 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|