1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove
25*b1cdbd2cSJim Jagielski #include "precompiled_i18npool.hxx"
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski #include <characterclassificationImpl.hxx>
28*b1cdbd2cSJim Jagielski #include <rtl/ustrbuf.hxx>
29*b1cdbd2cSJim Jagielski
30*b1cdbd2cSJim Jagielski using namespace com::sun::star::uno;
31*b1cdbd2cSJim Jagielski using namespace com::sun::star::lang;
32*b1cdbd2cSJim Jagielski using namespace rtl;
33*b1cdbd2cSJim Jagielski
34*b1cdbd2cSJim Jagielski namespace com { namespace sun { namespace star { namespace i18n {
35*b1cdbd2cSJim Jagielski
CharacterClassificationImpl(const Reference<lang::XMultiServiceFactory> & rxMSF)36*b1cdbd2cSJim Jagielski CharacterClassificationImpl::CharacterClassificationImpl(
37*b1cdbd2cSJim Jagielski const Reference < lang::XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF )
38*b1cdbd2cSJim Jagielski {
39*b1cdbd2cSJim Jagielski if (createLocaleSpecificCharacterClassification(OUString::createFromAscii("Unicode"), Locale()))
40*b1cdbd2cSJim Jagielski xUCI = cachedItem->xCI;
41*b1cdbd2cSJim Jagielski }
42*b1cdbd2cSJim Jagielski
~CharacterClassificationImpl()43*b1cdbd2cSJim Jagielski CharacterClassificationImpl::~CharacterClassificationImpl() {
44*b1cdbd2cSJim Jagielski // Clear lookuptable
45*b1cdbd2cSJim Jagielski for (size_t l = 0; l < lookupTable.size(); l++)
46*b1cdbd2cSJim Jagielski delete lookupTable[l];
47*b1cdbd2cSJim Jagielski lookupTable.clear();
48*b1cdbd2cSJim Jagielski }
49*b1cdbd2cSJim Jagielski
50*b1cdbd2cSJim Jagielski
51*b1cdbd2cSJim Jagielski OUString SAL_CALL
toUpper(const OUString & Text,sal_Int32 nPos,sal_Int32 nCount,const Locale & rLocale)52*b1cdbd2cSJim Jagielski CharacterClassificationImpl::toUpper( const OUString& Text, sal_Int32 nPos,
53*b1cdbd2cSJim Jagielski sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
54*b1cdbd2cSJim Jagielski {
55*b1cdbd2cSJim Jagielski return getLocaleSpecificCharacterClassification(rLocale)->toUpper(Text, nPos, nCount, rLocale);
56*b1cdbd2cSJim Jagielski }
57*b1cdbd2cSJim Jagielski
58*b1cdbd2cSJim Jagielski OUString SAL_CALL
toLower(const OUString & Text,sal_Int32 nPos,sal_Int32 nCount,const Locale & rLocale)59*b1cdbd2cSJim Jagielski CharacterClassificationImpl::toLower( const OUString& Text, sal_Int32 nPos,
60*b1cdbd2cSJim Jagielski sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
61*b1cdbd2cSJim Jagielski {
62*b1cdbd2cSJim Jagielski return getLocaleSpecificCharacterClassification(rLocale)->toLower(Text, nPos, nCount, rLocale);
63*b1cdbd2cSJim Jagielski }
64*b1cdbd2cSJim Jagielski
65*b1cdbd2cSJim Jagielski OUString SAL_CALL
toTitle(const OUString & Text,sal_Int32 nPos,sal_Int32 nCount,const Locale & rLocale)66*b1cdbd2cSJim Jagielski CharacterClassificationImpl::toTitle( const OUString& Text, sal_Int32 nPos,
67*b1cdbd2cSJim Jagielski sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
68*b1cdbd2cSJim Jagielski {
69*b1cdbd2cSJim Jagielski return getLocaleSpecificCharacterClassification(rLocale)->toTitle(Text, nPos, nCount, rLocale);
70*b1cdbd2cSJim Jagielski }
71*b1cdbd2cSJim Jagielski
72*b1cdbd2cSJim Jagielski sal_Int16 SAL_CALL
getType(const OUString & Text,sal_Int32 nPos)73*b1cdbd2cSJim Jagielski CharacterClassificationImpl::getType( const OUString& Text, sal_Int32 nPos )
74*b1cdbd2cSJim Jagielski throw(RuntimeException)
75*b1cdbd2cSJim Jagielski {
76*b1cdbd2cSJim Jagielski if (xUCI.is())
77*b1cdbd2cSJim Jagielski return xUCI->getType(Text, nPos);
78*b1cdbd2cSJim Jagielski throw RuntimeException();
79*b1cdbd2cSJim Jagielski }
80*b1cdbd2cSJim Jagielski
81*b1cdbd2cSJim Jagielski sal_Int16 SAL_CALL
getCharacterDirection(const OUString & Text,sal_Int32 nPos)82*b1cdbd2cSJim Jagielski CharacterClassificationImpl::getCharacterDirection( const OUString& Text, sal_Int32 nPos )
83*b1cdbd2cSJim Jagielski throw(RuntimeException)
84*b1cdbd2cSJim Jagielski {
85*b1cdbd2cSJim Jagielski if (xUCI.is())
86*b1cdbd2cSJim Jagielski return xUCI->getCharacterDirection(Text, nPos);
87*b1cdbd2cSJim Jagielski throw RuntimeException();
88*b1cdbd2cSJim Jagielski }
89*b1cdbd2cSJim Jagielski
90*b1cdbd2cSJim Jagielski sal_Int16 SAL_CALL
getScript(const OUString & Text,sal_Int32 nPos)91*b1cdbd2cSJim Jagielski CharacterClassificationImpl::getScript( const OUString& Text, sal_Int32 nPos )
92*b1cdbd2cSJim Jagielski throw(RuntimeException)
93*b1cdbd2cSJim Jagielski {
94*b1cdbd2cSJim Jagielski if (xUCI.is())
95*b1cdbd2cSJim Jagielski return xUCI->getScript(Text, nPos);
96*b1cdbd2cSJim Jagielski throw RuntimeException();
97*b1cdbd2cSJim Jagielski }
98*b1cdbd2cSJim Jagielski
99*b1cdbd2cSJim Jagielski sal_Int32 SAL_CALL
getCharacterType(const OUString & Text,sal_Int32 nPos,const Locale & rLocale)100*b1cdbd2cSJim Jagielski CharacterClassificationImpl::getCharacterType( const OUString& Text, sal_Int32 nPos,
101*b1cdbd2cSJim Jagielski const Locale& rLocale ) throw(RuntimeException)
102*b1cdbd2cSJim Jagielski {
103*b1cdbd2cSJim Jagielski return getLocaleSpecificCharacterClassification(rLocale)->getCharacterType(Text, nPos, rLocale);
104*b1cdbd2cSJim Jagielski }
105*b1cdbd2cSJim Jagielski
106*b1cdbd2cSJim Jagielski sal_Int32 SAL_CALL
getStringType(const OUString & Text,sal_Int32 nPos,sal_Int32 nCount,const Locale & rLocale)107*b1cdbd2cSJim Jagielski CharacterClassificationImpl::getStringType( const OUString& Text, sal_Int32 nPos,
108*b1cdbd2cSJim Jagielski sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
109*b1cdbd2cSJim Jagielski {
110*b1cdbd2cSJim Jagielski return getLocaleSpecificCharacterClassification(rLocale)->getStringType(Text, nPos, nCount, rLocale);
111*b1cdbd2cSJim Jagielski }
112*b1cdbd2cSJim Jagielski
parseAnyToken(const OUString & Text,sal_Int32 nPos,const Locale & rLocale,sal_Int32 startCharTokenType,const OUString & userDefinedCharactersStart,sal_Int32 contCharTokenType,const OUString & userDefinedCharactersCont)113*b1cdbd2cSJim Jagielski ParseResult SAL_CALL CharacterClassificationImpl::parseAnyToken(
114*b1cdbd2cSJim Jagielski const OUString& Text, sal_Int32 nPos, const Locale& rLocale,
115*b1cdbd2cSJim Jagielski sal_Int32 startCharTokenType, const OUString& userDefinedCharactersStart,
116*b1cdbd2cSJim Jagielski sal_Int32 contCharTokenType, const OUString& userDefinedCharactersCont )
117*b1cdbd2cSJim Jagielski throw(RuntimeException)
118*b1cdbd2cSJim Jagielski {
119*b1cdbd2cSJim Jagielski return getLocaleSpecificCharacterClassification(rLocale)->parseAnyToken(Text, nPos, rLocale,
120*b1cdbd2cSJim Jagielski startCharTokenType,userDefinedCharactersStart,
121*b1cdbd2cSJim Jagielski contCharTokenType, userDefinedCharactersCont);
122*b1cdbd2cSJim Jagielski }
123*b1cdbd2cSJim Jagielski
124*b1cdbd2cSJim Jagielski
parsePredefinedToken(sal_Int32 nTokenType,const OUString & Text,sal_Int32 nPos,const Locale & rLocale,sal_Int32 startCharTokenType,const OUString & userDefinedCharactersStart,sal_Int32 contCharTokenType,const OUString & userDefinedCharactersCont)125*b1cdbd2cSJim Jagielski ParseResult SAL_CALL CharacterClassificationImpl::parsePredefinedToken(
126*b1cdbd2cSJim Jagielski sal_Int32 nTokenType, const OUString& Text, sal_Int32 nPos,
127*b1cdbd2cSJim Jagielski const Locale& rLocale, sal_Int32 startCharTokenType,
128*b1cdbd2cSJim Jagielski const OUString& userDefinedCharactersStart, sal_Int32 contCharTokenType,
129*b1cdbd2cSJim Jagielski const OUString& userDefinedCharactersCont ) throw(RuntimeException)
130*b1cdbd2cSJim Jagielski {
131*b1cdbd2cSJim Jagielski return getLocaleSpecificCharacterClassification(rLocale)->parsePredefinedToken(
132*b1cdbd2cSJim Jagielski nTokenType, Text, nPos, rLocale, startCharTokenType, userDefinedCharactersStart,
133*b1cdbd2cSJim Jagielski contCharTokenType, userDefinedCharactersCont);
134*b1cdbd2cSJim Jagielski }
135*b1cdbd2cSJim Jagielski
createLocaleSpecificCharacterClassification(const OUString & serviceName,const Locale & rLocale)136*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL CharacterClassificationImpl::createLocaleSpecificCharacterClassification(const OUString& serviceName, const Locale& rLocale)
137*b1cdbd2cSJim Jagielski {
138*b1cdbd2cSJim Jagielski // to share service between same Language but different Country code, like zh_CN and zh_SG
139*b1cdbd2cSJim Jagielski for (size_t l = 0; l < lookupTable.size(); l++) {
140*b1cdbd2cSJim Jagielski cachedItem = lookupTable[l];
141*b1cdbd2cSJim Jagielski if (serviceName == cachedItem->aName) {
142*b1cdbd2cSJim Jagielski lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, cachedItem->xCI) );
143*b1cdbd2cSJim Jagielski return sal_True;
144*b1cdbd2cSJim Jagielski }
145*b1cdbd2cSJim Jagielski }
146*b1cdbd2cSJim Jagielski
147*b1cdbd2cSJim Jagielski Reference < XInterface > xI = xMSF->createInstance(
148*b1cdbd2cSJim Jagielski OUString::createFromAscii("com.sun.star.i18n.CharacterClassification_") + serviceName);
149*b1cdbd2cSJim Jagielski
150*b1cdbd2cSJim Jagielski Reference < XCharacterClassification > xCI;
151*b1cdbd2cSJim Jagielski if ( xI.is() ) {
152*b1cdbd2cSJim Jagielski xI->queryInterface(::getCppuType((const Reference< XCharacterClassification>*)0) ) >>= xCI;
153*b1cdbd2cSJim Jagielski if (xCI.is()) {
154*b1cdbd2cSJim Jagielski lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, xCI) );
155*b1cdbd2cSJim Jagielski return sal_True;
156*b1cdbd2cSJim Jagielski }
157*b1cdbd2cSJim Jagielski }
158*b1cdbd2cSJim Jagielski return sal_False;
159*b1cdbd2cSJim Jagielski }
160*b1cdbd2cSJim Jagielski
161*b1cdbd2cSJim Jagielski Reference < XCharacterClassification > SAL_CALL
getLocaleSpecificCharacterClassification(const Locale & rLocale)162*b1cdbd2cSJim Jagielski CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale& rLocale)
163*b1cdbd2cSJim Jagielski throw(RuntimeException)
164*b1cdbd2cSJim Jagielski {
165*b1cdbd2cSJim Jagielski // reuse instance if locale didn't change
166*b1cdbd2cSJim Jagielski if (cachedItem && cachedItem->equals(rLocale))
167*b1cdbd2cSJim Jagielski return cachedItem->xCI;
168*b1cdbd2cSJim Jagielski else if (xMSF.is()) {
169*b1cdbd2cSJim Jagielski for (size_t i = 0; i < lookupTable.size(); i++) {
170*b1cdbd2cSJim Jagielski cachedItem = lookupTable[i];
171*b1cdbd2cSJim Jagielski if (cachedItem->equals(rLocale))
172*b1cdbd2cSJim Jagielski return cachedItem->xCI;
173*b1cdbd2cSJim Jagielski }
174*b1cdbd2cSJim Jagielski
175*b1cdbd2cSJim Jagielski static sal_Unicode under = (sal_Unicode)'_';
176*b1cdbd2cSJim Jagielski static OUString tw(OUString::createFromAscii("TW"));
177*b1cdbd2cSJim Jagielski sal_Int32 l = rLocale.Language.getLength();
178*b1cdbd2cSJim Jagielski sal_Int32 c = rLocale.Country.getLength();
179*b1cdbd2cSJim Jagielski sal_Int32 v = rLocale.Variant.getLength();
180*b1cdbd2cSJim Jagielski OUStringBuffer aBuf(l+c+v+3);
181*b1cdbd2cSJim Jagielski
182*b1cdbd2cSJim Jagielski // load service with name <base>_<lang>_<country>_<varian>
183*b1cdbd2cSJim Jagielski if ((l > 0 && c > 0 && v > 0 &&
184*b1cdbd2cSJim Jagielski createLocaleSpecificCharacterClassification(aBuf.append(rLocale.Language).append(under).append(
185*b1cdbd2cSJim Jagielski rLocale.Country).append(under).append(rLocale.Variant).makeStringAndClear(), rLocale)) ||
186*b1cdbd2cSJim Jagielski // load service with name <base>_<lang>_<country>
187*b1cdbd2cSJim Jagielski (l > 0 && c > 0 &&
188*b1cdbd2cSJim Jagielski createLocaleSpecificCharacterClassification(aBuf.append(rLocale.Language).append(under).append(
189*b1cdbd2cSJim Jagielski rLocale.Country).makeStringAndClear(), rLocale)) ||
190*b1cdbd2cSJim Jagielski (l > 0 && c > 0 && rLocale.Language.compareToAscii("zh") == 0 &&
191*b1cdbd2cSJim Jagielski (rLocale.Country.compareToAscii("HK") == 0 ||
192*b1cdbd2cSJim Jagielski rLocale.Country.compareToAscii("MO") == 0) &&
193*b1cdbd2cSJim Jagielski // if the country code is HK or MO, one more step to try TW.
194*b1cdbd2cSJim Jagielski createLocaleSpecificCharacterClassification(aBuf.append(rLocale.Language).append(under).append(
195*b1cdbd2cSJim Jagielski tw).makeStringAndClear(), rLocale)) ||
196*b1cdbd2cSJim Jagielski (l > 0 &&
197*b1cdbd2cSJim Jagielski // load service with name <base>_<lang>
198*b1cdbd2cSJim Jagielski createLocaleSpecificCharacterClassification(rLocale.Language, rLocale))) {
199*b1cdbd2cSJim Jagielski return cachedItem->xCI;
200*b1cdbd2cSJim Jagielski } else if (xUCI.is()) {
201*b1cdbd2cSJim Jagielski lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, OUString::createFromAscii("Unicode"), xUCI) );
202*b1cdbd2cSJim Jagielski return cachedItem->xCI;
203*b1cdbd2cSJim Jagielski }
204*b1cdbd2cSJim Jagielski }
205*b1cdbd2cSJim Jagielski throw RuntimeException();
206*b1cdbd2cSJim Jagielski }
207*b1cdbd2cSJim Jagielski
208*b1cdbd2cSJim Jagielski const sal_Char cClass[] = "com.sun.star.i18n.CharacterClassification";
209*b1cdbd2cSJim Jagielski
210*b1cdbd2cSJim Jagielski OUString SAL_CALL
getImplementationName(void)211*b1cdbd2cSJim Jagielski CharacterClassificationImpl::getImplementationName(void)
212*b1cdbd2cSJim Jagielski throw( RuntimeException )
213*b1cdbd2cSJim Jagielski {
214*b1cdbd2cSJim Jagielski return OUString::createFromAscii(cClass);
215*b1cdbd2cSJim Jagielski }
216*b1cdbd2cSJim Jagielski
217*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL
supportsService(const rtl::OUString & rServiceName)218*b1cdbd2cSJim Jagielski CharacterClassificationImpl::supportsService(const rtl::OUString& rServiceName)
219*b1cdbd2cSJim Jagielski throw( RuntimeException )
220*b1cdbd2cSJim Jagielski {
221*b1cdbd2cSJim Jagielski return !rServiceName.compareToAscii(cClass);
222*b1cdbd2cSJim Jagielski }
223*b1cdbd2cSJim Jagielski
224*b1cdbd2cSJim Jagielski Sequence< OUString > SAL_CALL
getSupportedServiceNames(void)225*b1cdbd2cSJim Jagielski CharacterClassificationImpl::getSupportedServiceNames(void) throw( RuntimeException )
226*b1cdbd2cSJim Jagielski {
227*b1cdbd2cSJim Jagielski Sequence< OUString > aRet(1);
228*b1cdbd2cSJim Jagielski aRet[0] = OUString::createFromAscii(cClass);
229*b1cdbd2cSJim Jagielski return aRet;
230*b1cdbd2cSJim Jagielski }
231*b1cdbd2cSJim Jagielski
232*b1cdbd2cSJim Jagielski } } } }
233