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 39615 : CharacterClassificationImpl::CharacterClassificationImpl(
31 39615 : const Reference < uno::XComponentContext >& rxContext ) : m_xContext( rxContext )
32 : {
33 39615 : if (createLocaleSpecificCharacterClassification(OUString("Unicode"), Locale()))
34 39615 : xUCI = cachedItem->xCI;
35 39615 : }
36 :
37 118465 : CharacterClassificationImpl::~CharacterClassificationImpl() {
38 : // Clear lookuptable
39 116816 : for (size_t l = 0; l < lookupTable.size(); l++)
40 77327 : delete lookupTable[l];
41 39489 : lookupTable.clear();
42 78976 : }
43 :
44 :
45 : OUString SAL_CALL
46 1956520 : CharacterClassificationImpl::toUpper( const OUString& Text, sal_Int32 nPos,
47 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
48 : {
49 1956520 : return getLocaleSpecificCharacterClassification(rLocale)->toUpper(Text, nPos, nCount, rLocale);
50 : }
51 :
52 : OUString SAL_CALL
53 11850221 : CharacterClassificationImpl::toLower( const OUString& Text, sal_Int32 nPos,
54 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
55 : {
56 11850221 : return getLocaleSpecificCharacterClassification(rLocale)->toLower(Text, nPos, nCount, rLocale);
57 : }
58 :
59 : OUString SAL_CALL
60 6 : CharacterClassificationImpl::toTitle( const OUString& Text, sal_Int32 nPos,
61 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
62 : {
63 6 : return getLocaleSpecificCharacterClassification(rLocale)->toTitle(Text, nPos, nCount, rLocale);
64 : }
65 :
66 : sal_Int16 SAL_CALL
67 29253 : CharacterClassificationImpl::getType( const OUString& Text, sal_Int32 nPos )
68 : throw(RuntimeException, std::exception)
69 : {
70 29253 : if (xUCI.is())
71 58506 : return xUCI->getType(Text, nPos);
72 0 : throw RuntimeException();
73 : }
74 :
75 : sal_Int16 SAL_CALL
76 17322 : CharacterClassificationImpl::getCharacterDirection( const OUString& Text, sal_Int32 nPos )
77 : throw(RuntimeException, std::exception)
78 : {
79 17322 : if (xUCI.is())
80 34644 : return xUCI->getCharacterDirection(Text, nPos);
81 0 : throw RuntimeException();
82 : }
83 :
84 : sal_Int16 SAL_CALL
85 18 : CharacterClassificationImpl::getScript( const OUString& Text, sal_Int32 nPos )
86 : throw(RuntimeException, std::exception)
87 : {
88 18 : if (xUCI.is())
89 36 : return xUCI->getScript(Text, nPos);
90 0 : throw RuntimeException();
91 : }
92 :
93 : sal_Int32 SAL_CALL
94 748118796 : CharacterClassificationImpl::getCharacterType( const OUString& Text, sal_Int32 nPos,
95 : const Locale& rLocale ) throw(RuntimeException, std::exception)
96 : {
97 748118796 : return getLocaleSpecificCharacterClassification(rLocale)->getCharacterType(Text, nPos, rLocale);
98 : }
99 :
100 : sal_Int32 SAL_CALL
101 91077762 : CharacterClassificationImpl::getStringType( const OUString& Text, sal_Int32 nPos,
102 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
103 : {
104 91077762 : return getLocaleSpecificCharacterClassification(rLocale)->getStringType(Text, nPos, nCount, rLocale);
105 : }
106 :
107 18417 : 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 36834 : return getLocaleSpecificCharacterClassification(rLocale)->parseAnyToken(Text, nPos, rLocale,
114 : startCharTokenType,userDefinedCharactersStart,
115 36834 : contCharTokenType, userDefinedCharactersCont);
116 : }
117 :
118 :
119 49133 : 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 98266 : return getLocaleSpecificCharacterClassification(rLocale)->parsePredefinedToken(
126 : nTokenType, Text, nPos, rLocale, startCharTokenType, userDefinedCharactersStart,
127 98266 : contCharTokenType, userDefinedCharactersCont);
128 : }
129 :
130 115438 : 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 191510 : for (size_t l = 0; l < lookupTable.size(); l++) {
134 76072 : cachedItem = lookupTable[l];
135 76072 : if (serviceName == cachedItem->aName) {
136 0 : lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, cachedItem->xCI) );
137 0 : return true;
138 : }
139 : }
140 :
141 230876 : Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
142 115438 : "com.sun.star.i18n.CharacterClassification_" + serviceName, m_xContext);
143 :
144 230876 : Reference < XCharacterClassification > xCI;
145 115438 : if ( xI.is() ) {
146 39615 : xCI.set( xI, UNO_QUERY );
147 39615 : if (xCI.is()) {
148 39615 : lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, xCI) );
149 39615 : return true;
150 : }
151 : }
152 191261 : return false;
153 : }
154 :
155 : Reference < XCharacterClassification > SAL_CALL
156 853070855 : CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale& rLocale)
157 : throw(RuntimeException)
158 : {
159 : // reuse instance if locale didn't change
160 853070855 : if (cachedItem && cachedItem->equals(rLocale))
161 853032726 : return cachedItem->xCI;
162 : else {
163 76455 : for (size_t i = 0; i < lookupTable.size(); i++) {
164 38521 : cachedItem = lookupTable[i];
165 38521 : if (cachedItem->equals(rLocale))
166 195 : return cachedItem->xCI;
167 : }
168 :
169 : // Load service with name <base>_<lang>_<country> or
170 : // <base>_<bcp47> and fallbacks.
171 : bool bLoaded = createLocaleSpecificCharacterClassification(
172 37934 : LocaleDataImpl::getFirstLocaleServiceName( rLocale), rLocale);
173 37934 : if (!bLoaded)
174 : {
175 37934 : ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
176 75823 : for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
177 : {
178 37889 : bLoaded = createLocaleSpecificCharacterClassification( *it, rLocale);
179 37889 : if (bLoaded)
180 0 : break;
181 37934 : }
182 : }
183 37934 : if (bLoaded)
184 0 : return cachedItem->xCI;
185 37934 : else if (xUCI.is())
186 : {
187 37934 : lookupTable.push_back( cachedItem = new lookupTableItem( rLocale, OUString("Unicode"), xUCI));
188 37934 : return cachedItem->xCI;
189 : }
190 : }
191 0 : throw RuntimeException();
192 : }
193 :
194 : OUString SAL_CALL
195 1 : CharacterClassificationImpl::getImplementationName()
196 : throw( RuntimeException, std::exception )
197 : {
198 1 : 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 1 : CharacterClassificationImpl::getSupportedServiceNames() throw( RuntimeException, std::exception )
210 : {
211 1 : Sequence< OUString > aRet(1);
212 1 : aRet[0] = "com.sun.star.i18n.CharacterClassification";
213 1 : return aRet;
214 : }
215 :
216 : } } } }
217 :
218 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
219 39613 : com_sun_star_i18n_CharacterClassification_get_implementation(
220 : css::uno::XComponentContext *context,
221 : css::uno::Sequence<css::uno::Any> const &)
222 : {
223 39613 : return cppu::acquire(new css::i18n::CharacterClassificationImpl(context));
224 : }
225 :
226 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|