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

Generated by: LCOV version 1.10