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

Generated by: LCOV version 1.10