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