Branch data 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 : 34872 : CharacterClassificationImpl::CharacterClassificationImpl(
33 [ + - ]: 34872 : const Reference < lang::XMultiServiceFactory >& rxMSF ) : xMSF( rxMSF )
34 : : {
35 [ + - ][ + - ]: 34872 : if (createLocaleSpecificCharacterClassification(OUString("Unicode"), Locale()))
36 [ + - ]: 34872 : xUCI = cachedItem->xCI;
37 : 34872 : }
38 : :
39 : 34612 : CharacterClassificationImpl::~CharacterClassificationImpl() {
40 : : // Clear lookuptable
41 [ + + ]: 98951 : for (size_t l = 0; l < lookupTable.size(); l++)
42 [ + - ][ + - ]: 64339 : delete lookupTable[l];
43 : 34612 : lookupTable.clear();
44 [ - + ]: 69224 : }
45 : :
46 : :
47 : : OUString SAL_CALL
48 : 859538 : CharacterClassificationImpl::toUpper( const OUString& Text, sal_Int32 nPos,
49 : : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
50 : : {
51 [ + - ][ + - ]: 859538 : return getLocaleSpecificCharacterClassification(rLocale)->toUpper(Text, nPos, nCount, rLocale);
52 : : }
53 : :
54 : : OUString SAL_CALL
55 : 34319 : CharacterClassificationImpl::toLower( const OUString& Text, sal_Int32 nPos,
56 : : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
57 : : {
58 [ + - ][ + - ]: 34319 : return getLocaleSpecificCharacterClassification(rLocale)->toLower(Text, nPos, nCount, rLocale);
59 : : }
60 : :
61 : : OUString SAL_CALL
62 : 6 : CharacterClassificationImpl::toTitle( const OUString& Text, sal_Int32 nPos,
63 : : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
64 : : {
65 [ + - ][ + - ]: 6 : return getLocaleSpecificCharacterClassification(rLocale)->toTitle(Text, nPos, nCount, rLocale);
66 : : }
67 : :
68 : : sal_Int16 SAL_CALL
69 : 40278 : CharacterClassificationImpl::getType( const OUString& Text, sal_Int32 nPos )
70 : : throw(RuntimeException)
71 : : {
72 [ + - ]: 40278 : if (xUCI.is())
73 : 40278 : return xUCI->getType(Text, nPos);
74 [ # # ]: 0 : throw RuntimeException();
75 : : }
76 : :
77 : : sal_Int16 SAL_CALL
78 : 11210 : CharacterClassificationImpl::getCharacterDirection( const OUString& Text, sal_Int32 nPos )
79 : : throw(RuntimeException)
80 : : {
81 [ + - ]: 11210 : if (xUCI.is())
82 : 11210 : 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 : 182160 : CharacterClassificationImpl::getCharacterType( const OUString& Text, sal_Int32 nPos,
97 : : const Locale& rLocale ) throw(RuntimeException)
98 : : {
99 [ + - ][ + - ]: 182160 : return getLocaleSpecificCharacterClassification(rLocale)->getCharacterType(Text, nPos, rLocale);
100 : : }
101 : :
102 : : sal_Int32 SAL_CALL
103 : 46714 : CharacterClassificationImpl::getStringType( const OUString& Text, sal_Int32 nPos,
104 : : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
105 : : {
106 [ + - ][ + - ]: 46714 : return getLocaleSpecificCharacterClassification(rLocale)->getStringType(Text, nPos, nCount, rLocale);
107 : : }
108 : :
109 : 26969 : 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 [ + - ]: 53938 : return getLocaleSpecificCharacterClassification(rLocale)->parseAnyToken(Text, nPos, rLocale,
116 : : startCharTokenType,userDefinedCharactersStart,
117 [ + - ]: 53938 : contCharTokenType, userDefinedCharactersCont);
118 : : }
119 : :
120 : :
121 : 27337 : 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 [ + - ]: 54674 : return getLocaleSpecificCharacterClassification(rLocale)->parsePredefinedToken(
128 : : nTokenType, Text, nPos, rLocale, startCharTokenType, userDefinedCharactersStart,
129 [ + - ]: 54674 : contCharTokenType, userDefinedCharactersCont);
130 : : }
131 : :
132 : 94791 : 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 [ + + ]: 154798 : for (size_t l = 0; l < lookupTable.size(); l++) {
136 : 60007 : cachedItem = lookupTable[l];
137 [ - + ]: 60007 : 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 [ + - ]: 94791 : Reference < XInterface > xI = xMSF->createInstance(
144 [ + - ]: 94791 : OUString("com.sun.star.i18n.CharacterClassification_") + serviceName);
145 : :
146 : 94791 : Reference < XCharacterClassification > xCI;
147 [ + + ]: 94791 : if ( xI.is() ) {
148 [ + - ][ + - ]: 34872 : xI->queryInterface(::getCppuType((const Reference< XCharacterClassification>*)0) ) >>= xCI;
[ + - ][ + - ]
149 [ + - ]: 34872 : if (xCI.is()) {
150 [ + - ][ + - ]: 34872 : lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, xCI) );
[ + - ]
151 : 34872 : return sal_True;
152 : : }
153 : : }
154 : 94791 : return sal_False;
155 : : }
156 : :
157 : : Reference < XCharacterClassification > SAL_CALL
158 : 1177043 : CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale& rLocale)
159 : : throw(RuntimeException)
160 : : {
161 : : // reuse instance if locale didn't change
162 [ + - ][ + + ]: 1177043 : if (cachedItem && cachedItem->equals(rLocale))
[ + + ]
163 : 1147042 : return cachedItem->xCI;
164 [ + - ]: 30001 : else if (xMSF.is()) {
165 [ + + ]: 60047 : for (size_t i = 0; i < lookupTable.size(); i++) {
166 : 30086 : cachedItem = lookupTable[i];
167 [ + + ]: 30086 : if (cachedItem->equals(rLocale))
168 : 40 : return cachedItem->xCI;
169 : : }
170 : :
171 : : static sal_Unicode under = (sal_Unicode)'_';
172 : 29961 : sal_Int32 l = rLocale.Language.getLength();
173 : 29961 : sal_Int32 c = rLocale.Country.getLength();
174 : 29961 : sal_Int32 v = rLocale.Variant.getLength();
175 : 29961 : OUStringBuffer aBuf(l+c+v+3);
176 : :
177 : : // load service with name <base>_<lang>_<country>_<varian>
178 [ + + ][ - + ]: 149799 : if ((l > 0 && c > 0 && v > 0 &&
[ # # ][ + - ]
[ + + ][ + - ]
[ + - ][ + +
- + # # #
# ][ # # ]
[ + - ][ - + ]
[ - + ][ + - ]
179 [ # # ][ # # ]: 0 : createLocaleSpecificCharacterClassification(aBuf.append(rLocale.Language).append(under).append(
180 [ # # ][ # # ]: 29961 : rLocale.Country).append(under).append(rLocale.Variant).makeStringAndClear(), rLocale)) ||
[ # # ][ # # ]
[ # # ][ - + ]
[ # # ]
181 : : // load service with name <base>_<lang>_<country>
182 : : (l > 0 && c > 0 &&
183 [ + - ][ + - ]: 29958 : createLocaleSpecificCharacterClassification(aBuf.append(rLocale.Language).append(under).append(
184 [ + - ][ + - ]: 59919 : rLocale.Country).makeStringAndClear(), rLocale)) ||
[ + - ][ + + ]
[ # # ]
185 : 29958 : (l > 0 && c > 0 && rLocale.Language.compareToAscii("zh") == 0 &&
186 : 0 : (rLocale.Country.compareToAscii("HK") == 0 ||
187 : 0 : 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 [ # # ][ # # ]: 29961 : "TW").makeStringAndClear(), rLocale)) ||
[ # # ][ - + ]
[ # # ]
191 : : (l > 0 &&
192 : : // load service with name <base>_<lang>
193 [ + - ]: 29961 : createLocaleSpecificCharacterClassification(rLocale.Language, rLocale))) {
194 : 0 : return cachedItem->xCI;
195 [ + - ]: 29961 : } else if (xUCI.is()) {
196 [ + - ][ + - ]: 29961 : lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, OUString("Unicode"), xUCI) );
[ + - ]
197 : 29961 : return cachedItem->xCI;
198 [ - + ]: 30001 : }
199 : : }
200 [ # # ]: 1177043 : 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: */
|