Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / collator / collatorImpl.cxx
blobfde6eef3373a4df0c5f16979b1a38d4d55509fab
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 .
21 #include <collatorImpl.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>
27 using namespace com::sun::star;
28 using namespace com::sun::star::lang;
29 using namespace com::sun::star::uno;
31 using ::rtl::OUString;
32 using ::rtl::OUStringBuffer;
34 namespace com { namespace sun { namespace star { namespace i18n {
36 CollatorImpl::CollatorImpl( const Reference < XComponentContext >& rxContext ) : m_xContext(rxContext)
38 mxLocaleData.set( LocaleData::create(rxContext) );
39 cachedItem = NULL;
42 CollatorImpl::~CollatorImpl()
44 // Clear lookuptable
45 for (size_t l = 0; l < lookupTable.size(); l++)
46 delete lookupTable[l];
47 lookupTable.clear();
50 sal_Int32 SAL_CALL
51 CollatorImpl::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
52 const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(RuntimeException)
54 if (cachedItem)
55 return cachedItem->xC->compareSubstring(str1, off1, len1, str2, off2, len2);
57 sal_Unicode *unistr1 = (sal_Unicode*) str1.getStr() + off1;
58 sal_Unicode *unistr2 = (sal_Unicode*) str2.getStr() + off2;
59 for (int i = 0; i < len1 && i < len2; i++)
60 if (unistr1[i] != unistr2[i])
61 return unistr1[i] < unistr2[i] ? -1 : 1;
62 return len1 == len2 ? 0 : (len1 < len2 ? -1 : 1);
65 sal_Int32 SAL_CALL
66 CollatorImpl::compareString( const OUString& in_str1, const OUString& in_str2) throw(RuntimeException)
68 if (cachedItem)
69 return cachedItem->xC->compareString(in_str1, in_str2);
71 return CollatorImpl::compareSubstring(in_str1, 0, in_str1.getLength(), in_str2, 0, in_str2.getLength());
75 sal_Int32 SAL_CALL
76 CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions) throw(RuntimeException)
78 const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
79 for (sal_Int16 i = 0; i < imp.getLength(); i++)
80 if (imp[i].isDefault)
81 return loadCollatorAlgorithm(imp[i].unoID, rLocale, collatorOptions);
83 throw RuntimeException(); // not default is defined
84 //return 0;
87 sal_Int32 SAL_CALL
88 CollatorImpl::loadCollatorAlgorithm(const OUString& impl, const lang::Locale& rLocale, sal_Int32 collatorOptions)
89 throw(RuntimeException)
91 if (! cachedItem || ! cachedItem->equals(rLocale, impl))
92 loadCachedCollator(rLocale, impl);
94 if (cachedItem)
95 cachedItem->xC->loadCollatorAlgorithm(cachedItem->algorithm, nLocale = rLocale, collatorOptions);
96 else
97 throw RuntimeException(); // impl could not be loaded
99 return 0;
102 void SAL_CALL
103 CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString& impl, const lang::Locale& rLocale,
104 const Sequence< sal_Int32 >& collatorOptions) throw(RuntimeException)
106 sal_Int32 options = 0;
107 for (sal_Int32 i = 0; i < collatorOptions.getLength(); i++)
108 options |= collatorOptions[i];
109 loadCollatorAlgorithm(impl, rLocale, options);
112 Sequence< OUString > SAL_CALL
113 CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale ) throw(RuntimeException)
115 nLocale = rLocale;
116 const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
117 Sequence< OUString > list(imp.getLength());
119 for (sal_Int32 i = 0; i < imp.getLength(); i++) {
120 //if the current algorithm is default and the position is not on the first one, then switch
121 if (imp[i].isDefault && i) {
122 list[i] = list[0];
123 list[0] = imp[i].unoID;
125 else
126 list[i] = imp[i].unoID;
128 return list;
131 Sequence< sal_Int32 > SAL_CALL
132 CollatorImpl::listCollatorOptions( const OUString& /*collatorAlgorithmName*/ ) throw(RuntimeException)
134 Sequence< OUString > option_str = mxLocaleData->getCollationOptions(nLocale);
135 Sequence< sal_Int32 > option_int(option_str.getLength());
137 for (sal_Int32 i = 0; i < option_str.getLength(); i++)
138 option_int[i] =
139 option_str[i] == "IGNORE_CASE" ? CollatorOptions::CollatorOptions_IGNORE_CASE :
140 option_str[i] == "IGNORE_KANA" ? CollatorOptions::CollatorOptions_IGNORE_KANA :
141 option_str[i] == "IGNORE_WIDTH" ? CollatorOptions::CollatorOptions_IGNORE_WIDTH : 0;
143 return option_int;
146 sal_Bool SAL_CALL
147 CollatorImpl::createCollator(const lang::Locale& rLocale, const OUString& serviceName, const OUString& rSortAlgorithm)
148 throw(RuntimeException)
150 for (size_t l = 0; l < lookupTable.size(); l++) {
151 cachedItem = lookupTable[l];
152 if (cachedItem->service.equals(serviceName)) {// cross locale sharing
153 lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, cachedItem->xC));
154 return sal_True;
157 Reference < XInterface > xI =
158 m_xContext->getServiceManager()->createInstanceWithContext( OUString("com.sun.star.i18n.Collator_") + serviceName, m_xContext );
160 if (xI.is()) {
161 Reference < XCollator > xC;
162 xI->queryInterface( getCppuType((const Reference< XCollator>*)0) ) >>= xC;
163 if (xC.is()) {
164 lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, xC));
165 return sal_True;
168 return sal_False;
171 void SAL_CALL
172 CollatorImpl::loadCachedCollator(const lang::Locale& rLocale, const OUString& rSortAlgorithm)
173 throw(RuntimeException)
175 for (size_t i = 0; i < lookupTable.size(); i++) {
176 cachedItem = lookupTable[i];
177 if (cachedItem->equals(rLocale, rSortAlgorithm)) {
178 return;
182 static sal_Unicode under = (sal_Unicode) '_';
184 sal_Int32 l = rLocale.Language.getLength();
185 sal_Int32 c = rLocale.Country.getLength();
186 sal_Int32 v = rLocale.Variant.getLength();
187 sal_Int32 a = rSortAlgorithm.getLength();
188 OUStringBuffer aBuf(l+c+v+a+4);
190 if ((l > 0 && c > 0 && v > 0 && a > 0 &&
191 // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
192 createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rLocale.Country).append(
193 under).append(rLocale.Variant).append(under).append(rSortAlgorithm).makeStringAndClear(),
194 rSortAlgorithm)) ||
195 (l > 0 && c > 0 && a > 0 &&
196 // load service with name <base>_<lang>_<country>_<algorithm>
197 createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rLocale.Country).append(
198 under).append(rSortAlgorithm).makeStringAndClear(), rSortAlgorithm)) ||
199 (l > 0 && c > 0 && a > 0 && rLocale.Language == "zh" && (rLocale.Country == "HK" || rLocale.Country == "MO") &&
200 // if the country code is HK or MO, one more step to try TW.
201 createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append("TW").append(under).append(
202 rSortAlgorithm).makeStringAndClear(), rSortAlgorithm)) ||
203 (l > 0 && a > 0 &&
204 // load service with name <base>_<lang>_<algorithm>
205 createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rSortAlgorithm).makeStringAndClear(),
206 rSortAlgorithm)) ||
207 // load service with name <base>_<algorithm>
208 (a > 0 &&
209 createCollator(rLocale, rSortAlgorithm, rSortAlgorithm)) ||
210 // load default service with name <base>_Unicode
211 createCollator(rLocale, "Unicode", rSortAlgorithm)) {
212 return;
213 } else {
214 cachedItem = NULL;
215 throw RuntimeException(); // could not load any service
219 const sal_Char cCollator[] = "com.sun.star.i18n.Collator";
221 OUString SAL_CALL
222 CollatorImpl::getImplementationName() throw( RuntimeException )
224 return OUString::createFromAscii(cCollator);
227 sal_Bool SAL_CALL
228 CollatorImpl::supportsService(const OUString& rServiceName)
229 throw( RuntimeException )
231 return rServiceName.equalsAscii(cCollator);
234 Sequence< OUString > SAL_CALL
235 CollatorImpl::getSupportedServiceNames() throw( RuntimeException )
237 Sequence< OUString > aRet(1);
238 aRet[0] = OUString::createFromAscii(cCollator);
239 return aRet;
242 } } } }
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */