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 : : #include <comphelper/processfactory.hxx>
21 : : #include <string.h>
22 : : #include "ordinalsuffix.hxx"
23 : :
24 : : #include <unicode/rbnf.h>
25 : : #include <unicode/normlzr.h>
26 : :
27 : : #define CSTR( ouStr ) rtl::OUStringToOString( ouStr, RTL_TEXTENCODING_UTF8 ).getStr( )
28 : :
29 : : using namespace ::com::sun::star::i18n;
30 : : using namespace ::com::sun::star::uno;
31 : : using namespace ::com::sun::star;
32 : : using namespace ::rtl;
33 : :
34 : : namespace com { namespace sun { namespace star { namespace i18n {
35 : :
36 : :
37 : 3 : OrdinalSuffix::OrdinalSuffix(
38 : : const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory >& rxMSF) :
39 : 3 : _xServiceManager( rxMSF )
40 : : {
41 : 3 : }
42 : :
43 : 0 : OrdinalSuffix::~OrdinalSuffix()
44 : : {
45 [ # # ]: 0 : }
46 : :
47 : :
48 : : /*
49 : : * For this method to properly return the ordinal suffix for other locales
50 : : * than english ones, ICU 4.2+ has to be used.
51 : : */
52 : 6 : uno::Sequence< OUString > SAL_CALL OrdinalSuffix::getOrdinalSuffix( sal_Int32 nNumber,
53 : : const lang::Locale &aLocale ) throw( RuntimeException )
54 : : {
55 [ + - ]: 6 : uno::Sequence< OUString > retValue;
56 : :
57 : : // Get the value from ICU
58 : 6 : UErrorCode nCode = U_ZERO_ERROR;
59 : : const icu::Locale rIcuLocale(
60 : : CSTR( aLocale.Language ),
61 : : CSTR( aLocale.Country ),
62 [ + - ][ + - ]: 6 : CSTR( aLocale.Variant ) );
[ + - ][ + - ]
63 : : icu::RuleBasedNumberFormat formatter(
64 [ + - ]: 6 : icu::URBNF_ORDINAL, rIcuLocale, nCode );
65 : :
66 [ + - ]: 6 : if ( U_SUCCESS( nCode ) )
67 : : {
68 [ + - ]: 6 : int32_t nRuleSets = formatter.getNumberOfRuleSetNames( );
69 [ + + ]: 12 : for ( int32_t i = 0; i < nRuleSets; i++ )
70 : : {
71 [ + - ]: 6 : icu::UnicodeString ruleSet = formatter.getRuleSetName( i );
72 : : // format the string
73 [ + - ]: 6 : icu::UnicodeString icuRet;
74 [ + - ]: 6 : icu::FieldPosition icuPos;
75 [ + - ]: 6 : formatter.format( (int32_t)nNumber, ruleSet, icuRet, icuPos, nCode );
76 : :
77 [ + - ]: 6 : if ( U_SUCCESS( nCode ) )
78 : : {
79 : : // Apply NFKC normalization to get normal letters
80 [ + - ]: 6 : icu::UnicodeString normalized;
81 : 6 : nCode = U_ZERO_ERROR;
82 [ + - ]: 6 : icu::Normalizer::normalize( icuRet, UNORM_NFKC, 0, normalized, nCode );
83 [ + - ][ + - ]: 6 : if ( U_SUCCESS( nCode ) && ( normalized != icuRet ) )
[ - + ][ - + ]
84 : : {
85 : : // Convert the normalized UnicodeString to OUString
86 : 0 : OUString sValue( reinterpret_cast<const sal_Unicode *>( normalized.getBuffer( ) ), normalized.length() );
87 : :
88 : : // Remove the number to get the prefix
89 : 0 : sal_Int32 len = OUString::valueOf( nNumber ).getLength( );
90 : :
91 : 0 : sal_Int32 newLength = retValue.getLength() + 1;
92 [ # # ]: 0 : retValue.realloc( newLength );
93 [ # # ]: 0 : retValue[ newLength - 1 ] = sValue.copy( len );
94 [ + - ]: 6 : }
95 : : }
96 [ + - ][ + - ]: 6 : }
[ + - ]
97 : : }
98 : :
99 [ + - ][ + - ]: 6 : return retValue;
100 : : }
101 : :
102 : :
103 : : const sal_Char cOrdinalSuffix[] = "com.sun.star.i18n.OrdinalSuffix";
104 : :
105 : 0 : OUString SAL_CALL OrdinalSuffix::getImplementationName(void) throw( RuntimeException )
106 : : {
107 : 0 : return OUString::createFromAscii(cOrdinalSuffix);
108 : : }
109 : :
110 : 0 : sal_Bool SAL_CALL OrdinalSuffix::supportsService( const OUString& rServiceName) throw( RuntimeException )
111 : : {
112 : 0 : return !rServiceName.compareToAscii(cOrdinalSuffix);
113 : : }
114 : :
115 : 0 : Sequence< OUString > SAL_CALL OrdinalSuffix::getSupportedServiceNames(void) throw( RuntimeException )
116 : : {
117 : 0 : Sequence< OUString > aRet(1);
118 [ # # ]: 0 : aRet[0] = OUString::createFromAscii(cOrdinalSuffix);
119 : 0 : return aRet;
120 : : }
121 : :
122 : : } } } }
123 : :
124 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|