update dev300-m58
[ooovba.git] / comphelper / source / misc / officeresourcebundle.cxx
blob6d255eb9dd9d8638f559dd86bf349679f9d11042
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: officeresourcebundle.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/officeresourcebundle.hxx>
35 /** === begin UNO includes === **/
36 #include <com/sun/star/resource/XResourceBundle.hpp>
37 #include <com/sun/star/resource/XResourceBundleLoader.hpp>
38 #include <com/sun/star/lang/NullPointerException.hpp>
39 /** === end UNO includes === **/
40 #include <osl/mutex.hxx>
41 #include <osl/diagnose.h>
42 #include <rtl/ustrbuf.hxx>
44 //........................................................................
45 namespace comphelper
47 //........................................................................
49 /** === begin UNO using === **/
50 using ::com::sun::star::uno::Reference;
51 using com::sun::star::resource::XResourceBundle;
52 using com::sun::star::resource::XResourceBundleLoader;
53 using com::sun::star::resource::MissingResourceException;
54 using ::com::sun::star::uno::XComponentContext;
55 using ::com::sun::star::lang::NullPointerException;
56 using ::com::sun::star::uno::UNO_QUERY;
57 using ::com::sun::star::uno::Exception;
58 using ::com::sun::star::uno::Any;
59 /** === end UNO using === **/
61 //====================================================================
62 //= ResourceBundle_Impl
63 //====================================================================
64 class ResourceBundle_Impl
66 private:
67 Reference< XComponentContext > m_xContext;
68 ::rtl::OUString m_sBaseName;
69 Reference< XResourceBundle > m_xBundle;
70 bool m_bAttemptedCreate;
71 mutable ::osl::Mutex m_aMutex;
73 public:
74 ResourceBundle_Impl( const Reference< XComponentContext >& _context, const ::rtl::OUString& _baseName )
75 :m_xContext( _context )
76 ,m_sBaseName( _baseName )
77 ,m_bAttemptedCreate( false )
81 public:
82 /** loads the string with the given resource id from the resource bundle
83 @param _resourceId
84 the id of the string to load
85 @return
86 the requested resource string. If no string with the given id exists in the resource bundle,
87 an empty string is returned. In a non-product version, an OSL_ENSURE will notify you of this
88 then.
90 ::rtl::OUString loadString( sal_Int32 _resourceId ) const;
92 /** determines whether the resource bundle has a string with the given id
93 @param _resourceId
94 the id of the string whose existence is to be checked
95 @return
96 <TRUE/> if and only if a string with the given ID exists in the resource
97 bundle.
99 bool hasString( sal_Int32 _resourceId ) const;
101 private:
102 /** loads the bundle represented by the instance
104 The method is safe against multiple calls: If a previos call succeeded or failed, the
105 previous result will be returned, without any other processing.
107 @precond
108 Our mutex is locked.
110 bool impl_loadBundle_nothrow();
112 /** returns the resource bundle key for a string with a given resource id
114 static ::rtl::OUString
115 impl_getStringResourceKey( sal_Int32 _resourceId );
118 //--------------------------------------------------------------------
119 ::rtl::OUString ResourceBundle_Impl::impl_getStringResourceKey( sal_Int32 _resourceId )
121 ::rtl::OUStringBuffer key;
122 key.appendAscii( "string:" );
123 key.append( _resourceId );
124 return key.makeStringAndClear();
127 //--------------------------------------------------------------------
128 ::rtl::OUString ResourceBundle_Impl::loadString( sal_Int32 _resourceId ) const
130 ::osl::MutexGuard aGuard( m_aMutex );
132 ::rtl::OUString sString;
134 if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() )
138 OSL_VERIFY( m_xBundle->getByName( impl_getStringResourceKey( _resourceId ) ) >>= sString );
140 catch( const Exception& )
142 OSL_ENSURE( false, "ResourceBundle_Impl::loadString: caught an exception!" );
145 return sString;
148 //--------------------------------------------------------------------
149 bool ResourceBundle_Impl::hasString( sal_Int32 _resourceId ) const
151 ::osl::MutexGuard aGuard( m_aMutex );
153 bool has = false;
155 if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() )
159 has = m_xBundle->hasByName( impl_getStringResourceKey( _resourceId ) );
161 catch( const Exception& )
163 OSL_ENSURE( false, "ResourceBundle_Impl::hasString: caught an exception!" );
166 return has;
169 //--------------------------------------------------------------------
170 bool ResourceBundle_Impl::impl_loadBundle_nothrow()
172 if ( m_bAttemptedCreate )
173 return m_xBundle.is();
175 m_bAttemptedCreate = true;
177 Reference< XResourceBundleLoader > xLoader;
180 Any aValue( m_xContext->getValueByName(
181 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
182 "/singletons/com.sun.star.resource.OfficeResourceLoader" ) ) ) );
183 OSL_VERIFY( aValue >>= xLoader );
185 catch( const Exception& )
187 OSL_ENSURE( false, "ResourceBundle_Impl::impl_loadBundle_nopthrow: could not create the resource loader!" );
190 if ( !xLoader.is() )
191 return false;
195 m_xBundle = xLoader->loadBundle_Default( m_sBaseName );
197 catch( const MissingResourceException& )
199 OSL_ENSURE( false, "ResourceBundle_Impl::impl_loadBundle_nopthrow: missing the given resource bundle!" );
202 return m_xBundle.is();
205 //====================================================================
206 //= OfficeResourceBundle
207 //====================================================================
208 //--------------------------------------------------------------------
209 OfficeResourceBundle::OfficeResourceBundle( const Reference< XComponentContext >& _context, const ::rtl::OUString& _bundleBaseName )
210 :m_pImpl( new ResourceBundle_Impl( _context, _bundleBaseName ) )
212 if ( !_context.is() )
213 throw NullPointerException();
216 //--------------------------------------------------------------------
217 OfficeResourceBundle::OfficeResourceBundle( const Reference< XComponentContext >& _context, const sal_Char* _bundleBaseAsciiName )
218 :m_pImpl( new ResourceBundle_Impl( _context, ::rtl::OUString::createFromAscii( _bundleBaseAsciiName ) ) )
220 if ( !_context.is() )
221 throw NullPointerException();
224 //--------------------------------------------------------------------
225 OfficeResourceBundle::~OfficeResourceBundle()
229 //--------------------------------------------------------------------
230 ::rtl::OUString OfficeResourceBundle::loadString( sal_Int32 _resourceId ) const
232 return m_pImpl->loadString( _resourceId );
235 //--------------------------------------------------------------------
236 bool OfficeResourceBundle::hasString( sal_Int32 _resourceId ) const
238 return m_pImpl->hasString( _resourceId );
241 //........................................................................
242 } // namespace comphelper
243 //........................................................................