Bump version to 4.3-4
[LibreOffice.git] / i18npool / source / collator / collatorImpl.cxx
blobd11db2d6771e1243ad63e40b2ca100e629a35d86
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/LocaleData.hpp>
24 #include <rtl/ustrbuf.hxx>
25 #include <comphelper/processfactory.hxx>
26 #include <cppuhelper/supportsservice.hxx>
28 using namespace com::sun::star;
29 using namespace com::sun::star::lang;
30 using namespace com::sun::star::uno;
32 namespace com { namespace sun { namespace star { namespace i18n {
34 CollatorImpl::CollatorImpl( const Reference < XComponentContext >& rxContext ) : m_xContext(rxContext)
36 mxLocaleData.set( LocaleData::create(rxContext) );
37 cachedItem = NULL;
40 CollatorImpl::~CollatorImpl()
42 // Clear lookuptable
43 for (size_t l = 0; l < lookupTable.size(); l++)
44 delete lookupTable[l];
45 lookupTable.clear();
48 sal_Int32 SAL_CALL
49 CollatorImpl::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
50 const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(RuntimeException, std::exception)
52 if (cachedItem)
53 return cachedItem->xC->compareSubstring(str1, off1, len1, str2, off2, len2);
55 sal_Unicode *unistr1 = (sal_Unicode*) str1.getStr() + off1;
56 sal_Unicode *unistr2 = (sal_Unicode*) str2.getStr() + off2;
57 for (int i = 0; i < len1 && i < len2; i++)
58 if (unistr1[i] != unistr2[i])
59 return unistr1[i] < unistr2[i] ? -1 : 1;
60 return len1 == len2 ? 0 : (len1 < len2 ? -1 : 1);
63 sal_Int32 SAL_CALL
64 CollatorImpl::compareString( const OUString& in_str1, const OUString& in_str2) throw(RuntimeException, std::exception)
66 if (cachedItem)
67 return cachedItem->xC->compareString(in_str1, in_str2);
69 return CollatorImpl::compareSubstring(in_str1, 0, in_str1.getLength(), in_str2, 0, in_str2.getLength());
73 sal_Int32 SAL_CALL
74 CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions) throw(RuntimeException, std::exception)
76 const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
77 for (sal_Int16 i = 0; i < imp.getLength(); i++)
78 if (imp[i].isDefault)
79 return loadCollatorAlgorithm(imp[i].unoID, rLocale, collatorOptions);
81 throw RuntimeException(); // not default is defined
82 //return 0;
85 sal_Int32 SAL_CALL
86 CollatorImpl::loadCollatorAlgorithm(const OUString& impl, const lang::Locale& rLocale, sal_Int32 collatorOptions)
87 throw(RuntimeException, std::exception)
89 if (! cachedItem || ! cachedItem->equals(rLocale, impl))
90 loadCachedCollator(rLocale, impl);
92 if (cachedItem)
93 cachedItem->xC->loadCollatorAlgorithm(cachedItem->algorithm, nLocale = rLocale, collatorOptions);
94 else
95 throw RuntimeException(); // impl could not be loaded
97 return 0;
100 void SAL_CALL
101 CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString& impl, const lang::Locale& rLocale,
102 const Sequence< sal_Int32 >& collatorOptions) throw(RuntimeException, std::exception)
104 sal_Int32 options = 0;
105 for (sal_Int32 i = 0; i < collatorOptions.getLength(); i++)
106 options |= collatorOptions[i];
107 loadCollatorAlgorithm(impl, rLocale, options);
110 Sequence< OUString > SAL_CALL
111 CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale ) throw(RuntimeException, std::exception)
113 nLocale = rLocale;
114 const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
115 Sequence< OUString > list(imp.getLength());
117 for (sal_Int32 i = 0; i < imp.getLength(); i++) {
118 //if the current algorithm is default and the position is not on the first one, then switch
119 if (imp[i].isDefault && i) {
120 list[i] = list[0];
121 list[0] = imp[i].unoID;
123 else
124 list[i] = imp[i].unoID;
126 return list;
129 Sequence< sal_Int32 > SAL_CALL
130 CollatorImpl::listCollatorOptions( const OUString& /*collatorAlgorithmName*/ ) throw(RuntimeException, std::exception)
132 Sequence< OUString > option_str = mxLocaleData->getCollationOptions(nLocale);
133 Sequence< sal_Int32 > option_int(option_str.getLength());
135 for (sal_Int32 i = 0; i < option_str.getLength(); i++)
136 option_int[i] =
137 option_str[i] == "IGNORE_CASE" ? CollatorOptions::CollatorOptions_IGNORE_CASE :
138 option_str[i] == "IGNORE_KANA" ? CollatorOptions::CollatorOptions_IGNORE_KANA :
139 option_str[i] == "IGNORE_WIDTH" ? CollatorOptions::CollatorOptions_IGNORE_WIDTH : 0;
141 return option_int;
144 bool SAL_CALL
145 CollatorImpl::createCollator(const lang::Locale& rLocale, const OUString& serviceName, const OUString& rSortAlgorithm)
146 throw(RuntimeException)
148 for (size_t l = 0; l < lookupTable.size(); l++) {
149 cachedItem = lookupTable[l];
150 if (cachedItem->service.equals(serviceName)) {// cross locale sharing
151 lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, cachedItem->xC));
152 return true;
155 Reference < XInterface > xI =
156 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Collator_" + serviceName, m_xContext );
158 if (xI.is()) {
159 Reference < XCollator > xC;
160 xC.set( xI, UNO_QUERY );
161 if (xC.is()) {
162 lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, xC));
163 return true;
166 return false;
169 void SAL_CALL
170 CollatorImpl::loadCachedCollator(const lang::Locale& rLocale, const OUString& rSortAlgorithm)
171 throw(RuntimeException)
173 for (size_t i = 0; i < lookupTable.size(); i++) {
174 cachedItem = lookupTable[i];
175 if (cachedItem->equals(rLocale, rSortAlgorithm)) {
176 return;
180 bool bLoaded = false;
181 if (!rSortAlgorithm.isEmpty())
183 // Load service with name <base>_<lang>_<country>_<algorithm> or
184 // <base>_<bcp47>_<algorithm> and fallbacks.
185 bLoaded = createCollator( rLocale,
186 LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + rSortAlgorithm, rSortAlgorithm);
187 if (!bLoaded)
189 ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
190 for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
192 bLoaded = createCollator( rLocale, *it + "_" + rSortAlgorithm, rSortAlgorithm);
193 if (bLoaded)
194 break;
196 if (!bLoaded)
198 // load service with name <base>_<algorithm>
199 bLoaded = createCollator( rLocale, rSortAlgorithm, rSortAlgorithm);
203 if (!bLoaded)
205 // load default service with name <base>_Unicode
206 bLoaded = createCollator( rLocale, "Unicode", rSortAlgorithm);
207 if (!bLoaded)
209 cachedItem = NULL;
210 throw RuntimeException(); // could not load any service
215 OUString SAL_CALL CollatorImpl::getImplementationName() throw( RuntimeException, std::exception )
217 return OUString("com.sun.star.i18n.Collator");
220 sal_Bool SAL_CALL CollatorImpl::supportsService(const OUString& rServiceName)
221 throw( RuntimeException, std::exception )
223 return cppu::supportsService(this, rServiceName);
226 Sequence< OUString > SAL_CALL
227 CollatorImpl::getSupportedServiceNames() throw( RuntimeException, std::exception )
229 Sequence< OUString > aRet(1);
230 aRet[0] = OUString("com.sun.star.i18n.Collator");
231 return aRet;
234 } } } }
236 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
237 com_sun_star_i18n_Collator_get_implementation(
238 css::uno::XComponentContext *context,
239 css::uno::Sequence<css::uno::Any> const &)
241 return cppu::acquire(new com::sun::star::i18n::CollatorImpl(context));
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */