LCOV - code coverage report
Current view: top level - comphelper/source/misc - officeresourcebundle.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 34 48 70.8 %
Date: 2012-08-25 Functions: 8 10 80.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 25 80 31.2 %

           Branch data     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 <comphelper/officeresourcebundle.hxx>
      21                 :            : 
      22                 :            : #include <com/sun/star/resource/XResourceBundle.hpp>
      23                 :            : #include <com/sun/star/resource/XResourceBundleLoader.hpp>
      24                 :            : #include <com/sun/star/lang/NullPointerException.hpp>
      25                 :            : #include <osl/mutex.hxx>
      26                 :            : #include <osl/diagnose.h>
      27                 :            : #include <rtl/ustrbuf.hxx>
      28                 :            : 
      29                 :            : //........................................................................
      30                 :            : namespace comphelper
      31                 :            : {
      32                 :            : //........................................................................
      33                 :            : 
      34                 :            :     /** === begin UNO using === **/
      35                 :            :     using ::com::sun::star::uno::Reference;
      36                 :            :     using com::sun::star::resource::XResourceBundle;
      37                 :            :     using com::sun::star::resource::XResourceBundleLoader;
      38                 :            :     using com::sun::star::resource::MissingResourceException;
      39                 :            :     using ::com::sun::star::uno::XComponentContext;
      40                 :            :     using ::com::sun::star::lang::NullPointerException;
      41                 :            :     using ::com::sun::star::uno::UNO_QUERY;
      42                 :            :     using ::com::sun::star::uno::Exception;
      43                 :            :     using ::com::sun::star::uno::Any;
      44                 :            :     /** === end UNO using === **/
      45                 :            : 
      46                 :            :     //====================================================================
      47                 :            :     //= ResourceBundle_Impl
      48                 :            :     //====================================================================
      49         [ +  - ]:          5 :     class ResourceBundle_Impl
      50                 :            :     {
      51                 :            :     private:
      52                 :            :         Reference< XComponentContext >  m_xContext;
      53                 :            :         ::rtl::OUString                 m_sBaseName;
      54                 :            :         Reference< XResourceBundle >    m_xBundle;
      55                 :            :         bool                            m_bAttemptedCreate;
      56                 :            :         mutable ::osl::Mutex            m_aMutex;
      57                 :            : 
      58                 :            :     public:
      59                 :          5 :         ResourceBundle_Impl( const Reference< XComponentContext >& _context, const ::rtl::OUString& _baseName )
      60                 :            :             :m_xContext( _context )
      61                 :            :             ,m_sBaseName( _baseName )
      62         [ +  - ]:          5 :             ,m_bAttemptedCreate( false )
      63                 :            :         {
      64                 :          5 :         }
      65                 :            : 
      66                 :            :     public:
      67                 :            :         /** loads the string with the given resource id from the resource bundle
      68                 :            :             @param  _resourceId
      69                 :            :                 the id of the string to load
      70                 :            :             @return
      71                 :            :                 the requested resource string. If no string with the given id exists in the resource bundle,
      72                 :            :                 an empty string is returned. In a non-product version, an OSL_ENSURE will notify you of this
      73                 :            :                 then.
      74                 :            :         */
      75                 :            :         ::rtl::OUString loadString( sal_Int32 _resourceId ) const;
      76                 :            : 
      77                 :            :         /** determines whether the resource bundle has a string with the given id
      78                 :            :             @param  _resourceId
      79                 :            :                 the id of the string whose existence is to be checked
      80                 :            :             @return
      81                 :            :                 <TRUE/> if and only if a string with the given ID exists in the resource
      82                 :            :                 bundle.
      83                 :            :         */
      84                 :            :         bool            hasString( sal_Int32 _resourceId ) const;
      85                 :            : 
      86                 :            :     private:
      87                 :            :         /** loads the bundle represented by the instance
      88                 :            : 
      89                 :            :             The method is safe against multiple calls: If a previos call succeeded or failed, the
      90                 :            :             previous result will be returned, without any other processing.
      91                 :            : 
      92                 :            :             @precond
      93                 :            :                 Our mutex is locked.
      94                 :            :         */
      95                 :            :         bool    impl_loadBundle_nothrow();
      96                 :            : 
      97                 :            :         /** returns the resource bundle key for a string with a given resource id
      98                 :            :         */
      99                 :            :         static ::rtl::OUString
     100                 :            :                 impl_getStringResourceKey( sal_Int32 _resourceId );
     101                 :            :     };
     102                 :            : 
     103                 :            :     //--------------------------------------------------------------------
     104                 :         46 :     ::rtl::OUString ResourceBundle_Impl::impl_getStringResourceKey( sal_Int32 _resourceId )
     105                 :            :     {
     106                 :         46 :         ::rtl::OUStringBuffer key;
     107         [ +  - ]:         46 :         key.appendAscii( "string:" );
     108         [ +  - ]:         46 :         key.append( _resourceId );
     109         [ +  - ]:         46 :         return key.makeStringAndClear();
     110                 :            :     }
     111                 :            : 
     112                 :            :     //--------------------------------------------------------------------
     113                 :         46 :     ::rtl::OUString ResourceBundle_Impl::loadString( sal_Int32 _resourceId ) const
     114                 :            :     {
     115         [ +  - ]:         46 :         ::osl::MutexGuard aGuard( m_aMutex );
     116                 :            : 
     117                 :         46 :         ::rtl::OUString sString;
     118                 :            : 
     119 [ +  - ][ +  - ]:         46 :         if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() )
     120                 :            :         {
     121                 :            :             try
     122                 :            :             {
     123 [ +  - ][ +  - ]:         46 :                 OSL_VERIFY( m_xBundle->getByName( impl_getStringResourceKey( _resourceId ) ) >>= sString );
         [ +  - ][ #  # ]
     124                 :            :             }
     125         [ #  # ]:          0 :             catch( const Exception& )
     126                 :            :             {
     127                 :            :                 OSL_FAIL( "ResourceBundle_Impl::loadString: caught an exception!" );
     128                 :            :             }
     129                 :            :         }
     130         [ +  - ]:         46 :         return sString;
     131                 :            :     }
     132                 :            : 
     133                 :            :     //--------------------------------------------------------------------
     134                 :          0 :     bool ResourceBundle_Impl::hasString( sal_Int32 _resourceId ) const
     135                 :            :     {
     136         [ #  # ]:          0 :         ::osl::MutexGuard aGuard( m_aMutex );
     137                 :            : 
     138                 :          0 :         bool has = false;
     139                 :            : 
     140 [ #  # ][ #  # ]:          0 :         if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() )
     141                 :            :         {
     142                 :            :             try
     143                 :            :             {
     144 [ #  # ][ #  # ]:          0 :                 has = m_xBundle->hasByName( impl_getStringResourceKey( _resourceId ) );
         [ #  # ][ #  # ]
     145                 :            :             }
     146         [ #  # ]:          0 :             catch( const Exception& )
     147                 :            :             {
     148                 :            :                 OSL_FAIL( "ResourceBundle_Impl::hasString: caught an exception!" );
     149                 :            :             }
     150                 :            :         }
     151         [ #  # ]:          0 :         return has;
     152                 :            :     }
     153                 :            : 
     154                 :            :     //--------------------------------------------------------------------
     155                 :         46 :     bool ResourceBundle_Impl::impl_loadBundle_nothrow()
     156                 :            :     {
     157         [ +  + ]:         46 :         if ( m_bAttemptedCreate )
     158                 :         41 :             return m_xBundle.is();
     159                 :            : 
     160                 :          5 :         m_bAttemptedCreate = true;
     161                 :            : 
     162                 :          5 :         Reference< XResourceBundleLoader > xLoader;
     163                 :            :         try
     164                 :            :         {
     165         [ +  - ]:          5 :             Any aValue( m_xContext->getValueByName(
     166                 :            :                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
     167 [ +  - ][ +  - ]:          5 :                     "/singletons/com.sun.star.resource.OfficeResourceLoader" ) ) ) );
     168 [ #  # ][ +  - ]:          5 :             OSL_VERIFY( aValue >>= xLoader );
     169                 :            :         }
     170         [ #  # ]:          0 :         catch( const Exception& )
     171                 :            :         {
     172                 :            :             OSL_FAIL( "ResourceBundle_Impl::impl_loadBundle_nopthrow: could not create the resource loader!" );
     173                 :            :         }
     174                 :            : 
     175         [ -  + ]:          5 :         if ( !xLoader.is() )
     176                 :          0 :             return false;
     177                 :            : 
     178                 :            :         try
     179                 :            :         {
     180 [ +  - ][ +  - ]:          5 :             m_xBundle = xLoader->loadBundle_Default( m_sBaseName );
         [ +  - ][ #  # ]
     181                 :            :         }
     182         [ #  # ]:          0 :         catch( const MissingResourceException& )
     183                 :            :         {
     184                 :            :             OSL_FAIL( "ResourceBundle_Impl::impl_loadBundle_nopthrow: missing the given resource bundle!" );
     185                 :            :         }
     186                 :            : 
     187                 :         46 :         return m_xBundle.is();
     188                 :            :     }
     189                 :            : 
     190                 :            :     //====================================================================
     191                 :            :     //= OfficeResourceBundle
     192                 :            :     //====================================================================
     193                 :            :     //--------------------------------------------------------------------
     194                 :          5 :     OfficeResourceBundle::OfficeResourceBundle( const Reference< XComponentContext >& _context, const sal_Char* _bundleBaseAsciiName )
     195 [ +  - ][ +  - ]:          5 :         :m_pImpl( new ResourceBundle_Impl( _context, ::rtl::OUString::createFromAscii( _bundleBaseAsciiName ) ) )
     196                 :            :     {
     197         [ -  + ]:          5 :         if ( !_context.is() )
     198         [ #  # ]:          0 :             throw NullPointerException();
     199                 :          5 :     }
     200                 :            : 
     201                 :            :     //--------------------------------------------------------------------
     202                 :          5 :     OfficeResourceBundle::~OfficeResourceBundle()
     203                 :            :     {
     204                 :          5 :     }
     205                 :            : 
     206                 :            :     //--------------------------------------------------------------------
     207                 :         46 :     ::rtl::OUString OfficeResourceBundle::loadString( sal_Int32 _resourceId ) const
     208                 :            :     {
     209                 :         46 :         return m_pImpl->loadString( _resourceId );
     210                 :            :     }
     211                 :            : 
     212                 :            :     //--------------------------------------------------------------------
     213                 :          0 :     bool OfficeResourceBundle::hasString( sal_Int32 _resourceId ) const
     214                 :            :     {
     215                 :          0 :         return m_pImpl->hasString( _resourceId );
     216                 :            :     }
     217                 :            : 
     218                 :            : //........................................................................
     219                 :            : } // namespace comphelper
     220                 :            : //........................................................................
     221                 :            : 
     222                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10