bump product version to 4.2.0.1
[LibreOffice.git] / i18npool / source / characterclassification / characterclassificationImpl.cxx
blobd65b5a5e8018834628debf66561bc425d76db543
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <localedata.hxx>
23 #include <rtl/ustrbuf.hxx>
25 using namespace com::sun::star::uno;
26 using namespace com::sun::star::lang;
29 namespace com { namespace sun { namespace star { namespace i18n {
31 CharacterClassificationImpl::CharacterClassificationImpl(
32 const Reference < uno::XComponentContext >& rxContext ) : m_xContext( rxContext )
34 if (createLocaleSpecificCharacterClassification(OUString("Unicode"), Locale()))
35 xUCI = cachedItem->xCI;
38 CharacterClassificationImpl::~CharacterClassificationImpl() {
39 // Clear lookuptable
40 for (size_t l = 0; l < lookupTable.size(); l++)
41 delete lookupTable[l];
42 lookupTable.clear();
46 OUString SAL_CALL
47 CharacterClassificationImpl::toUpper( const OUString& Text, sal_Int32 nPos,
48 sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
50 return getLocaleSpecificCharacterClassification(rLocale)->toUpper(Text, nPos, nCount, rLocale);
53 OUString SAL_CALL
54 CharacterClassificationImpl::toLower( const OUString& Text, sal_Int32 nPos,
55 sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
57 return getLocaleSpecificCharacterClassification(rLocale)->toLower(Text, nPos, nCount, rLocale);
60 OUString SAL_CALL
61 CharacterClassificationImpl::toTitle( const OUString& Text, sal_Int32 nPos,
62 sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
64 return getLocaleSpecificCharacterClassification(rLocale)->toTitle(Text, nPos, nCount, rLocale);
67 sal_Int16 SAL_CALL
68 CharacterClassificationImpl::getType( const OUString& Text, sal_Int32 nPos )
69 throw(RuntimeException)
71 if (xUCI.is())
72 return xUCI->getType(Text, nPos);
73 throw RuntimeException();
76 sal_Int16 SAL_CALL
77 CharacterClassificationImpl::getCharacterDirection( const OUString& Text, sal_Int32 nPos )
78 throw(RuntimeException)
80 if (xUCI.is())
81 return xUCI->getCharacterDirection(Text, nPos);
82 throw RuntimeException();
85 sal_Int16 SAL_CALL
86 CharacterClassificationImpl::getScript( const OUString& Text, sal_Int32 nPos )
87 throw(RuntimeException)
89 if (xUCI.is())
90 return xUCI->getScript(Text, nPos);
91 throw RuntimeException();
94 sal_Int32 SAL_CALL
95 CharacterClassificationImpl::getCharacterType( const OUString& Text, sal_Int32 nPos,
96 const Locale& rLocale ) throw(RuntimeException)
98 return getLocaleSpecificCharacterClassification(rLocale)->getCharacterType(Text, nPos, rLocale);
101 sal_Int32 SAL_CALL
102 CharacterClassificationImpl::getStringType( const OUString& Text, sal_Int32 nPos,
103 sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException)
105 return getLocaleSpecificCharacterClassification(rLocale)->getStringType(Text, nPos, nCount, rLocale);
108 ParseResult SAL_CALL CharacterClassificationImpl::parseAnyToken(
109 const OUString& Text, sal_Int32 nPos, const Locale& rLocale,
110 sal_Int32 startCharTokenType, const OUString& userDefinedCharactersStart,
111 sal_Int32 contCharTokenType, const OUString& userDefinedCharactersCont )
112 throw(RuntimeException)
114 return getLocaleSpecificCharacterClassification(rLocale)->parseAnyToken(Text, nPos, rLocale,
115 startCharTokenType,userDefinedCharactersStart,
116 contCharTokenType, userDefinedCharactersCont);
120 ParseResult SAL_CALL CharacterClassificationImpl::parsePredefinedToken(
121 sal_Int32 nTokenType, const OUString& Text, sal_Int32 nPos,
122 const Locale& rLocale, sal_Int32 startCharTokenType,
123 const OUString& userDefinedCharactersStart, sal_Int32 contCharTokenType,
124 const OUString& userDefinedCharactersCont ) throw(RuntimeException)
126 return getLocaleSpecificCharacterClassification(rLocale)->parsePredefinedToken(
127 nTokenType, Text, nPos, rLocale, startCharTokenType, userDefinedCharactersStart,
128 contCharTokenType, userDefinedCharactersCont);
131 sal_Bool SAL_CALL CharacterClassificationImpl::createLocaleSpecificCharacterClassification(const OUString& serviceName, const Locale& rLocale)
133 // to share service between same Language but different Country code, like zh_CN and zh_SG
134 for (size_t l = 0; l < lookupTable.size(); l++) {
135 cachedItem = lookupTable[l];
136 if (serviceName == cachedItem->aName) {
137 lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, cachedItem->xCI) );
138 return sal_True;
142 Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
143 OUString("com.sun.star.i18n.CharacterClassification_") + serviceName, m_xContext);
145 Reference < XCharacterClassification > xCI;
146 if ( xI.is() ) {
147 xCI.set( xI, UNO_QUERY );
148 if (xCI.is()) {
149 lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, xCI) );
150 return sal_True;
153 return sal_False;
156 Reference < XCharacterClassification > SAL_CALL
157 CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale& rLocale)
158 throw(RuntimeException)
160 // reuse instance if locale didn't change
161 if (cachedItem && cachedItem->equals(rLocale))
162 return cachedItem->xCI;
163 else {
164 for (size_t i = 0; i < lookupTable.size(); i++) {
165 cachedItem = lookupTable[i];
166 if (cachedItem->equals(rLocale))
167 return cachedItem->xCI;
170 // Load service with name <base>_<lang>_<country> or
171 // <base>_<bcp47> and fallbacks.
172 bool bLoaded = createLocaleSpecificCharacterClassification(
173 LocaleDataImpl::getFirstLocaleServiceName( rLocale), rLocale);
174 if (!bLoaded)
176 ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
177 for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
179 bLoaded = createLocaleSpecificCharacterClassification( *it, rLocale);
180 if (bLoaded)
181 break;
184 if (bLoaded)
185 return cachedItem->xCI;
186 else if (xUCI.is())
188 lookupTable.push_back( cachedItem = new lookupTableItem( rLocale, OUString("Unicode"), xUCI));
189 return cachedItem->xCI;
192 throw RuntimeException();
195 const sal_Char cClass[] = "com.sun.star.i18n.CharacterClassification";
197 OUString SAL_CALL
198 CharacterClassificationImpl::getImplementationName(void)
199 throw( RuntimeException )
201 return OUString::createFromAscii(cClass);
204 sal_Bool SAL_CALL
205 CharacterClassificationImpl::supportsService(const OUString& rServiceName)
206 throw( RuntimeException )
208 return rServiceName.equalsAscii(cClass);
211 Sequence< OUString > SAL_CALL
212 CharacterClassificationImpl::getSupportedServiceNames(void) throw( RuntimeException )
214 Sequence< OUString > aRet(1);
215 aRet[0] = OUString::createFromAscii(cClass);
216 return aRet;
219 } } } }
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */