Line data Source code
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 :
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>
26 :
27 : using namespace com::sun::star;
28 : using namespace com::sun::star::lang;
29 : using namespace com::sun::star::uno;
30 :
31 :
32 : namespace com { namespace sun { namespace star { namespace i18n {
33 :
34 370 : CollatorImpl::CollatorImpl( const Reference < XComponentContext >& rxContext ) : m_xContext(rxContext)
35 : {
36 370 : mxLocaleData.set( LocaleData::create(rxContext) );
37 370 : cachedItem = NULL;
38 370 : }
39 :
40 1062 : CollatorImpl::~CollatorImpl()
41 : {
42 : // Clear lookuptable
43 704 : for (size_t l = 0; l < lookupTable.size(); l++)
44 350 : delete lookupTable[l];
45 354 : lookupTable.clear();
46 708 : }
47 :
48 : sal_Int32 SAL_CALL
49 608146 : CollatorImpl::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
50 : const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(RuntimeException)
51 : {
52 608146 : if (cachedItem)
53 608146 : return cachedItem->xC->compareSubstring(str1, off1, len1, str2, off2, len2);
54 :
55 0 : sal_Unicode *unistr1 = (sal_Unicode*) str1.getStr() + off1;
56 0 : sal_Unicode *unistr2 = (sal_Unicode*) str2.getStr() + off2;
57 0 : for (int i = 0; i < len1 && i < len2; i++)
58 0 : if (unistr1[i] != unistr2[i])
59 0 : return unistr1[i] < unistr2[i] ? -1 : 1;
60 0 : return len1 == len2 ? 0 : (len1 < len2 ? -1 : 1);
61 : }
62 :
63 : sal_Int32 SAL_CALL
64 76790 : CollatorImpl::compareString( const OUString& in_str1, const OUString& in_str2) throw(RuntimeException)
65 : {
66 76790 : if (cachedItem)
67 76790 : return cachedItem->xC->compareString(in_str1, in_str2);
68 :
69 0 : return CollatorImpl::compareSubstring(in_str1, 0, in_str1.getLength(), in_str2, 0, in_str2.getLength());
70 : }
71 :
72 :
73 : sal_Int32 SAL_CALL
74 342 : CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions) throw(RuntimeException)
75 : {
76 342 : const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
77 342 : for (sal_Int16 i = 0; i < imp.getLength(); i++)
78 342 : if (imp[i].isDefault)
79 684 : return loadCollatorAlgorithm(imp[i].unoID, rLocale, collatorOptions);
80 :
81 342 : throw RuntimeException(); // not default is defined
82 : //return 0;
83 : }
84 :
85 : sal_Int32 SAL_CALL
86 366 : CollatorImpl::loadCollatorAlgorithm(const OUString& impl, const lang::Locale& rLocale, sal_Int32 collatorOptions)
87 : throw(RuntimeException)
88 : {
89 366 : if (! cachedItem || ! cachedItem->equals(rLocale, impl))
90 366 : loadCachedCollator(rLocale, impl);
91 :
92 366 : if (cachedItem)
93 366 : cachedItem->xC->loadCollatorAlgorithm(cachedItem->algorithm, nLocale = rLocale, collatorOptions);
94 : else
95 0 : throw RuntimeException(); // impl could not be loaded
96 :
97 366 : return 0;
98 : }
99 :
100 : void SAL_CALL
101 0 : CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString& impl, const lang::Locale& rLocale,
102 : const Sequence< sal_Int32 >& collatorOptions) throw(RuntimeException)
103 : {
104 0 : sal_Int32 options = 0;
105 0 : for (sal_Int32 i = 0; i < collatorOptions.getLength(); i++)
106 0 : options |= collatorOptions[i];
107 0 : loadCollatorAlgorithm(impl, rLocale, options);
108 0 : }
109 :
110 : Sequence< OUString > SAL_CALL
111 2 : CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale ) throw(RuntimeException)
112 : {
113 2 : nLocale = rLocale;
114 2 : const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
115 2 : Sequence< OUString > list(imp.getLength());
116 :
117 4 : 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 2 : if (imp[i].isDefault && i) {
120 0 : list[i] = list[0];
121 0 : list[0] = imp[i].unoID;
122 : }
123 : else
124 2 : list[i] = imp[i].unoID;
125 : }
126 2 : return list;
127 : }
128 :
129 : Sequence< sal_Int32 > SAL_CALL
130 0 : CollatorImpl::listCollatorOptions( const OUString& /*collatorAlgorithmName*/ ) throw(RuntimeException)
131 : {
132 0 : Sequence< OUString > option_str = mxLocaleData->getCollationOptions(nLocale);
133 0 : Sequence< sal_Int32 > option_int(option_str.getLength());
134 :
135 0 : for (sal_Int32 i = 0; i < option_str.getLength(); i++)
136 0 : option_int[i] =
137 0 : option_str[i] == "IGNORE_CASE" ? CollatorOptions::CollatorOptions_IGNORE_CASE :
138 0 : option_str[i] == "IGNORE_KANA" ? CollatorOptions::CollatorOptions_IGNORE_KANA :
139 0 : option_str[i] == "IGNORE_WIDTH" ? CollatorOptions::CollatorOptions_IGNORE_WIDTH : 0;
140 :
141 0 : return option_int;
142 : }
143 :
144 : sal_Bool SAL_CALL
145 1444 : CollatorImpl::createCollator(const lang::Locale& rLocale, const OUString& serviceName, const OUString& rSortAlgorithm)
146 : throw(RuntimeException)
147 : {
148 1444 : for (size_t l = 0; l < lookupTable.size(); l++) {
149 0 : cachedItem = lookupTable[l];
150 0 : if (cachedItem->service.equals(serviceName)) {// cross locale sharing
151 0 : lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, cachedItem->xC));
152 0 : return sal_True;
153 : }
154 : }
155 : Reference < XInterface > xI =
156 1444 : m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Collator_" + serviceName, m_xContext );
157 :
158 1444 : if (xI.is()) {
159 366 : Reference < XCollator > xC;
160 366 : xC.set( xI, UNO_QUERY );
161 366 : if (xC.is()) {
162 366 : lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, xC));
163 366 : return sal_True;
164 0 : }
165 : }
166 1078 : return sal_False;
167 : }
168 :
169 : void SAL_CALL
170 366 : CollatorImpl::loadCachedCollator(const lang::Locale& rLocale, const OUString& rSortAlgorithm)
171 : throw(RuntimeException)
172 : {
173 366 : for (size_t i = 0; i < lookupTable.size(); i++) {
174 0 : cachedItem = lookupTable[i];
175 0 : if (cachedItem->equals(rLocale, rSortAlgorithm)) {
176 0 : return;
177 : }
178 : }
179 :
180 : static sal_Unicode under = (sal_Unicode) '_';
181 :
182 366 : sal_Int32 l = rLocale.Language.getLength();
183 366 : sal_Int32 c = rLocale.Country.getLength();
184 366 : sal_Int32 v = rLocale.Variant.getLength();
185 366 : sal_Int32 a = rSortAlgorithm.getLength();
186 366 : OUStringBuffer aBuf(l+c+v+a+4);
187 :
188 1454 : if ((l > 0 && c > 0 && v > 0 && a > 0 &&
189 : // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
190 0 : createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rLocale.Country).append(
191 0 : under).append(rLocale.Variant).append(under).append(rSortAlgorithm).makeStringAndClear(),
192 732 : rSortAlgorithm)) ||
193 1068 : (l > 0 && c > 0 && a > 0 &&
194 : // load service with name <base>_<lang>_<country>_<algorithm>
195 356 : createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rLocale.Country).append(
196 1800 : under).append(rSortAlgorithm).makeStringAndClear(), rSortAlgorithm)) ||
197 356 : (l > 0 && c > 0 && a > 0 && rLocale.Language == "zh" && (rLocale.Country == "HK" || rLocale.Country == "MO") &&
198 : // if the country code is HK or MO, one more step to try TW.
199 0 : createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append("TW").append(under).append(
200 732 : rSortAlgorithm).makeStringAndClear(), rSortAlgorithm)) ||
201 1068 : (l > 0 && a > 0 &&
202 : // load service with name <base>_<lang>_<algorithm>
203 356 : createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rSortAlgorithm).makeStringAndClear(),
204 1444 : rSortAlgorithm)) ||
205 : // load service with name <base>_<algorithm>
206 366 : (a > 0 &&
207 1464 : createCollator(rLocale, rSortAlgorithm, rSortAlgorithm)) ||
208 : // load default service with name <base>_Unicode
209 1098 : createCollator(rLocale, "Unicode", rSortAlgorithm)) {
210 366 : return;
211 : } else {
212 0 : cachedItem = NULL;
213 0 : throw RuntimeException(); // could not load any service
214 366 : }
215 : }
216 :
217 : const sal_Char cCollator[] = "com.sun.star.i18n.Collator";
218 :
219 : OUString SAL_CALL
220 0 : CollatorImpl::getImplementationName() throw( RuntimeException )
221 : {
222 0 : return OUString::createFromAscii(cCollator);
223 : }
224 :
225 : sal_Bool SAL_CALL
226 0 : CollatorImpl::supportsService(const OUString& rServiceName)
227 : throw( RuntimeException )
228 : {
229 0 : return rServiceName.equalsAscii(cCollator);
230 : }
231 :
232 : Sequence< OUString > SAL_CALL
233 0 : CollatorImpl::getSupportedServiceNames() throw( RuntimeException )
234 : {
235 0 : Sequence< OUString > aRet(1);
236 0 : aRet[0] = OUString::createFromAscii(cCollator);
237 0 : return aRet;
238 : }
239 :
240 : } } } }
241 :
242 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|