merge the formfield patch from ooo-build
[ooovba.git] / i18npool / source / numberformatcode / numberformatcode.cxx
blobfb466483201d5cc905f86d9b2837a6f1cddf46fb
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: numberformatcode.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
34 #include <numberformatcode.hxx>
35 #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
36 #include <com/sun/star/i18n/KNumberFormatType.hpp>
40 NumberFormatCodeMapper::NumberFormatCodeMapper(
41 const ::com::sun::star::uno::Reference <
42 ::com::sun::star::lang::XMultiServiceFactory >& rxMSF )
44 xMSF( rxMSF ),
45 bFormatsValid( sal_False )
50 NumberFormatCodeMapper::~NumberFormatCodeMapper()
55 ::com::sun::star::i18n::NumberFormatCode SAL_CALL
56 NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
59 ::rtl::OUString elementType = mapElementTypeShortToString(formatType);
60 ::rtl::OUString elementUsage = mapElementUsageShortToString(formatUsage);
62 getFormats( rLocale );
64 for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
65 if(aFormatSeq[i].isDefault && aFormatSeq[i].formatType == elementType &&
66 aFormatSeq[i].formatUsage == elementUsage) {
67 com::sun::star::i18n::NumberFormatCode anumberFormatCode(formatType,
68 formatUsage,
69 aFormatSeq[i].formatCode,
70 aFormatSeq[i].formatName,
71 aFormatSeq[i].formatKey,
72 aFormatSeq[i].formatIndex,
73 sal_True);
74 return anumberFormatCode;
77 com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
78 return defaultNumberFormatCode;
83 ::com::sun::star::i18n::NumberFormatCode SAL_CALL
84 NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
86 getFormats( rLocale );
88 for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
89 if(aFormatSeq[i].formatIndex == formatIndex) {
90 com::sun::star::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
91 mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
92 aFormatSeq[i].formatCode,
93 aFormatSeq[i].formatName,
94 aFormatSeq[i].formatKey,
95 aFormatSeq[i].formatIndex,
96 aFormatSeq[i].isDefault);
97 return anumberFormatCode;
100 com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
101 return defaultNumberFormatCode;
107 ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
108 NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
110 getFormats( rLocale );
112 sal_Int32 i, count;
113 count = 0;
114 for(i = 0; i < aFormatSeq.getLength(); i++) {
115 sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
116 if( elementUsage == formatUsage)
117 count++;
120 ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(count);
121 sal_Int32 j = 0;
122 for(i = 0; i < aFormatSeq.getLength(); i++) {
123 sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
124 if( elementUsage == formatUsage) {
125 seq[j] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
126 formatUsage,
127 aFormatSeq[i].formatCode,
128 aFormatSeq[i].formatName,
129 aFormatSeq[i].formatKey,
130 aFormatSeq[i].formatIndex,
131 aFormatSeq[i].isDefault);
132 j++;
135 return seq;
140 ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
141 NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
143 getFormats( rLocale );
145 ::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(aFormatSeq.getLength());
146 for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++)
148 seq[i] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
149 mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
150 aFormatSeq[i].formatCode,
151 aFormatSeq[i].formatName,
152 aFormatSeq[i].formatKey,
153 aFormatSeq[i].formatIndex,
154 aFormatSeq[i].isDefault);
156 return seq;
160 // --- private implementation -----------------------------------------
162 void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale& rLocale )
164 if ( aLocale.Country != rLocale.Country
165 || aLocale.Language != rLocale.Language
166 || aLocale.Variant != rLocale.Variant )
168 bFormatsValid = sal_False;
169 aLocale = rLocale;
174 void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale& rLocale )
176 setupLocale( rLocale );
177 if ( !bFormatsValid )
179 createLocaleDataObject();
180 if( !xlocaleData.is() )
181 aFormatSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > (0);
182 else
183 aFormatSeq = xlocaleData->getAllFormats( aLocale );
184 bFormatsValid = sal_True;
189 ::rtl::OUString
190 NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType)
193 switch ( formatType )
195 case com::sun::star::i18n::KNumberFormatType::SHORT :
196 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "short" ) );
197 case com::sun::star::i18n::KNumberFormatType::MEDIUM :
198 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "medium" ) );
199 case com::sun::star::i18n::KNumberFormatType::LONG :
200 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "long" ) );
202 return ::rtl::OUString();
205 sal_Int16
206 NumberFormatCodeMapper::mapElementTypeStringToShort(const ::rtl::OUString& formatType)
208 if(formatType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "short" ) ))
209 return com::sun::star::i18n::KNumberFormatType::SHORT;
210 if(formatType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "medium" ) ))
211 return com::sun::star::i18n::KNumberFormatType::MEDIUM;
212 if(formatType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "long" ) ))
213 return com::sun::star::i18n::KNumberFormatType::LONG;
215 return com::sun::star::i18n::KNumberFormatType::SHORT;
218 ::rtl::OUString
219 NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage)
221 switch ( formatUsage )
223 case com::sun::star::i18n::KNumberFormatUsage::DATE :
224 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DATE" ) );
225 case com::sun::star::i18n::KNumberFormatUsage::TIME :
226 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "TIME" ) );
227 case com::sun::star::i18n::KNumberFormatUsage::DATE_TIME :
228 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DATE_TIME" ) );
229 case com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER :
230 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FIXED_NUMBER" ) );
231 case com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER :
232 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FRACTION_NUMBER" ) );
233 case com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER :
234 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PERCENT_NUMBER" ) );
235 case com::sun::star::i18n::KNumberFormatUsage::CURRENCY :
236 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CURRENCY" ) );
237 case com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER :
238 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SCIENTIFIC_NUMBER" ) );
240 return ::rtl::OUString();
244 sal_Int16
245 NumberFormatCodeMapper::mapElementUsageStringToShort(const ::rtl::OUString& formatUsage)
247 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DATE" ) ))
248 return com::sun::star::i18n::KNumberFormatUsage::DATE;
249 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "TIME" ) ))
250 return com::sun::star::i18n::KNumberFormatUsage::TIME;
251 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DATE_TIME" ) ))
252 return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME;
253 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FIXED_NUMBER" ) ))
254 return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER;
255 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FRACTION_NUMBER" ) ))
256 return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER;
257 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "PERCENT_NUMBER" ) ))
258 return com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER;
259 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "CURRENCY" ) ))
260 return com::sun::star::i18n::KNumberFormatUsage::CURRENCY;
261 if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "SCIENTIFIC_NUMBER" ) ))
262 return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
264 return 0;
268 void
269 NumberFormatCodeMapper::createLocaleDataObject() {
271 if(xlocaleData.is())
272 return;
274 ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface >
275 xI = xMSF->createInstance(
276 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.i18n.LocaleData" ) ));
278 if ( xI.is() ) {
279 ::com::sun::star::uno::Any x = xI->queryInterface( ::getCppuType((const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XLocaleData >*)0) );
280 x >>= xlocaleData;
284 ::rtl::OUString SAL_CALL
285 NumberFormatCodeMapper::getImplementationName(void)
286 throw( ::com::sun::star::uno::RuntimeException )
288 return ::rtl::OUString::createFromAscii("com.sun.star.i18n.NumberFormatCodeMapper");
291 const sal_Char cNumFormat[] = "com.sun.star.i18n.NumberFormatMapper";
293 sal_Bool SAL_CALL
294 NumberFormatCodeMapper::supportsService(const rtl::OUString& rServiceName)
295 throw( ::com::sun::star::uno::RuntimeException )
297 return !rServiceName.compareToAscii(cNumFormat);
300 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
301 NumberFormatCodeMapper::getSupportedServiceNames(void) throw( ::com::sun::star::uno::RuntimeException )
303 ::com::sun::star::uno::Sequence< ::rtl::OUString > aRet(1);
304 aRet[0] = ::rtl::OUString::createFromAscii(cNumFormat);
305 return aRet;