LibreOffice Module i18npool (master) 1
collatorImpl.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 <collatorImpl.hxx>
21#include <localedata.hxx>
22#include <com/sun/star/i18n/CollatorOptions.hpp>
23#include <com/sun/star/i18n/LocaleData2.hpp>
25#include <numeric>
26
27using namespace com::sun::star;
28using namespace com::sun::star::i18n;
29using namespace com::sun::star::lang;
30using namespace com::sun::star::uno;
31
32namespace i18npool {
33
35{
36 mxLocaleData.set( LocaleData2::create(rxContext) );
37}
38
40{
41}
42
43sal_Int32 SAL_CALL
44CollatorImpl::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
45 const OUString& str2, sal_Int32 off2, sal_Int32 len2)
46{
47 if (cachedItem)
48 return cachedItem->xC->compareSubstring(str1, off1, len1, str2, off2, len2);
49
50 sal_Unicode *unistr1 = const_cast<sal_Unicode*>(str1.getStr()) + off1;
51 sal_Unicode *unistr2 = const_cast<sal_Unicode*>(str2.getStr()) + off2;
52 for (int i = 0; i < len1 && i < len2; i++)
53 if (unistr1[i] != unistr2[i])
54 return unistr1[i] < unistr2[i] ? -1 : 1;
55 return len1 == len2 ? 0 : (len1 < len2 ? -1 : 1);
56}
57
58sal_Int32 SAL_CALL
59CollatorImpl::compareString( const OUString& in_str1, const OUString& in_str2)
60{
61 if (cachedItem)
62 return cachedItem->xC->compareString(in_str1, in_str2);
63
64 return CollatorImpl::compareSubstring(in_str1, 0, in_str1.getLength(), in_str2, 0, in_str2.getLength());
65}
66
67
68sal_Int32 SAL_CALL
69CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions)
70{
71 const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
72 auto pImpl = std::find_if(imp.begin(), imp.end(),
73 [](const Implementation& rImp) { return rImp.isDefault; });
74 if (pImpl != imp.end())
75 return loadCollatorAlgorithm(pImpl->unoID, rLocale, collatorOptions);
76
77 throw RuntimeException(); // not default is defined
78 //return 0;
79}
80
81sal_Int32 SAL_CALL
82CollatorImpl::loadCollatorAlgorithm(const OUString& impl, const lang::Locale& rLocale, sal_Int32 collatorOptions)
83{
84 if (! cachedItem || ! cachedItem->equals(rLocale, impl))
85 loadCachedCollator(rLocale, impl);
86
87 if (!cachedItem)
88 throw RuntimeException(); // impl could not be loaded
89 nLocale = rLocale;
90 cachedItem->xC->loadCollatorAlgorithm(cachedItem->algorithm, nLocale, collatorOptions);
91
92 return 0;
93}
94
95void SAL_CALL
96CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString& impl, const lang::Locale& rLocale,
97 const Sequence< sal_Int32 >& collatorOptions)
98{
99 sal_Int32 options = std::accumulate(collatorOptions.begin(), collatorOptions.end(),
100 sal_Int32(0), [](sal_Int32 nSum, sal_Int32 nOpt) { return nSum | nOpt; });
101 loadCollatorAlgorithm(impl, rLocale, options);
102}
103
105CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale )
106{
107 nLocale = rLocale;
108 const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
109 Sequence< OUString > list(imp.getLength());
110 auto pBegin = list.getArray();
111 auto pId = pBegin;
112
113 for (const auto& rImpl : imp) {
114 *pId = rImpl.unoID;
115 //if the current algorithm is default and the position is not on the first one, then switch
116 if (rImpl.isDefault && pId != pBegin)
117 std::swap(*pBegin, *pId);
118 ++pId;
119 }
120 return list;
121}
122
124CollatorImpl::listCollatorOptions( const OUString& /*collatorAlgorithmName*/ )
125{
126 const Sequence< OUString > option_str = mxLocaleData->getCollationOptions(nLocale);
127 Sequence< sal_Int32 > option_int(option_str.getLength());
128
129 std::transform(option_str.begin(), option_str.end(), option_int.getArray(), [](const OUString& rOpt) {
130 return rOpt == "IGNORE_CASE" ? CollatorOptions::CollatorOptions_IGNORE_CASE :
131 rOpt == "IGNORE_KANA" ? CollatorOptions::CollatorOptions_IGNORE_KANA :
132 rOpt == "IGNORE_WIDTH" ? CollatorOptions::CollatorOptions_IGNORE_WIDTH : 0; });
133
134 return option_int;
135}
136
137bool
138CollatorImpl::createCollator(const lang::Locale& rLocale, const OUString& serviceName, const OUString& rSortAlgorithm)
139{
140 for (size_t l = 0; l < lookupTable.size(); l++) {
142 if (cachedItem->service == serviceName) {// cross locale sharing
143 lookupTable.emplace_back(rLocale, rSortAlgorithm, serviceName, cachedItem->xC);
144 cachedItem = lookupTable.back();
145 return true;
146 }
147 }
149 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Collator_" + serviceName, m_xContext );
150
151 if (xI.is()) {
153 xC.set( xI, UNO_QUERY );
154 if (xC.is()) {
155 lookupTable.emplace_back(rLocale, rSortAlgorithm, serviceName, xC);
156 cachedItem = lookupTable.back();
157 return true;
158 }
159 }
160 return false;
161}
162
163void
164CollatorImpl::loadCachedCollator(const lang::Locale& rLocale, const OUString& rSortAlgorithm)
165{
166 for (const auto& i : lookupTable) {
167 cachedItem = i;
168 if (cachedItem->equals(rLocale, rSortAlgorithm)) {
169 return;
170 }
171 }
172
173 bool bLoaded = false;
174 if (!rSortAlgorithm.isEmpty())
175 {
176 // Load service with name <base>_<lang>_<country>_<algorithm> or
177 // <base>_<bcp47>_<algorithm> and fallbacks.
178 bLoaded = createCollator( rLocale,
179 LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + rSortAlgorithm, rSortAlgorithm);
180 if (!bLoaded)
181 {
182 ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
183 for (auto const& fallback : aFallbacks)
184 {
185 bLoaded = createCollator( rLocale, fallback + "_" + rSortAlgorithm, rSortAlgorithm);
186 if (bLoaded)
187 break;
188 }
189 if (!bLoaded)
190 {
191 // load service with name <base>_<algorithm>
192 bLoaded = createCollator( rLocale, rSortAlgorithm, rSortAlgorithm);
193 }
194 }
195 }
196 if (!bLoaded)
197 {
198 // load default service with name <base>_Unicode
199 bLoaded = createCollator( rLocale, "Unicode", rSortAlgorithm);
200 if (!bLoaded)
201 {
202 cachedItem.reset();
203 throw RuntimeException(); // could not load any service
204 }
205 }
206}
207
209{
210 return "com.sun.star.i18n.Collator";
211}
212
213sal_Bool SAL_CALL CollatorImpl::supportsService(const OUString& rServiceName)
214{
215 return cppu::supportsService(this, rServiceName);
216}
217
220{
221 return { "com.sun.star.i18n.Collator" };
222}
223
224}
225
226extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
228 css::uno::XComponentContext *context,
229 css::uno::Sequence<css::uno::Any> const &)
230{
231 return cppu::acquire(new i18npool::CollatorImpl(context));
232}
233
234/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual ~CollatorImpl() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
css::uno::Reference< css::i18n::XLocaleData5 > mxLocaleData
virtual sal_Int32 SAL_CALL loadDefaultCollator(const css::lang::Locale &rLocale, sal_Int32 collatorOptions) override
virtual sal_Int32 SAL_CALL compareSubstring(const OUString &s1, sal_Int32 off1, sal_Int32 len1, const OUString &s2, sal_Int32 off2, sal_Int32 len2) override
virtual sal_Int32 SAL_CALL loadCollatorAlgorithm(const OUString &impl, const css::lang::Locale &rLocale, sal_Int32 collatorOptions) override
std::optional< lookupTableItem > cachedItem
virtual OUString SAL_CALL getImplementationName() override
css::lang::Locale nLocale
std::vector< lookupTableItem > lookupTable
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual css::uno::Sequence< sal_Int32 > SAL_CALL listCollatorOptions(const OUString &collatorAlgorithmName) override
virtual void SAL_CALL loadCollatorAlgorithmWithEndUserOption(const OUString &impl, const css::lang::Locale &rLocale, const css::uno::Sequence< sal_Int32 > &collatorOptions) override
void loadCachedCollator(const css::lang::Locale &rLocale, const OUString &rSortAlgorithm)
virtual css::uno::Sequence< OUString > SAL_CALL listCollatorAlgorithms(const css::lang::Locale &rLocale) override
bool createCollator(const css::lang::Locale &rLocale, const OUString &serviceName, const OUString &rSortAlgorithm)
CollatorImpl(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual sal_Int32 SAL_CALL compareString(const OUString &s1, const OUString &s2) override
static ::std::vector< OUString > getFallbackLocaleServiceNames(const css::lang::Locale &rLocale)
Generates fallback strings suitable as parts of service names, excluding the one obtained via getFirs...
static OUString getFirstLocaleServiceName(const css::lang::Locale &rLocale)
Generates a <Language>_<Country> or <Variant> (if Language=="qlt") string suitable as part of service...
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_i18n_Collator_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
Constant values shared between i18npool and, for example, the number formatter.
unsigned char sal_Bool
sal_uInt16 sal_Unicode