1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_i18npool.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <characterclassificationImpl.hxx> 32*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir using namespace com::sun::star::uno; 35*cdf0e10cSrcweir using namespace com::sun::star::lang; 36*cdf0e10cSrcweir using namespace rtl; 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace i18n { 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir CharacterClassificationImpl::CharacterClassificationImpl( 41*cdf0e10cSrcweir const Reference < lang::XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF ) 42*cdf0e10cSrcweir { 43*cdf0e10cSrcweir if (createLocaleSpecificCharacterClassification(OUString::createFromAscii("Unicode"), Locale())) 44*cdf0e10cSrcweir xUCI = cachedItem->xCI; 45*cdf0e10cSrcweir } 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir CharacterClassificationImpl::~CharacterClassificationImpl() { 48*cdf0e10cSrcweir // Clear lookuptable 49*cdf0e10cSrcweir for (size_t l = 0; l < lookupTable.size(); l++) 50*cdf0e10cSrcweir delete lookupTable[l]; 51*cdf0e10cSrcweir lookupTable.clear(); 52*cdf0e10cSrcweir } 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir OUString SAL_CALL 56*cdf0e10cSrcweir CharacterClassificationImpl::toUpper( const OUString& Text, sal_Int32 nPos, 57*cdf0e10cSrcweir sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException) 58*cdf0e10cSrcweir { 59*cdf0e10cSrcweir return getLocaleSpecificCharacterClassification(rLocale)->toUpper(Text, nPos, nCount, rLocale); 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir OUString SAL_CALL 63*cdf0e10cSrcweir CharacterClassificationImpl::toLower( const OUString& Text, sal_Int32 nPos, 64*cdf0e10cSrcweir sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException) 65*cdf0e10cSrcweir { 66*cdf0e10cSrcweir return getLocaleSpecificCharacterClassification(rLocale)->toLower(Text, nPos, nCount, rLocale); 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir OUString SAL_CALL 70*cdf0e10cSrcweir CharacterClassificationImpl::toTitle( const OUString& Text, sal_Int32 nPos, 71*cdf0e10cSrcweir sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException) 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir return getLocaleSpecificCharacterClassification(rLocale)->toTitle(Text, nPos, nCount, rLocale); 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir sal_Int16 SAL_CALL 77*cdf0e10cSrcweir CharacterClassificationImpl::getType( const OUString& Text, sal_Int32 nPos ) 78*cdf0e10cSrcweir throw(RuntimeException) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir if (xUCI.is()) 81*cdf0e10cSrcweir return xUCI->getType(Text, nPos); 82*cdf0e10cSrcweir throw RuntimeException(); 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir sal_Int16 SAL_CALL 86*cdf0e10cSrcweir CharacterClassificationImpl::getCharacterDirection( const OUString& Text, sal_Int32 nPos ) 87*cdf0e10cSrcweir throw(RuntimeException) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir if (xUCI.is()) 90*cdf0e10cSrcweir return xUCI->getCharacterDirection(Text, nPos); 91*cdf0e10cSrcweir throw RuntimeException(); 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir sal_Int16 SAL_CALL 95*cdf0e10cSrcweir CharacterClassificationImpl::getScript( const OUString& Text, sal_Int32 nPos ) 96*cdf0e10cSrcweir throw(RuntimeException) 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir if (xUCI.is()) 99*cdf0e10cSrcweir return xUCI->getScript(Text, nPos); 100*cdf0e10cSrcweir throw RuntimeException(); 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir sal_Int32 SAL_CALL 104*cdf0e10cSrcweir CharacterClassificationImpl::getCharacterType( const OUString& Text, sal_Int32 nPos, 105*cdf0e10cSrcweir const Locale& rLocale ) throw(RuntimeException) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir return getLocaleSpecificCharacterClassification(rLocale)->getCharacterType(Text, nPos, rLocale); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir sal_Int32 SAL_CALL 111*cdf0e10cSrcweir CharacterClassificationImpl::getStringType( const OUString& Text, sal_Int32 nPos, 112*cdf0e10cSrcweir sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir return getLocaleSpecificCharacterClassification(rLocale)->getStringType(Text, nPos, nCount, rLocale); 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir ParseResult SAL_CALL CharacterClassificationImpl::parseAnyToken( 118*cdf0e10cSrcweir const OUString& Text, sal_Int32 nPos, const Locale& rLocale, 119*cdf0e10cSrcweir sal_Int32 startCharTokenType, const OUString& userDefinedCharactersStart, 120*cdf0e10cSrcweir sal_Int32 contCharTokenType, const OUString& userDefinedCharactersCont ) 121*cdf0e10cSrcweir throw(RuntimeException) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir return getLocaleSpecificCharacterClassification(rLocale)->parseAnyToken(Text, nPos, rLocale, 124*cdf0e10cSrcweir startCharTokenType,userDefinedCharactersStart, 125*cdf0e10cSrcweir contCharTokenType, userDefinedCharactersCont); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir ParseResult SAL_CALL CharacterClassificationImpl::parsePredefinedToken( 130*cdf0e10cSrcweir sal_Int32 nTokenType, const OUString& Text, sal_Int32 nPos, 131*cdf0e10cSrcweir const Locale& rLocale, sal_Int32 startCharTokenType, 132*cdf0e10cSrcweir const OUString& userDefinedCharactersStart, sal_Int32 contCharTokenType, 133*cdf0e10cSrcweir const OUString& userDefinedCharactersCont ) throw(RuntimeException) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir return getLocaleSpecificCharacterClassification(rLocale)->parsePredefinedToken( 136*cdf0e10cSrcweir nTokenType, Text, nPos, rLocale, startCharTokenType, userDefinedCharactersStart, 137*cdf0e10cSrcweir contCharTokenType, userDefinedCharactersCont); 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir sal_Bool SAL_CALL CharacterClassificationImpl::createLocaleSpecificCharacterClassification(const OUString& serviceName, const Locale& rLocale) 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir // to share service between same Language but different Country code, like zh_CN and zh_SG 143*cdf0e10cSrcweir for (size_t l = 0; l < lookupTable.size(); l++) { 144*cdf0e10cSrcweir cachedItem = lookupTable[l]; 145*cdf0e10cSrcweir if (serviceName == cachedItem->aName) { 146*cdf0e10cSrcweir lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, cachedItem->xCI) ); 147*cdf0e10cSrcweir return sal_True; 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir Reference < XInterface > xI = xMSF->createInstance( 152*cdf0e10cSrcweir OUString::createFromAscii("com.sun.star.i18n.CharacterClassification_") + serviceName); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir Reference < XCharacterClassification > xCI; 155*cdf0e10cSrcweir if ( xI.is() ) { 156*cdf0e10cSrcweir xI->queryInterface(::getCppuType((const Reference< XCharacterClassification>*)0) ) >>= xCI; 157*cdf0e10cSrcweir if (xCI.is()) { 158*cdf0e10cSrcweir lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, xCI) ); 159*cdf0e10cSrcweir return sal_True; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir return sal_False; 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir Reference < XCharacterClassification > SAL_CALL 166*cdf0e10cSrcweir CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale& rLocale) 167*cdf0e10cSrcweir throw(RuntimeException) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir // reuse instance if locale didn't change 170*cdf0e10cSrcweir if (cachedItem && cachedItem->equals(rLocale)) 171*cdf0e10cSrcweir return cachedItem->xCI; 172*cdf0e10cSrcweir else if (xMSF.is()) { 173*cdf0e10cSrcweir for (size_t i = 0; i < lookupTable.size(); i++) { 174*cdf0e10cSrcweir cachedItem = lookupTable[i]; 175*cdf0e10cSrcweir if (cachedItem->equals(rLocale)) 176*cdf0e10cSrcweir return cachedItem->xCI; 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir static sal_Unicode under = (sal_Unicode)'_'; 180*cdf0e10cSrcweir static OUString tw(OUString::createFromAscii("TW")); 181*cdf0e10cSrcweir sal_Int32 l = rLocale.Language.getLength(); 182*cdf0e10cSrcweir sal_Int32 c = rLocale.Country.getLength(); 183*cdf0e10cSrcweir sal_Int32 v = rLocale.Variant.getLength(); 184*cdf0e10cSrcweir OUStringBuffer aBuf(l+c+v+3); 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir // load service with name <base>_<lang>_<country>_<varian> 187*cdf0e10cSrcweir if ((l > 0 && c > 0 && v > 0 && 188*cdf0e10cSrcweir createLocaleSpecificCharacterClassification(aBuf.append(rLocale.Language).append(under).append( 189*cdf0e10cSrcweir rLocale.Country).append(under).append(rLocale.Variant).makeStringAndClear(), rLocale)) || 190*cdf0e10cSrcweir // load service with name <base>_<lang>_<country> 191*cdf0e10cSrcweir (l > 0 && c > 0 && 192*cdf0e10cSrcweir createLocaleSpecificCharacterClassification(aBuf.append(rLocale.Language).append(under).append( 193*cdf0e10cSrcweir rLocale.Country).makeStringAndClear(), rLocale)) || 194*cdf0e10cSrcweir (l > 0 && c > 0 && rLocale.Language.compareToAscii("zh") == 0 && 195*cdf0e10cSrcweir (rLocale.Country.compareToAscii("HK") == 0 || 196*cdf0e10cSrcweir rLocale.Country.compareToAscii("MO") == 0) && 197*cdf0e10cSrcweir // if the country code is HK or MO, one more step to try TW. 198*cdf0e10cSrcweir createLocaleSpecificCharacterClassification(aBuf.append(rLocale.Language).append(under).append( 199*cdf0e10cSrcweir tw).makeStringAndClear(), rLocale)) || 200*cdf0e10cSrcweir (l > 0 && 201*cdf0e10cSrcweir // load service with name <base>_<lang> 202*cdf0e10cSrcweir createLocaleSpecificCharacterClassification(rLocale.Language, rLocale))) { 203*cdf0e10cSrcweir return cachedItem->xCI; 204*cdf0e10cSrcweir } else if (xUCI.is()) { 205*cdf0e10cSrcweir lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, OUString::createFromAscii("Unicode"), xUCI) ); 206*cdf0e10cSrcweir return cachedItem->xCI; 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir throw RuntimeException(); 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir const sal_Char cClass[] = "com.sun.star.i18n.CharacterClassification"; 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir OUString SAL_CALL 215*cdf0e10cSrcweir CharacterClassificationImpl::getImplementationName(void) 216*cdf0e10cSrcweir throw( RuntimeException ) 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir return OUString::createFromAscii(cClass); 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir sal_Bool SAL_CALL 222*cdf0e10cSrcweir CharacterClassificationImpl::supportsService(const rtl::OUString& rServiceName) 223*cdf0e10cSrcweir throw( RuntimeException ) 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir return !rServiceName.compareToAscii(cClass); 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir Sequence< OUString > SAL_CALL 229*cdf0e10cSrcweir CharacterClassificationImpl::getSupportedServiceNames(void) throw( RuntimeException ) 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir Sequence< OUString > aRet(1); 232*cdf0e10cSrcweir aRet[0] = OUString::createFromAscii(cClass); 233*cdf0e10cSrcweir return aRet; 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir } } } } 237