xref: /trunk/main/i18npool/source/indexentry/indexentrysupplier.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 #include <rtl/ustrbuf.hxx>
27 #include <indexentrysupplier.hxx>
28 #include <localedata.hxx>
29 
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::lang;
32 using namespace ::rtl;
33 
34 static const sal_Unicode under = sal_Unicode('_');
35 
36 namespace com { namespace sun { namespace star { namespace i18n {
37 
IndexEntrySupplier(const Reference<XMultiServiceFactory> & rxMSF)38 IndexEntrySupplier::IndexEntrySupplier( const Reference < XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF )
39 {
40 }
41 
getLocaleList()42 Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList()
43 {
44         return LocaleData().getAllInstalledLocaleNames();
45 }
46 
getAlgorithmList(const Locale & rLocale)47 Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Locale& rLocale )
48 {
49         return LocaleData().getIndexAlgorithm(rLocale);
50 }
51 
loadAlgorithm(const Locale & rLocale,const OUString & SortAlgorithm,sal_Int32 collatorOptions)52 sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm,
53         sal_Int32 collatorOptions )
54 {
55         Sequence < OUString > algorithmList = getAlgorithmList( rLocale );
56         for (sal_Int32 i = 0; i < algorithmList.getLength(); i++) {
57             if (algorithmList[i] == SortAlgorithm) {
58                 if (getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is())
59                     return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions);
60             }
61         }
62         return sal_False;
63 }
64 
usePhoneticEntry(const Locale & rLocale)65 sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale )
66 {
67         return LocaleData().hasPhonetic(rLocale);
68 }
69 
getPhoneticCandidate(const OUString & rIndexEntry,const Locale & rLocale)70 OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
71         const Locale& rLocale )
72 {
73         if (getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
74             return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
75         else
76             throw RuntimeException();
77 }
78 
getIndexKey(const OUString & rIndexEntry,const OUString & rPhoneticEntry,const Locale & rLocale)79 OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
80         const OUString& rPhoneticEntry, const Locale& rLocale )
81 {
82         if (xIES.is())
83             return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
84         else
85             throw RuntimeException();
86 }
87 
compareIndexEntry(const OUString & rIndexEntry1,const OUString & rPhoneticEntry1,const Locale & rLocale1,const OUString & rIndexEntry2,const OUString & rPhoneticEntry2,const Locale & rLocale2)88 sal_Int16 SAL_CALL IndexEntrySupplier::compareIndexEntry(
89         const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
90         const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
91 {
92         if (xIES.is())
93             return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
94                                         rIndexEntry2, rPhoneticEntry2, rLocale2);
95         else
96             throw RuntimeException();
97 }
98 
getIndexCharacter(const OUString & rIndexEntry,const Locale & rLocale,const OUString & rSortAlgorithm)99 OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
100         const Locale& rLocale, const OUString& rSortAlgorithm )
101 {
102         return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
103                         getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
104 }
105 
createLocaleSpecificIndexEntrySupplier(const OUString & name)106 sal_Bool SAL_CALL IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString& name)
107 {
108         Reference < XInterface > xI = xMSF->createInstance(
109             OUString::createFromAscii("com.sun.star.i18n.IndexEntrySupplier_") + name);
110 
111         if ( xI.is() ) {
112             xI->queryInterface( ::getCppuType((const Reference< com::sun::star::i18n::XExtendedIndexEntrySupplier>*)0) ) >>= xIES;
113             return xIES.is();
114         }
115         return sal_False;
116 }
117 
118 Reference < com::sun::star::i18n::XExtendedIndexEntrySupplier > SAL_CALL
getLocaleSpecificIndexEntrySupplier(const Locale & rLocale,const OUString & rSortAlgorithm)119 IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm)
120 {
121         if (xIES.is() && rSortAlgorithm == aSortAlgorithm && rLocale.Language == aLocale.Language &&
122                 rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
123             return xIES;
124         else if (xMSF.is()) {
125             LocaleData ld;
126             aLocale = rLocale;
127             if (rSortAlgorithm.getLength() == 0)
128                 aSortAlgorithm = ld.getDefaultIndexAlgorithm( rLocale );
129             else
130                 aSortAlgorithm = rSortAlgorithm;
131 
132             OUString module = ld.getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
133             if (module.getLength() > 0 && createLocaleSpecificIndexEntrySupplier(module))
134                 return xIES;
135 
136             sal_Int32 l = rLocale.Language.getLength();
137             sal_Int32 c = rLocale.Country.getLength();
138             sal_Int32 v = rLocale.Variant.getLength();
139             sal_Int32 a = aSortAlgorithm.getLength();
140             OUStringBuffer aBuf(l+c+v+a+4);
141 
142             if ((l > 0 && c > 0 && v > 0 && a > 0 &&
143                         // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
144                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
145                                     rLocale.Country).append(under).append(rLocale.Variant).append(under).append(
146                                     aSortAlgorithm).makeStringAndClear())) ||
147                 (l > 0 && c > 0 && a > 0 &&
148                         // load service with name <base>_<lang>_<country>_<algorithm>
149                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
150                                     rLocale.Country).append(under).append(aSortAlgorithm).makeStringAndClear())) ||
151                 (l > 0 && c > 0 && a > 0 && rLocale.Language.compareToAscii("zh") == 0 &&
152                                             (rLocale.Country.compareToAscii("HK") == 0 ||
153                                             rLocale.Country.compareToAscii("MO") == 0) &&
154                         // if the country code is HK or MO, one more step to try TW.
155                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).appendAscii(
156                                     "TW").append(under).append(aSortAlgorithm).makeStringAndClear())) ||
157                 (l > 0 && a > 0 &&
158                         // load service with name <base>_<lang>_<algorithm>
159                         createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
160                                     aSortAlgorithm).makeStringAndClear())) ||
161                         // load service with name <base>_<algorithm>
162                 (a > 0 && createLocaleSpecificIndexEntrySupplier(aSortAlgorithm)) ||
163                         // load default service with name <base>_Unicode
164                         createLocaleSpecificIndexEntrySupplier(OUString::createFromAscii("Unicode"))) {
165                 return xIES;
166             }
167         }
168         throw RuntimeException();
169 }
170 
getIndexFollowPageWord(sal_Bool bMorePages,const Locale & rLocale)171 OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
172         const Locale& rLocale )
173 {
174         Sequence< OUString > aFollowPageWords = LocaleData().getFollowPageWords(rLocale);
175 
176         return (bMorePages && aFollowPageWords.getLength() > 1) ?
177             aFollowPageWords[1] : (aFollowPageWords.getLength() > 0 ?
178             aFollowPageWords[0] : OUString());
179 }
180 
181 #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
182 
183 OUString SAL_CALL
getImplementationName()184 IndexEntrySupplier::getImplementationName()
185 {
186         return OUString::createFromAscii( implementationName );
187 }
188 
189 sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)190 IndexEntrySupplier::supportsService(const OUString& rServiceName)
191 {
192         return rServiceName.compareToAscii(implementationName) == 0;
193 }
194 
195 Sequence< OUString > SAL_CALL
getSupportedServiceNames()196 IndexEntrySupplier::getSupportedServiceNames()
197 {
198         Sequence< OUString > aRet(1);
199         aRet[0] = OUString::createFromAscii( implementationName );
200         return aRet;
201 }
202 
203 } } } }
204