LCOV - code coverage report
Current view: top level - unotools/source/i18n - transliterationwrapper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 78 92 84.8 %
Date: 2015-06-13 12:38:46 Functions: 13 13 100.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 <sal/config.h>
      21             : 
      22             : #include <sal/log.hxx>
      23             : #include <unotools/transliterationwrapper.hxx>
      24             : #include <i18nlangtag/languagetag.hxx>
      25             : 
      26             : #include <com/sun/star/i18n/TransliterationModulesExtra.hpp>
      27             : #include <com/sun/star/i18n/Transliteration.hpp>
      28             : 
      29             : using namespace ::com::sun::star::lang;
      30             : using namespace ::com::sun::star::i18n;
      31             : using namespace ::com::sun::star::uno;
      32             : using namespace ::utl;
      33             : 
      34         323 : TransliterationWrapper::TransliterationWrapper(
      35             :                     const Reference< XComponentContext > & rxContext,
      36             :                     sal_uInt32 nTyp )
      37             :     : xTrans( Transliteration::create(rxContext) ),
      38         323 :       aLanguageTag( LANGUAGE_SYSTEM ), nType( nTyp ), bFirstCall( true )
      39             : {
      40         323 : }
      41             : 
      42         250 : TransliterationWrapper::~TransliterationWrapper()
      43             : {
      44         250 : }
      45             : 
      46          95 : OUString TransliterationWrapper::transliterate(const OUString& rStr, sal_uInt16 nLang,
      47             :                                                sal_Int32 nStart, sal_Int32 nLen,
      48             :                                                Sequence <sal_Int32>* pOffset )
      49             : {
      50          95 :     OUString sRet;
      51          95 :     if( xTrans.is() )
      52             :     {
      53             :         try
      54             :         {
      55          95 :             loadModuleIfNeeded( nLang );
      56             : 
      57          95 :             if ( pOffset )
      58          95 :                 sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
      59             :             else
      60           0 :                 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
      61             :         }
      62           0 :         catch( Exception&  )
      63             :         {
      64             :             SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" );
      65             :         }
      66             :     }
      67          95 :     return sRet;
      68             : }
      69             : 
      70           2 : OUString TransliterationWrapper::transliterate( const OUString& rStr,
      71             :                                                 sal_Int32 nStart, sal_Int32 nLen,
      72             :                                                 Sequence <sal_Int32>* pOffset ) const
      73             : {
      74           2 :     OUString sRet( rStr );
      75           2 :     if( xTrans.is() )
      76             :     {
      77             :         try
      78             :         {
      79           2 :             if ( pOffset )
      80           0 :                 sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
      81             :             else
      82           2 :                 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
      83             :         }
      84           0 :         catch( Exception&  )
      85             :         {
      86             :             SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" );
      87             :         }
      88             :     }
      89           2 :     return sRet;
      90             : }
      91             : 
      92         276 : bool TransliterationWrapper::needLanguageForTheMode() const
      93             : {
      94         550 :     return TransliterationModules_UPPERCASE_LOWERCASE == nType ||
      95         540 :            TransliterationModules_LOWERCASE_UPPERCASE == nType ||
      96         268 :            TransliterationModules_IGNORE_CASE == nType ||
      97           4 :            (sal_uInt32) TransliterationModulesExtra::SENTENCE_CASE == (sal_uInt32) nType ||
      98         280 :            (sal_uInt32) TransliterationModulesExtra::TITLE_CASE    == (sal_uInt32) nType ||
      99         278 :            (sal_uInt32) TransliterationModulesExtra::TOGGLE_CASE   == (sal_uInt32) nType;
     100             : }
     101             : 
     102         370 : void TransliterationWrapper::setLanguageLocaleImpl( sal_uInt16 nLang )
     103             : {
     104         370 :     if( LANGUAGE_NONE == nLang )
     105           0 :         nLang = LANGUAGE_SYSTEM;
     106         370 :     aLanguageTag.reset( nLang);
     107         370 : }
     108             : 
     109         908 : void TransliterationWrapper::loadModuleIfNeeded( sal_uInt16 nLang )
     110             : {
     111         908 :     bool bLoad = bFirstCall;
     112         908 :     bFirstCall = false;
     113             : 
     114         908 :     if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::SENTENCE_CASE )
     115             :     {
     116           1 :         if( bLoad )
     117           1 :             loadModuleByImplName(OUString("SENTENCE_CASE"), nLang);
     118             :     }
     119         907 :     else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TITLE_CASE )
     120             :     {
     121           1 :         if( bLoad )
     122           1 :             loadModuleByImplName(OUString("TITLE_CASE"), nLang);
     123             :     }
     124         906 :     else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TOGGLE_CASE )
     125             :     {
     126           1 :         if( bLoad )
     127           1 :             loadModuleByImplName(OUString("TOGGLE_CASE"), nLang);
     128             :     }
     129             :     else
     130             :     {
     131         905 :         if( aLanguageTag.getLanguageType() != nLang )
     132             :         {
     133         365 :             setLanguageLocaleImpl( nLang );
     134         365 :             if( !bLoad )
     135         270 :                 bLoad = needLanguageForTheMode();
     136             :         }
     137         905 :         if( bLoad )
     138         585 :             loadModuleImpl();
     139             :     }
     140         908 : }
     141             : 
     142         585 : void TransliterationWrapper::loadModuleImpl() const
     143             : {
     144         585 :     if ( bFirstCall )
     145           0 :         const_cast<TransliterationWrapper*>(this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM );
     146             : 
     147             :     try
     148             :     {
     149         585 :         if ( xTrans.is() )
     150         585 :             xTrans->loadModule( (TransliterationModules)nType, aLanguageTag.getLocale() );
     151             :     }
     152           0 :     catch ( const Exception& e )
     153             :     {
     154             :         SAL_WARN( "unotools.i18n", "loadModuleImpl: Exception caught " << e.Message );
     155             :     }
     156             : 
     157         585 :     bFirstCall = false;
     158         585 : }
     159             : 
     160           5 : void TransliterationWrapper::loadModuleByImplName(const OUString& rModuleName,
     161             :                                                   sal_uInt16 nLang )
     162             : {
     163             :     try
     164             :     {
     165           5 :         setLanguageLocaleImpl( nLang );
     166           5 :         com::sun::star::lang::Locale aLocale( aLanguageTag.getLocale());
     167             :         // Reset LanguageTag, so the next call to loadModuleIfNeeded() forces
     168             :         // new settings.
     169           5 :         aLanguageTag.reset( LANGUAGE_DONTKNOW);
     170           5 :         if ( xTrans.is() )
     171           5 :             xTrans->loadModuleByImplName( rModuleName, aLocale );
     172             :     }
     173           0 :     catch ( const Exception& e )
     174             :     {
     175             :         SAL_WARN( "unotools.i18n", "loadModuleByImplName: Exception caught " << e.Message );
     176             :     }
     177             : 
     178           5 :     bFirstCall = false;
     179           5 : }
     180             : 
     181       32493 : bool TransliterationWrapper::equals(
     182             :     const OUString& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1,
     183             :     const OUString& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const
     184             : {
     185             :     try
     186             :     {
     187       32493 :         if( bFirstCall )
     188           0 :             loadModuleImpl();
     189       32493 :         if ( xTrans.is() )
     190       32493 :             return xTrans->equals( rStr1, nPos1, nCount1, nMatch1, rStr2, nPos2, nCount2, nMatch2 );
     191             :     }
     192           0 :     catch ( const Exception& e )
     193             :     {
     194             :         SAL_WARN( "unotools.i18n", "equals: Exception caught " << e.Message );
     195             :     }
     196           0 :     return false;
     197             : }
     198             : 
     199          87 : sal_Int32 TransliterationWrapper::compareString( const OUString& rStr1, const OUString& rStr2 ) const
     200             : {
     201             :     try
     202             :     {
     203          87 :         if( bFirstCall )
     204           0 :             loadModuleImpl();
     205          87 :         if ( xTrans.is() )
     206          87 :             return xTrans->compareString( rStr1, rStr2 );
     207             :     }
     208           0 :     catch (const Exception& e)
     209             :     {
     210             :         SAL_WARN( "unotools.i18n", "compareString: Exception caught " << e.Message );
     211             :     }
     212           0 :     return 0;
     213             : }
     214             : 
     215             : // --- helpers --------------------------------------------------------
     216             : 
     217       14939 : bool TransliterationWrapper::isEqual( const OUString& rStr1, const OUString& rStr2 ) const
     218             : {
     219       14939 :     sal_Int32 nMatch1(0), nMatch2(0);
     220             :     bool bMatch = equals(
     221             :         rStr1, 0, rStr1.getLength(), nMatch1,
     222       14939 :         rStr2, 0, rStr2.getLength(), nMatch2 );
     223       14939 :     return bMatch;
     224             : }
     225             : 
     226       17466 : bool TransliterationWrapper::isMatch( const OUString& rStr1, const OUString& rStr2 ) const
     227             : {
     228       17466 :     sal_Int32 nMatch1(0), nMatch2(0);
     229             :     equals(
     230             :         rStr1, 0, rStr1.getLength(), nMatch1,
     231       17466 :         rStr2, 0, rStr2.getLength(), nMatch2 );
     232       17466 :     return (nMatch1 <= nMatch2) && (nMatch1 == rStr1.getLength());
     233             : }
     234             : 
     235             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11