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 3 : OrdinalSuffix::OrdinalSuffix(
39 : const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory >& rxMSF) :
40 3 : _xServiceManager( rxMSF )
41 : {
42 3 : }
43 :
44 4 : OrdinalSuffix::~OrdinalSuffix()
45 : {
46 4 : }
47 :
48 : namespace
49 : {
50 18 : OUString mungeUnicodeStringToOUString(const icu::UnicodeString &rIn, UErrorCode &rCode)
51 : {
52 : // Apply NFKC normalization to get normal letters
53 18 : icu::UnicodeString normalized;
54 18 : icu::Normalizer::normalize(rIn, UNORM_NFKC, 0, normalized, rCode);
55 : // Convert the normalized UnicodeString to OUString
56 18 : OUString sRet = (U_SUCCESS(rCode))
57 18 : ? OUString(reinterpret_cast<const sal_Unicode *>(normalized.getBuffer()), normalized.length())
58 36 : : 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 18 : 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 7 : uno::Sequence< OUString > SAL_CALL OrdinalSuffix::getOrdinalSuffix( sal_Int32 nNumber,
71 : const lang::Locale &aLocale ) throw( RuntimeException )
72 : {
73 7 : uno::Sequence< OUString > retValue;
74 :
75 : // Get the value from ICU
76 7 : UErrorCode nCode = U_ZERO_ERROR;
77 : const icu::Locale rIcuLocale(
78 : CSTR( aLocale.Language ),
79 : CSTR( aLocale.Country ),
80 7 : CSTR( aLocale.Variant ) );
81 :
82 7 : icu::RuleBasedNumberFormat formatter(icu::URBNF_ORDINAL, rIcuLocale, nCode);
83 7 : if (!U_SUCCESS(nCode))
84 : return retValue;
85 :
86 7 : boost::scoped_ptr<NumberFormat> xNumberFormat(icu::NumberFormat::createInstance(rIcuLocale, nCode));
87 7 : if (!U_SUCCESS(nCode))
88 : return retValue;
89 :
90 7 : icu::UnicodeString sFormatWithNoOrdinal;
91 7 : icu::Formattable ftmNumber((int32_t)nNumber);
92 7 : icu::FieldPosition icuPosA;
93 7 : xNumberFormat->format(ftmNumber, sFormatWithNoOrdinal, icuPosA, nCode);
94 7 : if (!U_SUCCESS(nCode))
95 : return retValue;
96 :
97 7 : OUString sValueWithNoOrdinal = mungeUnicodeStringToOUString(sFormatWithNoOrdinal, nCode);
98 7 : if (!U_SUCCESS(nCode))
99 : return retValue;
100 :
101 7 : int32_t nRuleSets = formatter.getNumberOfRuleSetNames( );
102 18 : for (int32_t i = 0; i < nRuleSets; ++i)
103 : {
104 11 : icu::UnicodeString ruleSet = formatter.getRuleSetName(i);
105 :
106 : // format the string
107 11 : icu::UnicodeString sFormatWithOrdinal;
108 11 : icu::FieldPosition icuPosB;
109 11 : formatter.format((int32_t)nNumber, ruleSet, sFormatWithOrdinal, icuPosB, nCode);
110 :
111 11 : if (!U_SUCCESS(nCode))
112 0 : continue;
113 :
114 11 : OUString sValueWithOrdinal = mungeUnicodeStringToOUString(sFormatWithOrdinal, nCode);
115 11 : 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 11 : 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 11 : if (!bSimpleOrdinalSuffix)
128 0 : continue;
129 :
130 : // Remove the number to get the prefix
131 11 : sal_Int32 len = sValueWithNoOrdinal.getLength();
132 :
133 11 : sal_Int32 newLength = retValue.getLength() + 1;
134 11 : retValue.realloc( newLength );
135 11 : retValue[ newLength - 1 ] = sValueWithOrdinal.copy( len );
136 11 : }
137 :
138 7 : 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: */
|