Line data Source code
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 :
20 : #include <cppuhelper/supportsservice.hxx>
21 : #include <characterclassificationImpl.hxx>
22 : #include <localedata.hxx>
23 : #include <rtl/ustrbuf.hxx>
24 :
25 : using namespace com::sun::star::uno;
26 : using namespace com::sun::star::lang;
27 :
28 : namespace com { namespace sun { namespace star { namespace i18n {
29 :
30 0 : CharacterClassificationImpl::CharacterClassificationImpl(
31 0 : const Reference < uno::XComponentContext >& rxContext ) : m_xContext( rxContext )
32 : {
33 0 : if (createLocaleSpecificCharacterClassification(OUString("Unicode"), Locale()))
34 0 : xUCI = cachedItem->xCI;
35 0 : }
36 :
37 0 : CharacterClassificationImpl::~CharacterClassificationImpl() {
38 : // Clear lookuptable
39 0 : for (size_t l = 0; l < lookupTable.size(); l++)
40 0 : delete lookupTable[l];
41 0 : lookupTable.clear();
42 0 : }
43 :
44 :
45 : OUString SAL_CALL
46 0 : CharacterClassificationImpl::toUpper( const OUString& Text, sal_Int32 nPos,
47 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
48 : {
49 0 : return getLocaleSpecificCharacterClassification(rLocale)->toUpper(Text, nPos, nCount, rLocale);
50 : }
51 :
52 : OUString SAL_CALL
53 0 : CharacterClassificationImpl::toLower( const OUString& Text, sal_Int32 nPos,
54 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
55 : {
56 0 : return getLocaleSpecificCharacterClassification(rLocale)->toLower(Text, nPos, nCount, rLocale);
57 : }
58 :
59 : OUString SAL_CALL
60 0 : CharacterClassificationImpl::toTitle( const OUString& Text, sal_Int32 nPos,
61 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
62 : {
63 0 : return getLocaleSpecificCharacterClassification(rLocale)->toTitle(Text, nPos, nCount, rLocale);
64 : }
65 :
66 : sal_Int16 SAL_CALL
67 0 : CharacterClassificationImpl::getType( const OUString& Text, sal_Int32 nPos )
68 : throw(RuntimeException, std::exception)
69 : {
70 0 : if (xUCI.is())
71 0 : return xUCI->getType(Text, nPos);
72 0 : throw RuntimeException();
73 : }
74 :
75 : sal_Int16 SAL_CALL
76 0 : CharacterClassificationImpl::getCharacterDirection( const OUString& Text, sal_Int32 nPos )
77 : throw(RuntimeException, std::exception)
78 : {
79 0 : if (xUCI.is())
80 0 : return xUCI->getCharacterDirection(Text, nPos);
81 0 : throw RuntimeException();
82 : }
83 :
84 : sal_Int16 SAL_CALL
85 0 : CharacterClassificationImpl::getScript( const OUString& Text, sal_Int32 nPos )
86 : throw(RuntimeException, std::exception)
87 : {
88 0 : if (xUCI.is())
89 0 : return xUCI->getScript(Text, nPos);
90 0 : throw RuntimeException();
91 : }
92 :
93 : sal_Int32 SAL_CALL
94 0 : CharacterClassificationImpl::getCharacterType( const OUString& Text, sal_Int32 nPos,
95 : const Locale& rLocale ) throw(RuntimeException, std::exception)
96 : {
97 0 : return getLocaleSpecificCharacterClassification(rLocale)->getCharacterType(Text, nPos, rLocale);
98 : }
99 :
100 : sal_Int32 SAL_CALL
101 0 : CharacterClassificationImpl::getStringType( const OUString& Text, sal_Int32 nPos,
102 : sal_Int32 nCount, const Locale& rLocale ) throw(RuntimeException, std::exception)
103 : {
104 0 : return getLocaleSpecificCharacterClassification(rLocale)->getStringType(Text, nPos, nCount, rLocale);
105 : }
106 :
107 0 : ParseResult SAL_CALL CharacterClassificationImpl::parseAnyToken(
108 : const OUString& Text, sal_Int32 nPos, const Locale& rLocale,
109 : sal_Int32 startCharTokenType, const OUString& userDefinedCharactersStart,
110 : sal_Int32 contCharTokenType, const OUString& userDefinedCharactersCont )
111 : throw(RuntimeException, std::exception)
112 : {
113 0 : return getLocaleSpecificCharacterClassification(rLocale)->parseAnyToken(Text, nPos, rLocale,
114 : startCharTokenType,userDefinedCharactersStart,
115 0 : contCharTokenType, userDefinedCharactersCont);
116 : }
117 :
118 :
119 0 : ParseResult SAL_CALL CharacterClassificationImpl::parsePredefinedToken(
120 : sal_Int32 nTokenType, const OUString& Text, sal_Int32 nPos,
121 : const Locale& rLocale, sal_Int32 startCharTokenType,
122 : const OUString& userDefinedCharactersStart, sal_Int32 contCharTokenType,
123 : const OUString& userDefinedCharactersCont ) throw(RuntimeException, std::exception)
124 : {
125 0 : return getLocaleSpecificCharacterClassification(rLocale)->parsePredefinedToken(
126 : nTokenType, Text, nPos, rLocale, startCharTokenType, userDefinedCharactersStart,
127 0 : contCharTokenType, userDefinedCharactersCont);
128 : }
129 :
130 0 : sal_Bool SAL_CALL CharacterClassificationImpl::createLocaleSpecificCharacterClassification(const OUString& serviceName, const Locale& rLocale)
131 : {
132 : // to share service between same Language but different Country code, like zh_CN and zh_SG
133 0 : for (size_t l = 0; l < lookupTable.size(); l++) {
134 0 : cachedItem = lookupTable[l];
135 0 : if (serviceName == cachedItem->aName) {
136 0 : lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, cachedItem->xCI) );
137 0 : return sal_True;
138 : }
139 : }
140 :
141 0 : Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
142 0 : OUString("com.sun.star.i18n.CharacterClassification_") + serviceName, m_xContext);
143 :
144 0 : Reference < XCharacterClassification > xCI;
145 0 : if ( xI.is() ) {
146 0 : xCI.set( xI, UNO_QUERY );
147 0 : if (xCI.is()) {
148 0 : lookupTable.push_back( cachedItem = new lookupTableItem(rLocale, serviceName, xCI) );
149 0 : return sal_True;
150 : }
151 : }
152 0 : return sal_False;
153 : }
154 :
155 : Reference < XCharacterClassification > SAL_CALL
156 0 : CharacterClassificationImpl::getLocaleSpecificCharacterClassification(const Locale& rLocale)
157 : throw(RuntimeException)
158 : {
159 : // reuse instance if locale didn't change
160 0 : if (cachedItem && cachedItem->equals(rLocale))
161 0 : return cachedItem->xCI;
162 : else {
163 0 : for (size_t i = 0; i < lookupTable.size(); i++) {
164 0 : cachedItem = lookupTable[i];
165 0 : if (cachedItem->equals(rLocale))
166 0 : return cachedItem->xCI;
167 : }
168 :
169 : // Load service with name <base>_<lang>_<country> or
170 : // <base>_<bcp47> and fallbacks.
171 : bool bLoaded = createLocaleSpecificCharacterClassification(
172 0 : LocaleDataImpl::getFirstLocaleServiceName( rLocale), rLocale);
173 0 : if (!bLoaded)
174 : {
175 0 : ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
176 0 : for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
177 : {
178 0 : bLoaded = createLocaleSpecificCharacterClassification( *it, rLocale);
179 0 : if (bLoaded)
180 0 : break;
181 0 : }
182 : }
183 0 : if (bLoaded)
184 0 : return cachedItem->xCI;
185 0 : else if (xUCI.is())
186 : {
187 0 : lookupTable.push_back( cachedItem = new lookupTableItem( rLocale, OUString("Unicode"), xUCI));
188 0 : return cachedItem->xCI;
189 : }
190 : }
191 0 : throw RuntimeException();
192 : }
193 :
194 : OUString SAL_CALL
195 0 : CharacterClassificationImpl::getImplementationName(void)
196 : throw( RuntimeException, std::exception )
197 : {
198 0 : return OUString("com.sun.star.i18n.CharacterClassification");
199 : }
200 :
201 : sal_Bool SAL_CALL
202 0 : CharacterClassificationImpl::supportsService(const OUString& rServiceName)
203 : throw( RuntimeException, std::exception )
204 : {
205 0 : return cppu::supportsService(this, rServiceName);
206 : }
207 :
208 : Sequence< OUString > SAL_CALL
209 0 : CharacterClassificationImpl::getSupportedServiceNames(void) throw( RuntimeException, std::exception )
210 : {
211 0 : Sequence< OUString > aRet(1);
212 0 : aRet[0] = OUString("com.sun.star.i18n.CharacterClassification");
213 0 : return aRet;
214 : }
215 :
216 : } } } }
217 :
218 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
219 0 : com_sun_star_i18n_CharacterClassification_get_implementation(
220 : css::uno::XComponentContext *context,
221 : css::uno::Sequence<css::uno::Any> const &)
222 : {
223 0 : return cppu::acquire(new css::i18n::CharacterClassificationImpl(context));
224 : }
225 :
226 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|