1449ab281SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3449ab281SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4449ab281SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5449ab281SAndrew Rist  * distributed with this work for additional information
6449ab281SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7449ab281SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8449ab281SAndrew Rist  * "License"); you may not use this file except in compliance
9449ab281SAndrew Rist  * with the License.  You may obtain a copy of the License at
10449ab281SAndrew Rist  *
11449ab281SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12449ab281SAndrew Rist  *
13449ab281SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14449ab281SAndrew Rist  * software distributed under the License is distributed on an
15449ab281SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16449ab281SAndrew Rist  * KIND, either express or implied.  See the License for the
17449ab281SAndrew Rist  * specific language governing permissions and limitations
18449ab281SAndrew Rist  * under the License.
19449ab281SAndrew Rist  *
20449ab281SAndrew Rist  *************************************************************/
21449ab281SAndrew Rist 
22449ab281SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // generated list of languages
28cdf0e10cSrcweir #include "lrl_include.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
31cdf0e10cSrcweir #include <collator_unicode.hxx>
32cdf0e10cSrcweir #include <localedata.hxx>
33cdf0e10cSrcweir #include <com/sun/star/i18n/CollatorOptions.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir using namespace ::com::sun::star::lang;
37cdf0e10cSrcweir using namespace ::com::sun::star::uno;
38cdf0e10cSrcweir using namespace ::rtl;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace i18n {
41cdf0e10cSrcweir 
Collator_Unicode()42cdf0e10cSrcweir Collator_Unicode::Collator_Unicode()
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	implementationName = "com.sun.star.i18n.Collator_Unicode";
45cdf0e10cSrcweir 	collator = NULL;
46cdf0e10cSrcweir 	uca_base = NULL;
47cdf0e10cSrcweir     hModule = NULL;
48cdf0e10cSrcweir }
49cdf0e10cSrcweir 
~Collator_Unicode()50cdf0e10cSrcweir Collator_Unicode::~Collator_Unicode()
51cdf0e10cSrcweir {
52cdf0e10cSrcweir 	if (collator) delete collator;
53cdf0e10cSrcweir 	if (uca_base) delete uca_base;
54cdf0e10cSrcweir     if (hModule) osl_unloadModule(hModule);
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir sal_Int32 SAL_CALL
compareSubstring(const OUString & str1,sal_Int32 off1,sal_Int32 len1,const OUString & str2,sal_Int32 off2,sal_Int32 len2)58cdf0e10cSrcweir Collator_Unicode::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
59cdf0e10cSrcweir 	const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(RuntimeException)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     return collator->compare(reinterpret_cast<const UChar *>(str1.getStr()) + off1, len1, reinterpret_cast<const UChar *>(str2.getStr()) + off2, len2);	// UChar != sal_Unicode in MinGW
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir sal_Int32 SAL_CALL
compareString(const OUString & str1,const OUString & str2)65cdf0e10cSrcweir Collator_Unicode::compareString( const OUString& str1, const OUString& str2) throw(RuntimeException)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     return collator->compare(reinterpret_cast<const UChar *>(str1.getStr()), reinterpret_cast<const UChar *>(str2.getStr()));	// UChar != sal_Unicode in MinGW
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
thisModule()70cdf0e10cSrcweir extern "C" { static void SAL_CALL thisModule() {} }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir sal_Int32 SAL_CALL
loadCollatorAlgorithm(const OUString & rAlgorithm,const lang::Locale & rLocale,sal_Int32 options)73cdf0e10cSrcweir Collator_Unicode::loadCollatorAlgorithm(const OUString& rAlgorithm, const lang::Locale& rLocale, sal_Int32 options)
74cdf0e10cSrcweir 	throw(RuntimeException)
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	if (!collator) {
77cdf0e10cSrcweir         UErrorCode status = U_ZERO_ERROR;
78cdf0e10cSrcweir         OUString rule = LocaleData().getCollatorRuleByAlgorithm(rLocale, rAlgorithm);
79cdf0e10cSrcweir         if (rule.getLength() > 0) {
80cdf0e10cSrcweir             collator = new RuleBasedCollator(reinterpret_cast<const UChar *>(rule.getStr()), status);	// UChar != sal_Unicode in MinGW
81cdf0e10cSrcweir 			if (! U_SUCCESS(status)) throw RuntimeException();
82cdf0e10cSrcweir 		}
83cdf0e10cSrcweir 		if (!collator && OUString::createFromAscii(LOCAL_RULE_LANGS).indexOf(rLocale.Language) >= 0) {
84cdf0e10cSrcweir 			OUStringBuffer aBuf;
85cdf0e10cSrcweir #ifdef SAL_DLLPREFIX
86cdf0e10cSrcweir 			aBuf.appendAscii(SAL_DLLPREFIX);
87cdf0e10cSrcweir #endif
88cdf0e10cSrcweir 			aBuf.appendAscii( "collator_data" ).appendAscii( SAL_DLLEXTENSION );
89cdf0e10cSrcweir 			hModule = osl_loadModuleRelative( &thisModule, aBuf.makeStringAndClear().pData, SAL_LOADMODULE_DEFAULT );
90cdf0e10cSrcweir 			if (hModule) {
91cdf0e10cSrcweir 				const sal_uInt8* (*func)() = NULL;
92cdf0e10cSrcweir 				aBuf.appendAscii("get_").append(rLocale.Language).appendAscii("_");
93cdf0e10cSrcweir 				if (rLocale.Language.equalsAscii("zh")) {
94cdf0e10cSrcweir 					OUString func_base = aBuf.makeStringAndClear();
95cdf0e10cSrcweir 					if (OUString::createFromAscii("TW HK MO").indexOf(rLocale.Country) >= 0)
96cdf0e10cSrcweir 						func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule,
97cdf0e10cSrcweir 									(func_base + OUString::createFromAscii("TW_") + rAlgorithm).pData);
98cdf0e10cSrcweir 					if (!func)
99cdf0e10cSrcweir 						func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule, (func_base + rAlgorithm).pData);
100cdf0e10cSrcweir 				} else {
101cdf0e10cSrcweir 					if (rLocale.Language.equalsAscii("ja")) {
102cdf0e10cSrcweir 						// replace algrithm name to implementation name.
103cdf0e10cSrcweir 						if (rAlgorithm.equalsAscii("phonetic (alphanumeric first)") )
104cdf0e10cSrcweir 							aBuf.appendAscii("phonetic_alphanumeric_first");
105cdf0e10cSrcweir 						else if (rAlgorithm.equalsAscii("phonetic (alphanumeric last)"))
106cdf0e10cSrcweir 							aBuf.appendAscii("phonetic_alphanumeric_last");
107cdf0e10cSrcweir 						else
108cdf0e10cSrcweir 							aBuf.append(rAlgorithm);
109cdf0e10cSrcweir 					} else {
110cdf0e10cSrcweir 						aBuf.append(rAlgorithm);
111cdf0e10cSrcweir 					}
112cdf0e10cSrcweir 					func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule, aBuf.makeStringAndClear().pData);
113cdf0e10cSrcweir 				}
114cdf0e10cSrcweir 				if (func) {
115cdf0e10cSrcweir 					const sal_uInt8* ruleImage=func();
116cdf0e10cSrcweir 					uca_base = new RuleBasedCollator(static_cast<UChar*>(NULL), status);
117cdf0e10cSrcweir 					if (! U_SUCCESS(status)) throw RuntimeException();
118cdf0e10cSrcweir 					collator = new RuleBasedCollator(reinterpret_cast<const uint8_t*>(ruleImage), -1, uca_base, status);
119cdf0e10cSrcweir 					if (! U_SUCCESS(status)) throw RuntimeException();
120cdf0e10cSrcweir 				}
121cdf0e10cSrcweir 			}
122cdf0e10cSrcweir 		}
123cdf0e10cSrcweir 		if (!collator) {
124cdf0e10cSrcweir 			/** ICU collators are loaded using a locale only.
125cdf0e10cSrcweir 				ICU uses Variant as collation algorithm name (like de__PHONEBOOK
126cdf0e10cSrcweir 				locale), note the empty territory (Country) designator in this special
127*a8f4084dSMatthias Seidel 				case here. The icu::Locale constructor changes the algorithm name to
128cdf0e10cSrcweir 				uppercase itself, so we don't have to bother with that.
129cdf0e10cSrcweir 			*/
130cdf0e10cSrcweir 			icu::Locale icuLocale(
131cdf0e10cSrcweir 				   OUStringToOString(rLocale.Language, RTL_TEXTENCODING_ASCII_US).getStr(),
132cdf0e10cSrcweir 				   OUStringToOString(rLocale.Country, RTL_TEXTENCODING_ASCII_US).getStr(),
133cdf0e10cSrcweir 				   OUStringToOString(rAlgorithm, RTL_TEXTENCODING_ASCII_US).getStr());
134cdf0e10cSrcweir 			// load ICU collator
135cdf0e10cSrcweir 			collator = (RuleBasedCollator*) icu::Collator::createInstance(icuLocale, status);
136cdf0e10cSrcweir 			if (! U_SUCCESS(status)) throw RuntimeException();
137cdf0e10cSrcweir 		}
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	if (options & CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT)
141cdf0e10cSrcweir         collator->setStrength(Collator::PRIMARY);
142cdf0e10cSrcweir 	else if (options & CollatorOptions::CollatorOptions_IGNORE_CASE)
143cdf0e10cSrcweir         collator->setStrength(Collator::SECONDARY);
144cdf0e10cSrcweir     else
145cdf0e10cSrcweir         collator->setStrength(Collator::TERTIARY);
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	return(0);
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 
151cdf0e10cSrcweir OUString SAL_CALL
getImplementationName()152cdf0e10cSrcweir Collator_Unicode::getImplementationName() throw( RuntimeException )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir     return OUString::createFromAscii(implementationName);
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir sal_Bool SAL_CALL
supportsService(const rtl::OUString & rServiceName)158cdf0e10cSrcweir Collator_Unicode::supportsService(const rtl::OUString& rServiceName) throw( RuntimeException )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     return !rServiceName.compareToAscii(implementationName);
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir Sequence< OUString > SAL_CALL
getSupportedServiceNames()164cdf0e10cSrcweir Collator_Unicode::getSupportedServiceNames() throw( RuntimeException )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir     Sequence< OUString > aRet(1);
167cdf0e10cSrcweir     aRet[0] = OUString::createFromAscii(implementationName);
168cdf0e10cSrcweir     return aRet;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir } } } }
172cdf0e10cSrcweir 
173