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 <comphelper/processfactory.hxx>
22 : #include <unotools/charclass.hxx>
23 : #include <tools/debug.hxx>
24 :
25 : #include <com/sun/star/i18n/CharacterClassification.hpp>
26 :
27 : using namespace ::com::sun::star;
28 : using namespace ::com::sun::star::i18n;
29 : using namespace ::com::sun::star::uno;
30 :
31 :
32 1796 : CharClass::CharClass(
33 : const Reference< uno::XComponentContext > & rxContext,
34 : const LanguageTag& rLanguageTag
35 : )
36 : :
37 1796 : maLanguageTag( rLanguageTag)
38 : {
39 1796 : xCC = CharacterClassification::create( rxContext );
40 1796 : }
41 :
42 :
43 13910 : CharClass::CharClass(
44 : const LanguageTag& rLanguageTag )
45 : :
46 13910 : maLanguageTag( rLanguageTag)
47 : {
48 13910 : xCC = CharacterClassification::create( comphelper::getProcessComponentContext() );
49 13910 : }
50 :
51 :
52 15588 : CharClass::~CharClass()
53 : {
54 15588 : }
55 :
56 :
57 83790 : void CharClass::setLanguageTag( const LanguageTag& rLanguageTag )
58 : {
59 83790 : ::osl::MutexGuard aGuard( aMutex );
60 83790 : maLanguageTag = rLanguageTag;
61 83790 : }
62 :
63 :
64 33370 : const LanguageTag& CharClass::getLanguageTag() const
65 : {
66 33370 : ::osl::MutexGuard aGuard( aMutex );
67 33370 : return maLanguageTag;
68 : }
69 :
70 :
71 1042783 : const ::com::sun::star::lang::Locale& CharClass::getMyLocale() const
72 : {
73 1042783 : ::osl::MutexGuard aGuard( aMutex );
74 1042783 : return maLanguageTag.getLocale();
75 : }
76 :
77 :
78 : // static
79 24308 : bool CharClass::isAsciiNumeric( const OUString& rStr )
80 : {
81 24308 : if ( rStr.isEmpty() )
82 0 : return false;
83 24308 : const sal_Unicode* p = rStr.getStr();
84 24308 : const sal_Unicode* const pStop = p + rStr.getLength();
85 :
86 0 : do
87 : {
88 24308 : if ( !isAsciiDigit( *p ) )
89 24308 : return false;
90 : }
91 : while ( ++p < pStop );
92 :
93 0 : return true;
94 : }
95 :
96 :
97 : // static
98 0 : bool CharClass::isAsciiAlpha( const OUString& rStr )
99 : {
100 0 : if ( rStr.isEmpty() )
101 0 : return false;
102 0 : const sal_Unicode* p = rStr.getStr();
103 0 : const sal_Unicode* const pStop = p + rStr.getLength();
104 :
105 0 : do
106 : {
107 0 : if ( !isAsciiAlpha( *p ) )
108 0 : return false;
109 : }
110 : while ( ++p < pStop );
111 :
112 0 : return true;
113 : }
114 :
115 :
116 :
117 0 : bool CharClass::isAlpha( const OUString& rStr, sal_Int32 nPos ) const
118 : {
119 0 : sal_Unicode c = rStr[nPos];
120 0 : if ( c < 128 )
121 0 : return isAsciiAlpha( c );
122 :
123 : try
124 : {
125 0 : if ( xCC.is() )
126 0 : return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
127 0 : nCharClassAlphaType) != 0;
128 : else
129 0 : return false;
130 : }
131 0 : catch ( const Exception& )
132 : {
133 : SAL_WARN( "unotools.i18n", "isAlpha: Exception caught!" );
134 0 : return false;
135 : }
136 : }
137 :
138 :
139 :
140 428841 : bool CharClass::isLetter( const OUString& rStr, sal_Int32 nPos ) const
141 : {
142 428841 : sal_Unicode c = rStr[nPos];
143 428841 : if ( c < 128 )
144 427977 : return isAsciiAlpha( c );
145 :
146 : try
147 : {
148 864 : if ( xCC.is() )
149 864 : return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
150 864 : nCharClassLetterType) != 0;
151 : else
152 0 : return false;
153 : }
154 0 : catch ( const Exception& )
155 : {
156 : SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
157 0 : return false;
158 : }
159 : }
160 :
161 :
162 0 : bool CharClass::isLetter( const OUString& rStr ) const
163 : {
164 : try
165 : {
166 0 : if ( xCC.is() )
167 0 : return isLetterType( xCC->getStringType( rStr, 0, rStr.getLength(), getMyLocale() ) );
168 : else
169 0 : return false;
170 : }
171 0 : catch ( const Exception& )
172 : {
173 : SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
174 0 : return false;
175 : }
176 : }
177 :
178 :
179 1210 : bool CharClass::isDigit( const OUString& rStr, sal_Int32 nPos ) const
180 : {
181 1210 : sal_Unicode c = rStr[ nPos ];
182 1210 : if ( c < 128 )
183 22 : return isAsciiDigit( c );
184 :
185 : try
186 : {
187 1188 : if ( xCC.is() )
188 1188 : return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
189 1188 : KCharacterType::DIGIT) != 0;
190 : else
191 0 : return false;
192 : }
193 0 : catch ( const Exception& )
194 : {
195 : SAL_WARN( "unotools.i18n", "isDigit: Exception caught!" );
196 0 : return false;
197 : }
198 : }
199 :
200 :
201 0 : bool CharClass::isNumeric( const OUString& rStr ) const
202 : {
203 : try
204 : {
205 0 : if ( xCC.is() )
206 0 : return isNumericType( xCC->getStringType( rStr, 0, rStr.getLength(), getMyLocale() ) );
207 : else
208 0 : return false;
209 : }
210 0 : catch ( const Exception& )
211 : {
212 : SAL_WARN( "unotools.i18n", "isNumeric: Exception caught!" );
213 0 : return false;
214 : }
215 : }
216 :
217 :
218 0 : bool CharClass::isAlphaNumeric( const OUString& rStr, sal_Int32 nPos ) const
219 : {
220 0 : sal_Unicode c = rStr[nPos];
221 0 : if ( c < 128 )
222 0 : return isAsciiAlphaNumeric( c );
223 :
224 : try
225 : {
226 0 : if ( xCC.is() )
227 0 : return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
228 0 : (nCharClassAlphaType | KCharacterType::DIGIT)) != 0;
229 : else
230 0 : return false;
231 : }
232 0 : catch ( const Exception& )
233 : {
234 : SAL_WARN( "unotools.i18n", "isAlphaNumeric: Exception caught!" );
235 0 : return false;
236 : }
237 : }
238 :
239 :
240 23495 : bool CharClass::isLetterNumeric( const OUString& rStr, sal_Int32 nPos ) const
241 : {
242 23495 : sal_Unicode c = rStr[nPos];
243 23495 : if ( c < 128 )
244 23437 : return isAsciiAlphaNumeric( c );
245 :
246 : try
247 : {
248 58 : if ( xCC.is() )
249 58 : return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
250 58 : (nCharClassLetterType | KCharacterType::DIGIT)) != 0;
251 : else
252 0 : return false;
253 : }
254 0 : catch ( const Exception& )
255 : {
256 : SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
257 0 : return false;
258 : }
259 : }
260 :
261 :
262 10942 : bool CharClass::isLetterNumeric( const OUString& rStr ) const
263 : {
264 : try
265 : {
266 10942 : if ( xCC.is() )
267 10942 : return isLetterNumericType( xCC->getStringType( rStr, 0, rStr.getLength(), getMyLocale() ) );
268 : else
269 0 : return false;
270 : }
271 0 : catch ( const Exception& )
272 : {
273 : SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
274 0 : return false;
275 : }
276 : }
277 :
278 1 : OUString CharClass::titlecase(const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount) const
279 : {
280 : try
281 : {
282 1 : if ( xCC.is() )
283 1 : return xCC->toTitle( rStr, nPos, nCount, getMyLocale() );
284 : else
285 0 : return rStr.copy( nPos, nCount );
286 : }
287 0 : catch ( const Exception& )
288 : {
289 : SAL_WARN( "unotools.i18n", "titlecase: Exception caught!" );
290 0 : return rStr.copy( nPos, nCount );
291 : }
292 : }
293 :
294 729010 : OUString CharClass::uppercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
295 : {
296 : try
297 : {
298 729010 : if ( xCC.is() )
299 729010 : return xCC->toUpper( rStr, nPos, nCount, getMyLocale() );
300 : else
301 0 : return rStr.copy( nPos, nCount );
302 : }
303 0 : catch ( const Exception& )
304 : {
305 : SAL_WARN( "unotools.i18n", "uppercase: Exception caught!" );
306 0 : return rStr.copy( nPos, nCount );
307 : }
308 : }
309 :
310 27743 : OUString CharClass::lowercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
311 : {
312 : try
313 : {
314 27743 : if ( xCC.is() )
315 27743 : return xCC->toLower( rStr, nPos, nCount, getMyLocale() );
316 : else
317 0 : return rStr.copy( nPos, nCount );
318 : }
319 0 : catch ( const Exception& )
320 : {
321 : SAL_WARN( "unotools.i18n", "lowercase: Exception caught!" );
322 0 : return rStr.copy( nPos, nCount );
323 : }
324 : }
325 :
326 19383 : sal_Int16 CharClass::getType( const OUString& rStr, sal_Int32 nPos ) const
327 : {
328 : try
329 : {
330 19383 : if ( xCC.is() )
331 19383 : return xCC->getType( rStr, nPos );
332 : else
333 0 : return 0;
334 : }
335 0 : catch ( const Exception& )
336 : {
337 : SAL_WARN( "unotools.i18n", "getType: Exception caught!" );
338 0 : return 0;
339 : }
340 : }
341 :
342 :
343 10406 : sal_Int16 CharClass::getCharacterDirection( const OUString& rStr, sal_Int32 nPos ) const
344 : {
345 : try
346 : {
347 10406 : if ( xCC.is() )
348 10406 : return xCC->getCharacterDirection( rStr, nPos );
349 : else
350 0 : return 0;
351 : }
352 0 : catch ( const Exception& )
353 : {
354 : SAL_WARN( "unotools.i18n", "getCharacterDirection: Exception caught!" );
355 0 : return 0;
356 : }
357 : }
358 :
359 :
360 18 : sal_Int16 CharClass::getScript( const OUString& rStr, sal_Int32 nPos ) const
361 : {
362 : try
363 : {
364 18 : if ( xCC.is() )
365 18 : return xCC->getScript( rStr, nPos );
366 : else
367 0 : return 0;
368 : }
369 0 : catch ( const Exception& )
370 : {
371 : SAL_WARN( "unotools.i18n", "getScript: Exception caught!" );
372 0 : return 0;
373 : }
374 : }
375 :
376 :
377 217845 : sal_Int32 CharClass::getCharacterType( const OUString& rStr, sal_Int32 nPos ) const
378 : {
379 : try
380 : {
381 217845 : if ( xCC.is() )
382 217845 : return xCC->getCharacterType( rStr, nPos, getMyLocale() );
383 : else
384 0 : return 0;
385 : }
386 0 : catch ( const Exception& )
387 : {
388 : SAL_WARN( "unotools.i18n", "getCharacterType: Exception caught!" );
389 0 : return 0;
390 : }
391 : }
392 :
393 :
394 5512 : sal_Int32 CharClass::getStringType( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
395 : {
396 : try
397 : {
398 5512 : if ( xCC.is() )
399 5512 : return xCC->getStringType( rStr, nPos, nCount, getMyLocale() );
400 : else
401 0 : return 0;
402 : }
403 0 : catch ( const Exception& )
404 : {
405 : SAL_WARN( "unotools.i18n", "getStringType: Exception caught!" );
406 0 : return 0;
407 : }
408 : }
409 :
410 :
411 12387 : ::com::sun::star::i18n::ParseResult CharClass::parseAnyToken(
412 : const OUString& rStr,
413 : sal_Int32 nPos,
414 : sal_Int32 nStartCharFlags,
415 : const OUString& userDefinedCharactersStart,
416 : sal_Int32 nContCharFlags,
417 : const OUString& userDefinedCharactersCont ) const
418 : {
419 : try
420 : {
421 12387 : if ( xCC.is() )
422 24774 : return xCC->parseAnyToken( rStr, nPos, getMyLocale(),
423 : nStartCharFlags, userDefinedCharactersStart,
424 24774 : nContCharFlags, userDefinedCharactersCont );
425 : else
426 0 : return ParseResult();
427 : }
428 0 : catch ( const Exception& e )
429 : {
430 : SAL_WARN( "unotools.i18n", "parseAnyToken: Exception caught " << e.Message );
431 0 : return ParseResult();
432 : }
433 : }
434 :
435 :
436 37233 : ::com::sun::star::i18n::ParseResult CharClass::parsePredefinedToken(
437 : sal_Int32 nTokenType,
438 : const OUString& rStr,
439 : sal_Int32 nPos,
440 : sal_Int32 nStartCharFlags,
441 : const OUString& userDefinedCharactersStart,
442 : sal_Int32 nContCharFlags,
443 : const OUString& userDefinedCharactersCont ) const
444 : {
445 : try
446 : {
447 37233 : if ( xCC.is() )
448 74466 : return xCC->parsePredefinedToken( nTokenType, rStr, nPos, getMyLocale(),
449 : nStartCharFlags, userDefinedCharactersStart,
450 74466 : nContCharFlags, userDefinedCharactersCont );
451 : else
452 0 : return ParseResult();
453 : }
454 0 : catch ( const Exception& e )
455 : {
456 : SAL_WARN( "unotools.i18n", "parsePredefinedToken: Exception caught " << e.Message );
457 0 : return ParseResult();
458 : }
459 : }
460 :
461 :
462 :
463 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|