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