/src/libreoffice/i18npool/source/characterclassification/characterclassificationImpl.cxx
Line | Count | Source |
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 | | |
24 | | #include <com/sun/star/uno/XComponentContext.hpp> |
25 | | |
26 | | using namespace com::sun::star::uno; |
27 | | using namespace ::com::sun::star::i18n; |
28 | | using namespace com::sun::star::lang; |
29 | | |
30 | | namespace i18npool { |
31 | | |
32 | | CharacterClassificationImpl::CharacterClassificationImpl( |
33 | 624k | const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext ) |
34 | 624k | { |
35 | 624k | if (createLocaleSpecificCharacterClassification(u"Unicode"_ustr, Locale())) |
36 | 624k | xUCI = cachedItem->xCI; |
37 | 624k | } |
38 | | |
39 | 624k | CharacterClassificationImpl::~CharacterClassificationImpl() { |
40 | 624k | } |
41 | | |
42 | | |
43 | | OUString SAL_CALL |
44 | | CharacterClassificationImpl::toUpper( const OUString& Text, sal_Int32 nPos, |
45 | | sal_Int32 nCount, const Locale& rLocale ) |
46 | 114M | { |
47 | 114M | return getLocaleSpecificCharacterClassification(rLocale)->toUpper(Text, nPos, nCount, rLocale); |
48 | 114M | } |
49 | | |
50 | | OUString SAL_CALL |
51 | | CharacterClassificationImpl::toLower( const OUString& Text, sal_Int32 nPos, |
52 | | sal_Int32 nCount, const Locale& rLocale ) |
53 | 2.06M | { |
54 | 2.06M | return getLocaleSpecificCharacterClassification(rLocale)->toLower(Text, nPos, nCount, rLocale); |
55 | 2.06M | } |
56 | | |
57 | | OUString SAL_CALL |
58 | | CharacterClassificationImpl::toTitle( const OUString& Text, sal_Int32 nPos, |
59 | | sal_Int32 nCount, const Locale& rLocale ) |
60 | 0 | { |
61 | 0 | return getLocaleSpecificCharacterClassification(rLocale)->toTitle(Text, nPos, nCount, rLocale); |
62 | 0 | } |
63 | | |
64 | | sal_Int16 SAL_CALL |
65 | | CharacterClassificationImpl::getType( const OUString& Text, sal_Int32 nPos ) |
66 | 1.41M | { |
67 | 1.41M | if (xUCI.is()) |
68 | 1.41M | return xUCI->getType(Text, nPos); |
69 | 0 | throw RuntimeException(); |
70 | 1.41M | } |
71 | | |
72 | | sal_Int16 SAL_CALL |
73 | | CharacterClassificationImpl::getCharacterDirection( const OUString& Text, sal_Int32 nPos ) |
74 | 0 | { |
75 | 0 | if (xUCI.is()) |
76 | 0 | return xUCI->getCharacterDirection(Text, nPos); |
77 | 0 | throw RuntimeException(); |
78 | 0 | } |
79 | | |
80 | | sal_Int16 SAL_CALL |
81 | | CharacterClassificationImpl::getScript( const OUString& Text, sal_Int32 nPos ) |
82 | 1.47k | { |
83 | 1.47k | if (xUCI.is()) |
84 | 1.47k | return xUCI->getScript(Text, nPos); |
85 | 0 | throw RuntimeException(); |
86 | 1.47k | } |
87 | | |
88 | | sal_Int32 SAL_CALL |
89 | | CharacterClassificationImpl::getCharacterType( const OUString& Text, sal_Int32 nPos, |
90 | | const Locale& rLocale ) |
91 | 1.99M | { |
92 | 1.99M | return getLocaleSpecificCharacterClassification(rLocale)->getCharacterType(Text, nPos, rLocale); |
93 | 1.99M | } |
94 | | |
95 | | sal_Int32 SAL_CALL |
96 | | CharacterClassificationImpl::getStringType( const OUString& Text, sal_Int32 nPos, |
97 | | sal_Int32 nCount, const Locale& rLocale ) |
98 | 0 | { |
99 | 0 | return getLocaleSpecificCharacterClassification(rLocale)->getStringType(Text, nPos, nCount, rLocale); |
100 | 0 | } |
101 | | |
102 | | ParseResult SAL_CALL CharacterClassificationImpl::parseAnyToken( |
103 | | const OUString& Text, sal_Int32 nPos, const Locale& rLocale, |
104 | | sal_Int32 startCharTokenType, const OUString& userDefinedCharactersStart, |
105 | | sal_Int32 contCharTokenType, const OUString& userDefinedCharactersCont ) |
106 | 3.06M | { |
107 | 3.06M | return getLocaleSpecificCharacterClassification(rLocale)->parseAnyToken(Text, nPos, rLocale, |
108 | 3.06M | startCharTokenType,userDefinedCharactersStart, |
109 | 3.06M | contCharTokenType, userDefinedCharactersCont); |
110 | 3.06M | } |
111 | | |
112 | | |
113 | | ParseResult SAL_CALL CharacterClassificationImpl::parsePredefinedToken( |
114 | | sal_Int32 nTokenType, const OUString& Text, sal_Int32 nPos, |
115 | | const Locale& rLocale, sal_Int32 startCharTokenType, |
116 | | const OUString& userDefinedCharactersStart, sal_Int32 contCharTokenType, |
117 | | const OUString& userDefinedCharactersCont ) |
118 | 1.32M | { |
119 | 1.32M | return getLocaleSpecificCharacterClassification(rLocale)->parsePredefinedToken( |
120 | 1.32M | nTokenType, Text, nPos, rLocale, startCharTokenType, userDefinedCharactersStart, |
121 | 1.32M | contCharTokenType, userDefinedCharactersCont); |
122 | 1.32M | } |
123 | | |
124 | | bool CharacterClassificationImpl::createLocaleSpecificCharacterClassification(const OUString& serviceName, const Locale& rLocale) |
125 | 1.66M | { |
126 | | // to share service between same Language but different Country code, like zh_CN and zh_SG |
127 | 2.71M | for (size_t l = 0; l < lookupTable.size(); l++) { |
128 | 1.04M | cachedItem = lookupTable[l]; |
129 | 1.04M | if (serviceName == cachedItem->aName) { |
130 | 0 | lookupTable.emplace_back( rLocale, serviceName, cachedItem->xCI ); |
131 | 0 | cachedItem = lookupTable.back(); |
132 | 0 | return true; |
133 | 0 | } |
134 | 1.04M | } |
135 | | |
136 | 1.66M | Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext( |
137 | 1.66M | "com.sun.star.i18n.CharacterClassification_" + serviceName, m_xContext); |
138 | | |
139 | 1.66M | Reference < XCharacterClassification > xCI; |
140 | 1.66M | if ( xI.is() ) { |
141 | 624k | xCI.set( xI, UNO_QUERY ); |
142 | 624k | if (xCI.is()) { |
143 | 624k | lookupTable.emplace_back( rLocale, serviceName, xCI ); |
144 | 624k | cachedItem = lookupTable.back(); |
145 | 624k | return true; |
146 | 624k | } |
147 | 624k | } |
148 | 1.04M | return false; |
149 | 1.66M | } |
150 | | |
151 | | Reference < XCharacterClassification > const & |
152 | | CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale& rLocale) |
153 | 123M | { |
154 | | // reuse instance if locale didn't change |
155 | 123M | if (cachedItem && cachedItem->equals(rLocale)) |
156 | 122M | return cachedItem->xCI; |
157 | 501k | else { |
158 | 501k | for (const auto & i : lookupTable) { |
159 | 501k | cachedItem = i; |
160 | 501k | if (cachedItem->equals(rLocale)) |
161 | 0 | return cachedItem->xCI; |
162 | 501k | } |
163 | | |
164 | | // Load service with name <base>_<lang>_<country> or |
165 | | // <base>_<bcp47> and fallbacks. |
166 | 501k | bool bLoaded = createLocaleSpecificCharacterClassification( |
167 | 501k | LocaleDataImpl::getFirstLocaleServiceName( rLocale), rLocale); |
168 | 501k | if (!bLoaded) |
169 | 501k | { |
170 | 501k | ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale)); |
171 | 501k | for (const auto& rFallback : aFallbacks) |
172 | 542k | { |
173 | 542k | bLoaded = createLocaleSpecificCharacterClassification(rFallback, rLocale); |
174 | 542k | if (bLoaded) |
175 | 0 | break; |
176 | 542k | } |
177 | 501k | } |
178 | 501k | if (bLoaded) |
179 | 0 | return cachedItem->xCI; |
180 | 501k | else if (xUCI.is()) |
181 | 501k | { |
182 | 501k | lookupTable.emplace_back( rLocale, "Unicode", xUCI ); |
183 | 501k | cachedItem = lookupTable.back(); |
184 | 501k | return cachedItem->xCI; |
185 | 501k | } |
186 | 501k | } |
187 | 9 | throw RuntimeException(); |
188 | 123M | } |
189 | | |
190 | | OUString SAL_CALL |
191 | | CharacterClassificationImpl::getImplementationName() |
192 | 0 | { |
193 | 0 | return u"com.sun.star.i18n.CharacterClassification"_ustr; |
194 | 0 | } |
195 | | |
196 | | sal_Bool SAL_CALL |
197 | | CharacterClassificationImpl::supportsService(const OUString& rServiceName) |
198 | 0 | { |
199 | 0 | return cppu::supportsService(this, rServiceName); |
200 | 0 | } |
201 | | |
202 | | Sequence< OUString > SAL_CALL |
203 | | CharacterClassificationImpl::getSupportedServiceNames() |
204 | 0 | { |
205 | 0 | return { u"com.sun.star.i18n.CharacterClassification"_ustr }; |
206 | 0 | } |
207 | | |
208 | | } |
209 | | |
210 | | extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * |
211 | | com_sun_star_i18n_CharacterClassification_get_implementation( |
212 | | css::uno::XComponentContext *context, |
213 | | css::uno::Sequence<css::uno::Any> const &) |
214 | 624k | { |
215 | 624k | return cppu::acquire(new i18npool::CharacterClassificationImpl(context)); |
216 | 624k | } |
217 | | |
218 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |