summaryrefslogtreecommitdiffstats
path: root/svtools/source/table/cellvalueconversion.cxx
blob: 53177af0d88f4157b56a5b875f643db1bbdfd567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "cellvalueconversion.hxx"

#include <com/sun/star/util/NumberFormatsSupplier.hpp>
#include <com/sun/star/util/NumberFormatter.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/util/Date.hpp>
#include <com/sun/star/util/Time.hpp>
#include <com/sun/star/util/DateTime.hpp>
#include <com/sun/star/util/XNumberFormatTypes.hpp>
#include <com/sun/star/util/NumberFormat.hpp>
#include <sal/log.hxx>
#include <tools/date.hxx>
#include <tools/time.hxx>
#include <tools/diagnose_ex.h>
#include <tools/long.hxx>
#include <unotools/syslocale.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <comphelper/processfactory.hxx>

#include <limits>
#include <memory>

namespace svt
{
using namespace ::com::sun::star::uno;
using ::com::sun::star::util::XNumberFormatter;
using ::com::sun::star::util::NumberFormatter;
using ::com::sun::star::util::XNumberFormatsSupplier;
using ::com::sun::star::util::NumberFormatsSupplier;
using ::com::sun::star::beans::XPropertySet;
using ::com::sun::star::lang::Locale;
using ::com::sun::star::util::DateTime;
using ::com::sun::star::util::XNumberFormatTypes;

namespace NumberFormat = ::com::sun::star::util::NumberFormat;

//= helper

namespace
{
double lcl_convertDateToDays(sal_uInt16 const i_day, sal_uInt16 const i_month,
                             sal_Int16 const i_year)
{
    tools::Long const nNullDateDays = ::Date::DateToDays(1, 1, 1900);
    tools::Long const nValueDateDays = ::Date::DateToDays(i_day, i_month, i_year);

    return nValueDateDays - nNullDateDays;
}

double lcl_convertTimeToDays(tools::Long const i_hours, tools::Long const i_minutes,
                             tools::Long const i_seconds, tools::Long const i_100thSeconds)
{
    return tools::Time(i_hours, i_minutes, i_seconds, i_100thSeconds).GetTimeInDays();
}
}

//= StandardFormatNormalizer

StandardFormatNormalizer::StandardFormatNormalizer(Reference<XNumberFormatter> const& i_formatter,
                                                   ::sal_Int32 const i_numberFormatType)
    : m_nFormatKey(0)
{
    try
    {
        ENSURE_OR_THROW(i_formatter.is(), "StandardFormatNormalizer: no formatter!");
        Reference<XNumberFormatsSupplier> const xSupplier(i_formatter->getNumberFormatsSupplier(),
                                                          UNO_SET_THROW);
        Reference<XNumberFormatTypes> const xTypes(xSupplier->getNumberFormats(), UNO_QUERY_THROW);
        m_nFormatKey = xTypes->getStandardFormat(i_numberFormatType,
                                                 SvtSysLocale().GetLanguageTag().getLocale());
    }
    catch (const Exception&)
    {
        DBG_UNHANDLED_EXCEPTION("svtools.table");
    }
}

//= DoubleNormalization

namespace
{
class DoubleNormalization : public StandardFormatNormalizer
{
public:
    explicit DoubleNormalization(Reference<XNumberFormatter> const& i_formatter)
        : StandardFormatNormalizer(i_formatter, NumberFormat::NUMBER)
    {
    }

    virtual double convertToDouble(Any const& i_value) const override
    {
        double returnValue = std::numeric_limits<double>::quiet_NaN();
        OSL_VERIFY(i_value >>= returnValue);
        return returnValue;
    }
};

//= IntegerNormalization

class IntegerNormalization : public StandardFormatNormalizer
{
public:
    explicit IntegerNormalization(Reference<XNumberFormatter> const& i_formatter)
        : StandardFormatNormalizer(i_formatter, NumberFormat::NUMBER)
    {
    }

    virtual double convertToDouble(Any const& i_value) const override
    {
        sal_Int64 value(0);
        OSL_VERIFY(i_value >>= value);
        return value;
    }
};

//= BooleanNormalization

class BooleanNormalization : public StandardFormatNormalizer
{
public:
    explicit BooleanNormalization(Reference<XNumberFormatter> const& i_formatter)
        : StandardFormatNormalizer(i_formatter, NumberFormat::LOGICAL)
    {
    }

    virtual double convertToDouble(Any const& i_value) const override
    {
        bool value(false);
        OSL_VERIFY(i_value >>= value);
        return value ? 1 : 0;
    }
};

//= DateTimeNormalization

class DateTimeNormalization : public StandardFormatNormalizer
{
public:
    explicit DateTimeNormalization(Reference<XNumberFormatter> const& i_formatter)
        : StandardFormatNormalizer(i_formatter, NumberFormat::DATETIME)
    {
    }

    virtual double convertToDouble(Any const& i_value) const override
    {
        double returnValue = std::numeric_limits<double>::quiet_NaN();

        // extract actual UNO value
        DateTime aDateTimeValue;
        ENSURE_OR_RETURN(i_value >>= aDateTimeValue, "allowed for DateTime values only",
                         returnValue);

        // date part
        returnValue
            = lcl_convertDateToDays(aDateTimeValue.Day, aDateTimeValue.Month, aDateTimeValue.Year);

        // time part
        returnValue += lcl_convertTimeToDays(aDateTimeValue.Hours, aDateTimeValue.Minutes,
                                             aDateTimeValue.Seconds, aDateTimeValue.NanoSeconds);

        // done
        return returnValue;
    }
};

//= DateNormalization

class DateNormalization : public StandardFormatNormalizer
{
public:
    explicit DateNormalization(Reference<XNumberFormatter> const& i_formatter)
        : StandardFormatNormalizer(i_formatter, NumberFormat::DATE)
    {
    }

    virtual double convertToDouble(Any const& i_value) const override
    {
        double returnValue = std::numeric_limits<double>::quiet_NaN();

        // extract
        css::util::Date aDateValue;
        ENSURE_OR_RETURN(i_value >>= aDateValue, "allowed for Date values only", returnValue);

        // convert
        returnValue = lcl_convertDateToDays(aDateValue.Day, aDateValue.Month, aDateValue.Year);

        // done
        return returnValue;
    }
};

//= TimeNormalization

class TimeNormalization : public StandardFormatNormalizer
{
public:
    explicit TimeNormalization(Reference<XNumberFormatter> const& i_formatter)
        : StandardFormatNormalizer(i_formatter, NumberFormat::TIME)
    {
    }

