LCOV - code coverage report
Current view: top level - i18npool/source/collator - collatorImpl.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 107 0.0 %
Date: 2014-04-14 Functions: 0 16 0.0 %
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           0 : CollatorImpl::CollatorImpl( const Reference < XComponentContext >& rxContext ) : m_xContext(rxContext)
      35             : {
      36           0 :     mxLocaleData.set( LocaleData::create(rxContext) );
      37           0 :     cachedItem = NULL;
      38           0 : }
      39             : 
      40           0 : CollatorImpl::~CollatorImpl()
      41             : {
      42             :     // Clear lookuptable
      43           0 :     for (size_t l = 0; l < lookupTable.size(); l++)
      44           0 :         delete lookupTable[l];
      45           0 :     lookupTable.clear();
      46           0 : }
      47             : 
      48             : sal_Int32 SAL_CALL
      49           0 : 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           0 :     if (cachedItem)
      53           0 :         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           0 : CollatorImpl::compareString( const OUString& in_str1, const OUString& in_str2) throw(RuntimeException, std::exception)
      65             : {
      66           0 :     if (cachedItem)
      67           0 :         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           0 : CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions) throw(RuntimeException, std::exception)
      75             : {
      76           0 :     const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
      77           0 :     for (sal_Int16 i = 0; i < imp.getLength(); i++)
      78           0 :         if (imp[i].isDefault)
      79           0 :             return loadCollatorAlgorithm(imp[i].unoID, rLocale, collatorOptions);
      80             : 
      81           0 :     throw RuntimeException(); // not default is defined
      82             :     //return 0;
      83             : }
      84             : 
      85             : sal_Int32 SAL_CALL
      86           0 : CollatorImpl::loadCollatorAlgorithm(const OUString& impl, const lang::Locale& rLocale, sal_Int32 collatorOptions)
      87             :     throw(RuntimeException, std::exception)
      88             : {
      89           0 :     if (! cachedItem || ! cachedItem->equals(rLocale, impl))
      90           0 :         loadCachedCollator(rLocale, impl);
      91             : 
      92           0 :     if (cachedItem)
      93           0 :         cachedItem->xC->loadCollatorAlgorithm(cachedItem->algorithm, nLocale = rLocale, collatorOptions);
      94             :     else
      95           0 :         throw RuntimeException(); // impl could not be loaded
      96             : 
      97           0 :     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           0 : CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale ) throw(RuntimeException, std::exception)
     112             : {
     113           0 :     nLocale = rLocale;
     114           0 :     const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale);
     115           0 :     Sequence< OUString > list(imp.getLength());
     116             : 
     117           0 :     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           0 :         if (imp[i].isDefault && i) {
     120           0 :             list[i] = list[0];
     121           0 :             list[0] = imp[i].unoID;
     122             :         }
     123             :         else
     124           0 :             list[i] = imp[i].unoID;
     125             :     }
     126           0 :     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             : sal_Bool SAL_CALL
     145           0 : CollatorImpl::createCollator(const lang::Locale& rLocale, const OUString& serviceName, const OUString& rSortAlgorithm)
     146             :     throw(RuntimeException)
     147             : {
     148           0 :     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 sal_True;
     153             :         }
     154             :     }
     155             :     Reference < XInterface > xI =
     156           0 :         m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Collator_" + serviceName, m_xContext );
     157             : 
     158           0 :     if (xI.is()) {
     159           0 :         Reference < XCollator > xC;
     160           0 :         xC.set( xI, UNO_QUERY );
     161           0 :         if (xC.is()) {
     162           0 :             lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, xC));
     163           0 :             return sal_True;
     164           0 :         }
     165             :     }
     166           0 :     return sal_False;
     167             : }
     168             : 
     169             : void SAL_CALL
     170           0 : CollatorImpl::loadCachedCollator(const lang::Locale& rLocale, const OUString& rSortAlgorithm)
     171             :     throw(RuntimeException)
     172             : {
     173           0 :     for (size_t i = 0; i < lookupTable.size(); i++) {
     174           0 :         cachedItem = lookupTable[i];
     175           0 :         if (cachedItem->equals(rLocale, rSortAlgorithm)) {
     176           0 :             return;
     177             :         }
     178             :     }
     179             : 
     180           0 :     bool bLoaded = false;
     181           0 :     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           0 :                 LocaleDataImpl::getFirstLocaleServiceName( rLocale) + "_" + rSortAlgorithm, rSortAlgorithm);
     187           0 :         if (!bLoaded)
     188             :         {
     189           0 :             ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( rLocale));
     190           0 :             for (::std::vector< OUString >::const_iterator it( aFallbacks.begin()); it != aFallbacks.end(); ++it)
     191             :             {
     192           0 :                 bLoaded = createCollator( rLocale, *it + "_" + rSortAlgorithm, rSortAlgorithm);
     193           0 :                 if (bLoaded)
     194           0 :                     break;
     195             :             }
     196           0 :             if (!bLoaded)
     197             :             {
     198             :                 // load service with name <base>_<algorithm>
     199           0 :                 bLoaded = createCollator( rLocale, rSortAlgorithm, rSortAlgorithm);
     200           0 :             }
     201             :         }
     202             :     }
     203           0 :     if (!bLoaded)
     204             :     {
     205             :         // load default service with name <base>_Unicode
     206           0 :         bLoaded = createCollator( rLocale, "Unicode", rSortAlgorithm);
     207           0 :         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           0 : com_sun_star_i18n_Collator_get_implementation(
     238             :     css::uno::XComponentContext *context,
     239             :     css::uno::Sequence<css::uno::Any> const &)
     240             : {
     241           0 :     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