bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / officeresourcebundle.cxx
blob535a20793cd0d0f33195eb35a421c995b732d897
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/officeresourcebundle.hxx>
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>
29 namespace comphelper
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;
42 class ResourceBundle_Impl
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;
51 public:
52 ResourceBundle_Impl( const Reference< XComponentContext >& _context, const OUString& _baseName )
53 :m_xContext( _context )
54 ,m_sBaseName( _baseName )
55 ,m_bAttemptedCreate( false )
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.
68 OUString loadString( sal_Int32 _resourceId ) const;
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.
77 bool hasString( sal_Int32 _resourceId ) const;
79 private:
80 /** loads the bundle represented by the instance
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.
85 @precond
86 Our mutex is locked.
88 bool impl_loadBundle_nothrow();
90 /** returns the resource bundle key for a string with a given resource id
92 static OUString
93 impl_getStringResourceKey( sal_Int32 _resourceId );
97 OUString ResourceBundle_Impl::impl_getStringResourceKey( sal_Int32 _resourceId )
99 OUStringBuffer key;
100 key.appendAscii( "string:" );
101 key.append( _resourceId );
102 return key.makeStringAndClear();
106 OUString ResourceBundle_Impl::loadString( sal_Int32 _resourceId ) const
108 ::osl::MutexGuard aGuard( m_aMutex );
110 OUString sString;
112 if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() )
116 OSL_VERIFY( m_xBundle->getByName( impl_getStringResourceKey( _resourceId ) ) >>= sString );
118 catch( const Exception& )
120 OSL_FAIL( "ResourceBundle_Impl::loadString: caught an exception!" );
123 return sString;
127 bool ResourceBundle_Impl::hasString( sal_Int32 _resourceId ) const
129 ::osl::MutexGuard aGuard( m_aMutex );
131 bool has = false;
133 if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() )
137 has = m_xBundle->hasByName( impl_getStringResourceKey( _resourceId ) );
139 catch( const Exception& )
141 OSL_FAIL( "ResourceBundle_Impl::hasString: caught an exception!" );
144 return has;
148 bool ResourceBundle_Impl::impl_loadBundle_nothrow()
150 if ( m_bAttemptedCreate )
151 return m_xBundle.is();
153 m_bAttemptedCreate = true;
155 Reference< XResourceBundleLoader > xLoader;
158 Any aValue( m_xContext->getValueByName(
159 OUString( "/singletons/com.sun.star.resource.OfficeResourceLoader" ) ) );
160 OSL_VERIFY( aValue >>= xLoader );
162 catch( const Exception& )
164 OSL_FAIL( "ResourceBundle_Impl::impl_loadBundle_nopthrow: could not create the resource loader!" );
167 if ( !xLoader.is() )
168 return false;
172 m_xBundle = xLoader->loadBundle_Default( m_sBaseName );
174 catch( const MissingResourceException& )
176 OSL_FAIL( "ResourceBundle_Impl::impl_loadBundle_nopthrow: missing the given resource bundle!" );
179 return m_xBundle.is();
183 OfficeResourceBundle::OfficeResourceBundle( const Reference< XComponentContext >& _context, const sal_Char* _bundleBaseAsciiName )
184 :m_pImpl( new ResourceBundle_Impl( _context, OUString::createFromAscii( _bundleBaseAsciiName ) ) )
189 OfficeResourceBundle::~OfficeResourceBundle()
194 OUString OfficeResourceBundle::loadString( sal_Int32 _resourceId ) const
196 return m_pImpl->loadString( _resourceId );
200 bool OfficeResourceBundle::hasString( sal_Int32 _resourceId ) const
202 return m_pImpl->hasString( _resourceId );
206 } // namespace comphelper
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */