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 <i18nlangtag/languagetag.hxx>
24 :
25 : #include <com/sun/star/i18n/TransliterationModulesExtra.hpp>
26 : #include <com/sun/star/i18n/Transliteration.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 215 : TransliterationWrapper::TransliterationWrapper(
34 : const Reference< XComponentContext > & rxContext,
35 : sal_uInt32 nTyp )
36 : : xTrans( Transliteration::create(rxContext) ),
37 215 : aLanguageTag( LANGUAGE_SYSTEM ), nType( nTyp ), bFirstCall( sal_True )
38 : {
39 215 : }
40 :
41 :
42 161 : TransliterationWrapper::~TransliterationWrapper()
43 : {
44 161 : }
45 :
46 :
47 36 : OUString TransliterationWrapper::transliterate(const OUString& rStr, sal_uInt16 nLang,
48 : sal_Int32 nStart, sal_Int32 nLen,
49 : Sequence <sal_Int32>* pOffset )
50 : {
51 36 : OUString sRet;
52 36 : if( xTrans.is() )
53 : {
54 : try
55 : {
56 36 : loadModuleIfNeeded( nLang );
57 :
58 36 : if ( pOffset )
59 36 : sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
60 : else
61 0 : sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
62 : }
63 0 : catch( Exception& )
64 : {
65 : SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" );
66 : }
67 : }
68 36 : return sRet;
69 : }
70 :
71 :
72 0 : OUString TransliterationWrapper::transliterate( const OUString& rStr,
73 : sal_Int32 nStart, sal_Int32 nLen,
74 : Sequence <sal_Int32>* pOffset ) const
75 : {
76 0 : OUString sRet( rStr );
77 0 : if( xTrans.is() )
78 : {
79 : try
80 : {
81 0 : if ( pOffset )
82 0 : sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
83 : else
84 0 : sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
85 : }
86 0 : catch( Exception& )
87 : {
88 : SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" );
89 : }
90 : }
91 0 : return sRet;
92 : }
93 :
94 267 : bool TransliterationWrapper::needLanguageForTheMode() const
95 : {
96 532 : return TransliterationModules_UPPERCASE_LOWERCASE == nType ||
97 528 : TransliterationModules_LOWERCASE_UPPERCASE == nType ||
98 265 : TransliterationModules_IGNORE_CASE == nType ||
99 4 : (sal_uInt32) TransliterationModulesExtra::SENTENCE_CASE == (sal_uInt32) nType ||
100 271 : (sal_uInt32) TransliterationModulesExtra::TITLE_CASE == (sal_uInt32) nType ||
101 269 : (sal_uInt32) TransliterationModulesExtra::TOGGLE_CASE == (sal_uInt32) nType;
102 : }
103 :
104 :
105 358 : void TransliterationWrapper::setLanguageLocaleImpl( sal_uInt16 nLang )
106 : {
107 358 : if( LANGUAGE_NONE == nLang )
108 0 : nLang = LANGUAGE_SYSTEM;
109 358 : aLanguageTag.reset( nLang);
110 358 : }
111 :
112 :
113 751 : void TransliterationWrapper::loadModuleIfNeeded( sal_uInt16 nLang )
114 : {
115 751 : bool bLoad = bFirstCall;
116 751 : bFirstCall = false;
117 :
118 751 : if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::SENTENCE_CASE )
119 : {
120 1 : if( bLoad )
121 1 : loadModuleByImplName(OUString("SENTENCE_CASE"), nLang);
122 : }
123 750 : else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TITLE_CASE )
124 : {
125 1 : if( bLoad )
126 1 : loadModuleByImplName(OUString("TITLE_CASE"), nLang);
127 : }
128 749 : else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TOGGLE_CASE )
129 : {
130 1 : if( bLoad )
131 1 : loadModuleByImplName(OUString("TOGGLE_CASE"), nLang);
132 : }
133 : else
134 : {
135 748 : if( aLanguageTag.getLanguageType() != nLang )
136 : {
137 355 : setLanguageLocaleImpl( nLang );
138 355 : if( !bLoad )
139 263 : bLoad = needLanguageForTheMode();
140 : }
141 748 : if( bLoad )
142 473 : loadModuleImpl();
143 : }
144 751 : }
145 :
146 :
147 473 : void TransliterationWrapper::loadModuleImpl() const
148 : {
149 473 : if ( bFirstCall )
150 0 : ((TransliterationWrapper*)this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM );
151 :
152 : try
153 : {
154 473 : if ( xTrans.is() )
155 473 : xTrans->loadModule( (TransliterationModules)nType, aLanguageTag.getLocale() );
156 : }
157 0 : catch ( const Exception& e )
158 : {
159 : SAL_WARN( "unotools.i18n", "loadModuleImpl: Exception caught " << e.Message );
160 : }
161 :
162 473 : bFirstCall = false;
163 473 : }
164 :
165 :
166 3 : void TransliterationWrapper::loadModuleByImplName(const OUString& rModuleName,
167 : sal_uInt16 nLang )
168 : {
169 : try
170 : {
171 3 : setLanguageLocaleImpl( nLang );
172 3 : com::sun::star::lang::Locale aLocale( aLanguageTag.getLocale());
173 : // Reset LanguageTag, so the next call to loadModuleIfNeeded() forces
174 : // new settings.
175 3 : aLanguageTag.reset( LANGUAGE_DONTKNOW);
176 3 : if ( xTrans.is() )
177 3 : xTrans->loadModuleByImplName( rModuleName, aLocale );
178 : }
179 0 : catch ( const Exception& e )
180 : {
181 : SAL_WARN( "unotools.i18n", "loadModuleByImplName: Exception caught " << e.Message );
182 : }
183 :
184 3 : bFirstCall = false;
185 3 : }
186 :
187 :
188 5886 : bool TransliterationWrapper::equals(
189 : const OUString& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1,
190 : const OUString& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const
191 : {
192 : try
193 : {
194 5886 : if( bFirstCall )
195 0 : loadModuleImpl();
196 5886 : if ( xTrans.is() )
197 5886 : return xTrans->equals( rStr1, nPos1, nCount1, nMatch1, rStr2, nPos2, nCount2, nMatch2 );
198 : }
199 0 : catch ( const Exception& e )
200 : {
201 : SAL_WARN( "unotools.i18n", "equals: Exception caught " << e.Message );
202 : }
203 0 : return false;
204 : }
205 :
206 74 : sal_Int32 TransliterationWrapper::compareString( const OUString& rStr1, const OUString& rStr2 ) const
207 : {
208 : try
209 : {
210 74 : if( bFirstCall )
211 0 : loadModuleImpl();
212 74 : if ( xTrans.is() )
213 74 : return xTrans->compareString( rStr1, rStr2 );
214 : }
215 0 : catch (const Exception& e)
216 : {
217 : SAL_WARN( "unotools.i18n", "compareString: Exception caught " << e.Message );
218 : }
219 0 : return 0;
220 : }
221 :
222 :
223 : // --- helpers --------------------------------------------------------
224 :
225 4287 : bool TransliterationWrapper::isEqual( const OUString& rStr1, const OUString& rStr2 ) const
226 : {
227 : sal_Int32 nMatch1, nMatch2;
228 : bool bMatch = equals(
229 : rStr1, 0, rStr1.getLength(), nMatch1,
230 4287 : rStr2, 0, rStr2.getLength(), nMatch2 );
231 4287 : return bMatch;
232 : }
233 :
234 :
235 1599 : bool TransliterationWrapper::isMatch( const OUString& rStr1, const OUString& rStr2 ) const
236 : {
237 : sal_Int32 nMatch1, nMatch2;
238 : equals(
239 : rStr1, 0, rStr1.getLength(), nMatch1,
240 1599 : rStr2, 0, rStr2.getLength(), nMatch2 );
241 1599 : return (nMatch1 <= nMatch2) && (nMatch1 == rStr1.getLength());
242 : }
243 :
244 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|