Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / i18npool / source / collator / collatorImpl.cxx
blob9d779be08bd6dca09a40a2f1ac6ad7e8de7eda3b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <collatorImpl.hxx>
21 #include <localedata.hxx>
22 #include <com/sun/star/i18n/CollatorOptions.hpp>
23 #include <com/sun/star/i18n/LocaleData2.hpp>
24 #include <rtl/ustrbuf.hxx>
25 #include <cppuhelper/supportsservice.hxx>
27 using namespace com::sun::star;
28 using namespace com::sun::star::i18n;
29 using namespace com::sun::star::lang;
30 using namespace com::sun::star::uno;
32 namespace i18npool {
34 CollatorImpl::CollatorImpl( const Reference < XComponentContext >& rxContext ) : m_xContext(rxContext)
36 mxLocaleData.set( LocaleData2::create(rxContext) );
37 cachedItem = nullptr;
40 CollatorImpl::~CollatorImpl()
44 sal_Int32 SAL_CALL
45 CollatorImpl::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
46 const OUString& str2, sal_Int32 off2, sal_Int32 len2)
48 if (cachedItem)
49 return cachedItem->xC->compareSubstring(str1, off1, len1, str2, off2, len2);
51 sal_Unicode *unistr1 = const_cast<sal_Unicode*>(str1.getStr()) + off1;
52 sal_Unicode *unistr2 = const_cast<sal_Unicode*>(str2.getStr()) + off2;
53 for (int i = 0; i < len1 && i < len2; i++)
54 if (unistr1[i] != unistr2[i])
55 return unistr1[i] < unistr2[i] ? -1 : 1;
56 return len1 == len2 ? 0 : (len1 < len2 ? -1 : 1);
59 sal_Int32 SAL_CALL
60 CollatorImpl::compareString( const OUString& in_str1, const OUString& in_str2)
62 if (cachedItem)
63 return cachedItem->xC->compareString(in_str1, in_str2);
65 return CollatorImpl::compareSubstring(in_str1, 0, in_str1.getLength(), in_str2, 0, in_str2.getLength());
69 sal_Int32 SAL_CALL
70 CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions)
72 const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
73 for (sal_Int32 i = 0; i < imp.getLength(); i++)
74 if (imp[i].isDefault)
75 return loadCollatorAlgorithm(imp[i].unoID, rLocale, collatorOptions);
77 throw RuntimeException(); // not default is defined
78 //return 0;
81 sal_Int32 SAL_CALL
82 CollatorImpl::loadCollatorAlgorithm(const OUString& impl, const lang::Locale& rLocale, sal_Int32 collatorOptions)
84 if (! cachedItem || ! cachedItem->equals(rLocale, impl))
85 loadCachedCollator(rLocale, impl);
87 if (!cachedItem)
88 throw RuntimeException(); // impl could not be loaded
89 cachedItem->xC->loadCollatorAlgorithm(cachedItem->algorithm, nLocale = rLocale, collatorOptions);
91 return 0;
94 void SAL_CALL
95 CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString& impl, const lang::Locale& rLocale,
96 const Sequence< sal_Int32 >& collatorOptions)
98 sal_Int32 options = 0;
99 for (sal_Int32 i = 0; i < collatorOptions.getLength(); i++)
100 options |= collatorOptions[i];
101 loadCollatorAlgorithm(impl, rLocale, options);
104 Sequence< OUString > SAL_CALL
105 CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale )
107 nLocale = rLocale;
108 const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
109 Sequence< OUString > list(imp.getLength());
111 for (sal_Int32 i = 0; i < imp.getLength(); i++) {
112 //if the current algorithm is default and the position is not on the first one, then switch
113 if (imp[i].isDefault && i) {
114 list[i] = list[0];
115 list[0] = imp[i].unoID;
117 else
118 list[i] = imp[i].unoID;
120 return list;
123 Sequence< sal_Int32 > SAL_CALL
124 CollatorImpl::listCollatorOptions( const OUString& /*collatorAlgorithmName*/ )
126 Sequence< OUString > option_str = mxLocaleData->getCollationOptions(nLocale);
127 Sequence< sal_Int32 > option_int(option_str.getLength());
129 for (sal_Int32 i = 0; i < option_str.getLength(); i++)
130 option_int[i] =
131 option_str[i] == "IGNORE_CASE" ? CollatorOptions::CollatorOptions_IGNORE_CASE :
132 option_str[i] == "IGNORE_KANA" ? CollatorOptions::CollatorOptions_IGNORE_KANA :
133 option_str[i] == "IGNORE_WIDTH" ? CollatorOptions::CollatorOptions_IGNORE_WIDTH : 0;
135 return option_int;
138 bool
139 CollatorImpl::createCollator(const lang::Locale& rLocale, const OUString& serviceName, const OUString& rSortAlgorithm)
141 for (size_t l = 0; l < lookupTable.size(); l++) {
142 cachedItem = lookupTable[l].get();
143 if (cachedItem->service == serviceName) {// cross locale sharing
144 lookupTable.emplace_back(new lookupTableItem(rLocale, rSortAlgorithm, serviceName, cachedItem->xC));
145 cachedItem = lookupTable.back().get();
146 return true;
149 Reference < XInterface > xI =
150 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Collator_" + serviceName, m_xContext );
152 if (xI.is()) {
153 Reference < XCollator > xC;
154 xC.set( xI, UNO_QUERY );
155 if (xC.is()) {
156 lookupTable.emplace_back(new lookupTableItem(rLocale, rSortAlgorithm, serviceName, xC));
157 cachedItem = lookupTable.back().get();
158 return true;
161 return false;
164 void
165 CollatorImpl::loadCachedCollator(const lang::Locale& rLocale, const OUString& rSortAlgorithm)
167 for (auto& i : lookupTable) {
168 cachedItem = i.get();
169 if (cachedItem->equals(rLocale, rSortAlgorithm)) {
170 return;
174 bool bLoaded = false;
175 if (!rSortAlgorithm.isEmpty())
177 // Load service with name <base>_<lang>_<country>_<algorithm> or
178 // <base>_<bcp47>_<algorithm> and fallbacks.
179 bLoaded = createCollator( rLocale,
180 LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + rSortAlgorithm, rSortAlgorithm);
181 if (!bLoaded)
183 ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
184 for (auto const& fallback : aFallbacks)
186 bLoaded = createCollator( rLocale, fallback + "_" + rSortAlgorithm, rSortAlgorithm);
187 if (bLoaded)
188 break;
190 if (!bLoaded)
192 // load service with name <base>_<algorithm>
193 bLoaded = createCollator( rLocale, rSortAlgorithm, rSortAlgorithm);
197 if (!bLoaded)
199 // load default service with name <base>_Unicode
200 bLoaded = createCollator( rLocale, "Unicode", rSortAlgorithm);
201 if (!bLoaded)
203 cachedItem = nullptr;
204 throw RuntimeException(); // could not load any service
209 OUString SAL_CALL CollatorImpl::getImplementationName()
211 return OUString("com.sun.star.i18n.Collator");
214 sal_Bool SAL_CALL CollatorImpl::supportsService(const OUString& rServiceName)
216 return cppu::supportsService(this, rServiceName);
219 Sequence< OUString > SAL_CALL
220 CollatorImpl::getSupportedServiceNames()
222 Sequence< OUString > aRet { "com.sun.star.i18n.Collator" };
223 return aRet;
228 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
229 com_sun_star_i18n_Collator_get_implementation(
230 css::uno::XComponentContext *context,
231 css::uno::Sequence<css::uno::Any> const &)
233 return cppu::acquire(new i18npool::CollatorImpl(context));
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */