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