1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_i18npool.hxx"
26
27 #include <collatorImpl.hxx>
28 #include <com/sun/star/i18n/CollatorOptions.hpp>
29 #include <rtl/ustrbuf.hxx>
30
31 using namespace com::sun::star;
32 using namespace com::sun::star::lang;
33 using namespace com::sun::star::uno;
34 using namespace rtl;
35
36 namespace com { namespace sun { namespace star { namespace i18n {
37
CollatorImpl(const Reference<XMultiServiceFactory> & rxMSF)38 CollatorImpl::CollatorImpl( const Reference < XMultiServiceFactory >& rxMSF ) : xMSF(rxMSF)
39 {
40 if ( rxMSF.is()) {
41 Reference < XInterface > xI =
42 xMSF->createInstance( OUString::createFromAscii("com.sun.star.i18n.LocaleData"));
43 if ( xI.is() )
44 xI->queryInterface(::getCppuType((const Reference< XLocaleData>*)0)) >>= localedata;
45 }
46 cachedItem = NULL;
47 }
48
~CollatorImpl()49 CollatorImpl::~CollatorImpl()
50 {
51 // Clear lookuptable
52 for (size_t l = 0; l < lookupTable.size(); l++)
53 delete lookupTable[l];
54 lookupTable.clear();
55 }
56
57 sal_Int32 SAL_CALL
compareSubstring(const OUString & str1,sal_Int32 off1,sal_Int32 len1,const OUString & str2,sal_Int32 off2,sal_Int32 len2)58 CollatorImpl::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
59 const OUString& str2, sal_Int32 off2, sal_Int32 len2)
60 {
61 if (cachedItem)
62 return cachedItem->xC->compareSubstring(str1, off1, len1, str2, off2, len2);
63
64 sal_Unicode *unistr1 = (sal_Unicode*) str1.getStr() + off1;
65 sal_Unicode *unistr2 = (sal_Unicode*) str2.getStr() + off2;
66 for (int i = 0; i < len1 && i < len2; i++)
67 if (unistr1[i] != unistr2[i])
68 return unistr1[i] < unistr2[i] ? -1 : 1;
69 return len1 == len2 ? 0 : (len1 < len2 ? -1 : 1);
70 }
71
72 sal_Int32 SAL_CALL
compareString(const OUString & in_str1,const OUString & in_str2)73 CollatorImpl::compareString( const OUString& in_str1, const OUString& in_str2)
74 {
75 if (cachedItem)
76 return cachedItem->xC->compareString(in_str1, in_str2);
77
78 return CollatorImpl::compareSubstring(in_str1, 0, in_str1.getLength(), in_str2, 0, in_str2.getLength());
79 }
80
81
82 sal_Int32 SAL_CALL
loadDefaultCollator(const lang::Locale & rLocale,sal_Int32 collatorOptions)83 CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions)
84 {
85 const Sequence< Implementation > &imp = localedata->getCollatorImplementations(rLocale);
86 for (sal_Int16 i = 0; i < imp.getLength(); i++)
87 if (imp[i].isDefault)
88 return loadCollatorAlgorithm(imp[i].unoID, rLocale, collatorOptions);
89
90 throw RuntimeException(); // not default is defined
91 //return 0;
92 }
93
94 sal_Int32 SAL_CALL
loadCollatorAlgorithm(const OUString & impl,const lang::Locale & rLocale,sal_Int32 collatorOptions)95 CollatorImpl::loadCollatorAlgorithm(const OUString& impl, const lang::Locale& rLocale, sal_Int32 collatorOptions)
96 {
97 if (! cachedItem || ! cachedItem->equals(rLocale, impl))
98 loadCachedCollator(rLocale, impl);
99
100 if (cachedItem)
101 cachedItem->xC->loadCollatorAlgorithm(cachedItem->algorithm, nLocale = rLocale, collatorOptions);
102 else
103 throw RuntimeException(); // impl could not be loaded
104
105 return 0;
106 }
107
108 void SAL_CALL
loadCollatorAlgorithmWithEndUserOption(const OUString & impl,const lang::Locale & rLocale,const Sequence<sal_Int32> & collatorOptions)109 CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString& impl, const lang::Locale& rLocale,
110 const Sequence< sal_Int32 >& collatorOptions)
111 {
112 sal_Int32 options = 0;
113 for (sal_Int32 i = 0; i < collatorOptions.getLength(); i++)
114 options |= collatorOptions[i];
115 loadCollatorAlgorithm(impl, rLocale, options);
116 }
117
118 Sequence< OUString > SAL_CALL
listCollatorAlgorithms(const lang::Locale & rLocale)119 CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale )
120 {
121 nLocale = rLocale;
122 const Sequence< Implementation > &imp = localedata->getCollatorImplementations(rLocale);
123 Sequence< OUString > list(imp.getLength());
124
125 for (sal_Int32 i = 0; i < imp.getLength(); i++) {
126 //if the current algorithm is default and the position is not on the first one, then switch
127 if (imp[i].isDefault && i) {
128 list[i] = list[0];
129 list[0] = imp[i].unoID;
130 }
131 else
132 list[i] = imp[i].unoID;
133 }
134 return list;
135 }
136
137 Sequence< sal_Int32 > SAL_CALL
listCollatorOptions(const OUString &)138 CollatorImpl::listCollatorOptions( const OUString& /*collatorAlgorithmName*/ )
139 {
140 Sequence< OUString > option_str = localedata->getCollationOptions(nLocale);
141 Sequence< sal_Int32 > option_int(option_str.getLength());
142
143 for (sal_Int32 i = 0; i < option_str.getLength(); i++)
144 option_int[i] =
145 option_str[i].equalsAscii("IGNORE_CASE") ? CollatorOptions::CollatorOptions_IGNORE_CASE :
146 option_str[i].equalsAscii("IGNORE_KANA") ? CollatorOptions::CollatorOptions_IGNORE_KANA :
147 option_str[i].equalsAscii("IGNORE_WIDTH") ? CollatorOptions::CollatorOptions_IGNORE_WIDTH : 0;
148
149 return option_int;
150 }
151
152 sal_Bool SAL_CALL
createCollator(const lang::Locale & rLocale,const OUString & serviceName,const OUString & rSortAlgorithm)153 CollatorImpl::createCollator(const lang::Locale& rLocale, const OUString& serviceName, const OUString& rSortAlgorithm)
154 {
155 for (size_t l = 0; l < lookupTable.size(); l++) {
156 cachedItem = lookupTable[l];
157 if (cachedItem->service.equals(serviceName)) {// cross locale sharing
158 lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, cachedItem->xC));
159 return sal_True;
160 }
161 }
162 if (xMSF.is()) {
163 Reference < XInterface > xI =
164 xMSF->createInstance(OUString::createFromAscii("com.sun.star.i18n.Collator_") + serviceName);
165
166 if (xI.is()) {
167 Reference < XCollator > xC;
168 xI->queryInterface( getCppuType((const Reference< XCollator>*)0) ) >>= xC;
169 if (xC.is()) {
170 lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, xC));
171 return sal_True;
172 }
173 }
174 return sal_False;
175 }
176 throw RuntimeException();
177 }
178
179 void SAL_CALL
loadCachedCollator(const lang::Locale & rLocale,const OUString & rSortAlgorithm)180 CollatorImpl::loadCachedCollator(const lang::Locale& rLocale, const OUString& rSortAlgorithm)
181 {
182 for (size_t i = 0; i < lookupTable.size(); i++) {
183 cachedItem = lookupTable[i];
184 if (cachedItem->equals(rLocale, rSortAlgorithm)) {
185 return;
186 }
187 }
188
189 static sal_Unicode under = (sal_Unicode) '_';
190 static OUString tw(OUString::createFromAscii("TW"));
191 static OUString unicode(OUString::createFromAscii("Unicode"));
192
193 sal_Int32 l = rLocale.Language.getLength();
194 sal_Int32 c = rLocale.Country.getLength();
195 sal_Int32 v = rLocale.Variant.getLength();
196 sal_Int32 a = rSortAlgorithm.getLength();
197 OUStringBuffer aBuf(l+c+v+a+4);
198
199 if ((l > 0 && c > 0 && v > 0 && a > 0 &&
200 // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
201 createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rLocale.Country).append(
202 under).append(rLocale.Variant).append(under).append(rSortAlgorithm).makeStringAndClear(),
203 rSortAlgorithm)) ||
204 (l > 0 && c > 0 && a > 0 &&
205 // load service with name <base>_<lang>_<country>_<algorithm>
206 createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rLocale.Country).append(
207 under).append(rSortAlgorithm).makeStringAndClear(), rSortAlgorithm)) ||
208 (l > 0 && c > 0 && a > 0 && rLocale.Language.equalsAscii("zh") &&
209 (rLocale.Country.equalsAscii("HK") ||
210 rLocale.Country.equalsAscii("MO")) &&
211 // if the country code is HK or MO, one more step to try TW.
212 createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(tw).append(under).append(
213 rSortAlgorithm).makeStringAndClear(), rSortAlgorithm)) ||
214 (l > 0 && a > 0 &&
215 // load service with name <base>_<lang>_<algorithm>
216 createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rSortAlgorithm).makeStringAndClear(),
217 rSortAlgorithm)) ||
218 // load service with name <base>_<algorithm>
219 (a > 0 &&
220 createCollator(rLocale, rSortAlgorithm, rSortAlgorithm)) ||
221 // load default service with name <base>_Unicode
222 createCollator(rLocale, unicode, rSortAlgorithm)) {
223 return;
224 } else {
225 cachedItem = NULL;
226 throw RuntimeException(); // could not load any service
227 }
228 }
229
230 const sal_Char cCollator[] = "com.sun.star.i18n.Collator";
231
232 OUString SAL_CALL
getImplementationName()233 CollatorImpl::getImplementationName()
234 {
235 return OUString::createFromAscii(cCollator);
236 }
237
238 sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)239 CollatorImpl::supportsService(const OUString& rServiceName)
240 {
241 return rServiceName.equalsAscii(cCollator);
242 }
243
244 Sequence< OUString > SAL_CALL
getSupportedServiceNames()245 CollatorImpl::getSupportedServiceNames()
246 {
247 Sequence< OUString > aRet(1);
248 aRet[0] = OUString::createFromAscii(cCollator);
249 return aRet;
250 }
251
252 } } } }
253