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

Generated by: LCOV version 1.11