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 0 : OrdinalSuffix::OrdinalSuffix()
39 : {
40 0 : }
41 :
42 0 : OrdinalSuffix::~OrdinalSuffix()
43 : {
44 0 : }
45 :
46 : namespace
47 : {
48 0 : OUString mungeUnicodeStringToOUString(const icu::UnicodeString &rIn, UErrorCode &rCode)
49 : {
50 : // Apply NFKC normalization to get normal letters
51 0 : icu::UnicodeString normalized;
52 0 : icu::Normalizer::normalize(rIn, UNORM_NFKC, 0, normalized, rCode);
53 : // Convert the normalized UnicodeString to OUString
54 0 : OUString sRet = (U_SUCCESS(rCode))
55 0 : ? OUString(reinterpret_cast<const sal_Unicode *>(normalized.getBuffer()), normalized.length())
56 0 : : 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 0 : 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 0 : uno::Sequence< OUString > SAL_CALL OrdinalSuffix::getOrdinalSuffix( sal_Int32 nNumber,
69 : const lang::Locale &rLocale ) throw( RuntimeException, std::exception )
70 : {
71 0 : uno::Sequence< OUString > retValue;
72 :
73 : // Get the value from ICU
74 0 : UErrorCode nCode = U_ZERO_ERROR;
75 0 : const icu::Locale aIcuLocale( LanguageTagIcu::getIcuLocale( LanguageTag( rLocale)));
76 :
77 0 : icu::RuleBasedNumberFormat formatter(icu::URBNF_ORDINAL, aIcuLocale, nCode);
78 0 : if (!U_SUCCESS(nCode))
79 0 : return retValue;
80 :
81 0 : boost::scoped_ptr<NumberFormat> xNumberFormat(icu::NumberFormat::createInstance(aIcuLocale, nCode));
82 0 : if (!U_SUCCESS(nCode))
83 0 : return retValue;
84 :
85 0 : icu::UnicodeString sFormatWithNoOrdinal;
86 0 : icu::Formattable ftmNumber((int32_t)nNumber);
87 0 : icu::FieldPosition icuPosA;
88 0 : xNumberFormat->format(ftmNumber, sFormatWithNoOrdinal, icuPosA, nCode);
89 0 : if (!U_SUCCESS(nCode))
90 0 : return retValue;
91 :
92 0 : OUString sValueWithNoOrdinal = mungeUnicodeStringToOUString(sFormatWithNoOrdinal, nCode);
93 0 : if (!U_SUCCESS(nCode))
94 0 : return retValue;
95 :
96 0 : int32_t nRuleSets = formatter.getNumberOfRuleSetNames( );
97 0 : for (int32_t i = 0; i < nRuleSets; ++i)
98 : {
99 0 : icu::UnicodeString ruleSet = formatter.getRuleSetName(i);
100 :
101 : // format the string
102 0 : icu::UnicodeString sFormatWithOrdinal;
103 0 : icu::FieldPosition icuPosB;
104 0 : formatter.format((int32_t)nNumber, ruleSet, sFormatWithOrdinal, icuPosB, nCode);
105 :
106 0 : if (!U_SUCCESS(nCode))
107 0 : continue;
108 :
109 0 : OUString sValueWithOrdinal = mungeUnicodeStringToOUString(sFormatWithOrdinal, nCode);
110 0 : 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 0 : 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 0 : if (!bSimpleOrdinalSuffix)
123 0 : continue;
124 :
125 : // Remove the number to get the prefix
126 0 : sal_Int32 len = sValueWithNoOrdinal.getLength();
127 :
128 0 : sal_Int32 newLength = retValue.getLength() + 1;
129 0 : retValue.realloc( newLength );
130 0 : retValue[ newLength - 1 ] = sValueWithOrdinal.copy( len );
131 0 : }
132 :
133 0 : return retValue;
134 : }
135 :
136 : const sal_Char cOrdinalSuffix[] = "com.sun.star.i18n.OrdinalSuffix";
137 :
138 0 : OUString SAL_CALL OrdinalSuffix::getImplementationName(void) throw( RuntimeException, std::exception )
139 : {
140 0 : return OUString::createFromAscii(cOrdinalSuffix);
141 : }
142 :
143 0 : sal_Bool SAL_CALL OrdinalSuffix::supportsService( const OUString& rServiceName) throw( RuntimeException, std::exception )
144 : {
145 0 : return cppu::supportsService(this, rServiceName);
146 : }
147 :
148 0 : Sequence< OUString > SAL_CALL OrdinalSuffix::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 0 : com_sun_star_i18n_OrdinalSuffix_get_implementation(
159 : css::uno::XComponentContext *,
160 : css::uno::Sequence<css::uno::Any> const &)
161 : {
162 0 : return cppu::acquire(new css::i18n::OrdinalSuffix());
163 : }
164 :
165 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|