Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / ordinalsuffix / ordinalsuffix.cxx
blob558429aa57ae166fa1a57e03fcdd01ec77ee5740
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <boost/scoped_ptr.hpp>
21 #include <comphelper/processfactory.hxx>
22 #include <string.h>
23 #include "ordinalsuffix.hxx"
25 #include <unicode/rbnf.h>
26 #include <unicode/normlzr.h>
28 #define CSTR( ouStr ) rtl::OUStringToOString( ouStr, RTL_TEXTENCODING_UTF8 ).getStr( )
30 using namespace ::com::sun::star::i18n;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star;
33 using namespace ::rtl;
35 namespace com { namespace sun { namespace star { namespace i18n {
38 OrdinalSuffix::OrdinalSuffix(
39 const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory >& rxMSF) :
40 _xServiceManager( rxMSF )
44 OrdinalSuffix::~OrdinalSuffix()
48 namespace
50 OUString mungeUnicodeStringToOUString(const icu::UnicodeString &rIn, UErrorCode &rCode)
52 // Apply NFKC normalization to get normal letters
53 icu::UnicodeString normalized;
54 icu::Normalizer::normalize(rIn, UNORM_NFKC, 0, normalized, rCode);
55 // Convert the normalized UnicodeString to OUString
56 OUString sRet = (U_SUCCESS(rCode))
57 ? OUString(reinterpret_cast<const sal_Unicode *>(normalized.getBuffer()), normalized.length())
58 : 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 return sRet.replace(0x2212, '-');
67 * For this method to properly return the ordinal suffix for other locales
68 * than english ones, ICU 4.2+ has to be used.
70 uno::Sequence< OUString > SAL_CALL OrdinalSuffix::getOrdinalSuffix( sal_Int32 nNumber,
71 const lang::Locale &aLocale ) throw( RuntimeException )
73 uno::Sequence< OUString > retValue;
75 // Get the value from ICU
76 UErrorCode nCode = U_ZERO_ERROR;
77 const icu::Locale rIcuLocale(
78 CSTR( aLocale.Language ),
79 CSTR( aLocale.Country ),
80 CSTR( aLocale.Variant ) );
82 icu::RuleBasedNumberFormat formatter(icu::URBNF_ORDINAL, rIcuLocale, nCode);
83 if (!U_SUCCESS(nCode))
84 return retValue;
86 boost::scoped_ptr<NumberFormat> xNumberFormat(icu::NumberFormat::createInstance(rIcuLocale, nCode));
87 if (!U_SUCCESS(nCode))
88 return retValue;
90 icu::UnicodeString sFormatWithNoOrdinal;
91 icu::Formattable ftmNumber((int32_t)nNumber);
92 icu::FieldPosition icuPosA;
93 xNumberFormat->format(ftmNumber, sFormatWithNoOrdinal, icuPosA, nCode);
94 if (!U_SUCCESS(nCode))
95 return retValue;
97 OUString sValueWithNoOrdinal = mungeUnicodeStringToOUString(sFormatWithNoOrdinal, nCode);
98 if (!U_SUCCESS(nCode))
99 return retValue;
101 int32_t nRuleSets = formatter.getNumberOfRuleSetNames( );
102 for (int32_t i = 0; i < nRuleSets; ++i)
104 icu::UnicodeString ruleSet = formatter.getRuleSetName(i);
106 // format the string
107 icu::UnicodeString sFormatWithOrdinal;
108 icu::FieldPosition icuPosB;
109 formatter.format((int32_t)nNumber, ruleSet, sFormatWithOrdinal, icuPosB, nCode);
111 if (!U_SUCCESS(nCode))
112 continue;
114 OUString sValueWithOrdinal = mungeUnicodeStringToOUString(sFormatWithOrdinal, nCode);
115 if (!U_SUCCESS(nCode))
116 continue;
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 bool bSimpleOrdinalSuffix = sValueWithOrdinal.startsWith(sValueWithNoOrdinal);
123 SAL_WARN_IF(!bSimpleOrdinalSuffix, "i18npool", "ordinal " <<
124 sValueWithOrdinal << " didn't start with expected " <<
125 sValueWithNoOrdinal << " prefix");
127 if (!bSimpleOrdinalSuffix)
128 continue;
130 // Remove the number to get the prefix
131 sal_Int32 len = sValueWithNoOrdinal.getLength();
133 sal_Int32 newLength = retValue.getLength() + 1;
134 retValue.realloc( newLength );
135 retValue[ newLength - 1 ] = sValueWithOrdinal.copy( len );
138 return retValue;
141 const sal_Char cOrdinalSuffix[] = "com.sun.star.i18n.OrdinalSuffix";
143 OUString SAL_CALL OrdinalSuffix::getImplementationName(void) throw( RuntimeException )
145 return OUString::createFromAscii(cOrdinalSuffix);
148 sal_Bool SAL_CALL OrdinalSuffix::supportsService( const OUString& rServiceName) throw( RuntimeException )
150 return !rServiceName.compareToAscii(cOrdinalSuffix);
153 Sequence< OUString > SAL_CALL OrdinalSuffix::getSupportedServiceNames(void) throw( RuntimeException )
155 Sequence< OUString > aRet(1);
156 aRet[0] = OUString::createFromAscii(cOrdinalSuffix);
157 return aRet;
160 } } } }
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */