LCOV - code coverage report
Current view: top level - i18npool/source/collator - collatorImpl.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 71 113 62.8 %
Date: 2012-08-25 Functions: 10 15 66.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 89 278 32.0 %

           Branch data     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                 :            : 
      21                 :            : #include <collatorImpl.hxx>
      22                 :            : #include <com/sun/star/i18n/CollatorOptions.hpp>
      23                 :            : #include <rtl/ustrbuf.hxx>
      24                 :            : 
      25                 :            : using namespace com::sun::star;
      26                 :            : using namespace com::sun::star::lang;
      27                 :            : using namespace com::sun::star::uno;
      28                 :            : 
      29                 :            : using ::rtl::OUString;
      30                 :            : using ::rtl::OUStringBuffer;
      31                 :            : 
      32                 :            : namespace com { namespace sun { namespace star { namespace i18n {
      33                 :            : 
      34         [ +  - ]:        581 : CollatorImpl::CollatorImpl( const Reference < XMultiServiceFactory >& rxMSF ) : xMSF(rxMSF)
      35                 :            : {
      36         [ +  - ]:        581 :     if ( rxMSF.is()) {
      37                 :            :         Reference < XInterface > xI =
      38 [ +  - ][ +  - ]:        581 :             xMSF->createInstance( OUString("com.sun.star.i18n.LocaleData"));
      39         [ +  - ]:        581 :         if ( xI.is() )
      40 [ +  - ][ +  - ]:        581 :             xI->queryInterface(::getCppuType((const Reference< XLocaleData>*)0)) >>= localedata;
         [ +  - ][ +  - ]
      41                 :            :     }
      42                 :        581 :     cachedItem = NULL;
      43                 :        581 : }
      44                 :            : 
      45                 :        560 : CollatorImpl::~CollatorImpl()
      46                 :            : {
      47                 :            :     // Clear lookuptable
      48         [ +  + ]:       1112 :     for (size_t l = 0; l < lookupTable.size(); l++)
      49 [ +  - ][ +  - ]:        552 :         delete lookupTable[l];
      50                 :        560 :     lookupTable.clear();
      51         [ -  + ]:       1120 : }
      52                 :            : 
      53                 :            : sal_Int32 SAL_CALL
      54                 :      82434 : CollatorImpl::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
      55                 :            :     const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(RuntimeException)
      56                 :            : {
      57         [ +  - ]:      82434 :     if (cachedItem)
      58                 :      82434 :         return cachedItem->xC->compareSubstring(str1, off1, len1, str2, off2, len2);
      59                 :            : 
      60                 :          0 :     sal_Unicode *unistr1 = (sal_Unicode*) str1.getStr() + off1;
      61                 :          0 :     sal_Unicode *unistr2 = (sal_Unicode*) str2.getStr() + off2;
      62 [ #  # ][ #  # ]:          0 :     for (int i = 0; i < len1 && i < len2; i++)
                 [ #  # ]
      63         [ #  # ]:          0 :         if (unistr1[i] != unistr2[i])
      64         [ #  # ]:          0 :             return unistr1[i] < unistr2[i] ? -1 : 1;
      65 [ #  # ][ #  # ]:      82434 :     return len1 == len2 ? 0 : (len1 < len2 ? -1 : 1);
      66                 :            : }
      67                 :            : 
      68                 :            : sal_Int32 SAL_CALL
      69                 :      10012 : CollatorImpl::compareString( const OUString& in_str1, const OUString& in_str2) throw(RuntimeException)
      70                 :            : {
      71         [ +  - ]:      10012 :     if (cachedItem)
      72                 :      10012 :         return cachedItem->xC->compareString(in_str1, in_str2);
      73                 :            : 
      74                 :      10012 :     return CollatorImpl::compareSubstring(in_str1, 0, in_str1.getLength(), in_str2, 0, in_str2.getLength());
      75                 :            : }
      76                 :            : 
      77                 :            : 
      78                 :            : sal_Int32 SAL_CALL
      79                 :        565 : CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions) throw(RuntimeException)
      80                 :            : {
      81                 :        565 :     const Sequence< Implementation > &imp = localedata->getCollatorImplementations(rLocale);
      82         [ +  - ]:        565 :     for (sal_Int16 i = 0; i < imp.getLength(); i++)
      83         [ +  - ]:        565 :         if (imp[i].isDefault)
      84         [ +  - ]:       1130 :             return loadCollatorAlgorithm(imp[i].unoID, rLocale, collatorOptions);
      85                 :            : 
      86         [ #  # ]:        565 :     throw RuntimeException(); // not default is defined
      87                 :            :     //return 0;
      88                 :            : }
      89                 :            : 
      90                 :            : sal_Int32 SAL_CALL
      91                 :        573 : CollatorImpl::loadCollatorAlgorithm(const OUString& impl, const lang::Locale& rLocale, sal_Int32 collatorOptions)
      92                 :            :     throw(RuntimeException)
      93                 :            : {
      94 [ -  + ][ #  # ]:        573 :     if (! cachedItem || ! cachedItem->equals(rLocale, impl))
                 [ +  - ]
      95                 :        573 :         loadCachedCollator(rLocale, impl);
      96                 :            : 
      97         [ +  - ]:        573 :     if (cachedItem)
      98                 :        573 :         cachedItem->xC->loadCollatorAlgorithm(cachedItem->algorithm, nLocale = rLocale, collatorOptions);
      99                 :            :     else
     100         [ #  # ]:          0 :         throw RuntimeException(); // impl could not be loaded
     101                 :            : 
     102                 :        573 :     return 0;
     103                 :            : }
     104                 :            : 
     105                 :            : void SAL_CALL
     106                 :          0 : CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString& impl, const lang::Locale& rLocale,
     107                 :            :     const Sequence< sal_Int32 >& collatorOptions) throw(RuntimeException)
     108                 :            : {
     109                 :          0 :     sal_Int32 options = 0;
     110         [ #  # ]:          0 :     for (sal_Int32 i = 0; i < collatorOptions.getLength(); i++)
     111                 :          0 :         options |= collatorOptions[i];
     112                 :          0 :     loadCollatorAlgorithm(impl, rLocale, options);
     113                 :          0 : }
     114                 :            : 
     115                 :            : Sequence< OUString > SAL_CALL
     116                 :          4 : CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale ) throw(RuntimeException)
     117                 :            : {
     118                 :          4 :     nLocale = rLocale;
     119                 :          4 :     const Sequence< Implementation > &imp = localedata->getCollatorImplementations(rLocale);
     120         [ +  - ]:          4 :     Sequence< OUString > list(imp.getLength());
     121                 :            : 
     122         [ +  + ]:          8 :     for (sal_Int32 i = 0; i < imp.getLength(); i++) {
     123                 :            :         //if the current algorithm is default and the position is not on the first one, then switch
     124 [ +  - ][ -  + ]:          4 :         if (imp[i].isDefault && i) {
                 [ -  + ]
     125 [ #  # ][ #  # ]:          0 :             list[i] = list[0];
     126         [ #  # ]:          0 :             list[0] = imp[i].unoID;
     127                 :            :         }
     128                 :            :         else
     129         [ +  - ]:          4 :             list[i] = imp[i].unoID;
     130                 :            :     }
     131                 :          4 :     return list;
     132                 :            : }
     133                 :            : 
     134                 :            : Sequence< sal_Int32 > SAL_CALL
     135                 :          0 : CollatorImpl::listCollatorOptions( const OUString& /*collatorAlgorithmName*/ ) throw(RuntimeException)
     136                 :            : {
     137 [ #  # ][ #  # ]:          0 :     Sequence< OUString > option_str = localedata->getCollationOptions(nLocale);
     138         [ #  # ]:          0 :     Sequence< sal_Int32 > option_int(option_str.getLength());
     139                 :            : 
     140         [ #  # ]:          0 :     for (sal_Int32 i = 0; i < option_str.getLength(); i++)
     141         [ #  # ]:          0 :         option_int[i] =
     142         [ #  # ]:          0 :             option_str[i] == "IGNORE_CASE" ?  CollatorOptions::CollatorOptions_IGNORE_CASE :
     143         [ #  # ]:          0 :             option_str[i] == "IGNORE_KANA" ?  CollatorOptions::CollatorOptions_IGNORE_KANA :
     144 [ #  # ][ #  # ]:          0 :             option_str[i] == "IGNORE_WIDTH" ?  CollatorOptions::CollatorOptions_IGNORE_WIDTH : 0;
         [ #  # ][ #  # ]
     145                 :            : 
     146         [ #  # ]:          0 :     return option_int;
     147                 :            : }
     148                 :            : 
     149                 :            : sal_Bool SAL_CALL
     150                 :       2268 : CollatorImpl::createCollator(const lang::Locale& rLocale, const OUString& serviceName, const OUString& rSortAlgorithm)
     151                 :            :     throw(RuntimeException)
     152                 :            : {
     153         [ -  + ]:       2268 :     for (size_t l = 0; l < lookupTable.size(); l++) {
     154                 :          0 :         cachedItem = lookupTable[l];
     155         [ #  # ]:          0 :         if (cachedItem->service.equals(serviceName)) {// cross locale sharing
     156         [ #  # ]:          0 :             lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, cachedItem->xC));
     157                 :          0 :             return sal_True;
     158                 :            :         }
     159                 :            :     }
     160         [ +  - ]:       2268 :     if (xMSF.is()) {
     161                 :            :         Reference < XInterface > xI =
     162 [ +  - ][ +  - ]:       2268 :             xMSF->createInstance(OUString("com.sun.star.i18n.Collator_") + serviceName);
     163                 :            : 
     164         [ +  + ]:       2268 :         if (xI.is()) {
     165                 :        573 :             Reference < XCollator > xC;
     166 [ +  - ][ +  - ]:        573 :             xI->queryInterface( getCppuType((const Reference< XCollator>*)0) ) >>= xC;
         [ +  - ][ +  - ]
     167         [ +  - ]:        573 :             if (xC.is()) {
     168 [ +  - ][ +  - ]:        573 :                 lookupTable.push_back(cachedItem = new lookupTableItem(rLocale, rSortAlgorithm, serviceName, xC));
                 [ +  - ]
     169                 :        573 :                 return sal_True;
     170         [ -  + ]:        573 :             }
     171                 :            :         }
     172                 :       2268 :         return sal_False;
     173                 :            :     }
     174         [ #  # ]:       2268 :     throw RuntimeException();
     175                 :            : }
     176                 :            : 
     177                 :            : void SAL_CALL
     178                 :        573 : CollatorImpl::loadCachedCollator(const lang::Locale& rLocale, const OUString& rSortAlgorithm)
     179                 :            :     throw(RuntimeException)
     180                 :            : {
     181         [ -  + ]:        573 :     for (size_t i = 0; i < lookupTable.size(); i++) {
     182                 :          0 :         cachedItem = lookupTable[i];
     183         [ #  # ]:          0 :         if (cachedItem->equals(rLocale, rSortAlgorithm)) {
     184                 :            :             return;
     185                 :            :         }
     186                 :            :     }
     187                 :            : 
     188                 :            :     static sal_Unicode under = (sal_Unicode) '_';
     189                 :            : 
     190                 :        573 :     sal_Int32 l = rLocale.Language.getLength();
     191                 :        573 :     sal_Int32 c = rLocale.Country.getLength();
     192                 :        573 :     sal_Int32 v = rLocale.Variant.getLength();
     193                 :        573 :     sal_Int32 a = rSortAlgorithm.getLength();
     194                 :        573 :     OUStringBuffer aBuf(l+c+v+a+4);
     195                 :            : 
     196 [ +  - ][ -  + ]:       3975 :     if ((l > 0 && c > 0 && v > 0 && a > 0 &&
         [ #  # ][ #  # ]
         [ +  + ][ +  - ]
         [ +  - ][ +  - ]
         [ +  + ][ +  - ]
           [ +  -  -  +  
             #  #  #  # ]
         [ #  # ][ +  + ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ -  + ]
                 [ +  + ]
     197                 :            :                 // load service with name <base>_<lang>_<country>_<varian>_<algorithm>
     198 [ #  # ][ #  # ]:          0 :                 createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rLocale.Country).append(
                 [ #  # ]
     199 [ #  # ][ #  # ]:          0 :                         under).append(rLocale.Variant).append(under).append(rSortAlgorithm).makeStringAndClear(),
         [ #  # ][ #  # ]
     200 [ #  # ][ #  # ]:        573 :                     rSortAlgorithm)) ||
         [ -  + ][ #  # ]
     201                 :            :             (l > 0 && c > 0 && a > 0 &&
     202                 :            :              // load service with name <base>_<lang>_<country>_<algorithm>
     203 [ +  - ][ +  - ]:        561 :              createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rLocale.Country).append(
                 [ +  - ]
     204 [ +  - ][ +  - ]:       1134 :                      under).append(rSortAlgorithm).makeStringAndClear(), rSortAlgorithm)) ||
         [ +  - ][ +  - ]
         [ +  + ][ #  # ]
     205                 :        561 :             (l > 0 && c > 0 && a > 0 && rLocale.Language == "zh" && (rLocale.Country == "HK" || rLocale.Country == "MO") &&
     206                 :            :              // if the country code is HK or MO, one more step to try TW.
     207 [ #  # ][ #  # ]:          0 :              createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append("TW").append(under).append(
         [ #  # ][ #  # ]
     208 [ #  # ][ #  # ]:        573 :                      rSortAlgorithm).makeStringAndClear(), rSortAlgorithm)) ||
         [ #  # ][ -  + ]
                 [ #  # ]
     209                 :            :             (l > 0 && a > 0 &&
     210                 :            :              // load service with name <base>_<lang>_<algorithm>
     211 [ +  - ][ +  - ]:        561 :              createCollator(rLocale, aBuf.append(rLocale.Language).append(under).append(rSortAlgorithm).makeStringAndClear(),
                 [ +  - ]
     212 [ +  - ][ +  - ]:       1134 :                  rSortAlgorithm)) ||
         [ +  + ][ #  # ]
     213                 :            :             // load service with name <base>_<algorithm>
     214                 :            :             (a > 0 &&
     215         [ +  - ]:        573 :              createCollator(rLocale, rSortAlgorithm, rSortAlgorithm)) ||
     216                 :            :             // load default service with name <base>_Unicode
     217 [ +  - ][ +  - ]:       1146 :             createCollator(rLocale, "Unicode", rSortAlgorithm)) {
                 [ +  - ]
           [ #  #  #  # ]
     218                 :            :                 return;
     219                 :            :             } else {
     220                 :          0 :                 cachedItem = NULL;
     221         [ #  # ]:          0 :                 throw RuntimeException(); // could not load any service
     222                 :        573 :             }
     223                 :            : }
     224                 :            : 
     225                 :            : const sal_Char cCollator[] = "com.sun.star.i18n.Collator";
     226                 :            : 
     227                 :            : OUString SAL_CALL
     228                 :          0 : CollatorImpl::getImplementationName() throw( RuntimeException )
     229                 :            : {
     230                 :          0 :     return OUString::createFromAscii(cCollator);
     231                 :            : }
     232                 :            : 
     233                 :            : sal_Bool SAL_CALL
     234                 :          0 : CollatorImpl::supportsService(const OUString& rServiceName)
     235                 :            :                 throw( RuntimeException )
     236                 :            : {
     237                 :          0 :     return rServiceName.equalsAscii(cCollator);
     238                 :            : }
     239                 :            : 
     240                 :            : Sequence< OUString > SAL_CALL
     241                 :          0 : CollatorImpl::getSupportedServiceNames() throw( RuntimeException )
     242                 :            : {
     243                 :          0 :     Sequence< OUString > aRet(1);
     244         [ #  # ]:          0 :     aRet[0] = OUString::createFromAscii(cCollator);
     245                 :          0 :     return aRet;
     246                 :            : }
     247                 :            : 
     248                 :            : } } } }
     249                 :            : 
     250                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10