LCOV - code coverage report
Current view: top level - i18npool/source/collator - collatorImpl.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 70 107 65.4 %
Date: 2015-06-13 12:38:46 Functions: 13 16 81.2 %
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          90 : CollatorImpl::CollatorImpl( const Reference < XComponentContext >& rxContext ) : m_xContext(rxContext)
      35             : {
      36          90 :     mxLocaleData.set( LocaleData::create(rxContext) );
      37          90 :     cachedItem = NULL;
      38          90 : }
      39             : 
      40         212 : CollatorImpl::~CollatorImpl()
      41             : {
      42             :     // Clear lookuptable
      43         138 :     for (size_t l = 0; l < lookupTable.size(); l++)
      44          67 :         delete lookupTable[l];
      45          71 :     lookupTable.clear();
      46         141 : }
      47             : 
      48             : sal_Int32 SAL_CALL
      49         462 : 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         462 :     if (cachedItem)
      53         462 :         return cachedItem->xC->compareSubstring(str1, off1, len1, str2, off2, len2);
      54             : 
      55           0 :     sal_Unicode *unistr1 = const_cast<sal_Unicode*>(str1.getStr()) + off1;
      56           0 :     sal_Unicode *unistr2 = const_cast<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       42784 : CollatorImpl::compareString( const OUString& in_str1, const OUString& in_str2) throw(RuntimeException, std::exception)
      65             : {
      66       42784 :     if (cachedItem)
      67       42784 :         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          74 : CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions) throw(RuntimeException, std::exception)
      75             : {
      76          74 :     const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
      77          74 :     for (sal_Int32 i = 0; i < imp.getLength(); i++)
      78          74 :         if (imp[i].isDefault)
      79         148 :             return loadCollatorAlgorithm(imp[i].unoID, rLocale, collatorOptions);
      80             : 
      81          74 :     throw RuntimeException(); // not default is defined
      82             :     //return 0;
      83             : }
      84             : 
      85             : sal_Int32 SAL_CALL
      86          86 : CollatorImpl::loadCollatorAlgorithm(const OUString& impl, const lang::Locale& rLocale, sal_Int32 collatorOptions)
      87             :     throw(RuntimeException, std::exception)
      88             : {
      89          86 :     if (! cachedItem || ! cachedItem->equals(rLocale, impl))
      90          86 :         loadCachedCollator(rLocale, impl);
      91             : 
      92          86 :     if (cachedItem)
      93          86 :         cachedItem->xC->loadCollatorAlgorithm(cachedItem->algorithm, nLocale = rLocale, collatorOptions);
      94             :     else
      95           0 :         throw RuntimeException(); // impl could not be loaded
      96             : 
      97          86 :     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           1 : CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale ) throw(RuntimeException, std::exception)
     112             : {
     113           1 :     nLocale = rLocale;
     114           1 :     const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
     115           1 :     Sequence< OUString > list(imp.getLength());
     116             : 
     117           2 :     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           1 :         if (imp[i].isDefault && i) {
     120           0 :             list[i] = list[0];
     121           0 :             list[0] = imp[i].unoID;
     122             :         }
     123             :         else
     124           1 :             list[i] = imp[i].unoID;
     125             :     }
     126           1 :     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         343 : CollatorImpl::createCollator(const lang::Locale& rLocale, const OUString& serviceName, const OUString& rSortAlgorithm)
     146             :     throw(RuntimeException)
     147             : {
     148         343 :     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         343 :         m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Collator_" + serviceName, m_xContext );
     157             : 
     158         343 :     if (xI.is()) {
     159          86 :         Reference < XCollator > xC;
     160          86 :         xC.set( xI, UNO_QUERY );
     161          86 :         if (xC.is()) {
     162          86 :             lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, xC));
     163          86 :             return true;
     164           0 :         }
     165             :     }
     166         257 :     return false;
     167             : }
     168             : 
     169             : void SAL_CALL
     170          86 : CollatorImpl::loadCachedCollator(const lang::Locale& rLocale, const OUString& rSortAlgorithm)
     171             :     throw(RuntimeException)
     172             : {
     173          86 :     for (size_t i = 0; i < lookupTable.size(); i++) {
     174           0 :         cachedItem = lookupTable[i];
     175           0 :         if (cachedItem->equals(rLocale, rSortAlgorithm)) {
     176          86 :             return;
     177             :         }
     178             :     }
     179             : 
     180          86 :     bool bLoaded = false;
     181          86 :     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          86 :                 LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + rSortAlgorithm, rSortAlgorithm);
     187          86 :         if (!bLoaded)
     188             :         {
     189          86 :             ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
     190         171 :             for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
     191             :             {
     192          85 :                 bLoaded = createCollator( rLocale, *it + "_" + rSortAlgorithm, rSortAlgorithm);
     193          85 :                 if (bLoaded)
     194           0 :                     break;
     195             :             }
     196          86 :             if (!bLoaded)
     197             :             {
     198             :                 // load service with name <base>_<algorithm>
     199          86 :                 bLoaded = createCollator( rLocale, rSortAlgorithm, rSortAlgorithm);
     200          86 :             }
     201             :         }
     202             :     }
     203          86 :     if (!bLoaded)
     204             :     {
     205             :         // load default service with name <base>_Unicode
     206          86 :         bLoaded = createCollator( rLocale, "Unicode", rSortAlgorithm);
     207          86 :         if (!bLoaded)
     208             :         {
     209           0 :             cachedItem = NULL;
     210           0 :             throw RuntimeException();   // could not load any service
     211             :         }
     212             :     }
     213             : }
     214             : 
     215           1 : OUString SAL_CALL CollatorImpl::getImplementationName() throw( RuntimeException, std::exception )
     216             : {
     217           1 :     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           1 : CollatorImpl::getSupportedServiceNames() throw( RuntimeException, std::exception )
     228             : {
     229           1 :     Sequence< OUString > aRet(1);
     230           1 :     aRet[0] = "com.sun.star.i18n.Collator";
     231           1 :     return aRet;
     232             : }
     233             : 
     234             : } } } }
     235             : 
     236             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     237          79 : com_sun_star_i18n_Collator_get_implementation(
     238             :     css::uno::XComponentContext *context,
     239             :     css::uno::Sequence<css::uno::Any> const &)
     240             : {
     241          79 :     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.11