LCOV - code coverage report
Current view: top level - unotools/source/i18n - transliterationwrapper.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 90 0.0 %
Date: 2014-04-14 Functions: 0 13 0.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           0 : TransliterationWrapper::TransliterationWrapper(
      33             :                     const Reference< XComponentContext > & rxContext,
      34             :                     sal_uInt32 nTyp )
      35             :     : xTrans( Transliteration::create(rxContext) ),
      36           0 :       aLanguageTag( LANGUAGE_SYSTEM ), nType( nTyp ), bFirstCall( true )
      37             : {
      38           0 : }
      39             : 
      40           0 : TransliterationWrapper::~TransliterationWrapper()
      41             : {
      42           0 : }
      43             : 
      44           0 : OUString TransliterationWrapper::transliterate(const OUString& rStr, sal_uInt16 nLang,
      45             :                                                sal_Int32 nStart, sal_Int32 nLen,
      46             :                                                Sequence <sal_Int32>* pOffset )
      47             : {
      48           0 :     OUString sRet;
      49           0 :     if( xTrans.is() )
      50             :     {
      51             :         try
      52             :         {
      53           0 :             loadModuleIfNeeded( nLang );
      54             : 
      55           0 :             if ( pOffset )
      56           0 :                 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           0 :     return sRet;
      66             : }
      67             : 
      68           0 : OUString TransliterationWrapper::transliterate( const OUString& rStr,
      69             :                                                 sal_Int32 nStart, sal_Int32 nLen,
      70             :                                                 Sequence <sal_Int32>* pOffset ) const
      71             : {
      72           0 :     OUString sRet( rStr );
      73           0 :     if( xTrans.is() )
      74             :     {
      75             :         try
      76             :         {
      77           0 :             if ( pOffset )
      78           0 :                 sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset );
      79             :             else
      80           0 :                 sRet = xTrans->transliterateString2String( rStr, nStart, nLen);
      81             :         }
      82           0 :         catch( Exception&  )
      83             :         {
      84             :             SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" );
      85             :         }
      86             :     }
      87           0 :     return sRet;
      88             : }
      89             : 
      90           0 : bool TransliterationWrapper::needLanguageForTheMode() const
      91             : {
      92           0 :     return TransliterationModules_UPPERCASE_LOWERCASE == nType ||
      93           0 :            TransliterationModules_LOWERCASE_UPPERCASE == nType ||
      94           0 :            TransliterationModules_IGNORE_CASE == nType ||
      95           0 :            (sal_uInt32) TransliterationModulesExtra::SENTENCE_CASE == (sal_uInt32) nType ||
      96           0 :            (sal_uInt32) TransliterationModulesExtra::TITLE_CASE    == (sal_uInt32) nType ||
      97           0 :            (sal_uInt32) TransliterationModulesExtra::TOGGLE_CASE   == (sal_uInt32) nType;
      98             : }
      99             : 
     100           0 : void TransliterationWrapper::setLanguageLocaleImpl( sal_uInt16 nLang )
     101             : {
     102           0 :     if( LANGUAGE_NONE == nLang )
     103           0 :         nLang = LANGUAGE_SYSTEM;
     104           0 :     aLanguageTag.reset( nLang);
     105           0 : }
     106             : 
     107           0 : void TransliterationWrapper::loadModuleIfNeeded( sal_uInt16 nLang )
     108             : {
     109           0 :     bool bLoad = bFirstCall;
     110           0 :     bFirstCall = false;
     111             : 
     112           0 :     if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::SENTENCE_CASE )
     113             :     {
     114           0 :         if( bLoad )
     115           0 :             loadModuleByImplName(OUString("SENTENCE_CASE"), nLang);
     116             :     }
     117           0 :     else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TITLE_CASE )
     118             :     {
     119           0 :         if( bLoad )
     120           0 :             loadModuleByImplName(OUString("TITLE_CASE"), nLang);
     121             :     }
     122           0 :     else if( static_cast< sal_Int32 >(nType) == TransliterationModulesExtra::TOGGLE_CASE )
     123             :     {
     124           0 :         if( bLoad )
     125           0 :             loadModuleByImplName(OUString("TOGGLE_CASE"), nLang);
     126             :     }
     127             :     else
     128             :     {
     129           0 :         if( aLanguageTag.getLanguageType() != nLang )
     130             :         {
     131           0 :             setLanguageLocaleImpl( nLang );
     132           0 :             if( !bLoad )
     133           0 :                 bLoad = needLanguageForTheMode();
     134             :         }
     135           0 :         if( bLoad )
     136           0 :             loadModuleImpl();
     137             :     }
     138           0 : }
     139             : 
     140           0 : void TransliterationWrapper::loadModuleImpl() const
     141             : {
     142           0 :     if ( bFirstCall )
     143           0 :         ((TransliterationWrapper*)this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM );
     144             : 
     145             :     try
     146             :     {
     147           0 :         if ( xTrans.is() )
     148           0 :             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           0 :     bFirstCall = false;
     156           0 : }
     157             : 
     158           0 : void TransliterationWrapper::loadModuleByImplName(const OUString& rModuleName,
     159             :                                                   sal_uInt16 nLang )
     160             : {
     161             :     try
     162             :     {
     163           0 :         setLanguageLocaleImpl( nLang );
     164           0 :         com::sun::star::lang::Locale aLocale( aLanguageTag.getLocale());
     165             :         // Reset LanguageTag, so the next call to loadModuleIfNeeded() forces
     166             :         // new settings.
     167           0 :         aLanguageTag.reset( LANGUAGE_DONTKNOW);
     168           0 :         if ( xTrans.is() )
     169           0 :             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           0 :     bFirstCall = false;
     177           0 : }
     178             : 
     179           0 : 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           0 :         if( bFirstCall )
     186           0 :             loadModuleImpl();
     187           0 :         if ( xTrans.is() )
     188           0 :             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           0 : sal_Int32 TransliterationWrapper::compareString( const OUString& rStr1, const OUString& rStr2 ) const
     198             : {
     199             :     try
     200             :     {
     201           0 :         if( bFirstCall )
     202           0 :             loadModuleImpl();
     203           0 :         if ( xTrans.is() )
     204           0 :             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           0 : 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           0 :         rStr2, 0, rStr2.getLength(), nMatch2 );
     221           0 :     return bMatch;
     222             : }
     223             : 
     224           0 : 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           0 :         rStr2, 0, rStr2.getLength(), nMatch2 );
     230           0 :     return (nMatch1 <= nMatch2) && (nMatch1 == rStr1.getLength());
     231             : }
     232             : 
     233             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10