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 <boost/scoped_ptr.hpp>
21 : #include <comphelper/processfactory.hxx>
22 : #include <string.h>
23 : #include "ordinalsuffix.hxx"
24 :
25 : #include <unicode/rbnf.h>
26 : #include <unicode/normlzr.h>
27 :
28 : #define CSTR( ouStr ) rtl::OUStringToOString( ouStr, RTL_TEXTENCODING_UTF8 ).getStr( )
29 :
30 : using namespace ::com::sun::star::i18n;
31 : using namespace ::com::sun::star::uno;
32 : using namespace ::com::sun::star;
33 : using namespace ::rtl;
34 :
35 : namespace com { namespace sun { namespace star { namespace i18n {
36 :
37 :
38 6 : OrdinalSuffix::OrdinalSuffix(
39 : const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory >& rxMSF) :
40 6 : _xServiceManager( rxMSF )
41 : {
42 6 : }
43 :
44 8 : OrdinalSuffix::~OrdinalSuffix()
45 : {
46 8 : }
47 :
48 : namespace
49 : {
50 36 : OUString mungeUnicodeStringToOUString(const icu::UnicodeString &rIn, UErrorCode &rCode)
51 : {
52 : // Apply NFKC normalization to get normal letters
53 36 : icu::UnicodeString normalized;
54 36 : icu::Normalizer::normalize(rIn, UNORM_NFKC, 0, normalized, rCode);
55 : // Convert the normalized UnicodeString to OUString
56 36 : OUString sRet = (U_SUCCESS(rCode))
57 36 : ? OUString(reinterpret_cast<const sal_Unicode *>(normalized.getBuffer()), normalized.length())
58 72 : : OUString();
59 : // replace any minus signs with hyphen-minus so that negative numbers
60 : // from the simple number formatter and heavy-duty pattern formatter
61 : // agree as to their negative number sign
62 36 : return sRet.replace(0x2212, '-');
63 : }
64 : }
65 :
66 : /*
67 : * For this method to properly return the ordinal suffix for other locales
68 : * than english ones, ICU 4.2+ has to be used.
69 : */
70 14 : uno::Sequence< OUString > SAL_CALL OrdinalSuffix::getOrdinalSuffix( sal_Int32 nNumber,
71 : const lang::Locale &aLocale ) throw( RuntimeException )
72 : {
73 14 : uno::Sequence< OUString > retValue;
74 :
75 : // Get the value from ICU
76 14 : UErrorCode nCode = U_ZERO_ERROR;
77 : const icu::Locale rIcuLocale(
78 : CSTR( aLocale.Language ),
79 : CSTR( aLocale.Country ),
80 14 : CSTR( aLocale.Variant ) );
81 :
82 14 : icu::RuleBasedNumberFormat formatter(icu::URBNF_ORDINAL, rIcuLocale, nCode);
83 14 : if (!U_SUCCESS(nCode))
84 : return retValue;
85 :
86 14 : boost::scoped_ptr<NumberFormat> xNumberFormat(icu::NumberFormat::createInstance(rIcuLocale, nCode));
87 14 : if (!U_SUCCESS(nCode))
88 : return retValue;
89 :
90 14 : icu::UnicodeString sFormatWithNoOrdinal;
91 14 : icu::Formattable ftmNumber((int32_t)nNumber);
92 14 : icu::FieldPosition icuPosA;
93 14 : xNumberFormat->format(ftmNumber, sFormatWithNoOrdinal, icuPosA, nCode);
94 14 : if (!U_SUCCESS(nCode))
95 : return retValue;
96 :
97 14 : OUString sValueWithNoOrdinal = mungeUnicodeStringToOUString(sFormatWithNoOrdinal, nCode);
98 14 : if (!U_SUCCESS(nCode))
99 : return retValue;
100 :
101 14 : int32_t nRuleSets = formatter.getNumberOfRuleSetNames( );
102 36 : for (int32_t i = 0; i < nRuleSets; ++i)
103 : {
104 22 : icu::UnicodeString ruleSet = formatter.getRuleSetName(i);
105 :
106 : // format the string
107 22 : icu::UnicodeString sFormatWithOrdinal;
108 22 : icu::FieldPosition icuPosB;
109 22 : formatter.format((int32_t)nNumber, ruleSet, sFormatWithOrdinal, icuPosB, nCode);
110 :
111 22 : if (!U_SUCCESS(nCode))
112 0 : continue;
113 :
114 22 : OUString sValueWithOrdinal = mungeUnicodeStringToOUString(sFormatWithOrdinal, nCode);
115 22 : if (!U_SUCCESS(nCode))
116 0 : continue;
117 :
118 : // fdo#54486 lets make sure that the ordinal format and the non-ordinal
119 : // format match at the start, so that the expectation can be verified
120 : // that there is some trailing "ordinal suffix" which can be extracted
121 22 : bool bSimpleOrdinalSuffix = sValueWithOrdinal.startsWith(sValueWithNoOrdinal);
122 :
123 : SAL_WARN_IF(!bSimpleOrdinalSuffix, "i18npool", "ordinal " <<
124 : sValueWithOrdinal << " didn't start with expected " <<
125 : sValueWithNoOrdinal << " prefix");
126 :
127 22 : if (!bSimpleOrdinalSuffix)
128 0 : continue;
129 :
130 : // Remove the number to get the prefix
131 22 : sal_Int32 len = sValueWithNoOrdinal.getLength();
132 :
133 22 : sal_Int32 newLength = retValue.getLength() + 1;
134 22 : retValue.realloc( newLength );
135 22 : retValue[ newLength - 1 ] = sValueWithOrdinal.copy( len );
136 22 : }
137 :
138 14 : return retValue;
139 : }
140 :
141 : const sal_Char cOrdinalSuffix[] = "com.sun.star.i18n.OrdinalSuffix";
142 :
143 0 : OUString SAL_CALL OrdinalSuffix::getImplementationName(void) throw( RuntimeException )
144 : {
145 0 : return OUString::createFromAscii(cOrdinalSuffix);
146 : }
147 :
148 0 : sal_Bool SAL_CALL OrdinalSuffix::supportsService( const OUString& rServiceName) throw( RuntimeException )
149 : {
150 0 : return !rServiceName.compareToAscii(cOrdinalSuffix);
151 : }
152 :
153 0 : Sequence< OUString > SAL_CALL OrdinalSuffix::getSupportedServiceNames(void) throw( RuntimeException )
154 : {
155 0 : Sequence< OUString > aRet(1);
156 0 : aRet[0] = OUString::createFromAscii(cOrdinalSuffix);
157 0 : return aRet;
158 : }
159 :
160 : } } } }
161 :
162 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|