1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <collatorImpl.hxx>
22 #include <com/sun/star/i18n/CollatorOptions.hpp>
23 #include <com/sun/star/i18n/LocaleData.hpp>
24 #include <rtl/ustrbuf.hxx>
25 #include <comphelper/processfactory.hxx>
27 using namespace com::sun::star
;
28 using namespace com::sun::star::lang
;
29 using namespace com::sun::star::uno
;
31 using ::rtl::OUString
;
32 using ::rtl::OUStringBuffer
;
34 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
36 CollatorImpl::CollatorImpl( const Reference
< XComponentContext
>& rxContext
) : m_xContext(rxContext
)
38 mxLocaleData
.set( LocaleData::create(rxContext
) );
42 CollatorImpl::~CollatorImpl()
45 for (size_t l
= 0; l
< lookupTable
.size(); l
++)
46 delete lookupTable
[l
];
51 CollatorImpl::compareSubstring( const OUString
& str1
, sal_Int32 off1
, sal_Int32 len1
,
52 const OUString
& str2
, sal_Int32 off2
, sal_Int32 len2
) throw(RuntimeException
)
55 return cachedItem
->xC
->compareSubstring(str1
, off1
, len1
, str2
, off2
, len2
);
57 sal_Unicode
*unistr1
= (sal_Unicode
*) str1
.getStr() + off1
;
58 sal_Unicode
*unistr2
= (sal_Unicode
*) str2
.getStr() + off2
;
59 for (int i
= 0; i
< len1
&& i
< len2
; i
++)
60 if (unistr1
[i
] != unistr2
[i
])
61 return unistr1
[i
] < unistr2
[i
] ? -1 : 1;
62 return len1
== len2
? 0 : (len1
< len2
? -1 : 1);
66 CollatorImpl::compareString( const OUString
& in_str1
, const OUString
& in_str2
) throw(RuntimeException
)
69 return cachedItem
->xC
->compareString(in_str1
, in_str2
);
71 return CollatorImpl::compareSubstring(in_str1
, 0, in_str1
.getLength(), in_str2
, 0, in_str2
.getLength());
76 CollatorImpl::loadDefaultCollator(const lang::Locale
& rLocale
, sal_Int32 collatorOptions
) throw(RuntimeException
)
78 const Sequence
< Implementation
> &imp
= mxLocaleData
->getCollatorImplementations(rLocale
);
79 for (sal_Int16 i
= 0; i
< imp
.getLength(); i
++)
81 return loadCollatorAlgorithm(imp
[i
].unoID
, rLocale
, collatorOptions
);
83 throw RuntimeException(); // not default is defined
88 CollatorImpl::loadCollatorAlgorithm(const OUString
& impl
, const lang::Locale
& rLocale
, sal_Int32 collatorOptions
)
89 throw(RuntimeException
)
91 if (! cachedItem
|| ! cachedItem
->equals(rLocale
, impl
))
92 loadCachedCollator(rLocale
, impl
);
95 cachedItem
->xC
->loadCollatorAlgorithm(cachedItem
->algorithm
, nLocale
= rLocale
, collatorOptions
);
97 throw RuntimeException(); // impl could not be loaded
103 CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString
& impl
, const lang::Locale
& rLocale
,
104 const Sequence
< sal_Int32
>& collatorOptions
) throw(RuntimeException
)
106 sal_Int32 options
= 0;
107 for (sal_Int32 i
= 0; i
< collatorOptions
.getLength(); i
++)
108 options
|= collatorOptions
[i
];
109 loadCollatorAlgorithm(impl
, rLocale
, options
);
112 Sequence
< OUString
> SAL_CALL
113 CollatorImpl::listCollatorAlgorithms( const lang::Locale
& rLocale
) throw(RuntimeException
)
116 const Sequence
< Implementation
> &imp
= mxLocaleData
->getCollatorImplementations(rLocale
);
117 Sequence
< OUString
> list(imp
.getLength());
119 for (sal_Int32 i
= 0; i
< imp
.getLength(); i
++) {
120 //if the current algorithm is default and the position is not on the first one, then switch
121 if (imp
[i
].isDefault
&& i
) {
123 list
[0] = imp
[i
].unoID
;
126 list
[i
] = imp
[i
].unoID
;
131 Sequence
< sal_Int32
> SAL_CALL
132 CollatorImpl::listCollatorOptions( const OUString
& /*collatorAlgorithmName*/ ) throw(RuntimeException
)
134 Sequence
< OUString
> option_str
= mxLocaleData
->getCollationOptions(nLocale
);
135 Sequence
< sal_Int32
> option_int(option_str
.getLength());
137 for (sal_Int32 i
= 0; i
< option_str
.getLength(); i
++)
139 option_str
[i
] == "IGNORE_CASE" ? CollatorOptions::CollatorOptions_IGNORE_CASE
:
140 option_str
[i
] == "IGNORE_KANA" ? CollatorOptions::CollatorOptions_IGNORE_KANA
:
141 option_str
[i
] == "IGNORE_WIDTH" ? CollatorOptions::CollatorOptions_IGNORE_WIDTH
: 0;
147 CollatorImpl::createCollator(const lang::Locale
& rLocale
, const OUString
& serviceName
, const OUString
& rSortAlgorithm
)
148 throw(RuntimeException
)
150 for (size_t l
= 0; l
< lookupTable
.size(); l
++) {
151 cachedItem
= lookupTable
[l
];
152 if (cachedItem
->service
.equals(serviceName
)) {// cross locale sharing
153 lookupTable
.push_back(cachedItem
= new lookupTableItem(rLocale
, rSortAlgorithm
, serviceName
, cachedItem
->xC
));
157 Reference
< XInterface
> xI
=
158 m_xContext
->getServiceManager()->createInstanceWithContext( OUString("com.sun.star.i18n.Collator_") + serviceName
, m_xContext
);
161 Reference
< XCollator
> xC
;
162 xI
->queryInterface( getCppuType((const Reference
< XCollator
>*)0) ) >>= xC
;
164 lookupTable
.push_back(cachedItem
= new lookupTableItem(rLocale
, rSortAlgorithm
, serviceName
, xC
));
172 CollatorImpl::loadCachedCollator(const lang::Locale
& rLocale
, const OUString
& rSortAlgorithm
)
173 throw(RuntimeException
)
175 for (size_t i
= 0; i
< lookupTable
.size(); i
++) {
176 cachedItem
= lookupTable
[i
];
177 if (cachedItem
->equals(rLocale
, rSortAlgorithm
)) {
182 static sal_Unicode under
= (sal_Unicode
) '_';
184 sal_Int32 l
= rLocale
.Language
.getLength();
185 sal_Int32 c
= rLocale
.Country
.getLength();
186 sal_Int32 v
= rLocale
.Variant
.getLength();
187 sal_Int32 a
= rSortAlgorithm
.getLength();
188 OUStringBuffer
aBuf(l
+c
+v
+a
+4);
190 if ((l
> 0 && c
> 0 && v
> 0 && a
> 0 &&
191 // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
192 createCollator(rLocale
, aBuf
.append(rLocale
.Language
).append(under
).append(rLocale
.Country
).append(
193 under
).append(rLocale
.Variant
).append(under
).append(rSortAlgorithm
).makeStringAndClear(),
195 (l
> 0 && c
> 0 && a
> 0 &&
196 // load service with name <base>_<lang>_<country>_<algorithm>
197 createCollator(rLocale
, aBuf
.append(rLocale
.Language
).append(under
).append(rLocale
.Country
).append(
198 under
).append(rSortAlgorithm
).makeStringAndClear(), rSortAlgorithm
)) ||
199 (l
> 0 && c
> 0 && a
> 0 && rLocale
.Language
== "zh" && (rLocale
.Country
== "HK" || rLocale
.Country
== "MO") &&
200 // if the country code is HK or MO, one more step to try TW.
201 createCollator(rLocale
, aBuf
.append(rLocale
.Language
).append(under
).append("TW").append(under
).append(
202 rSortAlgorithm
).makeStringAndClear(), rSortAlgorithm
)) ||
204 // load service with name <base>_<lang>_<algorithm>
205 createCollator(rLocale
, aBuf
.append(rLocale
.Language
).append(under
).append(rSortAlgorithm
).makeStringAndClear(),
207 // load service with name <base>_<algorithm>
209 createCollator(rLocale
, rSortAlgorithm
, rSortAlgorithm
)) ||
210 // load default service with name <base>_Unicode
211 createCollator(rLocale
, "Unicode", rSortAlgorithm
)) {
215 throw RuntimeException(); // could not load any service
219 const sal_Char cCollator
[] = "com.sun.star.i18n.Collator";
222 CollatorImpl::getImplementationName() throw( RuntimeException
)
224 return OUString::createFromAscii(cCollator
);
228 CollatorImpl::supportsService(const OUString
& rServiceName
)
229 throw( RuntimeException
)
231 return rServiceName
.equalsAscii(cCollator
);
234 Sequence
< OUString
> SAL_CALL
235 CollatorImpl::getSupportedServiceNames() throw( RuntimeException
)
237 Sequence
< OUString
> aRet(1);
238 aRet
[0] = OUString::createFromAscii(cCollator
);
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */