1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <cppuhelper/supportsservice.hxx>
21 #include <characterclassificationImpl.hxx>
22 #include <localedata.hxx>
24 #include <com/sun/star/uno/XComponentContext.hpp>
26 using namespace com::sun::star::uno
;
27 using namespace ::com::sun::star::i18n
;
28 using namespace com::sun::star::lang
;
32 CharacterClassificationImpl::CharacterClassificationImpl(
33 const Reference
< XComponentContext
>& rxContext
) : m_xContext( rxContext
)
35 if (createLocaleSpecificCharacterClassification(u
"Unicode"_ustr
, Locale()))
36 xUCI
= cachedItem
->xCI
;
39 CharacterClassificationImpl::~CharacterClassificationImpl() {
44 CharacterClassificationImpl::toUpper( const OUString
& Text
, sal_Int32 nPos
,
45 sal_Int32 nCount
, const Locale
& rLocale
)
47 return getLocaleSpecificCharacterClassification(rLocale
)->toUpper(Text
, nPos
, nCount
, rLocale
);
51 CharacterClassificationImpl::toLower( const OUString
& Text
, sal_Int32 nPos
,
52 sal_Int32 nCount
, const Locale
& rLocale
)
54 return getLocaleSpecificCharacterClassification(rLocale
)->toLower(Text
, nPos
, nCount
, rLocale
);
58 CharacterClassificationImpl::toTitle( const OUString
& Text
, sal_Int32 nPos
,
59 sal_Int32 nCount
, const Locale
& rLocale
)
61 return getLocaleSpecificCharacterClassification(rLocale
)->toTitle(Text
, nPos
, nCount
, rLocale
);
65 CharacterClassificationImpl::getType( const OUString
& Text
, sal_Int32 nPos
)
68 return xUCI
->getType(Text
, nPos
);
69 throw RuntimeException();
73 CharacterClassificationImpl::getCharacterDirection( const OUString
& Text
, sal_Int32 nPos
)
76 return xUCI
->getCharacterDirection(Text
, nPos
);
77 throw RuntimeException();
81 CharacterClassificationImpl::getScript( const OUString
& Text
, sal_Int32 nPos
)
84 return xUCI
->getScript(Text
, nPos
);
85 throw RuntimeException();
89 CharacterClassificationImpl::getCharacterType( const OUString
& Text
, sal_Int32 nPos
,
90 const Locale
& rLocale
)
92 return getLocaleSpecificCharacterClassification(rLocale
)->getCharacterType(Text
, nPos
, rLocale
);
96 CharacterClassificationImpl::getStringType( const OUString
& Text
, sal_Int32 nPos
,
97 sal_Int32 nCount
, const Locale
& rLocale
)
99 return getLocaleSpecificCharacterClassification(rLocale
)->getStringType(Text
, nPos
, nCount
, rLocale
);
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
)
107 return getLocaleSpecificCharacterClassification(rLocale
)->parseAnyToken(Text
, nPos
, rLocale
,
108 startCharTokenType
,userDefinedCharactersStart
,
109 contCharTokenType
, userDefinedCharactersCont
);
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
)
119 return getLocaleSpecificCharacterClassification(rLocale
)->parsePredefinedToken(
120 nTokenType
, Text
, nPos
, rLocale
, startCharTokenType
, userDefinedCharactersStart
,
121 contCharTokenType
, userDefinedCharactersCont
);
124 bool CharacterClassificationImpl::createLocaleSpecificCharacterClassification(const OUString
& serviceName
, const Locale
& rLocale
)
126 // to share service between same Language but different Country code, like zh_CN and zh_SG
127 for (size_t l
= 0; l
< lookupTable
.size(); l
++) {
128 cachedItem
= lookupTable
[l
];
129 if (serviceName
== cachedItem
->aName
) {
130 lookupTable
.emplace_back( rLocale
, serviceName
, cachedItem
->xCI
);
131 cachedItem
= lookupTable
.back();
136 Reference
< XInterface
> xI
= m_xContext
->getServiceManager()->createInstanceWithContext(
137 "com.sun.star.i18n.CharacterClassification_" + serviceName
, m_xContext
);
139 Reference
< XCharacterClassification
> xCI
;
141 xCI
.set( xI
, UNO_QUERY
);
143 lookupTable
.emplace_back( rLocale
, serviceName
, xCI
);
144 cachedItem
= lookupTable
.back();
151 Reference
< XCharacterClassification
> const &
152 CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale
& rLocale
)
154 // reuse instance if locale didn't change
155 if (cachedItem
&& cachedItem
->equals(rLocale
))
156 return cachedItem
->xCI
;
158 for (const auto & i
: lookupTable
) {
160 if (cachedItem
->equals(rLocale
))
161 return cachedItem
->xCI
;
164 // Load service with name <base>_<lang>_<country> or
165 // <base>_<bcp47> and fallbacks.
166 bool bLoaded
= createLocaleSpecificCharacterClassification(
167 LocaleDataImpl::getFirstLocaleServiceName( rLocale
), rLocale
);
170 ::std::vector
< OUString
> aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale
));
171 for (const auto& rFallback
: aFallbacks
)
173 bLoaded
= createLocaleSpecificCharacterClassification(rFallback
, rLocale
);
179 return cachedItem
->xCI
;
182 lookupTable
.emplace_back( rLocale
, "Unicode", xUCI
);
183 cachedItem
= lookupTable
.back();
184 return cachedItem
->xCI
;
187 throw RuntimeException();
191 CharacterClassificationImpl::getImplementationName()
193 return u
"com.sun.star.i18n.CharacterClassification"_ustr
;
197 CharacterClassificationImpl::supportsService(const OUString
& rServiceName
)
199 return cppu::supportsService(this, rServiceName
);
202 Sequence
< OUString
> SAL_CALL
203 CharacterClassificationImpl::getSupportedServiceNames()
205 return { u
"com.sun.star.i18n.CharacterClassification"_ustr
};
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 &)
215 return cppu::acquire(new i18npool::CharacterClassificationImpl(context
));
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */