LCOV - code coverage report
Current view: top level - i18npool/source/collator - collatorImpl.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 64 107 59.8 %
Date: 2014-11-03 Functions: 11 16 68.8 %
Legend: Lines: hit not hit

          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 <collatorImpl.hxx>
      21             : #include <localedata.hxx>
      22             : #include <com/sun/star/i18n/CollatorOptions.hpp>
      23             : #include <com/sun/star/i18n/LocaleData.hpp>
      24             : #include <rtl/ustrbuf.hxx>
      25             : #include <comphelper/processfactory.hxx>
      26             : #include <cppuhelper/supportsservice.hxx>
      27             : 
      28             : using namespace com::sun::star;
      29             : using namespace com::sun::star::lang;
      30             : using namespace com::sun::star::uno;
      31             : 
      32             : namespace com { namespace sun { namespace star { namespace i18n {
      33             : 
      34         169 : CollatorImpl::CollatorImpl( const Reference < XComponentContext >& rxContext ) : m_xContext(rxContext)
      35             : {
      36         169 :     mxLocaleData.set( LocaleData::create(rxContext) );
      37         169 :     cachedItem = NULL;
      38         169 : }
      39             : 
      40         391 : CollatorImpl::~CollatorImpl()
      41             : {
      42             :     // Clear lookuptable
      43         250 :     for (size_t l = 0; l < lookupTable.size(); l++)
      44         119 :         delete lookupTable[l];
      45         131 :     lookupTable.clear();
      46         260 : }
      47             : 
      48             : sal_Int32 SAL_CALL
      49        1593 : CollatorImpl::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
      50             :     const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(RuntimeException, std::exception)
      51             : {
      52        1593 :     if (cachedItem)
      53        1593 :         return cachedItem->xC->compareSubstring(str1, off1, len1, str2, off2, len2);
      54             : 
      55           0 :     sal_Unicode *unistr1 = (sal_Unicode*) str1.getStr() + off1;
      56           0 :     sal_Unicode *unistr2 = (sal_Unicode*) str2.getStr() + off2;
      57           0 :     for (int i = 0; i < len1 && i < len2; i++)
      58           0 :         if (unistr1[i] != unistr2[i])
      59           0 :             return unistr1[i] < unistr2[i] ? -1 : 1;
      60           0 :     return len1 == len2 ? 0 : (len1 < len2 ? -1 : 1);
      61             : }
      62             : 
      63             : sal_Int32 SAL_CALL
      64       85318 : CollatorImpl::compareString( const OUString& in_str1, const OUString& in_str2) throw(RuntimeException, std::exception)
      65             : {
      66       85318 :     if (cachedItem)
      67       85318 :         return cachedItem->xC->compareString(in_str1, in_str2);
      68             : 
      69           0 :     return CollatorImpl::compareSubstring(in_str1, 0, in_str1.getLength(), in_str2, 0, in_str2.getLength());
      70             : }
      71             : 
      72             : 
      73             : sal_Int32 SAL_CALL
      74         129 : CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions) throw(RuntimeException, std::exception)
      75             : {
      76         129 :     const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
      77         129 :     for (sal_Int16 i = 0; i < imp.getLength(); i++)
      78         129 :         if (imp[i].isDefault)
      79         258 :             return loadCollatorAlgorithm(imp[i].unoID, rLocale, collatorOptions);
      80             : 
      81         129 :     throw RuntimeException(); // not default is defined
      82             :     //return 0;
      83             : }
      84             : 
      85             : sal_Int32 SAL_CALL
      86         157 : CollatorImpl::loadCollatorAlgorithm(const OUString& impl, const lang::Locale& rLocale, sal_Int32 collatorOptions)
      87             :     throw(RuntimeException, std::exception)
      88             : {
      89         157 :     if (! cachedItem || ! cachedItem->equals(rLocale, impl))
      90         157 :         loadCachedCollator(rLocale, impl);
      91             : 
      92         157 :     if (cachedItem)
      93         157 :         cachedItem->xC->loadCollatorAlgorithm(cachedItem->algorithm, nLocale = rLocale, collatorOptions);
      94             :     else
      95           0 :         throw RuntimeException(); // impl could not be loaded
      96             : 
      97         157 :     return 0;
      98             : }
      99             : 
     100             : void SAL_CALL
     101           0 : CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString& impl, const lang::Locale& rLocale,
     102             :     const Sequence< sal_Int32 >& collatorOptions) throw(RuntimeException, std::exception)
     103             : {
     104           0 :     sal_Int32 options = 0;
     105           0 :     for (sal_Int32 i = 0; i < collatorOptions.getLength(); i++)
     106           0 :         options |= collatorOptions[i];
     107           0 :     loadCollatorAlgorithm(impl, rLocale, options);
     108           0 : }
     109             : 
     110             : Sequence< OUString > SAL_CALL
     111           4 : CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale ) throw(RuntimeException, std::exception)
     112             : {
     113           4 :     nLocale = rLocale;
     114           4 :     const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
     115           4 :     Sequence< OUString > list(imp.getLength());
     116             : 
     117           8 :     for (sal_Int32 i = 0; i < imp.getLength(); i++) {
     118             :         //if the current algorithm is default and the position is not on the first one, then switch
     119           4 :         if (imp[i].isDefault && i) {
     120           0 :             list[i] = list[0];
     121           0 :             list[0] = imp[i].unoID;
     122             :         }
     123             :         else
     124           4 :             list[i] = imp[i].unoID;
     125             :     }
     126           4 :     return list;
     127             : }
     128             : 
     129             : Sequence< sal_Int32 > SAL_CALL
     130           0 : CollatorImpl::listCollatorOptions( const OUString& /*collatorAlgorithmName*/ ) throw(RuntimeException, std::exception)
     131             : {
     132           0 :     Sequence< OUString > option_str = mxLocaleData->getCollationOptions(nLocale);
     133           0 :     Sequence< sal_Int32 > option_int(option_str.getLength());
     134             : 
     135           0 :     for (sal_Int32 i = 0; i < option_str.getLength(); i++)
     136           0 :         option_int[i] =
     137           0 :             option_str[i] == "IGNORE_CASE" ?  CollatorOptions::CollatorOptions_IGNORE_CASE :
     138           0 :             option_str[i] == "IGNORE_KANA" ?  CollatorOptions::CollatorOptions_IGNORE_KANA :
     139           0 :             option_str[i] == "IGNORE_WIDTH" ?  CollatorOptions::CollatorOptions_IGNORE_WIDTH : 0;
     140             : 
     141           0 :     return option_int;
     142             : }
     143             : 
     144             : bool SAL_CALL
     145         622 : CollatorImpl::createCollator(const lang::Locale& rLocale, const OUString& serviceName, const OUString& rSortAlgorithm)
     146             :     throw(RuntimeException)
     147             : {
     148         622 :     for (size_t l = 0; l < lookupTable.size(); l++) {
     149           0 :         cachedItem = lookupTable[l];
     150           0 :         if (cachedItem->service.equals(serviceName)) {// cross locale sharing
     151           0 :             lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, cachedItem->xC));
     152           0 :             return true;
     153             :         }
     154             :     }
     155             :     Reference < XInterface > xI =
     156         622 :         m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Collator_" + serviceName, m_xContext );
     157             : 
     158         622 :     if (xI.is()) {
     159         157 :         Reference < XCollator > xC;
     160         157 :         xC.set( xI, UNO_QUERY );
     161         157 :         if (xC.is()) {
     162         157 :             lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, xC));
     163         157 :             return true;
     164           0 :         }
     165             :     }
     166         465 :     return false;
     167             : }
     168             : 
     169             : void SAL_CALL
     170         157 : CollatorImpl::loadCachedCollator(const lang::Locale& rLocale, const OUString& rSortAlgorithm)
     171             :     throw(RuntimeException)
     172             : {
     173         157 :     for (size_t i = 0; i < lookupTable.size(); i++) {
     174           0 :         cachedItem = lookupTable[i];
     175           0 :         if (cachedItem->equals(rLocale, rSortAlgorithm)) {
     176         157 :             return;
     177             :         }
     178             :     }
     179             : 
     180         157 :     bool bLoaded = false;
     181         157 :     if (!rSortAlgorithm.isEmpty())
     182             :     {
     183             :         // Load service with name <base>_<lang>_<country>_<algorithm> or
     184             :         // <base>_<bcp47>_<algorithm> and fallbacks.
     185             :         bLoaded = createCollator( rLocale,
     186         157 :                 LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + rSortAlgorithm, rSortAlgorithm);
     187         157 :         if (!bLoaded)
     188             :         {
     189         157 :             ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
     190         308 :             for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
     191             :             {
     192         151 :                 bLoaded = createCollator( rLocale, *it + "_" + rSortAlgorithm, rSortAlgorithm);
     193         151 :                 if (bLoaded)
     194           0 :                     break;
     195             :             }
     196         157 :             if (!bLoaded)
     197             :             {
     198             :                 // load service with name <base>_<algorithm>
     199         157 :                 bLoaded = createCollator( rLocale, rSortAlgorithm, rSortAlgorithm);
     200         157 :             }
     201             :         }
     202             :     }
     203         157 :     if (!bLoaded)
     204             :     {
     205             :         // load default service with name <base>_Unicode
     206         157 :         bLoaded = createCollator( rLocale, "Unicode", rSortAlgorithm);
     207         157 :         if (!bLoaded)
     208             :         {
     209           0 :             cachedItem = NULL;
     210           0 :             throw RuntimeException();   // could not load any service
     211             :         }
     212             :     }
     213             : }
     214             : 
     215           0 : OUString SAL_CALL CollatorImpl::getImplementationName() throw( RuntimeException, std::exception )
     216             : {
     217           0 :     return OUString("com.sun.star.i18n.Collator");
     218             : }
     219             : 
     220           0 : sal_Bool SAL_CALL CollatorImpl::supportsService(const OUString& rServiceName)
     221             :                 throw( RuntimeException, std::exception )
     222             : {
     223           0 :     return cppu::supportsService(this, rServiceName);
     224             : }
     225             : 
     226             : Sequence< OUString > SAL_CALL
     227           0 : CollatorImpl::getSupportedServiceNames() throw( RuntimeException, std::exception )
     228             : {
     229           0 :     Sequence< OUString > aRet(1);
     230           0 :     aRet[0] = OUString("com.sun.star.i18n.Collator");
     231           0 :     return aRet;
     232             : }
     233             : 
     234             : } } } }
     235             : 
     236             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     237         147 : com_sun_star_i18n_Collator_get_implementation(
     238             :     css::uno::XComponentContext *context,
     239             :     css::uno::Sequence<css::uno::Any> const &)
     240             : {
     241         147 :     return cppu::acquire(new com::sun::star::i18n::CollatorImpl(context));
     242             : }
     243             : 
     244             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10