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
;
32 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
34 CollatorImpl::CollatorImpl( const Reference
< XComponentContext
>& rxContext
) : m_xContext(rxContext
)
36 mxLocaleData
.set( LocaleData::create(rxContext
) );
40 CollatorImpl::~CollatorImpl()
43 for (size_t l
= 0; l
< lookupTable
.size(); l
++)
44 delete lookupTable
[l
];
49 CollatorImpl::compareSubstring( const OUString
& str1
, sal_Int32 off1
, sal_Int32 len1
,
50 const OUString
& str2
, sal_Int32 off2
, sal_Int32 len2
) throw(RuntimeException
)
53 return cachedItem
->xC
->compareSubstring(str1
, off1
, len1
, str2
, off2
, len2
);
55 sal_Unicode
*unistr1
= (sal_Unicode
*) str1
.getStr() + off1
;
56 sal_Unicode
*unistr2
= (sal_Unicode
*) str2
.getStr() + off2
;
57 for (int i
= 0; i
< len1
&& i
< len2
; i
++)
58 if (unistr1
[i
] != unistr2
[i
])
59 return unistr1
[i
] < unistr2
[i
] ? -1 : 1;
60 return len1
== len2
? 0 : (len1
< len2
? -1 : 1);
64 CollatorImpl::compareString( const OUString
& in_str1
, const OUString
& in_str2
) throw(RuntimeException
)
67 return cachedItem
->xC
->compareString(in_str1
, in_str2
);
69 return CollatorImpl::compareSubstring(in_str1
, 0, in_str1
.getLength(), in_str2
, 0, in_str2
.getLength());
74 CollatorImpl::loadDefaultCollator(const lang::Locale
& rLocale
, sal_Int32 collatorOptions
) throw(RuntimeException
)
76 const Sequence
< Implementation
> &imp
= mxLocaleData
->getCollatorImplementations(rLocale
);
77 for (sal_Int16 i
= 0; i
< imp
.getLength(); i
++)
79 return loadCollatorAlgorithm(imp
[i
].unoID
, rLocale
, collatorOptions
);
81 throw RuntimeException(); // not default is defined
86 CollatorImpl::loadCollatorAlgorithm(const OUString
& impl
, const lang::Locale
& rLocale
, sal_Int32 collatorOptions
)
87 throw(RuntimeException
)
89 if (! cachedItem
|| ! cachedItem
->equals(rLocale
, impl
))
90 loadCachedCollator(rLocale
, impl
);
93 cachedItem
->xC
->loadCollatorAlgorithm(cachedItem
->algorithm
, nLocale
= rLocale
, collatorOptions
);
95 throw RuntimeException(); // impl could not be loaded
101 CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString
& impl
, const lang::Locale
& rLocale
,
102 const Sequence
< sal_Int32
>& collatorOptions
) throw(RuntimeException
)
104 sal_Int32 options
= 0;
105 for (sal_Int32 i
= 0; i
< collatorOptions
.getLength(); i
++)
106 options
|= collatorOptions
[i
];
107 loadCollatorAlgorithm(impl
, rLocale
, options
);
110 Sequence
< OUString
> SAL_CALL
111 CollatorImpl::listCollatorAlgorithms( const lang::Locale
& rLocale
) throw(RuntimeException
)
114 const Sequence
< Implementation
> &imp
= mxLocaleData
->getCollatorImplementations(rLocale
);
115 Sequence
< OUString
> list(imp
.getLength());
117 for (sal_Int32 i
= 0; i
< imp
.getLength(); i
++) {
118 //if the current algorithm is default and the position is not on the first one, then switch
119 if (imp
[i
].isDefault
&& i
) {
121 list
[0] = imp
[i
].unoID
;
124 list
[i
] = imp
[i
].unoID
;
129 Sequence
< sal_Int32
> SAL_CALL
130 CollatorImpl::listCollatorOptions( const OUString
& /*collatorAlgorithmName*/ ) throw(RuntimeException
)
132 Sequence
< OUString
> option_str
= mxLocaleData
->getCollationOptions(nLocale
);
133 Sequence
< sal_Int32
> option_int(option_str
.getLength());
135 for (sal_Int32 i
= 0; i
< option_str
.getLength(); i
++)
137 option_str
[i
] == "IGNORE_CASE" ? CollatorOptions::CollatorOptions_IGNORE_CASE
:
138 option_str
[i
] == "IGNORE_KANA" ? CollatorOptions::CollatorOptions_IGNORE_KANA
:
139 option_str
[i
] == "IGNORE_WIDTH" ? CollatorOptions::CollatorOptions_IGNORE_WIDTH
: 0;
145 CollatorImpl::createCollator(const lang::Locale
& rLocale
, const OUString
& serviceName
, const OUString
& rSortAlgorithm
)
146 throw(RuntimeException
)
148 for (size_t l
= 0; l
< lookupTable
.size(); l
++) {
149 cachedItem
= lookupTable
[l
];
150 if (cachedItem
->service
.equals(serviceName
)) {// cross locale sharing
151 lookupTable
.push_back(cachedItem
= new lookupTableItem(rLocale
, rSortAlgorithm
, serviceName
, cachedItem
->xC
));
155 Reference
< XInterface
> xI
=
156 m_xContext
->getServiceManager()->createInstanceWithContext( OUString("com.sun.star.i18n.Collator_") + serviceName
, m_xContext
);
159 Reference
< XCollator
> xC
;
160 xC
.set( xI
, UNO_QUERY
);
162 lookupTable
.push_back(cachedItem
= new lookupTableItem(rLocale
, rSortAlgorithm
, serviceName
, xC
));
170 CollatorImpl::loadCachedCollator(const lang::Locale
& rLocale
, const OUString
& rSortAlgorithm
)
171 throw(RuntimeException
)
173 for (size_t i
= 0; i
< lookupTable
.size(); i
++) {
174 cachedItem
= lookupTable
[i
];
175 if (cachedItem
->equals(rLocale
, rSortAlgorithm
)) {
180 static sal_Unicode under
= (sal_Unicode
) '_';
182 sal_Int32 l
= rLocale
.Language
.getLength();
183 sal_Int32 c
= rLocale
.Country
.getLength();
184 sal_Int32 v
= rLocale
.Variant
.getLength();
185 sal_Int32 a
= rSortAlgorithm
.getLength();
186 OUStringBuffer
aBuf(l
+c
+v
+a
+4);
188 if ((l
> 0 && c
> 0 && v
> 0 && a
> 0 &&
189 // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
190 createCollator(rLocale
, aBuf
.append(rLocale
.Language
).append(under
).append(rLocale
.Country
).append(
191 under
).append(rLocale
.Variant
).append(under
).append(rSortAlgorithm
).makeStringAndClear(),
193 (l
> 0 && c
> 0 && a
> 0 &&
194 // load service with name <base>_<lang>_<country>_<algorithm>
195 createCollator(rLocale
, aBuf
.append(rLocale
.Language
).append(under
).append(rLocale
.Country
).append(
196 under
).append(rSortAlgorithm
).makeStringAndClear(), rSortAlgorithm
)) ||
197 (l
> 0 && c
> 0 && a
> 0 && rLocale
.Language
== "zh" && (rLocale
.Country
== "HK" || rLocale
.Country
== "MO") &&
198 // if the country code is HK or MO, one more step to try TW.
199 createCollator(rLocale
, aBuf
.append(rLocale
.Language
).append(under
).append("TW").append(under
).append(
200 rSortAlgorithm
).makeStringAndClear(), rSortAlgorithm
)) ||
202 // load service with name <base>_<lang>_<algorithm>
203 createCollator(rLocale
, aBuf
.append(rLocale
.Language
).append(under
).append(rSortAlgorithm
).makeStringAndClear(),
205 // load service with name <base>_<algorithm>
207 createCollator(rLocale
, rSortAlgorithm
, rSortAlgorithm
)) ||
208 // load default service with name <base>_Unicode
209 createCollator(rLocale
, "Unicode", rSortAlgorithm
)) {
213 throw RuntimeException(); // could not load any service
217 const sal_Char cCollator
[] = "com.sun.star.i18n.Collator";
220 CollatorImpl::getImplementationName() throw( RuntimeException
)
222 return OUString::createFromAscii(cCollator
);
226 CollatorImpl::supportsService(const OUString
& rServiceName
)
227 throw( RuntimeException
)
229 return rServiceName
.equalsAscii(cCollator
);
232 Sequence
< OUString
> SAL_CALL
233 CollatorImpl::getSupportedServiceNames() throw( RuntimeException
)
235 Sequence
< OUString
> aRet(1);
236 aRet
[0] = OUString::createFromAscii(cCollator
);
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */