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 .
21 #include <characterclassificationImpl.hxx>
22 #include <rtl/ustrbuf.hxx>
24 using namespace com::sun::star::uno
;
25 using namespace com::sun::star::lang
;
27 using ::rtl::OUString
;
28 using ::rtl::OUStringBuffer
;
30 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
32 CharacterClassificationImpl::CharacterClassificationImpl(
33 const Reference
< lang::XMultiServiceFactory
>& rxMSF
) : xMSF( rxMSF
)
35 if (createLocaleSpecificCharacterClassification(OUString("Unicode"), Locale()))
36 xUCI
= cachedItem
->xCI
;
39 CharacterClassificationImpl::~CharacterClassificationImpl() {
41 for (size_t l
= 0; l
< lookupTable
.size(); l
++)
42 delete lookupTable
[l
];
48 CharacterClassificationImpl::toUpper( const OUString
& Text
, sal_Int32 nPos
,
49 sal_Int32 nCount
, const Locale
& rLocale
) throw(RuntimeException
)
51 return getLocaleSpecificCharacterClassification(rLocale
)->toUpper(Text
, nPos
, nCount
, rLocale
);
55 CharacterClassificationImpl::toLower( const OUString
& Text
, sal_Int32 nPos
,
56 sal_Int32 nCount
, const Locale
& rLocale
) throw(RuntimeException
)
58 return getLocaleSpecificCharacterClassification(rLocale
)->toLower(Text
, nPos
, nCount
, rLocale
);
62 CharacterClassificationImpl::toTitle( const OUString
& Text
, sal_Int32 nPos
,
63 sal_Int32 nCount
, const Locale
& rLocale
) throw(RuntimeException
)
65 return getLocaleSpecificCharacterClassification(rLocale
)->toTitle(Text
, nPos
, nCount
, rLocale
);
69 CharacterClassificationImpl::getType( const OUString
& Text
, sal_Int32 nPos
)
70 throw(RuntimeException
)
73 return xUCI
->getType(Text
, nPos
);
74 throw RuntimeException();
78 CharacterClassificationImpl::getCharacterDirection( const OUString
& Text
, sal_Int32 nPos
)
79 throw(RuntimeException
)
82 return xUCI
->getCharacterDirection(Text
, nPos
);
83 throw RuntimeException();
87 CharacterClassificationImpl::getScript( const OUString
& Text
, sal_Int32 nPos
)
88 throw(RuntimeException
)
91 return xUCI
->getScript(Text
, nPos
);
92 throw RuntimeException();
96 CharacterClassificationImpl::getCharacterType( const OUString
& Text
, sal_Int32 nPos
,
97 const Locale
& rLocale
) throw(RuntimeException
)
99 return getLocaleSpecificCharacterClassification(rLocale
)->getCharacterType(Text
, nPos
, rLocale
);
103 CharacterClassificationImpl::getStringType( const OUString
& Text
, sal_Int32 nPos
,
104 sal_Int32 nCount
, const Locale
& rLocale
) throw(RuntimeException
)
106 return getLocaleSpecificCharacterClassification(rLocale
)->getStringType(Text
, nPos
, nCount
, rLocale
);
109 ParseResult SAL_CALL
CharacterClassificationImpl::parseAnyToken(
110 const OUString
& Text
, sal_Int32 nPos
, const Locale
& rLocale
,
111 sal_Int32 startCharTokenType
, const OUString
& userDefinedCharactersStart
,
112 sal_Int32 contCharTokenType
, const OUString
& userDefinedCharactersCont
)
113 throw(RuntimeException
)
115 return getLocaleSpecificCharacterClassification(rLocale
)->parseAnyToken(Text
, nPos
, rLocale
,
116 startCharTokenType
,userDefinedCharactersStart
,
117 contCharTokenType
, userDefinedCharactersCont
);
121 ParseResult SAL_CALL
CharacterClassificationImpl::parsePredefinedToken(
122 sal_Int32 nTokenType
, const OUString
& Text
, sal_Int32 nPos
,
123 const Locale
& rLocale
, sal_Int32 startCharTokenType
,
124 const OUString
& userDefinedCharactersStart
, sal_Int32 contCharTokenType
,
125 const OUString
& userDefinedCharactersCont
) throw(RuntimeException
)
127 return getLocaleSpecificCharacterClassification(rLocale
)->parsePredefinedToken(
128 nTokenType
, Text
, nPos
, rLocale
, startCharTokenType
, userDefinedCharactersStart
,
129 contCharTokenType
, userDefinedCharactersCont
);
132 sal_Bool SAL_CALL
CharacterClassificationImpl::createLocaleSpecificCharacterClassification(const OUString
& serviceName
, const Locale
& rLocale
)
134 // to share service between same Language but different Country code, like zh_CN and zh_SG
135 for (size_t l
= 0; l
< lookupTable
.size(); l
++) {
136 cachedItem
= lookupTable
[l
];
137 if (serviceName
== cachedItem
->aName
) {
138 lookupTable
.push_back( cachedItem
= new lookupTableItem(rLocale
, serviceName
, cachedItem
->xCI
) );
143 Reference
< XInterface
> xI
= xMSF
->createInstance(
144 OUString("com.sun.star.i18n.CharacterClassification_") + serviceName
);
146 Reference
< XCharacterClassification
> xCI
;
148 xI
->queryInterface(::getCppuType((const Reference
< XCharacterClassification
>*)0) ) >>= xCI
;
150 lookupTable
.push_back( cachedItem
= new lookupTableItem(rLocale
, serviceName
, xCI
) );
157 Reference
< XCharacterClassification
> SAL_CALL
158 CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale
& rLocale
)
159 throw(RuntimeException
)
161 // reuse instance if locale didn't change
162 if (cachedItem
&& cachedItem
->equals(rLocale
))
163 return cachedItem
->xCI
;
164 else if (xMSF
.is()) {
165 for (size_t i
= 0; i
< lookupTable
.size(); i
++) {
166 cachedItem
= lookupTable
[i
];
167 if (cachedItem
->equals(rLocale
))
168 return cachedItem
->xCI
;
171 static sal_Unicode under
= (sal_Unicode
)'_';
172 sal_Int32 l
= rLocale
.Language
.getLength();
173 sal_Int32 c
= rLocale
.Country
.getLength();
174 sal_Int32 v
= rLocale
.Variant
.getLength();
175 OUStringBuffer
aBuf(l
+c
+v
+3);
177 // load service with name <base>_<lang>_<country>_<varian>
178 if ((l
> 0 && c
> 0 && v
> 0 &&
179 createLocaleSpecificCharacterClassification(aBuf
.append(rLocale
.Language
).append(under
).append(
180 rLocale
.Country
).append(under
).append(rLocale
.Variant
).makeStringAndClear(), rLocale
)) ||
181 // load service with name <base>_<lang>_<country>
183 createLocaleSpecificCharacterClassification(aBuf
.append(rLocale
.Language
).append(under
).append(
184 rLocale
.Country
).makeStringAndClear(), rLocale
)) ||
185 (l
> 0 && c
> 0 && rLocale
.Language
.compareToAscii("zh") == 0 &&
186 (rLocale
.Country
.compareToAscii("HK") == 0 ||
187 rLocale
.Country
.compareToAscii("MO") == 0) &&
188 // if the country code is HK or MO, one more step to try TW.
189 createLocaleSpecificCharacterClassification(aBuf
.append(rLocale
.Language
).append(under
).append(
190 "TW").makeStringAndClear(), rLocale
)) ||
192 // load service with name <base>_<lang>
193 createLocaleSpecificCharacterClassification(rLocale
.Language
, rLocale
))) {
194 return cachedItem
->xCI
;
195 } else if (xUCI
.is()) {
196 lookupTable
.push_back( cachedItem
= new lookupTableItem(rLocale
, OUString("Unicode"), xUCI
) );
197 return cachedItem
->xCI
;
200 throw RuntimeException();
203 const sal_Char cClass
[] = "com.sun.star.i18n.CharacterClassification";
206 CharacterClassificationImpl::getImplementationName(void)
207 throw( RuntimeException
)
209 return OUString::createFromAscii(cClass
);
213 CharacterClassificationImpl::supportsService(const rtl::OUString
& rServiceName
)
214 throw( RuntimeException
)
216 return !rServiceName
.compareToAscii(cClass
);
219 Sequence
< OUString
> SAL_CALL
220 CharacterClassificationImpl::getSupportedServiceNames(void) throw( RuntimeException
)
222 Sequence
< OUString
> aRet(1);
223 aRet
[0] = OUString::createFromAscii(cClass
);
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */