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

Generated by: LCOV version 1.10