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 : #include <cppuhelper/supportsservice.hxx>
21 : #include <characterclassificationImpl.hxx>
22 : #include <localedata.hxx>
23 : #include <rtl/ustrbuf.hxx>
24 :
25 : using namespace com::sun::star::uno;
26 : using namespace com::sun::star::lang;
27 :
28 : namespace com { namespace sun { namespace star { namespace i18n {
29 :
30 60441 : CharacterClassificationImpl::CharacterClassificationImpl(
31 60441 : const Reference < uno::XComponentContext >& rxContext ) : m_xContext( rxContext )
32 : {
33 60441 : if (createLocaleSpecificCharacterClassification(OUString("Unicode"), Locale()))
34 60441 : xUCI = cachedItem->xCI;
35 60441 : }
36 :
37 180728 : CharacterClassificationImpl::~CharacterClassificationImpl() {
38 : // Clear lookuptable
39 175958 : for (size_t l = 0; l < lookupTable.size(); l++)
40 115714 : delete lookupTable[l];
41 60244 : lookupTable.clear();
42 120484 : }
43 :
44 :
45 : OUString SAL_CALL
46 3056107 : CharacterClassificationImpl::toUpper( const OUString& Text, sal_Int32 nPos,
47 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
48 : {
49 3056107 : return getLocaleSpecificCharacterClassification(rLocale)->toUpper(Text, nPos, nCount, rLocale);
50 : }
51 :
52 : OUString SAL_CALL
53 32157 : CharacterClassificationImpl::toLower( const OUString& Text, sal_Int32 nPos,
54 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
55 : {
56 32157 : return getLocaleSpecificCharacterClassification(rLocale)->toLower(Text, nPos, nCount, rLocale);
57 : }
58 :
59 : OUString SAL_CALL
60 10 : CharacterClassificationImpl::toTitle( const OUString& Text, sal_Int32 nPos,
61 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
62 : {
63 10 : return getLocaleSpecificCharacterClassification(rLocale)->toTitle(Text, nPos, nCount, rLocale);
64 : }
65 :
66 : sal_Int16 SAL_CALL
67 57528 : CharacterClassificationImpl::getType( const OUString& Text, sal_Int32 nPos )
68 : throw(RuntimeException, std::exception)
69 : {
70 57528 : if (xUCI.is())
71 115056 : return xUCI->getType(Text, nPos);
72 0 : throw RuntimeException();
73 : }
74 :
75 : sal_Int16 SAL_CALL
76 19727 : CharacterClassificationImpl::getCharacterDirection( const OUString& Text, sal_Int32 nPos )
77 : throw(RuntimeException, std::exception)
78 : {
79 19727 : if (xUCI.is())
80 39454 : return xUCI->getCharacterDirection(Text, nPos);
81 0 : throw RuntimeException();
82 : }
83 :
84 : sal_Int16 SAL_CALL
85 36 : CharacterClassificationImpl::getScript( const OUString& Text, sal_Int32 nPos )
86 : throw(RuntimeException, std::exception)
87 : {
88 36 : if (xUCI.is())
89 72 : return xUCI->getScript(Text, nPos);
90 0 : throw RuntimeException();
91 : }
92 :
93 : sal_Int32 SAL_CALL
94 233449 : CharacterClassificationImpl::getCharacterType( const OUString& Text, sal_Int32 nPos,
95 : const Locale& rLocale ) throw(RuntimeException, std::exception)
96 : {
97 233449 : return getLocaleSpecificCharacterClassification(rLocale)->getCharacterType(Text, nPos, rLocale);
98 : }
99 :
100 : sal_Int32 SAL_CALL
101 18824 : CharacterClassificationImpl::getStringType( const OUString& Text, sal_Int32 nPos,
102 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
103 : {
104 18824 : return getLocaleSpecificCharacterClassification(rLocale)->getStringType(Text, nPos, nCount, rLocale);
105 : }
106 :
107 36168 : ParseResult SAL_CALL CharacterClassificationImpl::parseAnyToken(
108 : const OUString& Text, sal_Int32 nPos, const Locale& rLocale,
109 : sal_Int32 startCharTokenType, const OUString& userDefinedCharactersStart,
110 : sal_Int32 contCharTokenType, const OUString& userDefinedCharactersCont )
111 : throw(RuntimeException, std::exception)
112 : {
113 72336 : return getLocaleSpecificCharacterClassification(rLocale)->parseAnyToken(Text, nPos, rLocale,
114 : startCharTokenType,userDefinedCharactersStart,
115 72336 : contCharTokenType, userDefinedCharactersCont);
116 : }
117 :
118 :
119 95338 : ParseResult SAL_CALL CharacterClassificationImpl::parsePredefinedToken(
120 : sal_Int32 nTokenType, const OUString& Text, sal_Int32 nPos,
121 : const Locale& rLocale, sal_Int32 startCharTokenType,
122 : const OUString& userDefinedCharactersStart, sal_Int32 contCharTokenType,
123 : const OUString& userDefinedCharactersCont ) throw(RuntimeException, std::exception)
124 : {
125 190676 : return getLocaleSpecificCharacterClassification(rLocale)->parsePredefinedToken(
126 : nTokenType, Text, nPos, rLocale, startCharTokenType, userDefinedCharactersStart,
127 190676 : contCharTokenType, userDefinedCharactersCont);
128 : }
129 :
130 171537 : bool SAL_CALL CharacterClassificationImpl::createLocaleSpecificCharacterClassification(const OUString& serviceName, const Locale& rLocale)
131 : {
132 : // to share service between same Language but different Country code, like zh_CN and zh_SG
133 282865 : for (size_t l = 0; l < lookupTable.size(); l++) {
134 111328 : cachedItem = lookupTable[l];
135 111328 : if (serviceName == cachedItem->aName) {
136 0 : lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, cachedItem->xCI) );
137 0 : return true;
138 : }
139 : }
140 :
141 343074 : Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
142 171537 : OUString("com.sun.star.i18n.CharacterClassification_") + serviceName, m_xContext);
143 :
144 343074 : Reference < XCharacterClassification > xCI;
145 171537 : if ( xI.is() ) {
146 60441 : xCI.set( xI, UNO_QUERY );
147 60441 : if (xCI.is()) {
148 60441 : lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, xCI) );
149 60441 : return true;
150 : }
151 : }
152 282633 : return false;
153 : }
154 :
155 : Reference < XCharacterClassification > SAL_CALL
156 3472053 : CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale& rLocale)
157 : throw(RuntimeException)
158 : {
159 : // reuse instance if locale didn't change
160 3472053 : if (cachedItem && cachedItem->equals(rLocale))
161 3416344 : return cachedItem->xCI;
162 : else {
163 111574 : for (size_t i = 0; i < lookupTable.size(); i++) {
164 55989 : cachedItem = lookupTable[i];
165 55989 : if (cachedItem->equals(rLocale))
166 124 : return cachedItem->xCI;
167 : }
168 :
169 : // Load service with name <base>_<lang>_<country> or
170 : // <base>_<bcp47> and fallbacks.
171 : bool bLoaded = createLocaleSpecificCharacterClassification(
172 55585 : LocaleDataImpl::getFirstLocaleServiceName( rLocale), rLocale);
173 55585 : if (!bLoaded)
174 : {
175 55585 : ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
176 111096 : for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
177 : {
178 55511 : bLoaded = createLocaleSpecificCharacterClassification( *it, rLocale);
179 55511 : if (bLoaded)
180 0 : break;
181 55585 : }
182 : }
183 55585 : if (bLoaded)
184 0 : return cachedItem->xCI;
185 55585 : else if (xUCI.is())
186 : {
187 55585 : lookupTable.push_back( cachedItem = new lookupTableItem( rLocale, OUString("Unicode"), xUCI));
188 55585 : return cachedItem->xCI;
189 : }
190 : }
191 0 : throw RuntimeException();
192 : }
193 :
194 : OUString SAL_CALL
195 0 : CharacterClassificationImpl::getImplementationName(void)
196 : throw( RuntimeException, std::exception )
197 : {
198 0 : return OUString("com.sun.star.i18n.CharacterClassification");
199 : }
200 :
201 : sal_Bool SAL_CALL
202 0 : CharacterClassificationImpl::supportsService(const OUString& rServiceName)
203 : throw( RuntimeException, std::exception )
204 : {
205 0 : return cppu::supportsService(this, rServiceName);
206 : }
207 :
208 : Sequence< OUString > SAL_CALL
209 0 : CharacterClassificationImpl::getSupportedServiceNames(void) throw( RuntimeException, std::exception )
210 : {
211 0 : Sequence< OUString > aRet(1);
212 0 : aRet[0] = OUString("com.sun.star.i18n.CharacterClassification");
213 0 : return aRet;
214 : }
215 :
216 : } } } }
217 :
218 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
219 60437 : com_sun_star_i18n_CharacterClassification_get_implementation(
220 : css::uno::XComponentContext *context,
221 : css::uno::Sequence<css::uno::Any> const &)
222 : {
223 60437 : return cppu::acquire(new css::i18n::CharacterClassificationImpl(context));
224 : }
225 :
226 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|