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