merge the formfield patch from ooo-build
[ooovba.git] / i18npool / source / indexentry / indexentrysupplier.cxx
blob9d63dccbc0c6b0fb51fc8d302c4e06f6fe6073e7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: indexentrysupplier.cxx,v $
10 * $Revision: 1.20 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
33 #include <rtl/ustrbuf.hxx>
34 #include <indexentrysupplier.hxx>
35 #include <localedata.hxx>
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::rtl;
41 static const sal_Unicode under = sal_Unicode('_');
43 namespace com { namespace sun { namespace star { namespace i18n {
45 IndexEntrySupplier::IndexEntrySupplier( const Reference < XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF )
49 Sequence < Locale > SAL_CALL IndexEntrySupplier::getLocaleList() throw (RuntimeException)
51 return LocaleData().getAllInstalledLocaleNames();
54 Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Locale& rLocale ) throw (RuntimeException)
56 return LocaleData().getIndexAlgorithm(rLocale);
59 sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm,
60 sal_Int32 collatorOptions ) throw (RuntimeException)
62 Sequence < OUString > algorithmList = getAlgorithmList( rLocale );
63 for (sal_Int32 i = 0; i < algorithmList.getLength(); i++) {
64 if (algorithmList[i] == SortAlgorithm) {
65 if (getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is())
66 return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions);
69 return sal_False;
72 sal_Bool SAL_CALL IndexEntrySupplier::usePhoneticEntry( const Locale& rLocale ) throw (RuntimeException)
74 return LocaleData().hasPhonetic(rLocale);
77 OUString SAL_CALL IndexEntrySupplier::getPhoneticCandidate( const OUString& rIndexEntry,
78 const Locale& rLocale ) throw (RuntimeException)
80 if (getLocaleSpecificIndexEntrySupplier(rLocale, OUString()).is())
81 return xIES->getPhoneticCandidate(rIndexEntry, rLocale);
82 else
83 throw RuntimeException();
86 OUString SAL_CALL IndexEntrySupplier::getIndexKey( const OUString& rIndexEntry,
87 const OUString& rPhoneticEntry, const Locale& rLocale ) throw (RuntimeException)
89 if (xIES.is())
90 return xIES->getIndexKey(rIndexEntry, rPhoneticEntry, rLocale);
91 else
92 throw RuntimeException();
95 sal_Int16 SAL_CALL IndexEntrySupplier::compareIndexEntry(
96 const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
97 const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
98 throw (com::sun::star::uno::RuntimeException)
100 if (xIES.is())
101 return xIES->compareIndexEntry(rIndexEntry1, rPhoneticEntry1, rLocale1,
102 rIndexEntry2, rPhoneticEntry2, rLocale2);
103 else
104 throw RuntimeException();
107 OUString SAL_CALL IndexEntrySupplier::getIndexCharacter( const OUString& rIndexEntry,
108 const Locale& rLocale, const OUString& rSortAlgorithm )
109 throw (RuntimeException)
111 return getLocaleSpecificIndexEntrySupplier(rLocale, rSortAlgorithm)->
112 getIndexCharacter( rIndexEntry, rLocale, rSortAlgorithm );
115 sal_Bool SAL_CALL IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString& name) throw( RuntimeException )
117 Reference < XInterface > xI = xMSF->createInstance(
118 OUString::createFromAscii("com.sun.star.i18n.IndexEntrySupplier_") + name);
120 if ( xI.is() ) {
121 xI->queryInterface( ::getCppuType((const Reference< com::sun::star::i18n::XExtendedIndexEntrySupplier>*)0) ) >>= xIES;
122 return xIES.is();
124 return sal_False;
127 Reference < com::sun::star::i18n::XExtendedIndexEntrySupplier > SAL_CALL
128 IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale& rLocale, const OUString& rSortAlgorithm) throw (RuntimeException)
130 if (xIES.is() && rSortAlgorithm == aSortAlgorithm && rLocale.Language == aLocale.Language &&
131 rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
132 return xIES;
133 else if (xMSF.is()) {
134 LocaleData ld;
135 aLocale = rLocale;
136 if (rSortAlgorithm.getLength() == 0)
137 aSortAlgorithm = ld.getDefaultIndexAlgorithm( rLocale );
138 else
139 aSortAlgorithm = rSortAlgorithm;
141 OUString module = ld.getIndexModuleByAlgorithm(rLocale, aSortAlgorithm);
142 if (module.getLength() > 0 && createLocaleSpecificIndexEntrySupplier(module))
143 return xIES;
145 sal_Int32 l = rLocale.Language.getLength();
146 sal_Int32 c = rLocale.Country.getLength();
147 sal_Int32 v = rLocale.Variant.getLength();
148 sal_Int32 a = aSortAlgorithm.getLength();
149 OUStringBuffer aBuf(l+c+v+a+4);
151 if ((l > 0 && c > 0 && v > 0 && a > 0 &&
152 // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
153 createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
154 rLocale.Country).append(under).append(rLocale.Variant).append(under).append(
155 aSortAlgorithm).makeStringAndClear())) ||
156 (l > 0 && c > 0 && a > 0 &&
157 // load service with name <base>_<lang>_<country>_<algorithm>
158 createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
159 rLocale.Country).append(under).append(aSortAlgorithm).makeStringAndClear())) ||
160 (l > 0 && c > 0 && a > 0 && rLocale.Language.compareToAscii("zh") == 0 &&
161 (rLocale.Country.compareToAscii("HK") == 0 ||
162 rLocale.Country.compareToAscii("MO") == 0) &&
163 // if the country code is HK or MO, one more step to try TW.
164 createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).appendAscii(
165 "TW").append(under).append(aSortAlgorithm).makeStringAndClear())) ||
166 (l > 0 && a > 0 &&
167 // load service with name <base>_<lang>_<algorithm>
168 createLocaleSpecificIndexEntrySupplier(aBuf.append(rLocale.Language).append(under).append(
169 aSortAlgorithm).makeStringAndClear())) ||
170 // load service with name <base>_<algorithm>
171 (a > 0 && createLocaleSpecificIndexEntrySupplier(aSortAlgorithm)) ||
172 // load default service with name <base>_Unicode
173 createLocaleSpecificIndexEntrySupplier(OUString::createFromAscii("Unicode"))) {
174 return xIES;
177 throw RuntimeException();
180 OUString SAL_CALL IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages,
181 const Locale& rLocale ) throw (RuntimeException)
183 Sequence< OUString > aFollowPageWords = LocaleData().getFollowPageWords(rLocale);
185 return (bMorePages && aFollowPageWords.getLength() > 1) ?
186 aFollowPageWords[1] : (aFollowPageWords.getLength() > 0 ?
187 aFollowPageWords[0] : OUString());
190 #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
192 OUString SAL_CALL
193 IndexEntrySupplier::getImplementationName() throw( RuntimeException )
195 return OUString::createFromAscii( implementationName );
198 sal_Bool SAL_CALL
199 IndexEntrySupplier::supportsService(const OUString& rServiceName) throw( RuntimeException )
201 return rServiceName.compareToAscii(implementationName) == 0;
204 Sequence< OUString > SAL_CALL
205 IndexEntrySupplier::getSupportedServiceNames() throw( RuntimeException )
207 Sequence< OUString > aRet(1);
208 aRet[0] = OUString::createFromAscii( implementationName );
209 return aRet;
212 } } } }