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