1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: characterclassificationImpl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
34 #include <characterclassificationImpl.hxx>
35 #include <rtl/ustrbuf.hxx>
37 using namespace com::sun::star::uno
;
38 using namespace com::sun::star::lang
;
41 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
43 CharacterClassificationImpl::CharacterClassificationImpl(
44 const Reference
< lang::XMultiServiceFactory
>& rxMSF
) : xMSF( rxMSF
)
46 if (createLocaleSpecificCharacterClassification(OUString::createFromAscii("Unicode"), Locale()))
47 xUCI
= cachedItem
->xCI
;
50 CharacterClassificationImpl::~CharacterClassificationImpl() {
52 for (size_t l
= 0; l
< lookupTable
.size(); l
++)
53 delete lookupTable
[l
];
59 CharacterClassificationImpl::toUpper( const OUString
& Text
, sal_Int32 nPos
,
60 sal_Int32 nCount
, const Locale
& rLocale
) throw(RuntimeException
)
62 return getLocaleSpecificCharacterClassification(rLocale
)->toUpper(Text
, nPos
, nCount
, rLocale
);
66 CharacterClassificationImpl::toLower( const OUString
& Text
, sal_Int32 nPos
,
67 sal_Int32 nCount
, const Locale
& rLocale
) throw(RuntimeException
)
69 return getLocaleSpecificCharacterClassification(rLocale
)->toLower(Text
, nPos
, nCount
, rLocale
);
73 CharacterClassificationImpl::toTitle( const OUString
& Text
, sal_Int32 nPos
,
74 sal_Int32 nCount
, const Locale
& rLocale
) throw(RuntimeException
)
76 return getLocaleSpecificCharacterClassification(rLocale
)->toTitle(Text
, nPos
, nCount
, rLocale
);
80 CharacterClassificationImpl::getType( const OUString
& Text
, sal_Int32 nPos
)
81 throw(RuntimeException
)
84 return xUCI
->getType(Text
, nPos
);
85 throw RuntimeException();
89 CharacterClassificationImpl::getCharacterDirection( const OUString
& Text
, sal_Int32 nPos
)
90 throw(RuntimeException
)
93 return xUCI
->getCharacterDirection(Text
, nPos
);
94 throw RuntimeException();
98 CharacterClassificationImpl::getScript( const OUString
& Text
, sal_Int32 nPos
)
99 throw(RuntimeException
)
102 return xUCI
->getScript(Text
, nPos
);
103 throw RuntimeException();
107 CharacterClassificationImpl::getCharacterType( const OUString
& Text
, sal_Int32 nPos
,
108 const Locale
& rLocale
) throw(RuntimeException
)
110 return getLocaleSpecificCharacterClassification(rLocale
)->getCharacterType(Text
, nPos
, rLocale
);
114 CharacterClassificationImpl::getStringType( const OUString
& Text
, sal_Int32 nPos
,
115 sal_Int32 nCount
, const Locale
& rLocale
) throw(RuntimeException
)
117 return getLocaleSpecificCharacterClassification(rLocale
)->getStringType(Text
, nPos
, nCount
, rLocale
);
120 ParseResult SAL_CALL
CharacterClassificationImpl::parseAnyToken(
121 const OUString
& Text
, sal_Int32 nPos
, const Locale
& rLocale
,
122 sal_Int32 startCharTokenType
, const OUString
& userDefinedCharactersStart
,
123 sal_Int32 contCharTokenType
, const OUString
& userDefinedCharactersCont
)
124 throw(RuntimeException
)
126 return getLocaleSpecificCharacterClassification(rLocale
)->parseAnyToken(Text
, nPos
, rLocale
,
127 startCharTokenType
,userDefinedCharactersStart
,
128 contCharTokenType
, userDefinedCharactersCont
);
132 ParseResult SAL_CALL
CharacterClassificationImpl::parsePredefinedToken(
133 sal_Int32 nTokenType
, const OUString
& Text
, sal_Int32 nPos
,
134 const Locale
& rLocale
, sal_Int32 startCharTokenType
,
135 const OUString
& userDefinedCharactersStart
, sal_Int32 contCharTokenType
,
136 const OUString
& userDefinedCharactersCont
) throw(RuntimeException
)
138 return getLocaleSpecificCharacterClassification(rLocale
)->parsePredefinedToken(
139 nTokenType
, Text
, nPos
, rLocale
, startCharTokenType
, userDefinedCharactersStart
,
140 contCharTokenType
, userDefinedCharactersCont
);
143 sal_Bool SAL_CALL
CharacterClassificationImpl::createLocaleSpecificCharacterClassification(const OUString
& serviceName
, const Locale
& rLocale
)
145 // to share service between same Language but different Country code, like zh_CN and zh_SG
146 for (size_t l
= 0; l
< lookupTable
.size(); l
++) {
147 cachedItem
= lookupTable
[l
];
148 if (serviceName
== cachedItem
->aName
) {
149 lookupTable
.push_back( cachedItem
= new lookupTableItem(rLocale
, serviceName
, cachedItem
->xCI
) );
154 Reference
< XInterface
> xI
= xMSF
->createInstance(
155 OUString::createFromAscii("com.sun.star.i18n.CharacterClassification_") + serviceName
);
157 Reference
< XCharacterClassification
> xCI
;
159 xI
->queryInterface(::getCppuType((const Reference
< XCharacterClassification
>*)0) ) >>= xCI
;
161 lookupTable
.push_back( cachedItem
= new lookupTableItem(rLocale
, serviceName
, xCI
) );
168 Reference
< XCharacterClassification
> SAL_CALL
169 CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale
& rLocale
)
170 throw(RuntimeException
)
172 // reuse instance if locale didn't change
173 if (cachedItem
&& cachedItem
->equals(rLocale
))
174 return cachedItem
->xCI
;
175 else if (xMSF
.is()) {
176 for (size_t i
= 0; i
< lookupTable
.size(); i
++) {
177 cachedItem
= lookupTable
[i
];
178 if (cachedItem
->equals(rLocale
))
179 return cachedItem
->xCI
;
182 static sal_Unicode under
= (sal_Unicode
)'_';
183 static OUString
tw(OUString::createFromAscii("TW"));
184 sal_Int32 l
= rLocale
.Language
.getLength();
185 sal_Int32 c
= rLocale
.Country
.getLength();
186 sal_Int32 v
= rLocale
.Variant
.getLength();
187 OUStringBuffer
aBuf(l
+c
+v
+3);
189 // load service with name <base>_<lang>_<country>_<varian>
190 if ((l
> 0 && c
> 0 && v
> 0 &&
191 createLocaleSpecificCharacterClassification(aBuf
.append(rLocale
.Language
).append(under
).append(
192 rLocale
.Country
).append(under
).append(rLocale
.Variant
).makeStringAndClear(), rLocale
)) ||
193 // load service with name <base>_<lang>_<country>
195 createLocaleSpecificCharacterClassification(aBuf
.append(rLocale
.Language
).append(under
).append(
196 rLocale
.Country
).makeStringAndClear(), rLocale
)) ||
197 (l
> 0 && c
> 0 && rLocale
.Language
.compareToAscii("zh") == 0 &&
198 (rLocale
.Country
.compareToAscii("HK") == 0 ||
199 rLocale
.Country
.compareToAscii("MO") == 0) &&
200 // if the country code is HK or MO, one more step to try TW.
201 createLocaleSpecificCharacterClassification(aBuf
.append(rLocale
.Language
).append(under
).append(
202 tw
).makeStringAndClear(), rLocale
)) ||
204 // load service with name <base>_<lang>
205 createLocaleSpecificCharacterClassification(rLocale
.Language
, rLocale
))) {
206 return cachedItem
->xCI
;
207 } else if (xUCI
.is()) {
208 lookupTable
.push_back( cachedItem
= new lookupTableItem(rLocale
, OUString::createFromAscii("Unicode"), xUCI
) );
209 return cachedItem
->xCI
;
212 throw RuntimeException();
215 const sal_Char cClass
[] = "com.sun.star.i18n.CharacterClassification";
218 CharacterClassificationImpl::getImplementationName(void)
219 throw( RuntimeException
)
221 return OUString::createFromAscii(cClass
);
225 CharacterClassificationImpl::supportsService(const rtl::OUString
& rServiceName
)
226 throw( RuntimeException
)
228 return !rServiceName
.compareToAscii(cClass
);
231 Sequence
< OUString
> SAL_CALL
232 CharacterClassificationImpl::getSupportedServiceNames(void) throw( RuntimeException
)
234 Sequence
< OUString
> aRet(1);
235 aRet
[0] = OUString::createFromAscii(cClass
);