LibreOffice Module i18npool (master) 1
characterclassificationImpl.cxx
Go to the documentation of this file.
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
22#include <localedata.hxx>
23
24#include <com/sun/star/uno/XComponentContext.hpp>
25
26using namespace com::sun::star::uno;
27using namespace ::com::sun::star::i18n;
28using namespace com::sun::star::lang;
29
30namespace i18npool {
31
33 const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext )
34{
35 static constexpr OUStringLiteral sUnicode = u"Unicode";
37 xUCI = cachedItem->xCI;
38}
39
41}
42
43
44OUString SAL_CALL
45CharacterClassificationImpl::toUpper( const OUString& Text, sal_Int32 nPos,
46 sal_Int32 nCount, const Locale& rLocale )
47{
48 return getLocaleSpecificCharacterClassification(rLocale)->toUpper(Text, nPos, nCount, rLocale);
49}
50
51OUString SAL_CALL
52CharacterClassificationImpl::toLower( const OUString& Text, sal_Int32 nPos,
53 sal_Int32 nCount, const Locale& rLocale )
54{
55 return getLocaleSpecificCharacterClassification(rLocale)->toLower(Text, nPos, nCount, rLocale);
56}
57
58OUString SAL_CALL
59CharacterClassificationImpl::toTitle( const OUString& Text, sal_Int32 nPos,
60 sal_Int32 nCount, const Locale& rLocale )
61{
62 return getLocaleSpecificCharacterClassification(rLocale)->toTitle(Text, nPos, nCount, rLocale);
63}
64
65sal_Int16 SAL_CALL
66CharacterClassificationImpl::getType( const OUString& Text, sal_Int32 nPos )
67{
68 if (xUCI.is())
69 return xUCI->getType(Text, nPos);
70 throw RuntimeException();
71}
72
73sal_Int16 SAL_CALL
74CharacterClassificationImpl::getCharacterDirection( const OUString& Text, sal_Int32 nPos )
75{
76 if (xUCI.is())
77 return xUCI->getCharacterDirection(Text, nPos);
78 throw RuntimeException();
79}
80
81sal_Int16 SAL_CALL
82CharacterClassificationImpl::getScript( const OUString& Text, sal_Int32 nPos )
83{
84 if (xUCI.is())
85 return xUCI->getScript(Text, nPos);
86 throw RuntimeException();
87}
88
89sal_Int32 SAL_CALL
90CharacterClassificationImpl::getCharacterType( const OUString& Text, sal_Int32 nPos,
91 const Locale& rLocale )
92{
93 return getLocaleSpecificCharacterClassification(rLocale)->getCharacterType(Text, nPos, rLocale);
94}
95
96sal_Int32 SAL_CALL
97CharacterClassificationImpl::getStringType( const OUString& Text, sal_Int32 nPos,
98 sal_Int32 nCount, const Locale& rLocale )
99{
100 return getLocaleSpecificCharacterClassification(rLocale)->getStringType(Text, nPos, nCount, rLocale);
101}
102
104 const OUString& Text, sal_Int32 nPos, const Locale& rLocale,
105 sal_Int32 startCharTokenType, const OUString& userDefinedCharactersStart,
106 sal_Int32 contCharTokenType, const OUString& userDefinedCharactersCont )
107{
108 return getLocaleSpecificCharacterClassification(rLocale)->parseAnyToken(Text, nPos, rLocale,
109 startCharTokenType,userDefinedCharactersStart,
110 contCharTokenType, userDefinedCharactersCont);
111}
112
113
115 sal_Int32 nTokenType, const OUString& Text, sal_Int32 nPos,
116 const Locale& rLocale, sal_Int32 startCharTokenType,
117 const OUString& userDefinedCharactersStart, sal_Int32 contCharTokenType,
118 const OUString& userDefinedCharactersCont )
119{
120 return getLocaleSpecificCharacterClassification(rLocale)->parsePredefinedToken(
121 nTokenType, Text, nPos, rLocale, startCharTokenType, userDefinedCharactersStart,
122 contCharTokenType, userDefinedCharactersCont);
123}
124
125bool CharacterClassificationImpl::createLocaleSpecificCharacterClassification(const OUString& serviceName, const Locale& rLocale)
126{
127 // to share service between same Language but different Country code, like zh_CN and zh_SG
128 for (size_t l = 0; l < lookupTable.size(); l++) {
130 if (serviceName == cachedItem->aName) {
131 lookupTable.emplace_back( rLocale, serviceName, cachedItem->xCI );
132 cachedItem = lookupTable.back();
133 return true;
134 }
135 }
136
137 Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
138 "com.sun.star.i18n.CharacterClassification_" + serviceName, m_xContext);
139
141 if ( xI.is() ) {
142 xCI.set( xI, UNO_QUERY );
143 if (xCI.is()) {
144 lookupTable.emplace_back( rLocale, serviceName, xCI );
145 cachedItem = lookupTable.back();
146 return true;
147 }
148 }
149 return false;
150}
151
154{
155 // reuse instance if locale didn't change
156 if (cachedItem && cachedItem->equals(rLocale))
157 return cachedItem->xCI;
158 else {
159 for (const auto & i : lookupTable) {
160 cachedItem = i;
161 if (cachedItem->equals(rLocale))
162 return cachedItem->xCI;
163 }
164
165 // Load service with name <base>_<lang>_<country> or
166 // <base>_<bcp47> and fallbacks.
169 if (!bLoaded)
170 {
171 ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
172 for (const auto& rFallback : aFallbacks)
173 {
174 bLoaded = createLocaleSpecificCharacterClassification(rFallback, rLocale);
175 if (bLoaded)
176 break;
177 }
178 }
179 if (bLoaded)
180 return cachedItem->xCI;
181 else if (xUCI.is())
182 {
183 lookupTable.emplace_back( rLocale, "Unicode", xUCI );
184 cachedItem = lookupTable.back();
185 return cachedItem->xCI;
186 }
187 }
188 throw RuntimeException();
189}
190
191OUString SAL_CALL
193{
194 return "com.sun.star.i18n.CharacterClassification";
195}
196
197sal_Bool SAL_CALL
199{
200 return cppu::supportsService(this, rServiceName);
201}
202
205{
206 return { "com.sun.star.i18n.CharacterClassification" };
207}
208
209}
210
211extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
213 css::uno::XComponentContext *context,
214 css::uno::Sequence<css::uno::Any> const &)
215{
216 return cppu::acquire(new i18npool::CharacterClassificationImpl(context));
217}
218
219/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_i18n_CharacterClassification_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
bool createLocaleSpecificCharacterClassification(const OUString &serviceName, const css::lang::Locale &rLocale)
virtual sal_Int32 SAL_CALL getCharacterType(const OUString &text, sal_Int32 nPos, const css::lang::Locale &rLocale) override
virtual sal_Int16 SAL_CALL getScript(const OUString &Text, sal_Int32 nPos) override
virtual css::i18n::ParseResult SAL_CALL parsePredefinedToken(sal_Int32 nTokenType, const OUString &Text, sal_Int32 nPos, const css::lang::Locale &rLocale, sal_Int32 nStartCharFlags, const OUString &userDefinedCharactersStart, sal_Int32 nContCharFlags, const OUString &userDefinedCharactersCont) override
virtual OUString SAL_CALL toLower(const OUString &Text, sal_Int32 nPos, sal_Int32 nCount, const css::lang::Locale &rLocale) override
CharacterClassificationImpl(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual OUString SAL_CALL getImplementationName() override
virtual css::i18n::ParseResult SAL_CALL parseAnyToken(const OUString &Text, sal_Int32 nPos, const css::lang::Locale &rLocale, sal_Int32 nStartCharFlags, const OUString &userDefinedCharactersStart, sal_Int32 nContCharFlags, const OUString &userDefinedCharactersCont) override
css::uno::Reference< XCharacterClassification > const & getLocaleSpecificCharacterClassification(const css::lang::Locale &rLocale)
virtual sal_Int32 SAL_CALL getStringType(const OUString &text, sal_Int32 nPos, sal_Int32 nCount, const css::lang::Locale &rLocale) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL toTitle(const OUString &Text, sal_Int32 nPos, sal_Int32 nCount, const css::lang::Locale &rLocale) override
virtual sal_Int16 SAL_CALL getType(const OUString &Text, sal_Int32 nPos) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual sal_Int16 SAL_CALL getCharacterDirection(const OUString &Text, sal_Int32 nPos) override
virtual OUString SAL_CALL toUpper(const OUString &Text, sal_Int32 nPos, sal_Int32 nCount, const css::lang::Locale &rLocale) override
css::uno::Reference< XCharacterClassification > xUCI
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
static ::std::vector< OUString > getFallbackLocaleServiceNames(const css::lang::Locale &rLocale)
Generates fallback strings suitable as parts of service names, excluding the one obtained via getFirs...
static OUString getFirstLocaleServiceName(const css::lang::Locale &rLocale)
Generates a <Language>_<Country> or <Variant> (if Language=="qlt") string suitable as part of service...
int nCount
float u
sal_uInt16 nPos
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
Constant values shared between i18npool and, for example, the number formatter.
unsigned char sal_Bool