LibreOffice Module toolkit (master) 1
cellvalueconversion.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
21
22#include <com/sun/star/util/NumberFormatsSupplier.hpp>
23#include <com/sun/star/util/NumberFormatter.hpp>
24#include <com/sun/star/beans/XPropertySet.hpp>
25#include <com/sun/star/util/Date.hpp>
26#include <com/sun/star/util/Time.hpp>
27#include <com/sun/star/util/DateTime.hpp>
28#include <com/sun/star/util/XNumberFormatTypes.hpp>
29#include <com/sun/star/util/NumberFormat.hpp>
30#include <sal/log.hxx>
31#include <tools/date.hxx>
32#include <tools/time.hxx>
34#include <tools/long.hxx>
38
39#include <limits>
40#include <memory>
41
42namespace svt
43{
44using namespace ::com::sun::star::uno;
45using ::com::sun::star::util::XNumberFormatter;
46using ::com::sun::star::util::NumberFormatter;
47using ::com::sun::star::util::XNumberFormatsSupplier;
48using ::com::sun::star::util::NumberFormatsSupplier;
49using ::com::sun::star::beans::XPropertySet;
50using ::com::sun::star::lang::Locale;
51using ::com::sun::star::util::DateTime;
52using ::com::sun::star::util::XNumberFormatTypes;
53
54namespace NumberFormat = ::com::sun::star::util::NumberFormat;
55
56//= helper
57
58namespace
59{
60double lcl_convertDateToDays(sal_uInt16 const i_day, sal_uInt16 const i_month,
61 sal_Int16 const i_year)
62{
64 tools::Long const nValueDateDays = ::Date::DateToDays(i_day, i_month, i_year);
65
66 return nValueDateDays - nNullDateDays;
67}
68
69double lcl_convertTimeToDays(tools::Long const i_hours, tools::Long const i_minutes,
70 tools::Long const i_seconds, tools::Long const i_100thSeconds)
71{
72 return tools::Time(i_hours, i_minutes, i_seconds, i_100thSeconds).GetTimeInDays();
73}
74}
75
76//= StandardFormatNormalizer
77
78StandardFormatNormalizer::StandardFormatNormalizer(Reference<XNumberFormatter> const& i_formatter,
79 ::sal_Int32 const i_numberFormatType)
80 : m_nFormatKey(0)
81{
82 try
83 {
84 ENSURE_OR_THROW(i_formatter.is(), "StandardFormatNormalizer: no formatter!");
85 Reference<XNumberFormatsSupplier> const xSupplier(i_formatter->getNumberFormatsSupplier(),
86 UNO_SET_THROW);
87 Reference<XNumberFormatTypes> const xTypes(xSupplier->getNumberFormats(), UNO_QUERY_THROW);
88 m_nFormatKey = xTypes->getStandardFormat(i_numberFormatType,
89 SvtSysLocale().GetLanguageTag().getLocale());
90 }
91 catch (const Exception&)
92 {
93 DBG_UNHANDLED_EXCEPTION("svtools.table");
94 }
95}
96
97//= DoubleNormalization
98
99namespace
100{
101class DoubleNormalization : public StandardFormatNormalizer
102{
103public:
104 explicit DoubleNormalization(Reference<XNumberFormatter> const& i_formatter)
105 : StandardFormatNormalizer(i_formatter, NumberFormat::NUMBER)
106 {
107 }
108
109 virtual double convertToDouble(Any const& i_value) const override
110 {
111 double returnValue = std::numeric_limits<double>::quiet_NaN();
112 OSL_VERIFY(i_value >>= returnValue);
113 return returnValue;
114 }
115};
116
117//= IntegerNormalization
118
119class IntegerNormalization : public StandardFormatNormalizer
120{
121public:
122 explicit IntegerNormalization(Reference<XNumberFormatter> const& i_formatter)
123 : StandardFormatNormalizer(i_formatter, NumberFormat::NUMBER)
124 {
125 }
126
127 virtual double convertToDouble(Any const& i_value) const override
128 {
129 sal_Int64 value(0);
130 OSL_VERIFY(i_value >>= value);
131 return value;
132 }
133};
134
135//= BooleanNormalization
136
137class BooleanNormalization : public StandardFormatNormalizer
138{
139public:
140 explicit BooleanNormalization(Reference<XNumberFormatter> const& i_formatter)
141 : StandardFormatNormalizer(i_formatter, NumberFormat::LOGICAL)
142 {
143 }
144
145 virtual double convertToDouble(Any const& i_value) const override
146 {
147 bool value(false);
148 OSL_VERIFY(i_value >>= value);
149 return value ? 1 : 0;
150 }
151};
152
153//= DateTimeNormalization
154
155class DateTimeNormalization : public StandardFormatNormalizer
156{
157public:
158 explicit DateTimeNormalization(Reference<XNumberFormatter> const& i_formatter)
159 : StandardFormatNormalizer(i_formatter, NumberFormat::DATETIME)
160 {
161 }
162
163 virtual double convertToDouble(Any const& i_value) const override
164 {
165 double returnValue = std::numeric_limits<double>::quiet_NaN();
166
167 // extract actual UNO value
168 DateTime aDateTimeValue;
169 ENSURE_OR_RETURN(i_value >>= aDateTimeValue, "allowed for DateTime values only",
170 returnValue);
171
172 // date part
173 returnValue
174 = lcl_convertDateToDays(aDateTimeValue.Day, aDateTimeValue.Month, aDateTimeValue.Year);
175
176 // time part
177 returnValue += lcl_convertTimeToDays(aDateTimeValue.Hours, aDateTimeValue.Minutes,
178 aDateTimeValue.Seconds, aDateTimeValue.NanoSeconds);
179
180 // done
181 return returnValue;
182 }
183};
184
185//= DateNormalization
186
187class DateNormalization : public StandardFormatNormalizer
188{
189public:
190 explicit DateNormalization(Reference<XNumberFormatter> const& i_formatter)
191 : StandardFormatNormalizer(i_formatter, NumberFormat::DATE)
192 {
193 }
194
195 virtual double convertToDouble(Any const& i_value) const override
196 {
197 double returnValue = std::numeric_limits<double>::quiet_NaN();
198
199 // extract
200 css::util::Date aDateValue;
201 ENSURE_OR_RETURN(i_value >>= aDateValue, "allowed for Date values only", returnValue);
202
203 // convert
204 returnValue = lcl_convertDateToDays(aDateValue.Day, aDateValue.Month, aDateValue.Year);
205
206 // done
207 return returnValue;
208 }
209};
210
211//= TimeNormalization
212
213class TimeNormalization : public StandardFormatNormalizer
214{
215public:
216 explicit TimeNormalization(Reference<XNumberFormatter> const& i_formatter)
217 : StandardFormatNormalizer(i_formatter, NumberFormat::TIME)
218 {
219 }
220
221 virtual double convertToDouble(Any const& i_value) const override
222 {
223 double returnValue = std::numeric_limits<double>::quiet_NaN();
224
225 // extract
226 css::util::Time aTimeValue;
227 ENSURE_OR_RETURN(i_value >>= aTimeValue, "allowed for tools::Time values only",
228 returnValue);
229
230 // convert
231 returnValue += lcl_convertTimeToDays(aTimeValue.Hours, aTimeValue.Minutes,
232 aTimeValue.Seconds, aTimeValue.NanoSeconds);
233
234 // done
235 return returnValue;
236 }
237};
238}
239
240//= operations
241
243{
245 return xNumberFormatter.is();
247
248 try
249 {
250 Reference<XComponentContext> xContext = ::comphelper::getProcessComponentContext();
251 // a number formatter
252 Reference<XNumberFormatter> const xFormatter(NumberFormatter::create(xContext),
253 UNO_QUERY_THROW);
254
255 // a supplier of number formats
257
258 Reference<XNumberFormatsSupplier> const xSupplier
259 = NumberFormatsSupplier::createWithLocale(xContext, aLocale);
260
261 // ensure a NullDate we will assume later on
262 css::util::Date const aNullDate(1, 1, 1900);
263 Reference<XPropertySet> const xFormatSettings(xSupplier->getNumberFormatSettings(),
264 UNO_SET_THROW);
265 xFormatSettings->setPropertyValue("NullDate", Any(aNullDate));
266
267 // knit
268 xFormatter->attachNumberFormatsSupplier(xSupplier);
269
270 // done
271 xNumberFormatter = xFormatter;
272 }
273 catch (const Exception&)
274 {
275 DBG_UNHANDLED_EXCEPTION("svtools.table");
276 }
277
278 return xNumberFormatter.is();
279}
280
282 std::shared_ptr<StandardFormatNormalizer>& o_formatter)
283{
284 auto pos = aNormalizers.find(i_valueType.getTypeName());
285 if (pos == aNormalizers.end())
286 {
287 // never encountered this type before
288 o_formatter.reset();
289
290 OUString const sTypeName(i_valueType.getTypeName());
291 TypeClass const eTypeClass = i_valueType.getTypeClass();
292
293 if (sTypeName == ::cppu::UnoType<DateTime>::get().getTypeName())
294 {
295 o_formatter = std::make_shared<DateTimeNormalization>(xNumberFormatter);
296 }
297 else if (sTypeName == ::cppu::UnoType<css::util::Date>::get().getTypeName())
298 {
299 o_formatter = std::make_shared<DateNormalization>(xNumberFormatter);
300 }
301 else if (sTypeName == ::cppu::UnoType<css::util::Time>::get().getTypeName())
302 {
303 o_formatter = std::make_shared<TimeNormalization>(xNumberFormatter);
304 }
305 else if (sTypeName == ::cppu::UnoType<sal_Bool>::get().getTypeName())
306 {
307 o_formatter = std::make_shared<BooleanNormalization>(xNumberFormatter);
308 }
309 else if (sTypeName == ::cppu::UnoType<double>::get().getTypeName()
310 || sTypeName == ::cppu::UnoType<float>::get().getTypeName())
311 {
312 o_formatter = std::make_shared<DoubleNormalization>(xNumberFormatter);
313 }
314 else if ((eTypeClass == TypeClass_BYTE) || (eTypeClass == TypeClass_SHORT)
315 || (eTypeClass == TypeClass_UNSIGNED_SHORT) || (eTypeClass == TypeClass_LONG)
316 || (eTypeClass == TypeClass_UNSIGNED_LONG) || (eTypeClass == TypeClass_HYPER))
317 {
318 o_formatter = std::make_shared<IntegerNormalization>(xNumberFormatter);
319 }
320 else
321 {
322 SAL_WARN("svtools.table", "unsupported type '" << sTypeName << "'!");
323 }
324 aNormalizers[sTypeName] = o_formatter;
325 }
326 else
327 o_formatter = pos->second;
328
329 return bool(o_formatter);
330}
331
332//= CellValueConversion
333
335 : xNumberFormatter()
336 , bAttemptedFormatterCreation(false)
337 , aNormalizers()
338{
339}
340
342
343OUString CellValueConversion::convertToString(const Any& i_value)
344{
345 OUString sStringValue;
346 if (!i_value.hasValue())
347 return sStringValue;
348
349 if (!(i_value >>= sStringValue))
350 {
352 {
353 std::shared_ptr<StandardFormatNormalizer> pNormalizer;
354 if (getValueNormalizer(i_value.getValueType(), pNormalizer))
355 {
356 try
357 {
358 double const formatterCompliantValue = pNormalizer->convertToDouble(i_value);
359 sal_Int32 const formatKey = pNormalizer->getFormatKey();
360 sStringValue = xNumberFormatter->convertNumberToString(formatKey,
361 formatterCompliantValue);
362 }
363 catch (const Exception&)
364 {
365 DBG_UNHANDLED_EXCEPTION("svtools.table");
366 }
367 }
368 }
369 }
370
371 return sStringValue;
372}
373
374} // namespace svt
375
376/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static sal_Int32 DateToDays(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
const LanguageTag & GetLanguageTag() const
OUString convertToString(const css::uno::Any &i_cellValue)
bool getValueNormalizer(css::uno::Type const &i_valueType, std::shared_ptr< StandardFormatNormalizer > &o_formatter)
css::uno::Reference< css::util::XNumberFormatter > xNumberFormatter
StandardFormatNormalizer(css::uno::Reference< css::util::XNumberFormatter > const &i_formatter, ::sal_Int32 const i_numberFormatType)
double GetTimeInDays() const
Any value
#define ENSURE_OR_THROW(c, m)
#define ENSURE_OR_RETURN(c, m, r)
#define DBG_UNHANDLED_EXCEPTION(...)
#define SAL_WARN(area, stream)
@ Exception
const LanguageTag & getLocale()
constexpr sal_Int32 nNullDateDays
DATE
Type
rtl::OUString getTypeName(rtl::OUString const &rEnvDcp)
long Long
NUMBER
size_t pos