1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <rtl/ustrbuf.hxx>
30 #include <indexentrysupplier.hxx>
31 #include <localedata.hxx>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::lang
;
35 using namespace ::rtl
;
37 static const sal_Unicode under
= sal_Unicode('_');
39 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
41 IndexEntrySupplier::IndexEntrySupplier( const Reference
< XMultiServiceFactory
>& rxMSF
) : xMSF( rxMSF
)
45 Sequence
< Locale
> SAL_CALL
IndexEntrySupplier::getLocaleList() throw (RuntimeException
)
47 return LocaleData().getAllInstalledLocaleNames();
50 Sequence
< OUString
> SAL_CALL
IndexEntrySupplier::getAlgorithmList( const Locale
& rLocale
) throw (RuntimeException
)
52 return LocaleData().getIndexAlgorithm(rLocale
);
55 sal_Bool SAL_CALL
IndexEntrySupplier::loadAlgorithm( const Locale
& rLocale
, const OUString
& SortAlgorithm
,
56 sal_Int32 collatorOptions
) throw (RuntimeException
)
58 Sequence
< OUString
> algorithmList
= getAlgorithmList( rLocale
);
59 for (sal_Int32 i
= 0; i
< algorithmList
.getLength(); i
++) {
60 if (algorithmList
[i
] == SortAlgorithm
) {
61 if (getLocaleSpecificIndexEntrySupplier(rLocale
, SortAlgorithm
).is())
62 return xIES
->loadAlgorithm(rLocale
, SortAlgorithm
, collatorOptions
);
68 sal_Bool SAL_CALL
IndexEntrySupplier::usePhoneticEntry( const Locale
& rLocale
) throw (RuntimeException
)
70 return LocaleData().hasPhonetic(rLocale
);
73 OUString SAL_CALL
IndexEntrySupplier::getPhoneticCandidate( const OUString
& rIndexEntry
,
74 const Locale
& rLocale
) throw (RuntimeException
)
76 if (getLocaleSpecificIndexEntrySupplier(rLocale
, OUString()).is())
77 return xIES
->getPhoneticCandidate(rIndexEntry
, rLocale
);
79 throw RuntimeException();
82 OUString SAL_CALL
IndexEntrySupplier::getIndexKey( const OUString
& rIndexEntry
,
83 const OUString
& rPhoneticEntry
, const Locale
& rLocale
) throw (RuntimeException
)
86 return xIES
->getIndexKey(rIndexEntry
, rPhoneticEntry
, rLocale
);
88 throw RuntimeException();
91 sal_Int16 SAL_CALL
IndexEntrySupplier::compareIndexEntry(
92 const OUString
& rIndexEntry1
, const OUString
& rPhoneticEntry1
, const Locale
& rLocale1
,
93 const OUString
& rIndexEntry2
, const OUString
& rPhoneticEntry2
, const Locale
& rLocale2
)
94 throw (com::sun::star::uno::RuntimeException
)
97 return xIES
->compareIndexEntry(rIndexEntry1
, rPhoneticEntry1
, rLocale1
,
98 rIndexEntry2
, rPhoneticEntry2
, rLocale2
);
100 throw RuntimeException();
103 OUString SAL_CALL
IndexEntrySupplier::getIndexCharacter( const OUString
& rIndexEntry
,
104 const Locale
& rLocale
, const OUString
& rSortAlgorithm
)
105 throw (RuntimeException
)
107 return getLocaleSpecificIndexEntrySupplier(rLocale
, rSortAlgorithm
)->
108 getIndexCharacter( rIndexEntry
, rLocale
, rSortAlgorithm
);
111 sal_Bool SAL_CALL
IndexEntrySupplier::createLocaleSpecificIndexEntrySupplier(const OUString
& name
) throw( RuntimeException
)
113 Reference
< XInterface
> xI
= xMSF
->createInstance(
114 OUString("com.sun.star.i18n.IndexEntrySupplier_") + name
);
117 xI
->queryInterface( ::getCppuType((const Reference
< com::sun::star::i18n::XExtendedIndexEntrySupplier
>*)0) ) >>= xIES
;
123 Reference
< com::sun::star::i18n::XExtendedIndexEntrySupplier
> SAL_CALL
124 IndexEntrySupplier::getLocaleSpecificIndexEntrySupplier(const Locale
& rLocale
, const OUString
& rSortAlgorithm
) throw (RuntimeException
)
126 if (xIES
.is() && rSortAlgorithm
== aSortAlgorithm
&& rLocale
.Language
== aLocale
.Language
&&
127 rLocale
.Country
== aLocale
.Country
&& rLocale
.Variant
== aLocale
.Variant
)
129 else if (xMSF
.is()) {
132 if (rSortAlgorithm
.isEmpty())
133 aSortAlgorithm
= ld
.getDefaultIndexAlgorithm( rLocale
);
135 aSortAlgorithm
= rSortAlgorithm
;
137 OUString module
= ld
.getIndexModuleByAlgorithm(rLocale
, aSortAlgorithm
);
138 if (!module
.isEmpty() && createLocaleSpecificIndexEntrySupplier(module
))
141 sal_Int32 l
= rLocale
.Language
.getLength();
142 sal_Int32 c
= rLocale
.Country
.getLength();
143 sal_Int32 v
= rLocale
.Variant
.getLength();
144 sal_Int32 a
= aSortAlgorithm
.getLength();
145 OUStringBuffer
aBuf(l
+c
+v
+a
+4);
147 if ((l
> 0 && c
> 0 && v
> 0 && a
> 0 &&
148 // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
149 createLocaleSpecificIndexEntrySupplier(aBuf
.append(rLocale
.Language
).append(under
).append(
150 rLocale
.Country
).append(under
).append(rLocale
.Variant
).append(under
).append(
151 aSortAlgorithm
).makeStringAndClear())) ||
152 (l
> 0 && c
> 0 && a
> 0 &&
153 // load service with name <base>_<lang>_<country>_<algorithm>
154 createLocaleSpecificIndexEntrySupplier(aBuf
.append(rLocale
.Language
).append(under
).append(
155 rLocale
.Country
).append(under
).append(aSortAlgorithm
).makeStringAndClear())) ||
156 (l
> 0 && c
> 0 && a
> 0 && rLocale
.Language
.compareToAscii("zh") == 0 &&
157 (rLocale
.Country
.compareToAscii("HK") == 0 ||
158 rLocale
.Country
.compareToAscii("MO") == 0) &&
159 // if the country code is HK or MO, one more step to try TW.
160 createLocaleSpecificIndexEntrySupplier(aBuf
.append(rLocale
.Language
).append(under
).appendAscii(
161 "TW").append(under
).append(aSortAlgorithm
).makeStringAndClear())) ||
163 // load service with name <base>_<lang>_<algorithm>
164 createLocaleSpecificIndexEntrySupplier(aBuf
.append(rLocale
.Language
).append(under
).append(
165 aSortAlgorithm
).makeStringAndClear())) ||
166 // load service with name <base>_<algorithm>
167 (a
> 0 && createLocaleSpecificIndexEntrySupplier(aSortAlgorithm
)) ||
168 // load default service with name <base>_Unicode
169 createLocaleSpecificIndexEntrySupplier(OUString("Unicode"))) {
173 throw RuntimeException();
176 OUString SAL_CALL
IndexEntrySupplier::getIndexFollowPageWord( sal_Bool bMorePages
,
177 const Locale
& rLocale
) throw (RuntimeException
)
179 Sequence
< OUString
> aFollowPageWords
= LocaleData().getFollowPageWords(rLocale
);
181 return (bMorePages
&& aFollowPageWords
.getLength() > 1) ?
182 aFollowPageWords
[1] : (aFollowPageWords
.getLength() > 0 ?
183 aFollowPageWords
[0] : OUString());
186 #define implementationName "com.sun.star.i18n.IndexEntrySupplier"
189 IndexEntrySupplier::getImplementationName() throw( RuntimeException
)
191 return OUString::createFromAscii( implementationName
);
195 IndexEntrySupplier::supportsService(const OUString
& rServiceName
) throw( RuntimeException
)
197 return rServiceName
.compareToAscii(implementationName
) == 0;
200 Sequence
< OUString
> SAL_CALL
201 IndexEntrySupplier::getSupportedServiceNames() throw( RuntimeException
)
203 Sequence
< OUString
> aRet(1);
204 aRet
[0] = OUString::createFromAscii( implementationName
);
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */