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/LocaleData.hpp>
24 #include <cppuhelper/supportsservice.hxx>
26 NumberFormatCodeMapper::NumberFormatCodeMapper(
27 const ::com::sun::star::uno::Reference
<
28 ::com::sun::star::uno::XComponentContext
>& rxContext
)
30 mxContext( rxContext
),
31 bFormatsValid( false )
36 NumberFormatCodeMapper::~NumberFormatCodeMapper()
41 ::com::sun::star::i18n::NumberFormatCode SAL_CALL
42 NumberFormatCodeMapper::getDefault( sal_Int16 formatType
, sal_Int16 formatUsage
, const ::com::sun::star::lang::Locale
& rLocale
) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
45 OUString elementType
= mapElementTypeShortToString(formatType
);
46 OUString elementUsage
= mapElementUsageShortToString(formatUsage
);
48 getFormats( rLocale
);
50 for(sal_Int32 i
= 0; i
< aFormatSeq
.getLength(); i
++) {
51 if(aFormatSeq
[i
].isDefault
&& aFormatSeq
[i
].formatType
== elementType
&&
52 aFormatSeq
[i
].formatUsage
== elementUsage
) {
53 com::sun::star::i18n::NumberFormatCode
anumberFormatCode(formatType
,
55 aFormatSeq
[i
].formatCode
,
56 aFormatSeq
[i
].formatName
,
57 aFormatSeq
[i
].formatKey
,
58 aFormatSeq
[i
].formatIndex
,
60 return anumberFormatCode
;
63 com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode
;
64 return defaultNumberFormatCode
;
69 ::com::sun::star::i18n::NumberFormatCode SAL_CALL
70 NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex
, const ::com::sun::star::lang::Locale
& rLocale
) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
72 getFormats( rLocale
);
74 for(sal_Int32 i
= 0; i
< aFormatSeq
.getLength(); i
++) {
75 if(aFormatSeq
[i
].formatIndex
== formatIndex
) {
76 com::sun::star::i18n::NumberFormatCode
anumberFormatCode(mapElementTypeStringToShort(aFormatSeq
[i
].formatType
),
77 mapElementUsageStringToShort(aFormatSeq
[i
].formatUsage
),
78 aFormatSeq
[i
].formatCode
,
79 aFormatSeq
[i
].formatName
,
80 aFormatSeq
[i
].formatKey
,
81 aFormatSeq
[i
].formatIndex
,
82 aFormatSeq
[i
].isDefault
);
83 return anumberFormatCode
;
86 com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode
;
87 return defaultNumberFormatCode
;
93 ::com::sun::star::uno::Sequence
< ::com::sun::star::i18n::NumberFormatCode
> SAL_CALL
94 NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage
, const ::com::sun::star::lang::Locale
& rLocale
) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
96 getFormats( rLocale
);
100 for(i
= 0; i
< aFormatSeq
.getLength(); i
++) {
101 sal_Int16 elementUsage
= mapElementUsageStringToShort(aFormatSeq
[i
].formatUsage
);
102 if( elementUsage
== formatUsage
)
106 ::com::sun::star::uno::Sequence
<com::sun::star::i18n::NumberFormatCode
> seq(count
);
108 for(i
= 0; i
< aFormatSeq
.getLength(); i
++) {
109 sal_Int16 elementUsage
= mapElementUsageStringToShort(aFormatSeq
[i
].formatUsage
);
110 if( elementUsage
== formatUsage
) {
111 seq
[j
] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq
[i
].formatType
),
113 aFormatSeq
[i
].formatCode
,
114 aFormatSeq
[i
].formatName
,
115 aFormatSeq
[i
].formatKey
,
116 aFormatSeq
[i
].formatIndex
,
117 aFormatSeq
[i
].isDefault
);
126 ::com::sun::star::uno::Sequence
< ::com::sun::star::i18n::NumberFormatCode
> SAL_CALL
127 NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale
& rLocale
) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
129 getFormats( rLocale
);
131 ::com::sun::star::uno::Sequence
<com::sun::star::i18n::NumberFormatCode
> seq(aFormatSeq
.getLength());
132 for(sal_Int32 i
= 0; i
< aFormatSeq
.getLength(); i
++)
134 seq
[i
] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq
[i
].formatType
),
135 mapElementUsageStringToShort(aFormatSeq
[i
].formatUsage
),
136 aFormatSeq
[i
].formatCode
,
137 aFormatSeq
[i
].formatName
,
138 aFormatSeq
[i
].formatKey
,
139 aFormatSeq
[i
].formatIndex
,
140 aFormatSeq
[i
].isDefault
);
146 // --- private implementation -----------------------------------------
148 void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale
& rLocale
)
150 if ( aLocale
.Country
!= rLocale
.Country
151 || aLocale
.Language
!= rLocale
.Language
152 || aLocale
.Variant
!= rLocale
.Variant
)
154 bFormatsValid
= false;
160 void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale
& rLocale
)
162 setupLocale( rLocale
);
163 if ( !bFormatsValid
)
165 createLocaleDataObject();
166 if( !mxLocaleData
.is() )
167 aFormatSeq
= ::com::sun::star::uno::Sequence
< ::com::sun::star::i18n::FormatElement
> (0);
169 aFormatSeq
= mxLocaleData
->getAllFormats( aLocale
);
170 bFormatsValid
= true;
176 NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType
)
179 switch ( formatType
)
181 case com::sun::star::i18n::KNumberFormatType::SHORT
:
182 return OUString( "short" );
183 case com::sun::star::i18n::KNumberFormatType::MEDIUM
:
184 return OUString( "medium" );
185 case com::sun::star::i18n::KNumberFormatType::LONG
:
186 return OUString( "long" );
192 NumberFormatCodeMapper::mapElementTypeStringToShort(const OUString
& formatType
)
194 if ( formatType
== "short" )
195 return com::sun::star::i18n::KNumberFormatType::SHORT
;
196 if ( formatType
== "medium" )
197 return com::sun::star::i18n::KNumberFormatType::MEDIUM
;
198 if ( formatType
== "long" )
199 return com::sun::star::i18n::KNumberFormatType::LONG
;
201 return com::sun::star::i18n::KNumberFormatType::SHORT
;
205 NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage
)
207 switch ( formatUsage
)
209 case com::sun::star::i18n::KNumberFormatUsage::DATE
:
210 return OUString( "DATE" );
211 case com::sun::star::i18n::KNumberFormatUsage::TIME
:
212 return OUString( "TIME" );
213 case com::sun::star::i18n::KNumberFormatUsage::DATE_TIME
:
214 return OUString( "DATE_TIME" );
215 case com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER
:
216 return OUString( "FIXED_NUMBER" );
217 case com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER
:
218 return OUString( "FRACTION_NUMBER" );
219 case com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER
:
220 return OUString( "PERCENT_NUMBER" );
221 case com::sun::star::i18n::KNumberFormatUsage::CURRENCY
:
222 return OUString( "CURRENCY" );
223 case com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER
:
224 return OUString( "SCIENTIFIC_NUMBER" );
231 NumberFormatCodeMapper::mapElementUsageStringToShort(const OUString
& formatUsage
)
233 if ( formatUsage
== "DATE" )
234 return com::sun::star::i18n::KNumberFormatUsage::DATE
;
235 if ( formatUsage
== "TIME" )
236 return com::sun::star::i18n::KNumberFormatUsage::TIME
;
237 if ( formatUsage
== "DATE_TIME" )
238 return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME
;
239 if ( formatUsage
== "FIXED_NUMBER" )
240 return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER
;
241 if ( formatUsage
== "FRACTION_NUMBER" )
242 return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER
;
243 if ( formatUsage
== "PERCENT_NUMBER" )
244 return com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER
;
245 if ( formatUsage
== "CURRENCY" )
246 return com::sun::star::i18n::KNumberFormatUsage::CURRENCY
;
247 if ( formatUsage
== "SCIENTIFIC_NUMBER" )
248 return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER
;
255 NumberFormatCodeMapper::createLocaleDataObject() {
257 if(mxLocaleData
.is())
260 mxLocaleData
.set( com::sun::star::i18n::LocaleData::create(mxContext
) );
264 NumberFormatCodeMapper::getImplementationName(void)
265 throw( ::com::sun::star::uno::RuntimeException
, std::exception
)
267 return OUString("com.sun.star.i18n.NumberFormatCodeMapper");
270 sal_Bool SAL_CALL
NumberFormatCodeMapper::supportsService(const OUString
& rServiceName
)
271 throw( ::com::sun::star::uno::RuntimeException
, std::exception
)
273 return cppu::supportsService(this, rServiceName
);
276 ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
277 NumberFormatCodeMapper::getSupportedServiceNames(void) throw( ::com::sun::star::uno::RuntimeException
, std::exception
)
279 ::com::sun::star::uno::Sequence
< OUString
> aRet(1);
280 aRet
[0] = "com.sun.star.i18n.NumberFormatMapper";
284 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
285 com_sun_star_i18n_NumberFormatCodeMapper_get_implementation(
286 css::uno::XComponentContext
*context
,
287 css::uno::Sequence
<css::uno::Any
> const &)
289 return cppu::acquire(new NumberFormatCodeMapper(context
));
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */