1*449ab281SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*449ab281SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*449ab281SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*449ab281SAndrew Rist  * distributed with this work for additional information
6*449ab281SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*449ab281SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*449ab281SAndrew Rist  * "License"); you may not use this file except in compliance
9*449ab281SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*449ab281SAndrew Rist  *
11*449ab281SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*449ab281SAndrew Rist  *
13*449ab281SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*449ab281SAndrew Rist  * software distributed under the License is distributed on an
15*449ab281SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*449ab281SAndrew Rist  * KIND, either express or implied.  See the License for the
17*449ab281SAndrew Rist  * specific language governing permissions and limitations
18*449ab281SAndrew Rist  * under the License.
19*449ab281SAndrew Rist  *
20*449ab281SAndrew Rist  *************************************************************/
21*449ab281SAndrew Rist 
22*449ab281SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <numberformatcode.hxx>
28cdf0e10cSrcweir #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
29cdf0e10cSrcweir #include <com/sun/star/i18n/KNumberFormatType.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
NumberFormatCodeMapper(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & rxMSF)33cdf0e10cSrcweir NumberFormatCodeMapper::NumberFormatCodeMapper(
34cdf0e10cSrcweir 			const ::com::sun::star::uno::Reference <
35cdf0e10cSrcweir 				::com::sun::star::lang::XMultiServiceFactory >& rxMSF )
36cdf0e10cSrcweir 		:
37cdf0e10cSrcweir 		xMSF( rxMSF ),
38cdf0e10cSrcweir 		bFormatsValid( sal_False )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir }
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 
~NumberFormatCodeMapper()43cdf0e10cSrcweir NumberFormatCodeMapper::~NumberFormatCodeMapper()
44cdf0e10cSrcweir {
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir ::com::sun::star::i18n::NumberFormatCode SAL_CALL
getDefault(sal_Int16 formatType,sal_Int16 formatUsage,const::com::sun::star::lang::Locale & rLocale)49cdf0e10cSrcweir NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 	::rtl::OUString elementType	= mapElementTypeShortToString(formatType);
53cdf0e10cSrcweir 	::rtl::OUString elementUsage = mapElementUsageShortToString(formatUsage);
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	getFormats( rLocale );
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
58cdf0e10cSrcweir 		if(aFormatSeq[i].isDefault && aFormatSeq[i].formatType == elementType &&
59cdf0e10cSrcweir 			aFormatSeq[i].formatUsage == elementUsage) {
60cdf0e10cSrcweir 			com::sun::star::i18n::NumberFormatCode anumberFormatCode(formatType,
61cdf0e10cSrcweir 																	formatUsage,
62cdf0e10cSrcweir 																	aFormatSeq[i].formatCode,
63cdf0e10cSrcweir 																	aFormatSeq[i].formatName,
64cdf0e10cSrcweir 																	aFormatSeq[i].formatKey,
65cdf0e10cSrcweir 																	aFormatSeq[i].formatIndex,
66cdf0e10cSrcweir 																	sal_True);
67cdf0e10cSrcweir 			return anumberFormatCode;
68cdf0e10cSrcweir 		}
69cdf0e10cSrcweir 	}
70cdf0e10cSrcweir 	com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
71cdf0e10cSrcweir 	return defaultNumberFormatCode;
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 
76cdf0e10cSrcweir ::com::sun::star::i18n::NumberFormatCode SAL_CALL
getFormatCode(sal_Int16 formatIndex,const::com::sun::star::lang::Locale & rLocale)77cdf0e10cSrcweir NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
78cdf0e10cSrcweir {
79cdf0e10cSrcweir 	getFormats( rLocale );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 	for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) {
82cdf0e10cSrcweir 		if(aFormatSeq[i].formatIndex == formatIndex) {
83cdf0e10cSrcweir 			com::sun::star::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
84cdf0e10cSrcweir 																	mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
85cdf0e10cSrcweir 																	aFormatSeq[i].formatCode,
86cdf0e10cSrcweir 																	aFormatSeq[i].formatName,
87cdf0e10cSrcweir 																	aFormatSeq[i].formatKey,
88cdf0e10cSrcweir 																	aFormatSeq[i].formatIndex,
89cdf0e10cSrcweir 																	aFormatSeq[i].isDefault);
90cdf0e10cSrcweir 			return anumberFormatCode;
91cdf0e10cSrcweir 		}
92cdf0e10cSrcweir 	}
93cdf0e10cSrcweir 	com::sun::star::i18n::NumberFormatCode defaultNumberFormatCode;
94cdf0e10cSrcweir 	return defaultNumberFormatCode;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 
100cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
getAllFormatCode(sal_Int16 formatUsage,const::com::sun::star::lang::Locale & rLocale)101cdf0e10cSrcweir NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	getFormats( rLocale );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	sal_Int32 i, count;
106cdf0e10cSrcweir 	count = 0;
107cdf0e10cSrcweir 	for(i = 0; i < aFormatSeq.getLength(); i++) {
108cdf0e10cSrcweir 		sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
109cdf0e10cSrcweir 		if( elementUsage == formatUsage)
110cdf0e10cSrcweir 			count++;
111cdf0e10cSrcweir 	}
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(count);
114cdf0e10cSrcweir 	sal_Int32 j = 0;
115cdf0e10cSrcweir 	for(i = 0; i < aFormatSeq.getLength(); i++) {
116cdf0e10cSrcweir 		sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage);
117cdf0e10cSrcweir 		if( elementUsage == formatUsage) {
118cdf0e10cSrcweir 			seq[j] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
119cdf0e10cSrcweir 															formatUsage,
120cdf0e10cSrcweir 															aFormatSeq[i].formatCode,
121cdf0e10cSrcweir 															aFormatSeq[i].formatName,
122cdf0e10cSrcweir 															aFormatSeq[i].formatKey,
123cdf0e10cSrcweir 															aFormatSeq[i].formatIndex,
124cdf0e10cSrcweir 															aFormatSeq[i].isDefault);
125cdf0e10cSrcweir 			j++;
126cdf0e10cSrcweir 		}
127cdf0e10cSrcweir 	}
128cdf0e10cSrcweir 	return seq;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 
133cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::NumberFormatCode > SAL_CALL
getAllFormatCodes(const::com::sun::star::lang::Locale & rLocale)134cdf0e10cSrcweir NumberFormatCodeMapper::getAllFormatCodes( const ::com::sun::star::lang::Locale& rLocale ) throw(::com::sun::star::uno::RuntimeException)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	getFormats( rLocale );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	::com::sun::star::uno::Sequence<com::sun::star::i18n::NumberFormatCode> seq(aFormatSeq.getLength());
139cdf0e10cSrcweir 	for(sal_Int32 i = 0; i < aFormatSeq.getLength(); i++)
140cdf0e10cSrcweir 	{
141cdf0e10cSrcweir 		seq[i] = com::sun::star::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType),
142cdf0e10cSrcweir 														mapElementUsageStringToShort(aFormatSeq[i].formatUsage),
143cdf0e10cSrcweir 														aFormatSeq[i].formatCode,
144cdf0e10cSrcweir 														aFormatSeq[i].formatName,
145cdf0e10cSrcweir 														aFormatSeq[i].formatKey,
146cdf0e10cSrcweir 														aFormatSeq[i].formatIndex,
147cdf0e10cSrcweir 														aFormatSeq[i].isDefault);
148cdf0e10cSrcweir 	}
149cdf0e10cSrcweir 	return seq;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 
153cdf0e10cSrcweir // --- private implementation -----------------------------------------
154cdf0e10cSrcweir 
setupLocale(const::com::sun::star::lang::Locale & rLocale)155cdf0e10cSrcweir void NumberFormatCodeMapper::setupLocale( const ::com::sun::star::lang::Locale& rLocale )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	if ( aLocale.Country	!= rLocale.Country
158cdf0e10cSrcweir 	  || aLocale.Language	!= rLocale.Language
159cdf0e10cSrcweir 	  || aLocale.Variant	!= rLocale.Variant )
160cdf0e10cSrcweir 	{
161cdf0e10cSrcweir 		bFormatsValid = sal_False;
162cdf0e10cSrcweir 		aLocale = rLocale;
163cdf0e10cSrcweir 	}
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 
getFormats(const::com::sun::star::lang::Locale & rLocale)167cdf0e10cSrcweir void NumberFormatCodeMapper::getFormats( const ::com::sun::star::lang::Locale& rLocale )
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	setupLocale( rLocale );
170cdf0e10cSrcweir 	if ( !bFormatsValid )
171cdf0e10cSrcweir 	{
172cdf0e10cSrcweir 		createLocaleDataObject();
173cdf0e10cSrcweir 		if( !xlocaleData.is() )
174cdf0e10cSrcweir 			aFormatSeq = ::com::sun::star::uno::Sequence< ::com::sun::star::i18n::FormatElement > (0);
175cdf0e10cSrcweir 		else
176cdf0e10cSrcweir 			aFormatSeq = xlocaleData->getAllFormats( aLocale );
177cdf0e10cSrcweir 		bFormatsValid = sal_True;
178cdf0e10cSrcweir 	}
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 
182cdf0e10cSrcweir ::rtl::OUString
mapElementTypeShortToString(sal_Int16 formatType)183cdf0e10cSrcweir NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	switch ( formatType )
187cdf0e10cSrcweir 	{
188cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatType::SHORT :
189cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "short" ) );
190cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatType::MEDIUM :
191cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "medium" ) );
192cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatType::LONG :
193cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "long" ) );
194cdf0e10cSrcweir 	}
195cdf0e10cSrcweir 	return ::rtl::OUString();
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir sal_Int16
mapElementTypeStringToShort(const::rtl::OUString & formatType)199cdf0e10cSrcweir NumberFormatCodeMapper::mapElementTypeStringToShort(const ::rtl::OUString& formatType)
200cdf0e10cSrcweir {
201cdf0e10cSrcweir 	if(formatType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "short" ) ))
202cdf0e10cSrcweir 		return com::sun::star::i18n::KNumberFormatType::SHORT;
203cdf0e10cSrcweir 	if(formatType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "medium" ) ))
204cdf0e10cSrcweir 		return com::sun::star::i18n::KNumberFormatType::MEDIUM;
205cdf0e10cSrcweir 	if(formatType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "long" ) ))
206cdf0e10cSrcweir 		return com::sun::star::i18n::KNumberFormatType::LONG;
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 	return com::sun::star::i18n::KNumberFormatType::SHORT;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir ::rtl::OUString
mapElementUsageShortToString(sal_Int16 formatUsage)212cdf0e10cSrcweir NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage)
213cdf0e10cSrcweir {
214cdf0e10cSrcweir 	switch ( formatUsage )
215cdf0e10cSrcweir 	{
216cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatUsage::DATE :
217cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DATE" ) );
218cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatUsage::TIME :
219cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "TIME" ) );
220cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatUsage::DATE_TIME :
221cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DATE_TIME" ) );
222cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER :
223cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FIXED_NUMBER" ) );
224cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER :
225cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FRACTION_NUMBER" ) );
226cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER :
227cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PERCENT_NUMBER" ) );
228cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatUsage::CURRENCY :
229cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CURRENCY" ) );
230cdf0e10cSrcweir 		case com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER :
231cdf0e10cSrcweir 			return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SCIENTIFIC_NUMBER" ) );
232cdf0e10cSrcweir 	}
233cdf0e10cSrcweir 	return ::rtl::OUString();
234cdf0e10cSrcweir }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir 
237cdf0e10cSrcweir sal_Int16
mapElementUsageStringToShort(const::rtl::OUString & formatUsage)238cdf0e10cSrcweir NumberFormatCodeMapper::mapElementUsageStringToShort(const ::rtl::OUString& formatUsage)
239cdf0e10cSrcweir {
240cdf0e10cSrcweir 	if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DATE" ) ))
241cdf0e10cSrcweir 		return com::sun::star::i18n::KNumberFormatUsage::DATE;
242cdf0e10cSrcweir 	if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "TIME" ) ))
243cdf0e10cSrcweir 		return com::sun::star::i18n::KNumberFormatUsage::TIME;
244cdf0e10cSrcweir 	if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DATE_TIME" ) ))
245cdf0e10cSrcweir 		return com::sun::star::i18n::KNumberFormatUsage::DATE_TIME;
246cdf0e10cSrcweir 	if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FIXED_NUMBER" ) ))
247cdf0e10cSrcweir 		return com::sun::star::i18n::KNumberFormatUsage::FIXED_NUMBER;
248cdf0e10cSrcweir 	if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FRACTION_NUMBER" ) ))
249cdf0e10cSrcweir 		return com::sun::star::i18n::KNumberFormatUsage::FRACTION_NUMBER;
250cdf0e10cSrcweir 	if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "PERCENT_NUMBER" ) ))
251cdf0e10cSrcweir 		return  com::sun::star::i18n::KNumberFormatUsage::PERCENT_NUMBER;
252cdf0e10cSrcweir 	if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "CURRENCY" ) ))
253cdf0e10cSrcweir 		return com::sun::star::i18n::KNumberFormatUsage::CURRENCY;
254cdf0e10cSrcweir 	if(formatUsage.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "SCIENTIFIC_NUMBER" ) ))
255cdf0e10cSrcweir 		return com::sun::star::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 	return 0;
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 
261cdf0e10cSrcweir void
createLocaleDataObject()262cdf0e10cSrcweir NumberFormatCodeMapper::createLocaleDataObject() {
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 	if(xlocaleData.is())
265cdf0e10cSrcweir 		return;
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 	::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface >
268cdf0e10cSrcweir 		xI = xMSF->createInstance(
269cdf0e10cSrcweir 		::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.i18n.LocaleData" ) ));
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 	if ( xI.is() ) {
272cdf0e10cSrcweir 		::com::sun::star::uno::Any x = xI->queryInterface( ::getCppuType((const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XLocaleData >*)0) );
273cdf0e10cSrcweir 			x >>= xlocaleData;
274cdf0e10cSrcweir 	}
275cdf0e10cSrcweir }
276cdf0e10cSrcweir 
277cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getImplementationName(void)278cdf0e10cSrcweir NumberFormatCodeMapper::getImplementationName(void)
279cdf0e10cSrcweir                 throw( ::com::sun::star::uno::RuntimeException )
280cdf0e10cSrcweir {
281cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii("com.sun.star.i18n.NumberFormatCodeMapper");
282cdf0e10cSrcweir }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir const sal_Char cNumFormat[] = "com.sun.star.i18n.NumberFormatMapper";
285cdf0e10cSrcweir 
286cdf0e10cSrcweir sal_Bool SAL_CALL
supportsService(const rtl::OUString & rServiceName)287cdf0e10cSrcweir NumberFormatCodeMapper::supportsService(const rtl::OUString& rServiceName)
288cdf0e10cSrcweir                 throw( ::com::sun::star::uno::RuntimeException )
289cdf0e10cSrcweir {
290cdf0e10cSrcweir     return !rServiceName.compareToAscii(cNumFormat);
291cdf0e10cSrcweir }
292cdf0e10cSrcweir 
293cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
getSupportedServiceNames(void)294cdf0e10cSrcweir NumberFormatCodeMapper::getSupportedServiceNames(void) throw( ::com::sun::star::uno::RuntimeException )
295cdf0e10cSrcweir {
296cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::rtl::OUString > aRet(1);
297cdf0e10cSrcweir     aRet[0] = ::rtl::OUString::createFromAscii(cNumFormat);
298cdf0e10cSrcweir     return aRet;
299cdf0e10cSrcweir }
300cdf0e10cSrcweir 
301