1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <numberformatcode.hxx>
21 #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
22 #include <com/sun/star/i18n/KNumberFormatType.hpp>
23 #include <com/sun/star/i18n/LocaleData2.hpp>
24 #include <cppuhelper/supportsservice.hxx>
26 NumberFormatCodeMapper::NumberFormatCodeMapper(
27 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
)
29 m_xLocaleData
.set( css::i18n::LocaleData2::create( rxContext
) );
33 NumberFormatCodeMapper::~NumberFormatCodeMapper()
38 css::i18n::NumberFormatCode SAL_CALL
39 NumberFormatCodeMapper::getDefault( sal_Int16 formatType
, sal_Int16 formatUsage
, const css::lang::Locale
& rLocale
)
42 OUString elementType
= mapElementTypeShortToString(formatType
);
43 OUString elementUsage
= mapElementUsageShortToString(formatUsage
);
45 osl::MutexGuard
g(maMutex
);
46 const css::uno::Sequence
< css::i18n::FormatElement
> &aFormatSeq
= getFormats( rLocale
);
48 for (sal_Int32 i
= 0; i
< aFormatSeq
.getLength(); i
++) {
49 if (aFormatSeq
[i
].isDefault
&& aFormatSeq
[i
].formatType
== elementType
&&
50 aFormatSeq
[i
].formatUsage
== elementUsage
) {
51 css::i18n::NumberFormatCode
anumberFormatCode(formatType
,
53 aFormatSeq
[i
].formatCode
,
54 aFormatSeq
[i
].formatName
,
55 aFormatSeq
[i
].formatKey
,
56 aFormatSeq
[i
].formatIndex
,
58 return anumberFormatCode
;
61 css::i18n::NumberFormatCode defaultNumberFormatCode
;
62 return defaultNumberFormatCode
;
66 css::i18n::NumberFormatCode SAL_CALL
67 NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex
, const css::lang::Locale
& rLocale
)
69 osl::MutexGuard
g(maMutex
);
70 const css::uno::Sequence
< css::i18n::FormatElement
> &aFormatSeq
= getFormats( rLocale
);
72 for (sal_Int32 i
= 0; i
< aFormatSeq
.getLength(); i
++) {
73 if (aFormatSeq
[i
].formatIndex
== formatIndex
) {
74 css::i18n::NumberFormatCode
anumberFormatCode(mapElementTypeStringToShort(aFormatSeq
[i
].formatType
),
75 mapElementUsageStringToShort(aFormatSeq
[i
].formatUsage
),
76 aFormatSeq
[i
].formatCode
,
77 aFormatSeq
[i
].formatName
,
78 aFormatSeq
[i
].formatKey
,
79 aFormatSeq
[i
].formatIndex
,
80 aFormatSeq
[i
].isDefault
);
81 return anumberFormatCode
;
84 css::i18n::NumberFormatCode defaultNumberFormatCode
;
85 return defaultNumberFormatCode
;
89 css::uno::Sequence
< css::i18n::NumberFormatCode
> SAL_CALL
90 NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage
, const css::lang::Locale
& rLocale
)
92 osl::MutexGuard
g(maMutex
);
93 const css::uno::Sequence
< css::i18n::FormatElement
> &aFormatSeq
= getFormats( rLocale
);
97 for (i
= 0; i
< aFormatSeq
.getLength(); i
++) {
98 sal_Int16 elementUsage
= mapElementUsageStringToShort(aFormatSeq
[i
].formatUsage
);
99 if ( elementUsage
== formatUsage
)
103 css::uno::Sequence
<css::i18n::NumberFormatCode
> seq(count
);
105 for (i
= 0; i
< aFormatSeq
.getLength(); i
++) {
106 sal_Int16 elementUsage
= mapElementUsageStringToShort(aFormatSeq
[i
].formatUsage
);
107 if ( elementUsage
== formatUsage
) {
108 seq
[j
] = css::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq
[i
].formatType
),
110 aFormatSeq
[i
].formatCode
,
111 aFormatSeq
[i
].formatName
,
112 aFormatSeq
[i
].formatKey
,
113 aFormatSeq
[i
].formatIndex
,
114 aFormatSeq
[i
].isDefault
);
122 css::uno::Sequence
< css::i18n::NumberFormatCode
> SAL_CALL
123 NumberFormatCodeMapper::getAllFormatCodes( const css::lang::Locale
& rLocale
)
125 osl::MutexGuard
g(maMutex
);
126 const css::uno::Sequence
< css::i18n::FormatElement
> &aFormatSeq
= getFormats( rLocale
);
128 css::uno::Sequence
<css::i18n::NumberFormatCode
> seq(aFormatSeq
.getLength());
129 for (sal_Int32 i
= 0; i
< aFormatSeq
.getLength(); i
++)
131 seq
[i
] = css::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq
[i
].formatType
),
132 mapElementUsageStringToShort(aFormatSeq
[i
].formatUsage
),
133 aFormatSeq
[i
].formatCode
,
134 aFormatSeq
[i
].formatName
,
135 aFormatSeq
[i
].formatKey
,
136 aFormatSeq
[i
].formatIndex
,
137 aFormatSeq
[i
].isDefault
);
143 // --- private implementation -----------------------------------------
145 const css::uno::Sequence
< css::i18n::FormatElement
>& NumberFormatCodeMapper::getFormats( const css::lang::Locale
& rLocale
)
147 /* Find the FormatElement Sequence in the cache */
148 for (const FormatElementCacheItem
& item
: m_aFormatElementCache
)
150 if ( item
.first
== rLocale
)
154 /* Not found; Get the FormatElement Sequence for the given Locale */
155 css::uno::Sequence
< css::i18n::FormatElement
> aFormatElementSequence
;
156 if ( m_xLocaleData
.is() )
157 aFormatElementSequence
= m_xLocaleData
->getAllFormats( rLocale
);
159 /* Add the FormatElement Sequence to the cache */
160 const int FORMATELEMENTCACHE_SIZE
= 3;
161 if ( m_aFormatElementCache
.size() > FORMATELEMENTCACHE_SIZE
)
162 m_aFormatElementCache
.pop_front();
163 m_aFormatElementCache
.emplace_back( rLocale
, aFormatElementSequence
);
164 return m_aFormatElementCache
.back().second
;
169 NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType
)
172 switch ( formatType
)
174 case css::i18n::KNumberFormatType::SHORT
:
175 return OUString( "short" );
176 case css::i18n::KNumberFormatType::MEDIUM
:
177 return OUString( "medium" );
178 case css::i18n::KNumberFormatType::LONG
:
179 return OUString( "long" );
185 NumberFormatCodeMapper::mapElementTypeStringToShort(const OUString
& formatType
)
187 if ( formatType
== "short" )
188 return css::i18n::KNumberFormatType::SHORT
;
189 if ( formatType
== "medium" )
190 return css::i18n::KNumberFormatType::MEDIUM
;
191 if ( formatType
== "long" )
192 return css::i18n::KNumberFormatType::LONG
;
194 return css::i18n::KNumberFormatType::SHORT
;
198 NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage
)
200 switch ( formatUsage
)
202 case css::i18n::KNumberFormatUsage::DATE
:
203 return OUString( "DATE" );
204 case css::i18n::KNumberFormatUsage::TIME
:
205 return OUString( "TIME" );
206 case css::i18n::KNumberFormatUsage::DATE_TIME
:
207 return OUString( "DATE_TIME" );
208 case css::i18n::KNumberFormatUsage::FIXED_NUMBER
:
209 return OUString( "FIXED_NUMBER" );
210 case css::i18n::KNumberFormatUsage::FRACTION_NUMBER
:
211 return OUString( "FRACTION_NUMBER" );
212 case css::i18n::KNumberFormatUsage::PERCENT_NUMBER
:
213 return OUString( "PERCENT_NUMBER" );
214 case css::i18n::KNumberFormatUsage::CURRENCY
:
215 return OUString( "CURRENCY" );
216 case css::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER
:
217 return OUString( "SCIENTIFIC_NUMBER" );
224 NumberFormatCodeMapper::mapElementUsageStringToShort(const OUString
& formatUsage
)
226 if ( formatUsage
== "DATE" )
227 return css::i18n::KNumberFormatUsage::DATE
;
228 if ( formatUsage
== "TIME" )
229 return css::i18n::KNumberFormatUsage::TIME
;
230 if ( formatUsage
== "DATE_TIME" )
231 return css::i18n::KNumberFormatUsage::DATE_TIME
;
232 if ( formatUsage
== "FIXED_NUMBER" )
233 return css::i18n::KNumberFormatUsage::FIXED_NUMBER
;
234 if ( formatUsage
== "FRACTION_NUMBER" )
235 return css::i18n::KNumberFormatUsage::FRACTION_NUMBER
;
236 if ( formatUsage
== "PERCENT_NUMBER" )
237 return css::i18n::KNumberFormatUsage::PERCENT_NUMBER
;
238 if ( formatUsage
== "CURRENCY" )
239 return css::i18n::KNumberFormatUsage::CURRENCY
;
240 if ( formatUsage
== "SCIENTIFIC_NUMBER" )
241 return css::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER
;
248 NumberFormatCodeMapper::getImplementationName()
250 return OUString("com.sun.star.i18n.NumberFormatCodeMapper");
253 sal_Bool SAL_CALL
NumberFormatCodeMapper::supportsService(const OUString
& rServiceName
)
255 return cppu::supportsService(this, rServiceName
);
258 css::uno::Sequence
< OUString
> SAL_CALL
259 NumberFormatCodeMapper::getSupportedServiceNames()
261 css::uno::Sequence
< OUString
> aRet
{ "com.sun.star.i18n.NumberFormatMapper" };
265 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
266 com_sun_star_i18n_NumberFormatCodeMapper_get_implementation(
267 css::uno::XComponentContext
*context
,
268 css::uno::Sequence
<css::uno::Any
> const &)
270 return cppu::acquire(new NumberFormatCodeMapper(context
));
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */