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