1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: collator_unicode.cxx,v $
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"
34 // generated list of languages
35 #include "lrl_include.hxx"
37 #include <rtl/ustrbuf.hxx>
38 #include <collator_unicode.hxx>
39 #include <localedata.hxx>
40 #include <com/sun/star/i18n/CollatorOptions.hpp>
42 using namespace ::com::sun::star
;
43 using namespace ::com::sun::star::lang
;
44 using namespace ::com::sun::star::uno
;
45 using namespace ::rtl
;
47 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
49 Collator_Unicode::Collator_Unicode()
51 implementationName
= "com.sun.star.i18n.Collator_Unicode";
57 Collator_Unicode::~Collator_Unicode()
59 if (collator
) delete collator
;
60 if (uca_base
) delete uca_base
;
61 if (hModule
) osl_unloadModule(hModule
);
65 Collator_Unicode::compareSubstring( const OUString
& str1
, sal_Int32 off1
, sal_Int32 len1
,
66 const OUString
& str2
, sal_Int32 off2
, sal_Int32 len2
) throw(RuntimeException
)
68 return collator
->compare(reinterpret_cast<const UChar
*>(str1
.getStr()) + off1
, len1
, reinterpret_cast<const UChar
*>(str2
.getStr()) + off2
, len2
); // UChar != sal_Unicode in MinGW
72 Collator_Unicode::compareString( const OUString
& str1
, const OUString
& str2
) throw(RuntimeException
)
74 return collator
->compare(reinterpret_cast<const UChar
*>(str1
.getStr()), reinterpret_cast<const UChar
*>(str2
.getStr())); // UChar != sal_Unicode in MinGW
77 extern "C" { static void SAL_CALL
thisModule() {} }
80 Collator_Unicode::loadCollatorAlgorithm(const OUString
& rAlgorithm
, const lang::Locale
& rLocale
, sal_Int32 options
)
81 throw(RuntimeException
)
84 UErrorCode status
= U_ZERO_ERROR
;
85 OUString rule
= LocaleData().getCollatorRuleByAlgorithm(rLocale
, rAlgorithm
);
86 if (rule
.getLength() > 0) {
87 collator
= new RuleBasedCollator(reinterpret_cast<const UChar
*>(rule
.getStr()), status
); // UChar != sal_Unicode in MinGW
88 if (! U_SUCCESS(status
)) throw RuntimeException();
90 if (!collator
&& OUString::createFromAscii(LOCAL_RULE_LANGS
).indexOf(rLocale
.Language
) >= 0) {
93 aBuf
.appendAscii(SAL_DLLPREFIX
);
95 aBuf
.appendAscii( "collator_data" ).appendAscii( SAL_DLLEXTENSION
);
96 hModule
= osl_loadModuleRelative( &thisModule
, aBuf
.makeStringAndClear().pData
, SAL_LOADMODULE_DEFAULT
);
98 const sal_uInt8
* (*func
)() = NULL
;
99 aBuf
.appendAscii("get_").append(rLocale
.Language
).appendAscii("_");
100 if (rLocale
.Language
.equalsAscii("zh")) {
101 OUString func_base
= aBuf
.makeStringAndClear();
102 if (OUString::createFromAscii("TW HK MO").indexOf(rLocale
.Country
) >= 0)
103 func
=(const sal_uInt8
* (*)()) osl_getFunctionSymbol(hModule
,
104 (func_base
+ OUString::createFromAscii("TW_") + rAlgorithm
).pData
);
106 func
=(const sal_uInt8
* (*)()) osl_getFunctionSymbol(hModule
, (func_base
+ rAlgorithm
).pData
);
108 if (rLocale
.Language
.equalsAscii("ja")) {
109 // replace algrithm name to implementation name.
110 if (rAlgorithm
.equalsAscii("phonetic (alphanumeric first)") )
111 aBuf
.appendAscii("phonetic_alphanumeric_first");
112 else if (rAlgorithm
.equalsAscii("phonetic (alphanumeric last)"))
113 aBuf
.appendAscii("phonetic_alphanumeric_last");
115 aBuf
.append(rAlgorithm
);
117 aBuf
.append(rAlgorithm
);
119 func
=(const sal_uInt8
* (*)()) osl_getFunctionSymbol(hModule
, aBuf
.makeStringAndClear().pData
);
122 const sal_uInt8
* ruleImage
=func();
123 uca_base
= new RuleBasedCollator(static_cast<UChar
*>(NULL
), status
);
124 if (! U_SUCCESS(status
)) throw RuntimeException();
125 collator
= new RuleBasedCollator(reinterpret_cast<const uint8_t*>(ruleImage
), -1, uca_base
, status
);
126 if (! U_SUCCESS(status
)) throw RuntimeException();
131 /** ICU collators are loaded using a locale only.
132 ICU uses Variant as collation algorithm name (like de__PHONEBOOK
133 locale), note the empty territory (Country) designator in this special
134 case here. The icu::Locale contructor changes the algorithm name to
135 uppercase itself, so we don't have to bother with that.
137 icu::Locale
icuLocale(
138 OUStringToOString(rLocale
.Language
, RTL_TEXTENCODING_ASCII_US
).getStr(),
139 OUStringToOString(rLocale
.Country
, RTL_TEXTENCODING_ASCII_US
).getStr(),
140 OUStringToOString(rAlgorithm
, RTL_TEXTENCODING_ASCII_US
).getStr());
142 collator
= (RuleBasedCollator
*) icu::Collator::createInstance(icuLocale
, status
);
143 if (! U_SUCCESS(status
)) throw RuntimeException();
147 if (options
& CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT
)
148 collator
->setStrength(Collator::PRIMARY
);
149 else if (options
& CollatorOptions::CollatorOptions_IGNORE_CASE
)
150 collator
->setStrength(Collator::SECONDARY
);
152 collator
->setStrength(Collator::TERTIARY
);
159 Collator_Unicode::getImplementationName() throw( RuntimeException
)
161 return OUString::createFromAscii(implementationName
);
165 Collator_Unicode::supportsService(const rtl::OUString
& rServiceName
) throw( RuntimeException
)
167 return !rServiceName
.compareToAscii(implementationName
);
170 Sequence
< OUString
> SAL_CALL
171 Collator_Unicode::getSupportedServiceNames() throw( RuntimeException
)
173 Sequence
< OUString
> aRet(1);
174 aRet
[0] = OUString::createFromAscii(implementationName
);