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) throw(RuntimeException) 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) throw(RuntimeException) 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 throw(RuntimeException) 75 { 76 if (!collator) { 77 UErrorCode status = U_ZERO_ERROR; 78 OUString rule = LocaleData().getCollatorRuleByAlgorithm(rLocale, rAlgorithm); 79 if (rule.getLength() > 0) { 80 collator = new RuleBasedCollator(reinterpret_cast<const UChar *>(rule.getStr()), status); // UChar != sal_Unicode in MinGW 81 if (! U_SUCCESS(status)) throw RuntimeException(); 82 } 83 if (!collator && OUString::createFromAscii(LOCAL_RULE_LANGS).indexOf(rLocale.Language) >= 0) { 84 OUStringBuffer aBuf; 85 #ifdef SAL_DLLPREFIX 86 aBuf.appendAscii(SAL_DLLPREFIX); 87 #endif 88 aBuf.appendAscii( "collator_data" ).appendAscii( SAL_DLLEXTENSION ); 89 hModule = osl_loadModuleRelative( &thisModule, aBuf.makeStringAndClear().pData, SAL_LOADMODULE_DEFAULT ); 90 if (hModule) { 91 const sal_uInt8* (*func)() = NULL; 92 aBuf.appendAscii("get_").append(rLocale.Language).appendAscii("_"); 93 if (rLocale.Language.equalsAscii("zh")) { 94 OUString func_base = aBuf.makeStringAndClear(); 95 if (OUString::createFromAscii("TW HK MO").indexOf(rLocale.Country) >= 0) 96 func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule, 97 (func_base + OUString::createFromAscii("TW_") + rAlgorithm).pData); 98 if (!func) 99 func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule, (func_base + rAlgorithm).pData); 100 } else { 101 if (rLocale.Language.equalsAscii("ja")) { 102 // replace algrithm name to implementation name. 103 if (rAlgorithm.equalsAscii("phonetic (alphanumeric first)") ) 104 aBuf.appendAscii("phonetic_alphanumeric_first"); 105 else if (rAlgorithm.equalsAscii("phonetic (alphanumeric last)")) 106 aBuf.appendAscii("phonetic_alphanumeric_last"); 107 else 108 aBuf.append(rAlgorithm); 109 } else { 110 aBuf.append(rAlgorithm); 111 } 112 func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule, aBuf.makeStringAndClear().pData); 113 } 114 if (func) { 115 const sal_uInt8* ruleImage=func(); 116 uca_base = new RuleBasedCollator(static_cast<UChar*>(NULL), status); 117 if (! U_SUCCESS(status)) throw RuntimeException(); 118 collator = new RuleBasedCollator(reinterpret_cast<const uint8_t*>(ruleImage), -1, uca_base, status); 119 if (! U_SUCCESS(status)) throw RuntimeException(); 120 } 121 } 122 } 123 if (!collator) { 124 /** ICU collators are loaded using a locale only. 125 ICU uses Variant as collation algorithm name (like de__PHONEBOOK 126 locale), note the empty territory (Country) designator in this special 127 case here. The icu::Locale contructor changes the algorithm name to 128 uppercase itself, so we don't have to bother with that. 129 */ 130 icu::Locale icuLocale( 131 OUStringToOString(rLocale.Language, RTL_TEXTENCODING_ASCII_US).getStr(), 132 OUStringToOString(rLocale.Country, RTL_TEXTENCODING_ASCII_US).getStr(), 133 OUStringToOString(rAlgorithm, RTL_TEXTENCODING_ASCII_US).getStr()); 134 // load ICU collator 135 collator = (RuleBasedCollator*) icu::Collator::createInstance(icuLocale, status); 136 if (! U_SUCCESS(status)) throw RuntimeException(); 137 } 138 } 139 140 if (options & CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT) 141 collator->setStrength(Collator::PRIMARY); 142 else if (options & CollatorOptions::CollatorOptions_IGNORE_CASE) 143 collator->setStrength(Collator::SECONDARY); 144 else 145 collator->setStrength(Collator::TERTIARY); 146 147 return(0); 148 } 149 150 151 OUString SAL_CALL 152 Collator_Unicode::getImplementationName() throw( RuntimeException ) 153 { 154 return OUString::createFromAscii(implementationName); 155 } 156 157 sal_Bool SAL_CALL 158 Collator_Unicode::supportsService(const rtl::OUString& rServiceName) throw( RuntimeException ) 159 { 160 return !rServiceName.compareToAscii(implementationName); 161 } 162 163 Sequence< OUString > SAL_CALL 164 Collator_Unicode::getSupportedServiceNames() throw( RuntimeException ) 165 { 166 Sequence< OUString > aRet(1); 167 aRet[0] = OUString::createFromAscii(implementationName); 168 return aRet; 169 } 170 171 } } } } 172 173