LibreOffice Module unotools (master) 1
transliterationwrapper.cxx
Go to the documentation of this file.
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#include <sal/config.h>
21
26
27#include <com/sun/star/i18n/Transliteration.hpp>
28
29using namespace ::com::sun::star::lang;
30using namespace ::com::sun::star::i18n;
31using namespace ::com::sun::star::uno;
32using namespace ::utl;
33
34TransliterationWrapper::TransliterationWrapper(
35 const Reference< XComponentContext > & rxContext,
37 : xTrans( Transliteration::create(rxContext) ),
38 aLanguageTag( LANGUAGE_SYSTEM ), nType( nTyp ), bFirstCall( true )
39{
40}
41
42TransliterationWrapper::~TransliterationWrapper()
43{
44}
45
46OUString TransliterationWrapper::transliterate(const OUString& rStr, LanguageType nLang,
47 sal_Int32 nStart, sal_Int32 nLen,
48 Sequence <sal_Int32>* pOffset )
49{
50 OUString sRet;
51 if( xTrans.is() )
52 {
53 try
54 {
55 loadModuleIfNeeded( nLang );
56
57 if ( pOffset )
58 sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
59 else
60 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
61 }
62 catch( Exception& )
63 {
64 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
65 }
66 }
67 return sRet;
68}
69
70OUString TransliterationWrapper::transliterate( const OUString& rStr,
71 sal_Int32 nStart, sal_Int32 nLen ) const
72{
73 OUString sRet( rStr );
74 if( xTrans.is() )
75 {
76 try
77 {
78 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
79 }
80 catch( Exception& )
81 {
82 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
83 }
84 }
85 return sRet;
86}
87
88bool TransliterationWrapper::needLanguageForTheMode() const
89{
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;
96}
97
98void TransliterationWrapper::setLanguageLocaleImpl( LanguageType nLang )
99{
100 if( LANGUAGE_NONE == nLang )
101 nLang = LANGUAGE_SYSTEM;
102 aLanguageTag.reset( nLang);
103}
104
105void TransliterationWrapper::loadModuleIfNeeded( LanguageType nLang )
106{
107 bool bLoad = bFirstCall;
108 bFirstCall = false;
109
110 if( nType == TransliterationFlags::SENTENCE_CASE )
111 {
112 if( bLoad )
113 loadModuleByImplName("SENTENCE_CASE", nLang);
114 }
115 else if( nType == TransliterationFlags::TITLE_CASE )
116 {
117 if( bLoad )
118 loadModuleByImplName("TITLE_CASE", nLang);
119 }
120 else if( nType == TransliterationFlags::TOGGLE_CASE )
121 {
122 if( bLoad )
123 loadModuleByImplName("TOGGLE_CASE", nLang);
124 }
125 else
126 {
127 if( aLanguageTag.getLanguageType() != nLang )
128 {
129 setLanguageLocaleImpl( nLang );
130 if( !bLoad )
131 bLoad = needLanguageForTheMode();
132 }
133 if( bLoad )
134 loadModuleImpl();
135 }
136}
137
138void TransliterationWrapper::loadModuleImpl() const
139{
140 if ( bFirstCall )
141 const_cast<TransliterationWrapper*>(this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM );
142
143 try
144 {
145 if ( xTrans.is() )
146 xTrans->loadModule( static_cast<TransliterationModules>(nType), aLanguageTag.getLocale() );
147 }
148 catch ( const Exception& )
149 {
150 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleImpl" );
151 }
152
153 bFirstCall = false;
154}
155
156void TransliterationWrapper::loadModuleByImplName(const OUString& rModuleName,
157 LanguageType nLang )
158{
159 try
160 {
161 setLanguageLocaleImpl( nLang );
162 css::lang::Locale aLocale( aLanguageTag.getLocale());
163 // Reset LanguageTag, so the next call to loadModuleIfNeeded() forces
164 // new settings.
165 aLanguageTag.reset( LANGUAGE_DONTKNOW);
166 if ( xTrans.is() )
167 xTrans->loadModuleByImplName( rModuleName, aLocale );
168 }
169 catch ( const Exception& )
170 {
171 TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleByImplName" );
172 }
173
174 bFirstCall = false;
175}
176
177bool 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
180{
181 try
182 {
183 if( bFirstCall )
184 loadModuleImpl();
185 if ( xTrans.is() )
186 return xTrans->equals( rStr1, nPos1, nCount1, nMatch1, rStr2, nPos2, nCount2, nMatch2 );
187 }
188 catch ( const Exception& )
189 {
190 TOOLS_WARN_EXCEPTION( "unotools.i18n", "equals" );
191 }
192 return false;
193}
194
195sal_Int32 TransliterationWrapper::compareString( const OUString& rStr1, const OUString& rStr2 ) const
196{
197 try
198 {
199 if( bFirstCall )
200 loadModuleImpl();
201 if ( xTrans.is() )
202 return xTrans->compareString( rStr1, rStr2 );
203 }
204 catch (const Exception&)
205 {
206 TOOLS_WARN_EXCEPTION( "unotools.i18n", "compareString" );
207 }
208 return 0;
209}
210
211// --- helpers --------------------------------------------------------
212
213bool TransliterationWrapper::isEqual( const OUString& rStr1, const OUString& rStr2 ) const
214{
215 sal_Int32 nMatch1(0), nMatch2(0);
216 bool bMatch = equals(
217 rStr1, 0, rStr1.getLength(), nMatch1,
218 rStr2, 0, rStr2.getLength(), nMatch2 );
219 return bMatch;
220}
221
222bool TransliterationWrapper::isMatch( const OUString& rStr1, const OUString& rStr2 ) const
223{
224 sal_Int32 nMatch1(0), nMatch2(0);
225 equals(
226 rStr1, 0, rStr1.getLength(), nMatch1,
227 rStr2, 0, rStr2.getLength(), nMatch2 );
228 return (nMatch1 <= nMatch2) && (nMatch1 == rStr1.getLength());
229}
230
231/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define TOOLS_WARN_EXCEPTION(area, stream)
#define LANGUAGE_SYSTEM
#define LANGUAGE_NONE
#define LANGUAGE_DONTKNOW
@ Exception
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
QPRO_FUNC_TYPE nType
TransliterationFlags