merged tag ooo/OOO330_m14
[LibreOffice.git] / i18npool / source / numberformatcode / numberformatcode.cxx
blob8f5f926cc72804904a862a0d2a5253308e5c8927
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_i18npool.hxx"
31 #include <numberformatcode.hxx>
32 #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
33 #include <com/sun/star/i18n/KNumberFormatType.hpp>
37 NumberFormatCodeMapper::NumberFormatCodeMapper(
38 const ::com::sun::star::uno::Reference <
39 ::com::sun::star::lang::XMultiServiceFactory >& rxMSF )
41 xMSF( rxMSF ),
42 bFormatsValid( sal_False )
47 NumberFormatCodeMapper::~NumberFormatCodeMapper()
52 ::com::sun::star::i18n::NumberFormatCode SAL_CALL
53 NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
56 ::rtl::OUString elementType = mapElementTypeShortToString(formatType);
57 ::rtl::OUString elementUsage = mapElementUsageShortToString(formatUsage);
59 getFormats( rLocale );
61 for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
62 if(aFormatSeq[i].isDefault && aFormatSeq[i].formatType == elementType &&
63 aFormatSeq[i].formatUsage == elementUsage) {
64 com::sun::star::i18n::NumberFormatCode anumberFormatCode(formatType,
65 formatUsage,
66 aFormatSeq[i].formatCode,
67 aFormatSeq[i].formatName,
68 aFormatSeq[i].formatKey,
69 aFormatSeq[i].formatIndex,
70 sal_True);
71 return anumberFormatCode;
74 com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
75 return defaultNumberFormatCode;
80 ::com::sun::star::i18n::NumberFormatCode SAL_CALL
81 NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
83 getFormats( rLocale );
85 for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
86 if(aFormatSeq[i].formatIndex == formatIndex) {
87 com::sun::star::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
88 mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
89 aFormatSeq[i].formatCode,
90 aFormatSeq[i].formatName,
91 aFormatSeq[i].formatKey,
92 aFormatSeq[i].formatIndex,
93 aFormatSeq[i].isDefault);
94 return anumberFormatCode;
97 com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
98 return defaultNumberFormatCode;
104 ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
105 NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
107 getFormats( rLocale );
109 sal_Int32 i, count;
110 count = 0;
111 for(i = 0; i < aFormatSeq.getLength(); i++) {
112 sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
113 if( elementUsage == formatUsage)
114 count++;
117 ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(count);
118 sal_Int32 j = 0;
119 for(i = 0; i < aFormatSeq.getLength(); i++) {
120 sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
121 if( elementUsage == formatUsage) {
122 seq[j] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
123 formatUsage,
124 aFormatSeq[i].formatCode,
125 aFormatSeq[i].formatName,
126 aFormatSeq[i].formatKey,
127 aFormatSeq[i].formatIndex,
128 aFormatSeq[i].isDefault);
129 j++;
132 return seq;
137 ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
138 NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
140 getFormats( rLocale );
142 ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(aFormatSeq.getLength());
143 for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++)
145 seq[i] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
146 mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
147 aFormatSeq[i].formatCode,
148 aFormatSeq[i].formatName,
149 aFormatSeq[i].formatKey,
150 aFormatSeq[i].formatIndex,
151 aFormatSeq[i].isDefault);
153 return seq;
157 // --- private implementation -----------------------------------------
159 void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale& rLocale )
161 if ( aLocale.Country != rLocale.Country
162 || aLocale.Language != rLocale.Language
163 || aLocale.Variant != rLocale.Variant )
165 bFormatsValid = sal_False;
166 aLocale = rLocale;
171 void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale& rLocale )
173 setupLocale( rLocale );
174 if ( !bFormatsValid )
176 createLocaleDataObject();
177 if( !xlocaleData.is() )
178 aFormatSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > (0);
179 else
180 aFormatSeq = xlocaleData->getAllFormats( aLocale );
181 bFormatsValid = sal_True;
186 ::rtl::OUString
187 NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType)
190 switch ( formatType )
192 case com::sun::star::i18n::KNumberFormatType::SHORT :
193 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "short" ) );
194 case com::sun::star::i18n::KNumberFormatType::MEDIUM :
195 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "medium" ) );
196 case com::sun::star::i18n::KNumberFormatType::LONG :
197 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "long" ) );
199 return ::rtl::OUString();
202 sal_Int16
203 NumberFormatCodeMapper::mapElementTypeStringToShort(const ::rtl::OUString& formatType)
205 if(formatType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "short" ) ))
206 return com::sun::star::i18n::KNumberFormatType::SHORT;
207 if(formatType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "medium" ) ))
208 return com::sun::star::i18n::KNumberFormatType::MEDIUM;
209 if(formatType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "long" ) ))
210 return com::sun::star::i18n::KNumberFormatType::LONG;
212 return com::sun::star::i18n::KNumberFormatType::SHORT;
215 ::rtl::OUString
216 NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage)
218 switch ( formatUsage )
220 case com::sun::star::i18n::KNumberFormatUsage::DATE :
221 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DATE" ) );
222 case com::sun::star::i18n::KNumberFormatUsage::TIME :
223 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "TIME" ) );
224 case com::sun::star::i18n::KNumberFormatUsage::DATE_TIME :
225 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DATE_TIME" ) );
226 case com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER :
227 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FIXED_NUMBER" ) );
228 case com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER :
229 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FRACTION_NUMBER" ) );
230 case com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER :
231 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PERCENT_NUMBER" ) );
232 case com::sun::star::i18n::KNumberFormatUsage::CURRENCY :
233 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CURRENCY" ) );
234 case com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER :
235 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SCIENTIFIC_NUMBER" ) );
237 return ::rtl::OUString();
241 sal_Int16
242 NumberFormatCodeMapper::mapElementUsageStringToShort(const ::rtl::OUString& formatUsage)
244 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DATE" ) ))
245 return com::sun::star::i18n::KNumberFormatUsage::DATE;
246 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "TIME" ) ))
247 return com::sun::star::i18n::KNumberFormatUsage::TIME;
248 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DATE_TIME" ) ))
249 return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME;
250 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FIXED_NUMBER" ) ))
251 return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER;
252 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FRACTION_NUMBER" ) ))
253 return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER;
254 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "PERCENT_NUMBER" ) ))
255 return com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER;
256 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "CURRENCY" ) ))
257 return com::sun::star::i18n::KNumberFormatUsage::CURRENCY;
258 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "SCIENTIFIC_NUMBER" ) ))
259 return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
261 return 0;
265 void
266 NumberFormatCodeMapper::createLocaleDataObject() {
268 if(xlocaleData.is())
269 return;
271 ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface >
272 xI = xMSF->createInstance(
273 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.i18n.LocaleData" ) ));
275 if ( xI.is() ) {
276 ::com::sun::star::uno::Any x = xI->queryInterface( ::getCppuType((const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XLocaleData >*)0) );
277 x >>= xlocaleData;
281 ::rtl::OUString SAL_CALL
282 NumberFormatCodeMapper::getImplementationName(void)
283 throw( ::com::sun::star::uno::RuntimeException )
285 return ::rtl::OUString::createFromAscii("com.sun.star.i18n.NumberFormatCodeMapper");
288 const sal_Char cNumFormat[] = "com.sun.star.i18n.NumberFormatMapper";
290 sal_Bool SAL_CALL
291 NumberFormatCodeMapper::supportsService(const rtl::OUString& rServiceName)
292 throw( ::com::sun::star::uno::RuntimeException )
294 return !rServiceName.compareToAscii(cNumFormat);
297 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
298 NumberFormatCodeMapper::getSupportedServiceNames(void) throw( ::com::sun::star::uno::RuntimeException )
300 ::com::sun::star::uno::Sequence< ::rtl::OUString > aRet(1);
301 aRet[0] = ::rtl::OUString::createFromAscii(cNumFormat);
302 return aRet;