1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_i18npool.hxx"
30 #include <rtl/ustrbuf.hxx>
31 #include <indexentrysupplier.hxx>
32 #include <localedata.hxx>
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::lang
;
36 using namespace ::rtl
;
38 static const sal_Unicode under
= sal_Unicode('_');
40 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
42 IndexEntrySupplier::IndexEntrySupplier( const Reference
< XMultiServiceFactory
>& rxMSF
) : xMSF( rxMSF
)
46 Sequence
< Locale
> SAL_CALL
IndexEntrySupplier::getLocaleList() throw (RuntimeException
)
48 return LocaleData().getAllInstalledLocaleNames();
51 Sequence
< OUString
> SAL_CALL
IndexEntrySupplier::getAlgorithmList( const Locale
& rLocale
) throw (RuntimeException
)
53 return LocaleData().getIndexAlgorithm(rLocale
);
56 sal_Bool SAL_CALL
IndexEntrySupplier::loadAlgorithm( const Locale
& rLocale
, const OUString
& SortAlgorithm
,
57 sal_Int32 collatorOptions
) throw (RuntimeException
)
59 Sequence
< OUString
> algorithmList
= getAlgorithmList( rLocale
);
60 for (sal_Int32 i
= 0; i
< algorithmList
.getLength(); i
++) {
61 if (algorithmList
[i
] == SortAlgorithm
) {
62 if (getLocaleSpecificIndexEntrySupplier(rLocale
, SortAlgorithm
).is())
63 return xIES
->loadAlgorithm(rLocale
, SortAlgorithm
, collatorOptions
);
69 sal_Bool SAL_CALL
IndexEntrySupplier::usePhoneticEntry( const Locale
& rLocale
) throw (RuntimeException
)
71 return LocaleData().hasPhonetic(rLocale
);
74 OUString SAL_CALL
IndexEntrySupplier::getPhoneticCandidate( const OUString
& rIndexEntry
,
75 const Locale
& rLocale
) throw (RuntimeException
)
77 if (getLocaleSpecificIndexEntrySupplier(rLocale
, OUString()).is())
78 return xIES
->getPhoneticCandidate(rIndexEntry
, rLocale
);
80 throw RuntimeException();
83 OUString SAL_CALL
IndexEntrySupplier::getIndexKey( const OUString
& rIndexEntry
,
84 const OUString
& rPhoneticEntry
, const Locale
& rLocale
) throw (RuntimeException
)
87 return xIES
->getIndexKey(rIndexEntry
, rPhoneticEntry
, rLocale
);
89 throw RuntimeException();
92 sal_Int16 SAL_CALL
IndexEntrySupplier::compareIndexEntry(
93 const OUString
& rIndexEntry1
, const OUString
& rPhoneticEntry1
, const Locale
& rLocale1
,
94 const OUString
& rIndexEntry2
, const OUString
& rPhoneticEntry2
, const Locale
& rLocale2
)
95 throw (com::sun::star::uno::RuntimeException
)
98 return xIES
->compareIndexEntry(rIndexEntry1
, rPhoneticEntry1
, rLocale1
,
99 rIndexEntry2
, rPhoneticEntry2
, rLocale2
);
101 throw RuntimeException();
104 OUString SAL_CALL
IndexEntrySupplier::getIndexCharacter( const OUString
& rIndexEntry
,
105 const Locale
& rLocale
, const OUString
& rSortAlgorithm
)
106 throw (RuntimeException
)
108 return getLocaleSpecificIndexEntrySupplier(rLocale
, rSortAlgorithm
)->
109 getIndexCharacter( rIndexEntry
, rLocale
, rSortAlgorithm
);
112 sal_Bool SAL_CALL
IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString
& name
) throw( RuntimeException
)
114 Reference
< XInterface
> xI
= xMSF
->createInstance(
115 OUString::createFromAscii("com.sun.star.i18n.IndexEntrySupplier_") + name
);
118 xI
->queryInterface( ::getCppuType((const Reference
< com::sun::star::i18n::XExtendedIndexEntrySupplier
>*)0) ) >>= xIES
;
124 Reference
< com::sun::star::i18n::XExtendedIndexEntrySupplier
> SAL_CALL
125 IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale
& rLocale
, const OUString
& rSortAlgorithm
) throw (RuntimeException
)
127 if (xIES
.is() && rSortAlgorithm
== aSortAlgorithm
&& rLocale
.Language
== aLocale
.Language
&&
128 rLocale
.Country
== aLocale
.Country
&& rLocale
.Variant
== aLocale
.Variant
)
130 else if (xMSF
.is()) {
133 if (rSortAlgorithm
.getLength() == 0)
134 aSortAlgorithm
= ld
.getDefaultIndexAlgorithm( rLocale
);
136 aSortAlgorithm
= rSortAlgorithm
;
138 OUString module
= ld
.getIndexModuleByAlgorithm(rLocale
, aSortAlgorithm
);
139 if (module
.getLength() > 0 && createLocaleSpecificIndexEntrySupplier(module
))
142 sal_Int32 l
= rLocale
.Language
.getLength();
143 sal_Int32 c
= rLocale
.Country
.getLength();
144 sal_Int32 v
= rLocale
.Variant
.getLength();
145 sal_Int32 a
= aSortAlgorithm
.getLength();
146 OUStringBuffer
aBuf(l
+c
+v
+a
+4);
148 if ((l
> 0 && c
> 0 && v
> 0 && a
> 0 &&
149 // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
150 createLocaleSpecificIndexEntrySupplier(aBuf
.append(rLocale
.Language
).append(under
).append(
151 rLocale
.Country
).append(under
).append(rLocale
.Variant
).append(under
).append(
152 aSortAlgorithm
).makeStringAndClear())) ||
153 (l
> 0 && c
> 0 && a
> 0 &&
154 // load service with name <base>_<lang>_<country>_<algorithm>
155 createLocaleSpecificIndexEntrySupplier(aBuf
.append(rLocale
.Language
).append(under
).append(
156 rLocale
.Country
).append(under
).append(aSortAlgorithm
).makeStringAndClear())) ||
157 (l
> 0 && c
> 0 && a
> 0 && rLocale
.Language
.compareToAscii("zh") == 0 &&
158 (rLocale
.Country
.compareToAscii("HK") == 0 ||
159 rLocale
.Country
.compareToAscii("MO") == 0) &&
160 // if the country code is HK or MO, one more step to try TW.
161 createLocaleSpecificIndexEntrySupplier(aBuf
.append(rLocale
.Language
).append(under
).appendAscii(
162 "TW").append(under
).append(aSortAlgorithm
).makeStringAndClear())) ||
164 // load service with name <base>_<lang>_<algorithm>
165 createLocaleSpecificIndexEntrySupplier(aBuf
.append(rLocale
.Language
).append(under
).append(
166 aSortAlgorithm
).makeStringAndClear())) ||
167 // load service with name <base>_<algorithm>
168 (a
> 0 && createLocaleSpecificIndexEntrySupplier(aSortAlgorithm
)) ||
169 // load default service with name <base>_Unicode
170 createLocaleSpecificIndexEntrySupplier(OUString::createFromAscii("Unicode"))) {
174 throw RuntimeException();
177 OUString SAL_CALL
IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages
,
178 const Locale
& rLocale
) throw (RuntimeException
)
180 Sequence
< OUString
> aFollowPageWords
= LocaleData().getFollowPageWords(rLocale
);
182 return (bMorePages
&& aFollowPageWords
.getLength() > 1) ?
183 aFollowPageWords
[1] : (aFollowPageWords
.getLength() > 0 ?
184 aFollowPageWords
[0] : OUString());
187 #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
190 IndexEntrySupplier::getImplementationName() throw( RuntimeException
)
192 return OUString::createFromAscii( implementationName
);
196 IndexEntrySupplier::supportsService(const OUString
& rServiceName
) throw( RuntimeException
)
198 return rServiceName
.compareToAscii(implementationName
) == 0;
201 Sequence
< OUString
> SAL_CALL
202 IndexEntrySupplier::getSupportedServiceNames() throw( RuntimeException
)
204 Sequence
< OUString
> aRet(1);
205 aRet
[0] = OUString::createFromAscii( implementationName
);