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