xref: /AOO41X/main/comphelper/source/misc/officeresourcebundle.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
30*cdf0e10cSrcweir #include <comphelper/officeresourcebundle.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir /** === begin UNO includes === **/
33*cdf0e10cSrcweir #include <com/sun/star/resource/XResourceBundle.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/resource/XResourceBundleLoader.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/lang/NullPointerException.hpp>
36*cdf0e10cSrcweir /** === end UNO includes === **/
37*cdf0e10cSrcweir #include <osl/mutex.hxx>
38*cdf0e10cSrcweir #include <osl/diagnose.h>
39*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir //........................................................................
42*cdf0e10cSrcweir namespace comphelper
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir //........................................................................
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     /** === begin UNO using === **/
47*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
48*cdf0e10cSrcweir     using com::sun::star::resource::XResourceBundle;
49*cdf0e10cSrcweir     using com::sun::star::resource::XResourceBundleLoader;
50*cdf0e10cSrcweir     using com::sun::star::resource::MissingResourceException;
51*cdf0e10cSrcweir     using ::com::sun::star::uno::XComponentContext;
52*cdf0e10cSrcweir     using ::com::sun::star::lang::NullPointerException;
53*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
54*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
55*cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
56*cdf0e10cSrcweir     /** === end UNO using === **/
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir     //====================================================================
59*cdf0e10cSrcweir 	//= ResourceBundle_Impl
60*cdf0e10cSrcweir 	//====================================================================
61*cdf0e10cSrcweir     class ResourceBundle_Impl
62*cdf0e10cSrcweir     {
63*cdf0e10cSrcweir     private:
64*cdf0e10cSrcweir         Reference< XComponentContext >  m_xContext;
65*cdf0e10cSrcweir         ::rtl::OUString                 m_sBaseName;
66*cdf0e10cSrcweir         Reference< XResourceBundle >    m_xBundle;
67*cdf0e10cSrcweir         bool                            m_bAttemptedCreate;
68*cdf0e10cSrcweir         mutable ::osl::Mutex            m_aMutex;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     public:
71*cdf0e10cSrcweir         ResourceBundle_Impl( const Reference< XComponentContext >& _context, const ::rtl::OUString& _baseName )
72*cdf0e10cSrcweir             :m_xContext( _context )
73*cdf0e10cSrcweir             ,m_sBaseName( _baseName )
74*cdf0e10cSrcweir             ,m_bAttemptedCreate( false )
75*cdf0e10cSrcweir         {
76*cdf0e10cSrcweir         }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     public:
79*cdf0e10cSrcweir         /** loads the string with the given resource id from the resource bundle
80*cdf0e10cSrcweir             @param  _resourceId
81*cdf0e10cSrcweir                 the id of the string to load
82*cdf0e10cSrcweir             @return
83*cdf0e10cSrcweir                 the requested resource string. If no string with the given id exists in the resource bundle,
84*cdf0e10cSrcweir                 an empty string is returned. In a non-product version, an OSL_ENSURE will notify you of this
85*cdf0e10cSrcweir                 then.
86*cdf0e10cSrcweir         */
87*cdf0e10cSrcweir         ::rtl::OUString loadString( sal_Int32 _resourceId ) const;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir         /** determines whether the resource bundle has a string with the given id
90*cdf0e10cSrcweir             @param  _resourceId
91*cdf0e10cSrcweir                 the id of the string whose existence is to be checked
92*cdf0e10cSrcweir             @return
93*cdf0e10cSrcweir                 <TRUE/> if and only if a string with the given ID exists in the resource
94*cdf0e10cSrcweir                 bundle.
95*cdf0e10cSrcweir         */
96*cdf0e10cSrcweir         bool            hasString( sal_Int32 _resourceId ) const;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     private:
99*cdf0e10cSrcweir         /** loads the bundle represented by the instance
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir             The method is safe against multiple calls: If a previos call succeeded or failed, the
102*cdf0e10cSrcweir             previous result will be returned, without any other processing.
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir             @precond
105*cdf0e10cSrcweir                 Our mutex is locked.
106*cdf0e10cSrcweir         */
107*cdf0e10cSrcweir         bool    impl_loadBundle_nothrow();
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         /** returns the resource bundle key for a string with a given resource id
110*cdf0e10cSrcweir         */
111*cdf0e10cSrcweir         static ::rtl::OUString
112*cdf0e10cSrcweir                 impl_getStringResourceKey( sal_Int32 _resourceId );
113*cdf0e10cSrcweir     };
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 	//--------------------------------------------------------------------
116*cdf0e10cSrcweir     ::rtl::OUString ResourceBundle_Impl::impl_getStringResourceKey( sal_Int32 _resourceId )
117*cdf0e10cSrcweir     {
118*cdf0e10cSrcweir         ::rtl::OUStringBuffer key;
119*cdf0e10cSrcweir         key.appendAscii( "string:" );
120*cdf0e10cSrcweir         key.append( _resourceId );
121*cdf0e10cSrcweir         return key.makeStringAndClear();
122*cdf0e10cSrcweir     }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	//--------------------------------------------------------------------
125*cdf0e10cSrcweir     ::rtl::OUString ResourceBundle_Impl::loadString( sal_Int32 _resourceId ) const
126*cdf0e10cSrcweir     {
127*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         ::rtl::OUString sString;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() )
132*cdf0e10cSrcweir         {
133*cdf0e10cSrcweir             try
134*cdf0e10cSrcweir             {
135*cdf0e10cSrcweir                 OSL_VERIFY( m_xBundle->getByName( impl_getStringResourceKey( _resourceId ) ) >>= sString );
136*cdf0e10cSrcweir             }
137*cdf0e10cSrcweir             catch( const Exception& )
138*cdf0e10cSrcweir             {
139*cdf0e10cSrcweir                 OSL_ENSURE( false, "ResourceBundle_Impl::loadString: caught an exception!" );
140*cdf0e10cSrcweir             }
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir         return sString;
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	//--------------------------------------------------------------------
146*cdf0e10cSrcweir     bool ResourceBundle_Impl::hasString( sal_Int32 _resourceId ) const
147*cdf0e10cSrcweir     {
148*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         bool has = false;
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() )
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             try
155*cdf0e10cSrcweir             {
156*cdf0e10cSrcweir                 has = m_xBundle->hasByName( impl_getStringResourceKey( _resourceId ) );
157*cdf0e10cSrcweir             }
158*cdf0e10cSrcweir             catch( const Exception& )
159*cdf0e10cSrcweir             {
160*cdf0e10cSrcweir                 OSL_ENSURE( false, "ResourceBundle_Impl::hasString: caught an exception!" );
161*cdf0e10cSrcweir             }
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir         return has;
164*cdf0e10cSrcweir     }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	//--------------------------------------------------------------------
167*cdf0e10cSrcweir     bool ResourceBundle_Impl::impl_loadBundle_nothrow()
168*cdf0e10cSrcweir     {
169*cdf0e10cSrcweir         if ( m_bAttemptedCreate )
170*cdf0e10cSrcweir             return m_xBundle.is();
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir         m_bAttemptedCreate = true;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         Reference< XResourceBundleLoader > xLoader;
175*cdf0e10cSrcweir         try
176*cdf0e10cSrcweir         {
177*cdf0e10cSrcweir             Any aValue( m_xContext->getValueByName(
178*cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
179*cdf0e10cSrcweir                     "/singletons/com.sun.star.resource.OfficeResourceLoader" ) ) ) );
180*cdf0e10cSrcweir             OSL_VERIFY( aValue >>= xLoader );
181*cdf0e10cSrcweir         }
182*cdf0e10cSrcweir         catch( const Exception& )
183*cdf0e10cSrcweir         {
184*cdf0e10cSrcweir             OSL_ENSURE( false, "ResourceBundle_Impl::impl_loadBundle_nopthrow: could not create the resource loader!" );
185*cdf0e10cSrcweir         }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         if ( !xLoader.is() )
188*cdf0e10cSrcweir             return false;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         try
191*cdf0e10cSrcweir         {
192*cdf0e10cSrcweir             m_xBundle = xLoader->loadBundle_Default( m_sBaseName );
193*cdf0e10cSrcweir         }
194*cdf0e10cSrcweir         catch( const MissingResourceException& )
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             OSL_ENSURE( false, "ResourceBundle_Impl::impl_loadBundle_nopthrow: missing the given resource bundle!" );
197*cdf0e10cSrcweir         }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         return m_xBundle.is();
200*cdf0e10cSrcweir     }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 	//====================================================================
203*cdf0e10cSrcweir 	//= OfficeResourceBundle
204*cdf0e10cSrcweir 	//====================================================================
205*cdf0e10cSrcweir 	//--------------------------------------------------------------------
206*cdf0e10cSrcweir     OfficeResourceBundle::OfficeResourceBundle( const Reference< XComponentContext >& _context, const ::rtl::OUString& _bundleBaseName )
207*cdf0e10cSrcweir         :m_pImpl( new ResourceBundle_Impl( _context, _bundleBaseName ) )
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         if ( !_context.is() )
210*cdf0e10cSrcweir             throw NullPointerException();
211*cdf0e10cSrcweir     }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 	//--------------------------------------------------------------------
214*cdf0e10cSrcweir     OfficeResourceBundle::OfficeResourceBundle( const Reference< XComponentContext >& _context, const sal_Char* _bundleBaseAsciiName )
215*cdf0e10cSrcweir         :m_pImpl( new ResourceBundle_Impl( _context, ::rtl::OUString::createFromAscii( _bundleBaseAsciiName ) ) )
216*cdf0e10cSrcweir     {
217*cdf0e10cSrcweir         if ( !_context.is() )
218*cdf0e10cSrcweir             throw NullPointerException();
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 	//--------------------------------------------------------------------
222*cdf0e10cSrcweir     OfficeResourceBundle::~OfficeResourceBundle()
223*cdf0e10cSrcweir     {
224*cdf0e10cSrcweir     }
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 	//--------------------------------------------------------------------
227*cdf0e10cSrcweir     ::rtl::OUString OfficeResourceBundle::loadString( sal_Int32 _resourceId ) const
228*cdf0e10cSrcweir     {
229*cdf0e10cSrcweir         return m_pImpl->loadString( _resourceId );
230*cdf0e10cSrcweir     }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 	//--------------------------------------------------------------------
233*cdf0e10cSrcweir     bool OfficeResourceBundle::hasString( sal_Int32 _resourceId ) const
234*cdf0e10cSrcweir     {
235*cdf0e10cSrcweir         return m_pImpl->hasString( _resourceId );
236*cdf0e10cSrcweir     }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir //........................................................................
239*cdf0e10cSrcweir } // namespace comphelper
240*cdf0e10cSrcweir //........................................................................
241