    virtual double convertToDouble(Any const& i_value) const override
    {
        double returnValue = std::numeric_limits<double>::quiet_NaN();

        // extract
        css::util::Time aTimeValue;
        ENSURE_OR_RETURN(i_value >>= aTimeValue, "allowed for tools::Time values only",
                         returnValue);

        // convert
        returnValue += lcl_convertTimeToDays(aTimeValue.Hours, aTimeValue.Minutes,
                                             aTimeValue.Seconds, aTimeValue.NanoSeconds);

        // done
        return returnValue;
    }
};
}

//= operations

bool CellValueConversion::ensureNumberFormatter()
{
    if (bAttemptedFormatterCreation)
        return xNumberFormatter.is();
    bAttemptedFormatterCreation = true;

    try
    {
        Reference<XComponentContext> xContext = ::comphelper::getProcessComponentContext();
        // a number formatter
        Reference<XNumberFormatter> const xFormatter(NumberFormatter::create(xContext),
                                                     UNO_QUERY_THROW);

        // a supplier of number formats
        Locale aLocale = SvtSysLocale().GetLanguageTag().getLocale();

        Reference<XNumberFormatsSupplier> const xSupplier
            = NumberFormatsSupplier::createWithLocale(xContext, aLocale);

        // ensure a NullDate we will assume later on
        css::util::Date const aNullDate(1, 1, 1900);
        Reference<XPropertySet> const xFormatSettings(xSupplier->getNumberFormatSettings(),
                                                      UNO_SET_THROW);
        xFormatSettings->setPropertyValue("NullDate", Any(aNullDate));

        // knit
        xFormatter->attachNumberFormatsSupplier(xSupplier);

        // done
        xNumberFormatter = xFormatter;
    }
    catch (const Exception&)
    {
        DBG_UNHANDLED_EXCEPTION("svtools.table");
    }

    return xNumberFormatter.is();
}

bool CellValueConversion::getValueNormalizer(Type const& i_valueType,
                                             std::shared_ptr<StandardFormatNormalizer>& o_formatter)
{
    auto pos = aNormalizers.find(i_valueType.getTypeName());
    if (pos == aNormalizers.end())
    {
        // never encountered this type before
        o_formatter.reset();

        OUString const sTypeName(i_valueType.getTypeName());
        TypeClass const eTypeClass = i_valueType.getTypeClass();

        if (sTypeName == ::cppu::UnoType<DateTime>::get().getTypeName())
        {
            o_formatter = std::make_shared<DateTimeNormalization>(xNumberFormatter);
        }
        else if (sTypeName == ::cppu::UnoType<css::util::Date>::get().getTypeName())
        {
            o_formatter = std::make_shared<DateNormalization>(xNumberFormatter);
        }
        else if (sTypeName == ::cppu::UnoType<css::util::Time>::get().getTypeName())
        {
            o_formatter = std::make_shared<TimeNormalization>(xNumberFormatter);
        }
        else if (sTypeName == ::cppu::UnoType<sal_Bool>::get().getTypeName())
        {
            o_formatter = std::make_shared<BooleanNormalization>(xNumberFormatter);
        }
        else if (sTypeName == ::cppu::UnoType<double>::get().getTypeName()
                 || sTypeName == ::cppu::UnoType<float>::get().getTypeName())
        {
            o_formatter = std::make_shared<DoubleNormalization>(xNumberFormatter);
        }
        else if ((eTypeClass == TypeClass_BYTE) || (eTypeClass == TypeClass_SHORT)
                 || (eTypeClass == TypeClass_UNSIGNED_SHORT) || (eTypeClass == TypeClass_LONG)
                 || (eTypeClass == TypeClass_UNSIGNED_LONG) || (eTypeClass == TypeClass_HYPER))
        {
            o_formatter = std::make_shared<IntegerNormalization>(xNumberFormatter);
        }
        else
        {
            SAL_WARN("svtools.table", "unsupported type '" << sTypeName << "'!");
        }
        aNormalizers[sTypeName] = o_formatter;
    }
    else
        o_formatter = pos->second;

    return bool(o_formatter);
}

//= CellValueConversion

CellValueConversion::CellValueConversion()
    : xNumberFormatter()
    , bAttemptedFormatterCreation(false)
    , aNormalizers()
{
}

CellValueConversion::~CellValueConversion() {}

OUString CellValueConversion::convertToString(const Any& i_value)
{
    OUString sStringValue;
    if (!i_value.hasValue())
        return sStringValue;

    if (!(i_value >>= sStringValue))
    {
        if (ensureNumberFormatter())
        {
            std::shared_ptr<StandardFormatNormalizer> pNormalizer;
            if (getValueNormalizer(i_value.getValueType(), pNormalizer))
            {
                try
                {
                    double const formatterCompliantValue = pNormalizer->convertToDouble(i_value);
                    sal_Int32 const formatKey = pNormalizer->getFormatKey();
                    sStringValue = xNumberFormatter->convertNumberToString(formatKey,
                                                                           formatterCompliantValue);
                }
                catch (const Exception&)
                {
                    DBG_UNHANDLED_EXCEPTION("svtools.table");
                }
            }
        }
    }

    return sStringValue;
}

} // namespace svt

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */