summaryrefslogtreecommitdiffstats
path: root/i18npool/source/numberformatcode/numberformatcode.cxx
blob: c20ed5b20edc2cd9f1b94d0bec3f9f49600f0fe3 (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
/* -*- 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 <numberformatcode.hxx>
#include <com/sun/star/i18n/KNumberFormatUsage.hpp>
#include <com/sun/star/i18n/KNumberFormatType.hpp>
#include <com/sun/star/i18n/LocaleData2.hpp>
#include <comphelper/sequence.hxx>
#include <cppuhelper/supportsservice.hxx>

NumberFormatCodeMapper::NumberFormatCodeMapper(
            const css::uno::Reference < css::uno::XComponentContext >& rxContext )
{
        m_xLocaleData.set( css::i18n::LocaleData2::create( rxContext ) );
}


NumberFormatCodeMapper::~NumberFormatCodeMapper()
{
}


css::i18n::NumberFormatCode SAL_CALL
NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, const css::lang::Locale& rLocale )
{

    OUString elementType = mapElementTypeShortToString(formatType);
    OUString elementUsage = mapElementUsageShortToString(formatUsage);

    std::scoped_lock g(maMutex);
    const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale );

    auto pFormat = std::find_if(aFormatSeq.begin(), aFormatSeq.end(),
        [&elementType, &elementUsage](const css::i18n::FormatElement& rFormat) {
            return rFormat.isDefault
                && rFormat.formatType == elementType
                && rFormat.formatUsage == elementUsage; });
    if (pFormat != aFormatSeq.end())
        return css::i18n::NumberFormatCode(formatType,
                                           formatUsage,
                                           pFormat->formatCode,
                                           pFormat->formatName,
                                           pFormat->formatKey,
                                           pFormat->formatIndex,
                                           true);
    css::i18n::NumberFormatCode defaultNumberFormatCode;
    return defaultNumberFormatCode;
}


css::i18n::NumberFormatCode SAL_CALL
NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const css::lang::Locale& rLocale )
{
    std::scoped_lock g(maMutex);
    const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale );

    auto pFormat = std::find_if(aFormatSeq.begin(), aFormatSeq.end(),
        [formatIndex](const css::i18n::FormatElement& rFormat) { return rFormat.formatIndex == formatIndex; });
    if (pFormat != aFormatSeq.end())
        return css::i18n::NumberFormatCode(mapElementTypeStringToShort(pFormat->formatType),
                                           mapElementUsageStringToShort(pFormat->formatUsage),
                                           pFormat->formatCode,
                                           pFormat->formatName,
                                           pFormat->formatKey,
                                           pFormat->formatIndex,
                                           pFormat->isDefault);
    css::i18n::NumberFormatCode defaultNumberFormatCode;
    return defaultNumberFormatCode;
}


css::uno::Sequence< css::i18n::NumberFormatCode > SAL_CALL
NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const css::lang::Locale& rLocale )
{
    std::scoped_lock g(maMutex);
    const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale );

    std::vector<css::i18n::NumberFormatCode> aVec;
    aVec.reserve(aFormatSeq.getLength());

    for (const auto& rFormat : aFormatSeq) {
        sal_Int16 elementUsage = mapElementUsageStringToShort(rFormat.formatUsage);
        if ( elementUsage == formatUsage ) {
            aVec.emplace_back(mapElementTypeStringToShort(rFormat.formatType),
                              formatUsage,
                              rFormat.formatCode,
                              rFormat.formatName,
                              rFormat.formatKey,
                              rFormat.formatIndex,
                              rFormat.isDefault);
        }
    }
    return comphelper::containerToSequence(aVec);
}


css::uno::Sequence< css::i18n::NumberFormatCode > SAL_CALL
NumberFormatCodeMapper::getAllFormatCodes( const css::lang::Locale& rLocale )
{
    std::scoped_lock g(maMutex);
    const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale );

    std::vector<css::i18n::NumberFormatCode> aVec;
    aVec.reserve(aFormatSeq.getLength());

    std::transform(aFormatSeq.begin(), aFormatSeq.end(), std::back_inserter(aVec),
        [](const css::i18n::FormatElement& rFormat) -> css::i18n::NumberFormatCode {
            return { mapElementTypeStringToShort(rFormat.formatType),
                     mapElementUsageStringToShort(rFormat.formatUsage),
                     rFormat.formatCode,
                     rFormat.formatName,
                     rFormat.formatKey,
                     rFormat.formatIndex,
                     rFormat.isDefault };
        });
    return comphelper::containerToSequence(aVec);
}


// --- private implementation -----------------------------------------

const css::uno::Sequence< css::i18n::FormatElement >& NumberFormatCodeMapper::getFormats( const css::lang::Locale& rLocale )
{
    /* Find the FormatElement Sequence in the cache */
    for (const FormatElementCacheItem& item : m_aFormatElementCache)
    {
        if ( item.first == rLocale )
            return item.second;
    }

    /* Not found; Get the FormatElement Sequence for the given Locale */
    css::uno::Sequence< css::i18n::FormatElement > aFormatElementSequence;
    if ( m_xLocaleData.is() )
        aFormatElementSequence = m_xLocaleData->getAllFormats( rLocale );

    /* Add the FormatElement Sequence to the cache */
    const int FORMATELEMENTCACHE_SIZE = 3;
    if ( m_aFormatElementCache.size() > FORMATELEMENTCACHE_SIZE )
        m_aFormatElementCache.pop_front();
    m_aFormatElementCache.emplace_back( rLocale, aFormatElementSequence );
    return m_aFormatElementCache.back().second;
}


