LibreOffice Module i18npool (master)  1
numberformatcode.cxx
Go to the documentation of this file.
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 #include <numberformatcode.hxx>
21 #include <com/sun/star/i18n/KNumberFormatUsage.hpp>
22 #include <com/sun/star/i18n/KNumberFormatType.hpp>
23 #include <com/sun/star/i18n/LocaleData2.hpp>
24 #include <comphelper/sequence.hxx>
26 
28  const css::uno::Reference < css::uno::XComponentContext >& rxContext )
29 {
30  m_xLocaleData.set( css::i18n::LocaleData2::create( rxContext ) );
31 }
32 
33 
35 {
36 }
37 
38 
39 css::i18n::NumberFormatCode SAL_CALL
40 NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, const css::lang::Locale& rLocale )
41 {
42 
43  OUString elementType = mapElementTypeShortToString(formatType);
44  OUString elementUsage = mapElementUsageShortToString(formatUsage);
45 
46  osl::MutexGuard g(maMutex);
47  const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale );
48 
49  auto pFormat = std::find_if(aFormatSeq.begin(), aFormatSeq.end(),
50  [&elementType, &elementUsage](const css::i18n::FormatElement& rFormat) {
51  return rFormat.isDefault
52  && rFormat.formatType == elementType
53  && rFormat.formatUsage == elementUsage; });
54  if (pFormat != aFormatSeq.end())
55  return css::i18n::NumberFormatCode(formatType,
56  formatUsage,
57  pFormat->formatCode,
58  pFormat->formatName,
59  pFormat->formatKey,
60  pFormat->formatIndex,
61  true);
62  css::i18n::NumberFormatCode defaultNumberFormatCode;
63  return defaultNumberFormatCode;
64 }
65 
66 
67 css::i18n::NumberFormatCode SAL_CALL
68 NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const css::lang::Locale& rLocale )
69 {
70  osl::MutexGuard g(maMutex);
71  const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale );
72 
73  auto pFormat = std::find_if(aFormatSeq.begin(), aFormatSeq.end(),
74  [formatIndex](const css::i18n::FormatElement& rFormat) { return rFormat.formatIndex == formatIndex; });
75  if (pFormat != aFormatSeq.end())
76  return css::i18n::NumberFormatCode(mapElementTypeStringToShort(pFormat->formatType),
77  mapElementUsageStringToShort(pFormat->formatUsage),
78  pFormat->formatCode,
79  pFormat->formatName,
80  pFormat->formatKey,
81  pFormat->formatIndex,
82  pFormat->isDefault);
83  css::i18n::NumberFormatCode defaultNumberFormatCode;
84  return defaultNumberFormatCode;
85 }
86 
87 
88 css::uno::Sequence< css::i18n::NumberFormatCode > SAL_CALL
89 NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const css::lang::Locale& rLocale )
90 {
91  osl::MutexGuard g(maMutex);
92  const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale );
93 
94  std::vector<css::i18n::NumberFormatCode> aVec;
95  aVec.reserve(aFormatSeq.getLength());
96 
97  for (const auto& rFormat : aFormatSeq) {
98  sal_Int16 elementUsage = mapElementUsageStringToShort(rFormat.formatUsage);
99  if ( elementUsage == formatUsage ) {
100  aVec.emplace_back(mapElementTypeStringToShort(rFormat.formatType),
101  formatUsage,
102  rFormat.formatCode,
103  rFormat.formatName,
104  rFormat.formatKey,
105  rFormat.formatIndex,
106  rFormat.isDefault);
107  }
108  }
109  return comphelper::containerToSequence(aVec);
110 }
111 
112 
113 css::uno::Sequence< css::i18n::NumberFormatCode > SAL_CALL
114 NumberFormatCodeMapper::getAllFormatCodes( const css::lang::Locale& rLocale )
115 {
116  osl::MutexGuard g(maMutex);
117  const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale );
118 
119  std::vector<css::i18n::NumberFormatCode> aVec;
120  aVec.reserve(aFormatSeq.getLength());
121 
122  std::transform(aFormatSeq.begin(), aFormatSeq.end(), std::back_inserter(aVec),
123  [](const css::i18n::FormatElement& rFormat) -> css::i18n::NumberFormatCode {
124  return { mapElementTypeStringToShort(rFormat.formatType),
125  mapElementUsageStringToShort(rFormat.formatUsage),
126  rFormat.formatCode,
127  rFormat.formatName,
128  rFormat.formatKey,
129  rFormat.formatIndex,
130  rFormat.isDefault };
131  });
132  return comphelper::containerToSequence(aVec);
133 }
134 
135 
136 // --- private implementation -----------------------------------------
137 
138 const css::uno::Sequence< css::i18n::FormatElement >& NumberFormatCodeMapper::getFormats( const css::lang::Locale& rLocale )
139 {
140  /* Find the FormatElement Sequence in the cache */
142  {
143  if ( item.first == rLocale )
144  return item.second;
145  }
146 
147  /* Not found; Get the FormatElement Sequence for the given Locale */
148  css::uno::Sequence< css::i18n::FormatElement > aFormatElementSequence;
149  if ( m_xLocaleData.is() )
150  aFormatElementSequence = m_xLocaleData->getAllFormats( rLocale );
151 
152  /* Add the FormatElement Sequence to the cache */
153  const int FORMATELEMENTCACHE_SIZE = 3;
154  if ( m_aFormatElementCache.size() > FORMATELEMENTCACHE_SIZE )
155  m_aFormatElementCache.pop_front();
156  m_aFormatElementCache.emplace_back( rLocale, aFormatElementSequence );
157  return m_aFormatElementCache.back().second;
158 }
159 
160 
161 OUString
163 {
164 
165  switch ( formatType )
166  {
167  case css::i18n::KNumberFormatType::SHORT :
168  return "short";
169  case css::i18n::KNumberFormatType::MEDIUM :
170  return "medium";
171  case css::i18n::KNumberFormatType::LONG :
172  return "long";
173  }
174  return OUString();
175 }
176 
177 sal_Int16
179 {
180  if ( formatType == u"short" )
181  return css::i18n::KNumberFormatType::SHORT;
182  if ( formatType == u"medium" )
183  return css::i18n::KNumberFormatType::MEDIUM;
184  if ( formatType == u"long" )
185  return css::i18n::KNumberFormatType::LONG;
186 
187  return css::i18n::KNumberFormatType::SHORT;
188 }
189 
190 OUString
192 {
193  switch ( formatUsage )
194  {
195  case css::i18n::KNumberFormatUsage::DATE :
196  return "DATE";
197  case css::i18n::KNumberFormatUsage::TIME :
198  return "TIME";
199  case css::i18n::KNumberFormatUsage::DATE_TIME :
200  return "DATE_TIME";
201  case css::i18n::KNumberFormatUsage::FIXED_NUMBER :
202  return "FIXED_NUMBER";
203  case css::i18n::KNumberFormatUsage::FRACTION_NUMBER :
204  return "FRACTION_NUMBER";
205  case css::i18n::KNumberFormatUsage::PERCENT_NUMBER :
206  return "PERCENT_NUMBER";
207  case css::i18n::KNumberFormatUsage::CURRENCY :
208  return "CURRENCY";
209  case css::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER :
210  return "SCIENTIFIC_NUMBER";
211  }
212  return OUString();
213 }
214 
215 
216 sal_Int16
218 {
219  if ( formatUsage == u"DATE" )
220  return css::i18n::KNumberFormatUsage::DATE;
221  if ( formatUsage == u"TIME" )
222  return css::i18n::KNumberFormatUsage::TIME;
223  if ( formatUsage == u"DATE_TIME" )
224  return css::i18n::KNumberFormatUsage::DATE_TIME;
225  if ( formatUsage == u"FIXED_NUMBER" )
226  return css::i18n::KNumberFormatUsage::FIXED_NUMBER;
227  if ( formatUsage == u"FRACTION_NUMBER" )
228  return css::i18n::KNumberFormatUsage::FRACTION_NUMBER;
229  if ( formatUsage == u"PERCENT_NUMBER" )
230  return css::i18n::KNumberFormatUsage::PERCENT_NUMBER;
231  if ( formatUsage == u"CURRENCY" )
232  return css::i18n::KNumberFormatUsage::CURRENCY;
233  if ( formatUsage == u"SCIENTIFIC_NUMBER" )
234  return css::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;
235 
236  return 0;
237 }
238 
239 
240 OUString SAL_CALL
242 {
243  return "com.sun.star.i18n.NumberFormatCodeMapper";
244 }
245 
246 sal_Bool SAL_CALL NumberFormatCodeMapper::supportsService(const OUString& rServiceName)
247 {
248  return cppu::supportsService(this, rServiceName);
249 }
250 
251 css::uno::Sequence< OUString > SAL_CALL
253 {
254  return { "com.sun.star.i18n.NumberFormatMapper" };
255 }
256 
257 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
259  css::uno::XComponentContext *context,
260  css::uno::Sequence<css::uno::Any> const &)
261 {
262  return cppu::acquire(new NumberFormatCodeMapper(context));
263 }
264 
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_i18n_NumberFormatCodeMapper_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
static OUString mapElementUsageShortToString(sal_Int16 formatUsage)
static OUString mapElementTypeShortToString(sal_Int16 formatType)
const css::uno::Sequence< css::i18n::FormatElement > & getFormats(const css::lang::Locale &rLocale)
virtual css::uno::Sequence< css::i18n::NumberFormatCode > SAL_CALL getAllFormatCodes(const css::lang::Locale &rLocale) override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual OUString SAL_CALL getImplementationName() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
static sal_Int16 mapElementTypeStringToShort(std::u16string_view formatType)
NumberFormatCodeMapper(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
float u
unsigned char sal_Bool
Type elementType(Type type)
std::pair< css::lang::Locale, css::uno::Sequence< css::i18n::FormatElement > > FormatElementCacheItem
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
std::deque< FormatElementCacheItem > m_aFormatElementCache
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
static sal_Int16 mapElementUsageStringToShort(std::u16string_view formatUsage)
css::uno::Reference< css::i18n::XLocaleData5 > m_xLocaleData
virtual css::uno::Sequence< css::i18n::NumberFormatCode > SAL_CALL getAllFormatCode(sal_Int16 nFormatUsage, const css::lang::Locale &rLocale) override
virtual ~NumberFormatCodeMapper() override
virtual css::i18n::NumberFormatCode SAL_CALL getDefault(sal_Int16 nFormatType, sal_Int16 nFormatUsage, const css::lang::Locale &rLocale) override
virtual css::i18n::NumberFormatCode SAL_CALL getFormatCode(sal_Int16 nFormatIndex, const css::lang::Locale &rLocale) override