Branch data 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 : :
21 : : #include <numberformatcode.hxx>
22 : : #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
23 : : #include <com/sun/star/i18n/KNumberFormatType.hpp>
24 : :
25 : :
26 : :
27 : 16910 : NumberFormatCodeMapper::NumberFormatCodeMapper(
28 : : const ::com::sun::star::uno::Reference <
29 : : ::com::sun::star::lang::XMultiServiceFactory >& rxMSF )
30 : : :
31 : : xMSF( rxMSF ),
32 [ + - ]: 16910 : bFormatsValid( sal_False )
33 : : {
34 : 16910 : }
35 : :
36 : :
37 [ + - ]: 16910 : NumberFormatCodeMapper::~NumberFormatCodeMapper()
38 : : {
39 [ - + ]: 33820 : }
40 : :
41 : :
42 : : ::com::sun::star::i18n::NumberFormatCode SAL_CALL
43 : 0 : NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
44 : : {
45 : :
46 [ # # ]: 0 : ::rtl::OUString elementType = mapElementTypeShortToString(formatType);
47 [ # # ]: 0 : ::rtl::OUString elementUsage = mapElementUsageShortToString(formatUsage);
48 : :
49 [ # # ]: 0 : getFormats( rLocale );
50 : :
51 [ # # ]: 0 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
52 [ # # ][ # # ]: 0 : if(aFormatSeq[i].isDefault && aFormatSeq[i].formatType == elementType &&
[ # # ]
[ # # # # ]
[ # # ]
53 [ # # ]: 0 : aFormatSeq[i].formatUsage == elementUsage) {
54 : : com::sun::star::i18n::NumberFormatCode anumberFormatCode(formatType,
55 : : formatUsage,
56 [ # # ]: 0 : aFormatSeq[i].formatCode,
57 [ # # ]: 0 : aFormatSeq[i].formatName,
58 [ # # ]: 0 : aFormatSeq[i].formatKey,
59 [ # # ]: 0 : aFormatSeq[i].formatIndex,
60 : 0 : sal_True);
61 : 0 : return anumberFormatCode;
62 : : }
63 : : }
64 : 0 : com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
65 : 0 : return defaultNumberFormatCode;
66 : : }
67 : :
68 : :
69 : :
70 : : ::com::sun::star::i18n::NumberFormatCode SAL_CALL
71 : 7203 : NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
72 : : {
73 [ + - ]: 7203 : getFormats( rLocale );
74 : :
75 [ + - ]: 7203 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
76 [ + - ][ + - ]: 7203 : if(aFormatSeq[i].formatIndex == formatIndex) {
77 [ + - ][ + - ]: 7203 : com::sun::star::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
78 [ + - ][ + - ]: 7203 : mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
79 [ + - ]: 7203 : aFormatSeq[i].formatCode,
80 [ + - ]: 7203 : aFormatSeq[i].formatName,
81 [ + - ]: 7203 : aFormatSeq[i].formatKey,
82 [ + - ]: 7203 : aFormatSeq[i].formatIndex,
83 [ + - ]: 14406 : aFormatSeq[i].isDefault);
84 : 7203 : return anumberFormatCode;
85 : : }
86 : : }
87 : 0 : com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
88 : 7203 : return defaultNumberFormatCode;
89 : :
90 : : }
91 : :
92 : :
93 : :
94 : : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
95 : 26787 : NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
96 : : {
97 : 26787 : getFormats( rLocale );
98 : :
99 : : sal_Int32 i, count;
100 : 26787 : count = 0;
101 [ + + ]: 1504653 : for(i = 0; i < aFormatSeq.getLength(); i++) {
102 : 1477866 : sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
103 [ + + ]: 1477866 : if( elementUsage == formatUsage)
104 : 202152 : count++;
105 : : }
106 : :
107 : 26787 : ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(count);
108 : 26787 : sal_Int32 j = 0;
109 [ + + ]: 1504653 : for(i = 0; i < aFormatSeq.getLength(); i++) {
110 [ + - ][ + - ]: 1477866 : sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
111 [ + + ]: 1477866 : if( elementUsage == formatUsage) {
112 [ + - ]: 404304 : seq[j] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
[ + - + - ]
113 : : formatUsage,
114 [ + - ]: 202152 : aFormatSeq[i].formatCode,
115 [ + - ]: 202152 : aFormatSeq[i].formatName,
116 [ + - ]: 202152 : aFormatSeq[i].formatKey,
117 [ + - ]: 202152 : aFormatSeq[i].formatIndex,
118 [ + - ]: 606456 : aFormatSeq[i].isDefault);
119 : 202152 : j++;
120 : : }
121 : : }
122 : 26787 : return seq;
123 : :
124 : : }
125 : :
126 : :
127 : : ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
128 : 2440 : NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
129 : : {
130 : 2440 : getFormats( rLocale );
131 : :
132 : 2440 : ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(aFormatSeq.getLength());
133 [ + + ]: 143780 : for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++)
134 : : {
135 [ + - ]: 282680 : seq[i] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
[ + - + - ]
136 [ + - ][ + - ]: 141340 : mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
137 [ + - ]: 141340 : aFormatSeq[i].formatCode,
138 [ + - ]: 141340 : aFormatSeq[i].formatName,
139 [ + - ]: 141340 : aFormatSeq[i].formatKey,
140 [ + - ]: 141340 : aFormatSeq[i].formatIndex,
141 [ + - ]: 424020 : aFormatSeq[i].isDefault);
142 : : }
143 : 2440 : return seq;
144 : : }
145 : :
146 : :
147 : : // --- private implementation -----------------------------------------
148 : :
149 : 36430 : void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale& rLocale )
150 : : {
151 [ + + + + : 75614 : if ( aLocale.Country != rLocale.Country
- + ][ + + ]
152 : 19664 : || aLocale.Language != rLocale.Language
153 : 19520 : || aLocale.Variant != rLocale.Variant )
154 : : {
155 : 16910 : bFormatsValid = sal_False;
156 : 16910 : aLocale = rLocale;
157 : : }
158 : 36430 : }
159 : :
160 : :
161 : 36430 : void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale& rLocale )
162 : : {
163 : 36430 : setupLocale( rLocale );
164 [ + + ]: 36430 : if ( !bFormatsValid )
165 : : {
166 : 16910 : createLocaleDataObject();
167 [ - + ]: 16910 : if( !xlocaleData.is() )
168 [ # # ]: 0 : aFormatSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > (0);
169 : : else
170 [ + - ]: 16910 : aFormatSeq = xlocaleData->getAllFormats( aLocale );
171 : 16910 : bFormatsValid = sal_True;
172 : : }
173 : 36430 : }
174 : :
175 : :
176 : : ::rtl::OUString
177 : 0 : NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType)
178 : : {
179 : :
180 [ # # # # ]: 0 : switch ( formatType )
181 : : {
182 : : case com::sun::star::i18n::KNumberFormatType::SHORT :
183 : 0 : return ::rtl::OUString( "short" );
184 : : case com::sun::star::i18n::KNumberFormatType::MEDIUM :
185 : 0 : return ::rtl::OUString( "medium" );
186 : : case com::sun::star::i18n::KNumberFormatType::LONG :
187 : 0 : return ::rtl::OUString( "long" );
188 : : }
189 : 0 : return ::rtl::OUString();
190 : : }
191 : :
192 : : sal_Int16
193 : 350695 : NumberFormatCodeMapper::mapElementTypeStringToShort(const ::rtl::OUString& formatType)
194 : : {
195 [ + + ]: 350695 : if ( formatType == "short" )
196 : 58443 : return com::sun::star::i18n::KNumberFormatType::SHORT;
197 [ + + ]: 292252 : if ( formatType == "medium" )
198 : 217914 : return com::sun::star::i18n::KNumberFormatType::MEDIUM;
199 [ + - ]: 74338 : if ( formatType == "long" )
200 : 74338 : return com::sun::star::i18n::KNumberFormatType::LONG;
201 : :
202 : 350695 : return com::sun::star::i18n::KNumberFormatType::SHORT;
203 : : }
204 : :
205 : : ::rtl::OUString
206 : 0 : NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage)
207 : : {
208 [ # # # # : 0 : switch ( formatUsage )
# # # #
# ]
209 : : {
210 : : case com::sun::star::i18n::KNumberFormatUsage::DATE :
211 : 0 : return ::rtl::OUString( "DATE" );
212 : : case com::sun::star::i18n::KNumberFormatUsage::TIME :
213 : 0 : return ::rtl::OUString( "TIME" );
214 : : case com::sun::star::i18n::KNumberFormatUsage::DATE_TIME :
215 : 0 : return ::rtl::OUString( "DATE_TIME" );
216 : : case com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER :
217 : 0 : return ::rtl::OUString( "FIXED_NUMBER" );
218 : : case com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER :
219 : 0 : return ::rtl::OUString( "FRACTION_NUMBER" );
220 : : case com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER :
221 : 0 : return ::rtl::OUString( "PERCENT_NUMBER" );
222 : : case com::sun::star::i18n::KNumberFormatUsage::CURRENCY :
223 : 0 : return ::rtl::OUString( "CURRENCY" );
224 : : case com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER :
225 : 0 : return ::rtl::OUString( "SCIENTIFIC_NUMBER" );
226 : : }
227 : 0 : return ::rtl::OUString();
228 : : }
229 : :
230 : :
231 : : sal_Int16
232 : 3104275 : NumberFormatCodeMapper::mapElementUsageStringToShort(const ::rtl::OUString& formatUsage)
233 : : {
234 [ + + ]: 3104275 : if ( formatUsage == "DATE" )
235 : 1599028 : return com::sun::star::i18n::KNumberFormatUsage::DATE;
236 [ + + ]: 1505247 : if ( formatUsage == "TIME" )
237 : 395842 : return com::sun::star::i18n::KNumberFormatUsage::TIME;
238 [ + + ]: 1109405 : if ( formatUsage == "DATE_TIME" )
239 : 114548 : return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME;
240 [ + + ]: 994857 : if ( formatUsage == "FIXED_NUMBER" )
241 : 432629 : return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER;
242 [ - + ]: 562228 : if ( formatUsage == "FRACTION_NUMBER" )
243 : 0 : return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER;
244 [ + + ]: 562228 : if ( formatUsage == "PERCENT_NUMBER" )
245 : 112604 : return com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER;
246 [ + + ]: 449624 : if ( formatUsage == "CURRENCY" )
247 : 337524 : return com::sun::star::i18n::KNumberFormatUsage::CURRENCY;
248 [ + - ]: 112100 : if ( formatUsage == "SCIENTIFIC_NUMBER" )
249 : 112100 : return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
250 : :
251 : 3104275 : return 0;
252 : : }
253 : :
254 : :
255 : : void
256 : 16910 : NumberFormatCodeMapper::createLocaleDataObject() {
257 : :
258 [ + - ]: 16910 : if(xlocaleData.is())
259 : 16910 : return;
260 : :
261 : : ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface >
262 [ + - ]: 16910 : xI = xMSF->createInstance(
263 [ + - ]: 16910 : ::rtl::OUString( "com.sun.star.i18n.LocaleData" ));
264 : :
265 [ + - ]: 16910 : if ( xI.is() ) {
266 [ + - ][ + - ]: 16910 : ::com::sun::star::uno::Any x = xI->queryInterface( ::getCppuType((const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XLocaleData >*)0) );
[ + - ]
267 [ + - ]: 16910 : x >>= xlocaleData;
268 : 16910 : }
269 : : }
270 : :
271 : : ::rtl::OUString SAL_CALL
272 : 0 : NumberFormatCodeMapper::getImplementationName(void)
273 : : throw( ::com::sun::star::uno::RuntimeException )
274 : : {
275 : 0 : return ::rtl::OUString("com.sun.star.i18n.NumberFormatCodeMapper");
276 : : }
277 : :
278 : : const sal_Char cNumFormat[] = "com.sun.star.i18n.NumberFormatMapper";
279 : :
280 : : sal_Bool SAL_CALL
281 : 0 : NumberFormatCodeMapper::supportsService(const rtl::OUString& rServiceName)
282 : : throw( ::com::sun::star::uno::RuntimeException )
283 : : {
284 : 0 : return !rServiceName.compareToAscii(cNumFormat);
285 : : }
286 : :
287 : : ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
288 : 0 : NumberFormatCodeMapper::getSupportedServiceNames(void) throw( ::com::sun::star::uno::RuntimeException )
289 : : {
290 : 0 : ::com::sun::star::uno::Sequence< ::rtl::OUString > aRet(1);
291 [ # # ]: 0 : aRet[0] = ::rtl::OUString::createFromAscii(cNumFormat);
292 : 0 : return aRet;
293 : : }
294 : :
295 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|