OUString
NumberFormatCodeMapper::mapElementTypeShortToString(sal_Int16 formatType)
{

    switch ( formatType )
    {
        case css::i18n::KNumberFormatType::SHORT :
            return "short";
        case css::i18n::KNumberFormatType::MEDIUM :
            return "medium";
        case css::i18n::KNumberFormatType::LONG :
            return "long";
    }
    return OUString();
}

sal_Int16
NumberFormatCodeMapper::mapElementTypeStringToShort(std::u16string_view formatType)
{
    if ( formatType == u"short" )
        return css::i18n::KNumberFormatType::SHORT;
    if ( formatType == u"medium" )
        return css::i18n::KNumberFormatType::MEDIUM;
    if ( formatType == u"long" )
        return css::i18n::KNumberFormatType::LONG;

    return css::i18n::KNumberFormatType::SHORT;
}

OUString
NumberFormatCodeMapper::mapElementUsageShortToString(sal_Int16 formatUsage)
{
    switch ( formatUsage )
    {
        case css::i18n::KNumberFormatUsage::DATE :
            return "DATE";
        case css::i18n::KNumberFormatUsage::TIME :
            return "TIME";
        case css::i18n::KNumberFormatUsage::DATE_TIME :
            return "DATE_TIME";
        case css::i18n::KNumberFormatUsage::FIXED_NUMBER :
            return "FIXED_NUMBER";
        case css::i18n::KNumberFormatUsage::FRACTION_NUMBER :
            return "FRACTION_NUMBER";
        case css::i18n::KNumberFormatUsage::PERCENT_NUMBER :
            return "PERCENT_NUMBER";
        case css::i18n::KNumberFormatUsage::CURRENCY :
            return "CURRENCY";
        case css::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER :
            return "SCIENTIFIC_NUMBER";
    }
    return OUString();
}


sal_Int16
NumberFormatCodeMapper::mapElementUsageStringToShort(std::u16string_view formatUsage)
{
    if ( formatUsage == u"DATE" )
        return css::i18n::KNumberFormatUsage::DATE;
    if ( formatUsage == u"TIME" )
        return css::i18n::KNumberFormatUsage::TIME;
    if ( formatUsage == u"DATE_TIME" )
        return css::i18n::KNumberFormatUsage::DATE_TIME;
    if ( formatUsage == u"FIXED_NUMBER" )
        return css::i18n::KNumberFormatUsage::FIXED_NUMBER;
    if ( formatUsage == u"FRACTION_NUMBER" )
        return css::i18n::KNumberFormatUsage::FRACTION_NUMBER;
    if ( formatUsage == u"PERCENT_NUMBER" )
        return  css::i18n::KNumberFormatUsage::PERCENT_NUMBER;
    if ( formatUsage == u"CURRENCY" )
        return css::i18n::KNumberFormatUsage::CURRENCY;
    if ( formatUsage == u"SCIENTIFIC_NUMBER" )
        return css::i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER;

    return 0;
}


OUString SAL_CALL
NumberFormatCodeMapper::getImplementationName()
{
    return "com.sun.star.i18n.NumberFormatCodeMapper";
}

sal_Bool SAL_CALL NumberFormatCodeMapper::supportsService(const OUString& rServiceName)
{
    return cppu::supportsService(this, rServiceName);
}

css::uno::Sequence< OUString > SAL_CALL
NumberFormatCodeMapper::getSupportedServiceNames()
{
    return { "com.sun.star.i18n.NumberFormatMapper" };
}

extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_i18n_NumberFormatCodeMapper_get_implementation(
    css::uno::XComponentContext *context,
    css::uno::Sequence<css::uno::Any> const &)
{
    return cppu::acquire(new NumberFormatCodeMapper(context));
}

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