1*dde7d3faSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*dde7d3faSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*dde7d3faSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*dde7d3faSAndrew Rist * distributed with this work for additional information 6*dde7d3faSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*dde7d3faSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*dde7d3faSAndrew Rist * "License"); you may not use this file except in compliance 9*dde7d3faSAndrew Rist * with the License. You may obtain a copy of the License at 10*dde7d3faSAndrew Rist * 11*dde7d3faSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*dde7d3faSAndrew Rist * 13*dde7d3faSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*dde7d3faSAndrew Rist * software distributed under the License is distributed on an 15*dde7d3faSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*dde7d3faSAndrew Rist * KIND, either express or implied. See the License for the 17*dde7d3faSAndrew Rist * specific language governing permissions and limitations 18*dde7d3faSAndrew Rist * under the License. 19*dde7d3faSAndrew Rist * 20*dde7d3faSAndrew Rist *************************************************************/ 21*dde7d3faSAndrew Rist 22*dde7d3faSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_comphelper.hxx" 26cdf0e10cSrcweir #include <comphelper/officeresourcebundle.hxx> 27cdf0e10cSrcweir 28cdf0e10cSrcweir /** === begin UNO includes === **/ 29cdf0e10cSrcweir #include <com/sun/star/resource/XResourceBundle.hpp> 30cdf0e10cSrcweir #include <com/sun/star/resource/XResourceBundleLoader.hpp> 31cdf0e10cSrcweir #include <com/sun/star/lang/NullPointerException.hpp> 32cdf0e10cSrcweir /** === end UNO includes === **/ 33cdf0e10cSrcweir #include <osl/mutex.hxx> 34cdf0e10cSrcweir #include <osl/diagnose.h> 35cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 36cdf0e10cSrcweir 37cdf0e10cSrcweir //........................................................................ 38cdf0e10cSrcweir namespace comphelper 39cdf0e10cSrcweir { 40cdf0e10cSrcweir //........................................................................ 41cdf0e10cSrcweir 42cdf0e10cSrcweir /** === begin UNO using === **/ 43cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 44cdf0e10cSrcweir using com::sun::star::resource::XResourceBundle; 45cdf0e10cSrcweir using com::sun::star::resource::XResourceBundleLoader; 46cdf0e10cSrcweir using com::sun::star::resource::MissingResourceException; 47cdf0e10cSrcweir using ::com::sun::star::uno::XComponentContext; 48cdf0e10cSrcweir using ::com::sun::star::lang::NullPointerException; 49cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 50cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 51cdf0e10cSrcweir using ::com::sun::star::uno::Any; 52cdf0e10cSrcweir /** === end UNO using === **/ 53cdf0e10cSrcweir 54cdf0e10cSrcweir //==================================================================== 55cdf0e10cSrcweir //= ResourceBundle_Impl 56cdf0e10cSrcweir //==================================================================== 57cdf0e10cSrcweir class ResourceBundle_Impl 58cdf0e10cSrcweir { 59cdf0e10cSrcweir private: 60cdf0e10cSrcweir Reference< XComponentContext > m_xContext; 61cdf0e10cSrcweir ::rtl::OUString m_sBaseName; 62cdf0e10cSrcweir Reference< XResourceBundle > m_xBundle; 63cdf0e10cSrcweir bool m_bAttemptedCreate; 64cdf0e10cSrcweir mutable ::osl::Mutex m_aMutex; 65cdf0e10cSrcweir 66cdf0e10cSrcweir public: ResourceBundle_Impl(const Reference<XComponentContext> & _context,const::rtl::OUString & _baseName)67cdf0e10cSrcweir ResourceBundle_Impl( const Reference< XComponentContext >& _context, const ::rtl::OUString& _baseName ) 68cdf0e10cSrcweir :m_xContext( _context ) 69cdf0e10cSrcweir ,m_sBaseName( _baseName ) 70cdf0e10cSrcweir ,m_bAttemptedCreate( false ) 71cdf0e10cSrcweir { 72cdf0e10cSrcweir } 73cdf0e10cSrcweir 74cdf0e10cSrcweir public: 75cdf0e10cSrcweir /** loads the string with the given resource id from the resource bundle 76cdf0e10cSrcweir @param _resourceId 77cdf0e10cSrcweir the id of the string to load 78cdf0e10cSrcweir @return 79cdf0e10cSrcweir the requested resource string. If no string with the given id exists in the resource bundle, 80cdf0e10cSrcweir an empty string is returned. In a non-product version, an OSL_ENSURE will notify you of this 81cdf0e10cSrcweir then. 82cdf0e10cSrcweir */ 83cdf0e10cSrcweir ::rtl::OUString loadString( sal_Int32 _resourceId ) const; 84cdf0e10cSrcweir 85cdf0e10cSrcweir /** determines whether the resource bundle has a string with the given id 86cdf0e10cSrcweir @param _resourceId 87cdf0e10cSrcweir the id of the string whose existence is to be checked 88cdf0e10cSrcweir @return 89cdf0e10cSrcweir <TRUE/> if and only if a string with the given ID exists in the resource 90cdf0e10cSrcweir bundle. 91cdf0e10cSrcweir */ 92cdf0e10cSrcweir bool hasString( sal_Int32 _resourceId ) const; 93cdf0e10cSrcweir 94cdf0e10cSrcweir private: 95cdf0e10cSrcweir /** loads the bundle represented by the instance 96cdf0e10cSrcweir 97cdf0e10cSrcweir The method is safe against multiple calls: If a previos call succeeded or failed, the 98cdf0e10cSrcweir previous result will be returned, without any other processing. 99cdf0e10cSrcweir 100cdf0e10cSrcweir @precond 101cdf0e10cSrcweir Our mutex is locked. 102cdf0e10cSrcweir */ 103cdf0e10cSrcweir bool impl_loadBundle_nothrow(); 104cdf0e10cSrcweir 105cdf0e10cSrcweir /** returns the resource bundle key for a string with a given resource id 106cdf0e10cSrcweir */ 107cdf0e10cSrcweir static ::rtl::OUString 108cdf0e10cSrcweir impl_getStringResourceKey( sal_Int32 _resourceId ); 109cdf0e10cSrcweir }; 110cdf0e10cSrcweir 111cdf0e10cSrcweir //-------------------------------------------------------------------- impl_getStringResourceKey(sal_Int32 _resourceId)112cdf0e10cSrcweir ::rtl::OUString ResourceBundle_Impl::impl_getStringResourceKey( sal_Int32 _resourceId ) 113cdf0e10cSrcweir { 114cdf0e10cSrcweir ::rtl::OUStringBuffer key; 115cdf0e10cSrcweir key.appendAscii( "string:" ); 116cdf0e10cSrcweir key.append( _resourceId ); 117cdf0e10cSrcweir return key.makeStringAndClear(); 118cdf0e10cSrcweir } 119cdf0e10cSrcweir 120cdf0e10cSrcweir //-------------------------------------------------------------------- loadString(sal_Int32 _resourceId) const121cdf0e10cSrcweir ::rtl::OUString ResourceBundle_Impl::loadString( sal_Int32 _resourceId ) const 122cdf0e10cSrcweir { 123cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 124cdf0e10cSrcweir 125cdf0e10cSrcweir ::rtl::OUString sString; 126cdf0e10cSrcweir 127cdf0e10cSrcweir if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() ) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir try 130cdf0e10cSrcweir { 131cdf0e10cSrcweir OSL_VERIFY( m_xBundle->getByName( impl_getStringResourceKey( _resourceId ) ) >>= sString ); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir catch( const Exception& ) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir OSL_ENSURE( false, "ResourceBundle_Impl::loadString: caught an exception!" ); 136cdf0e10cSrcweir } 137cdf0e10cSrcweir } 138cdf0e10cSrcweir return sString; 139cdf0e10cSrcweir } 140cdf0e10cSrcweir 141cdf0e10cSrcweir //-------------------------------------------------------------------- hasString(sal_Int32 _resourceId) const142cdf0e10cSrcweir bool ResourceBundle_Impl::hasString( sal_Int32 _resourceId ) const 143cdf0e10cSrcweir { 144cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 145cdf0e10cSrcweir 146cdf0e10cSrcweir bool has = false; 147cdf0e10cSrcweir 148cdf0e10cSrcweir if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() ) 149cdf0e10cSrcweir { 150cdf0e10cSrcweir try 151cdf0e10cSrcweir { 152cdf0e10cSrcweir has = m_xBundle->hasByName( impl_getStringResourceKey( _resourceId ) ); 153cdf0e10cSrcweir } 154cdf0e10cSrcweir catch( const Exception& ) 155cdf0e10cSrcweir { 156cdf0e10cSrcweir OSL_ENSURE( false, "ResourceBundle_Impl::hasString: caught an exception!" ); 157cdf0e10cSrcweir } 158cdf0e10cSrcweir } 159cdf0e10cSrcweir return has; 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir //-------------------------------------------------------------------- impl_loadBundle_nothrow()163cdf0e10cSrcweir bool ResourceBundle_Impl::impl_loadBundle_nothrow() 164cdf0e10cSrcweir { 165cdf0e10cSrcweir if ( m_bAttemptedCreate ) 166cdf0e10cSrcweir return m_xBundle.is(); 167cdf0e10cSrcweir 168cdf0e10cSrcweir m_bAttemptedCreate = true; 169cdf0e10cSrcweir 170cdf0e10cSrcweir Reference< XResourceBundleLoader > xLoader; 171cdf0e10cSrcweir try 172cdf0e10cSrcweir { 173cdf0e10cSrcweir Any aValue( m_xContext->getValueByName( 174cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 175cdf0e10cSrcweir "/singletons/com.sun.star.resource.OfficeResourceLoader" ) ) ) ); 176cdf0e10cSrcweir OSL_VERIFY( aValue >>= xLoader ); 177cdf0e10cSrcweir } 178cdf0e10cSrcweir catch( const Exception& ) 179cdf0e10cSrcweir { 180cdf0e10cSrcweir OSL_ENSURE( false, "ResourceBundle_Impl::impl_loadBundle_nopthrow: could not create the resource loader!" ); 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir if ( !xLoader.is() ) 184cdf0e10cSrcweir return false; 185cdf0e10cSrcweir 186cdf0e10cSrcweir try 187cdf0e10cSrcweir { 188cdf0e10cSrcweir m_xBundle = xLoader->loadBundle_Default( m_sBaseName ); 189cdf0e10cSrcweir } 190cdf0e10cSrcweir catch( const MissingResourceException& ) 191cdf0e10cSrcweir { 192cdf0e10cSrcweir OSL_ENSURE( false, "ResourceBundle_Impl::impl_loadBundle_nopthrow: missing the given resource bundle!" ); 193cdf0e10cSrcweir } 194cdf0e10cSrcweir 195cdf0e10cSrcweir return m_xBundle.is(); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir //==================================================================== 199cdf0e10cSrcweir //= OfficeResourceBundle 200cdf0e10cSrcweir //==================================================================== 201cdf0e10cSrcweir //-------------------------------------------------------------------- OfficeResourceBundle(const Reference<XComponentContext> & _context,const::rtl::OUString & _bundleBaseName)202cdf0e10cSrcweir OfficeResourceBundle::OfficeResourceBundle( const Reference< XComponentContext >& _context, const ::rtl::OUString& _bundleBaseName ) 203cdf0e10cSrcweir :m_pImpl( new ResourceBundle_Impl( _context, _bundleBaseName ) ) 204cdf0e10cSrcweir { 205cdf0e10cSrcweir if ( !_context.is() ) 206cdf0e10cSrcweir throw NullPointerException(); 207cdf0e10cSrcweir } 208cdf0e10cSrcweir 209cdf0e10cSrcweir //-------------------------------------------------------------------- OfficeResourceBundle(const Reference<XComponentContext> & _context,const sal_Char * _bundleBaseAsciiName)210cdf0e10cSrcweir OfficeResourceBundle::OfficeResourceBundle( const Reference< XComponentContext >& _context, const sal_Char* _bundleBaseAsciiName ) 211cdf0e10cSrcweir :m_pImpl( new ResourceBundle_Impl( _context, ::rtl::OUString::createFromAscii( _bundleBaseAsciiName ) ) ) 212cdf0e10cSrcweir { 213cdf0e10cSrcweir if ( !_context.is() ) 214cdf0e10cSrcweir throw NullPointerException(); 215cdf0e10cSrcweir } 216cdf0e10cSrcweir 217cdf0e10cSrcweir //-------------------------------------------------------------------- ~OfficeResourceBundle()218cdf0e10cSrcweir OfficeResourceBundle::~OfficeResourceBundle() 219cdf0e10cSrcweir { 220cdf0e10cSrcweir } 221cdf0e10cSrcweir 222cdf0e10cSrcweir //-------------------------------------------------------------------- loadString(sal_Int32 _resourceId) const223cdf0e10cSrcweir ::rtl::OUString OfficeResourceBundle::loadString( sal_Int32 _resourceId ) const 224cdf0e10cSrcweir { 225cdf0e10cSrcweir return m_pImpl->loadString( _resourceId ); 226cdf0e10cSrcweir } 227cdf0e10cSrcweir 228cdf0e10cSrcweir //-------------------------------------------------------------------- hasString(sal_Int32 _resourceId) const229cdf0e10cSrcweir bool OfficeResourceBundle::hasString( sal_Int32 _resourceId ) const 230cdf0e10cSrcweir { 231cdf0e10cSrcweir return m_pImpl->hasString( _resourceId ); 232cdf0e10cSrcweir } 233cdf0e10cSrcweir 234cdf0e10cSrcweir //........................................................................ 235cdf0e10cSrcweir } // namespace comphelper 236cdf0e10cSrcweir //........................................................................ 237