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