xref: /AOO41X/main/extensions/source/resource/oooresourceloader.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_extensions.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef EXTENSIONS_SOURCE_RESOURCE_OOORESOURCELOADER_CXX
32*cdf0e10cSrcweir #define EXTENSIONS_SOURCE_RESOURCE_OOORESOURCELOADER_CXX
33*cdf0e10cSrcweir #include "res_services.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir /** === begin UNO includes === **/
36*cdf0e10cSrcweir #include <com/sun/star/resource/XResourceBundleLoader.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
39*cdf0e10cSrcweir /** === end UNO includes === **/
40*cdf0e10cSrcweir #include <vcl/svapp.hxx>
41*cdf0e10cSrcweir #include <tools/simplerm.hxx>
42*cdf0e10cSrcweir #include <tools/rcid.h>
43*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
44*cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
47*cdf0e10cSrcweir #include <map>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir //........................................................................
50*cdf0e10cSrcweir namespace res
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir //........................................................................
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir     /** === begin UNO using === **/
55*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
56*cdf0e10cSrcweir     using ::com::sun::star::resource::XResourceBundleLoader;
57*cdf0e10cSrcweir     using ::com::sun::star::resource::XResourceBundle;
58*cdf0e10cSrcweir     using ::com::sun::star::resource::MissingResourceException;
59*cdf0e10cSrcweir     using ::com::sun::star::uno::XComponentContext;
60*cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
61*cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
62*cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
63*cdf0e10cSrcweir     using ::com::sun::star::lang::Locale;
64*cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
65*cdf0e10cSrcweir     using ::com::sun::star::container::NoSuchElementException;
66*cdf0e10cSrcweir     using ::com::sun::star::lang::WrappedTargetException;
67*cdf0e10cSrcweir     using ::com::sun::star::uno::Type;
68*cdf0e10cSrcweir     using ::com::sun::star::uno::WeakReference;
69*cdf0e10cSrcweir     /** === end UNO using === **/
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	//====================================================================
72*cdf0e10cSrcweir 	//= helper
73*cdf0e10cSrcweir 	//====================================================================
74*cdf0e10cSrcweir     typedef ::std::pair< ::rtl::OUString, Locale >  ResourceBundleDescriptor;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     struct ResourceBundleDescriptorLess : public ::std::binary_function< ResourceBundleDescriptor, ResourceBundleDescriptor, bool >
77*cdf0e10cSrcweir     {
78*cdf0e10cSrcweir         bool operator()( const ResourceBundleDescriptor& _lhs, const ResourceBundleDescriptor& _rhs ) const
79*cdf0e10cSrcweir         {
80*cdf0e10cSrcweir             if ( _lhs.first < _rhs.first )
81*cdf0e10cSrcweir                 return true;
82*cdf0e10cSrcweir             if ( _lhs.second.Language < _rhs.second.Language )
83*cdf0e10cSrcweir                 return true;
84*cdf0e10cSrcweir             if ( _lhs.second.Country < _rhs.second.Country )
85*cdf0e10cSrcweir                 return true;
86*cdf0e10cSrcweir             if ( _lhs.second.Variant < _rhs.second.Variant )
87*cdf0e10cSrcweir                 return true;
88*cdf0e10cSrcweir             return false;
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir     };
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	//====================================================================
93*cdf0e10cSrcweir 	//= OpenOfficeResourceLoader
94*cdf0e10cSrcweir 	//====================================================================
95*cdf0e10cSrcweir     typedef ::cppu::WeakImplHelper1 <   XResourceBundleLoader
96*cdf0e10cSrcweir                                     >   OpenOfficeResourceLoader_Base;
97*cdf0e10cSrcweir     class OpenOfficeResourceLoader : public OpenOfficeResourceLoader_Base
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir     private:
100*cdf0e10cSrcweir         typedef ::std::map< ResourceBundleDescriptor, WeakReference< XResourceBundle >, ResourceBundleDescriptorLess >
101*cdf0e10cSrcweir                                         ResourceBundleCache;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     private:
104*cdf0e10cSrcweir         Reference< XComponentContext >  m_xContext;
105*cdf0e10cSrcweir         ::osl::Mutex                    m_aMutex;
106*cdf0e10cSrcweir         ResourceBundleCache             m_aBundleCache;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     protected:
109*cdf0e10cSrcweir         OpenOfficeResourceLoader( const Reference< XComponentContext >& _rxContext );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     public:
112*cdf0e10cSrcweir         static Sequence< ::rtl::OUString > getSupportedServiceNames_static();
113*cdf0e10cSrcweir         static ::rtl::OUString  getImplementationName_static();
114*cdf0e10cSrcweir         static ::rtl::OUString  getSingletonName_static();
115*cdf0e10cSrcweir         static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         // XResourceBundleLoader
118*cdf0e10cSrcweir         virtual Reference< XResourceBundle > SAL_CALL loadBundle_Default( const ::rtl::OUString& aBaseName ) throw (MissingResourceException, RuntimeException);
119*cdf0e10cSrcweir         virtual Reference< XResourceBundle > SAL_CALL loadBundle( const ::rtl::OUString& abaseName, const Locale& aLocale ) throw (MissingResourceException, RuntimeException);
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     private:
122*cdf0e10cSrcweir         OpenOfficeResourceLoader();                                             // never implemented
123*cdf0e10cSrcweir         OpenOfficeResourceLoader( const OpenOfficeResourceLoader& );            // never implemented
124*cdf0e10cSrcweir         OpenOfficeResourceLoader& operator=( const OpenOfficeResourceLoader& ); // never implemented
125*cdf0e10cSrcweir 	};
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 	//====================================================================
128*cdf0e10cSrcweir 	//= IResourceType
129*cdf0e10cSrcweir 	//====================================================================
130*cdf0e10cSrcweir     /** encapsulates access to a fixed resource type
131*cdf0e10cSrcweir     */
132*cdf0e10cSrcweir     class IResourceType
133*cdf0e10cSrcweir     {
134*cdf0e10cSrcweir     public:
135*cdf0e10cSrcweir         /** returns the RESOURCE_TYPE associated with this instance
136*cdf0e10cSrcweir         */
137*cdf0e10cSrcweir         virtual RESOURCE_TYPE getResourceType() const = 0;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         /** reads a single resource from the given resource manager
140*cdf0e10cSrcweir             @param  _resourceManager
141*cdf0e10cSrcweir                 the resource manager to read from
142*cdf0e10cSrcweir             @param  _resourceId
143*cdf0e10cSrcweir                 the id of the resource to read
144*cdf0e10cSrcweir             @return
145*cdf0e10cSrcweir                 the required resource
146*cdf0e10cSrcweir             @precond
147*cdf0e10cSrcweir                 the caler checked via <code>_resourceManager.IsAvailable( getResourceType(), _resourceId )</code>
148*cdf0e10cSrcweir                 that the required resource really exists
149*cdf0e10cSrcweir         */
150*cdf0e10cSrcweir         virtual Any getResource( SimpleResMgr& _resourceManager, sal_Int32 _resourceId ) const = 0;
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         virtual ~IResourceType() { };
153*cdf0e10cSrcweir     };
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 	//====================================================================
156*cdf0e10cSrcweir 	//= StringResourceAccess
157*cdf0e10cSrcweir 	//====================================================================
158*cdf0e10cSrcweir     class StringResourceAccess : public IResourceType
159*cdf0e10cSrcweir     {
160*cdf0e10cSrcweir     public:
161*cdf0e10cSrcweir         StringResourceAccess();
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         // IResourceType
164*cdf0e10cSrcweir         virtual RESOURCE_TYPE getResourceType() const;
165*cdf0e10cSrcweir         virtual Any getResource( SimpleResMgr& _resourceManager, sal_Int32 _resourceId ) const;
166*cdf0e10cSrcweir     };
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     //--------------------------------------------------------------------
169*cdf0e10cSrcweir     StringResourceAccess::StringResourceAccess()
170*cdf0e10cSrcweir     {
171*cdf0e10cSrcweir     }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     //--------------------------------------------------------------------
174*cdf0e10cSrcweir     RESOURCE_TYPE StringResourceAccess::getResourceType() const
175*cdf0e10cSrcweir     {
176*cdf0e10cSrcweir         return RSC_STRING;
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     //--------------------------------------------------------------------
180*cdf0e10cSrcweir     Any StringResourceAccess::getResource( SimpleResMgr& _resourceManager, sal_Int32 _resourceId ) const
181*cdf0e10cSrcweir     {
182*cdf0e10cSrcweir         OSL_PRECOND( _resourceManager.IsAvailable( getResourceType(), _resourceId ), "StringResourceAccess::getResource: precondition not met!" );
183*cdf0e10cSrcweir         Any aResource;
184*cdf0e10cSrcweir         aResource <<= ::rtl::OUString( _resourceManager.ReadString( _resourceId ) );
185*cdf0e10cSrcweir         return aResource;
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     //====================================================================
189*cdf0e10cSrcweir 	//= OpenOfficeResourceBundle
190*cdf0e10cSrcweir 	//====================================================================
191*cdf0e10cSrcweir     typedef ::cppu::WeakImplHelper1 <   XResourceBundle
192*cdf0e10cSrcweir                                     >   OpenOfficeResourceBundle_Base;
193*cdf0e10cSrcweir     class OpenOfficeResourceBundle : public OpenOfficeResourceBundle_Base
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir     private:
196*cdf0e10cSrcweir         typedef ::boost::shared_ptr< IResourceType >            ResourceTypePtr;
197*cdf0e10cSrcweir         typedef ::std::map< ::rtl::OUString, ResourceTypePtr >  ResourceTypes;
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         ::osl::Mutex                    m_aMutex;
200*cdf0e10cSrcweir         Reference< XResourceBundle >    m_xParent;
201*cdf0e10cSrcweir         Locale                          m_aLocale;
202*cdf0e10cSrcweir         SimpleResMgr*                   m_pResourceManager;
203*cdf0e10cSrcweir         ResourceTypes                   m_aResourceTypes;
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir     public:
206*cdf0e10cSrcweir         OpenOfficeResourceBundle(
207*cdf0e10cSrcweir             const Reference< XComponentContext >& _rxContext,
208*cdf0e10cSrcweir             const ::rtl::OUString& _rBaseName,
209*cdf0e10cSrcweir             const Locale& _rLocale
210*cdf0e10cSrcweir         );
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     protected:
213*cdf0e10cSrcweir         ~OpenOfficeResourceBundle();
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     public:
216*cdf0e10cSrcweir         // XResourceBundle
217*cdf0e10cSrcweir         virtual ::com::sun::star::uno::Reference< ::com::sun::star::resource::XResourceBundle > SAL_CALL getParent() throw (::com::sun::star::uno::RuntimeException);
218*cdf0e10cSrcweir         virtual void SAL_CALL setParent( const ::com::sun::star::uno::Reference< ::com::sun::star::resource::XResourceBundle >& _parent ) throw (::com::sun::star::uno::RuntimeException);
219*cdf0e10cSrcweir         virtual ::com::sun::star::lang::Locale SAL_CALL getLocale(  ) throw (::com::sun::star::uno::RuntimeException);
220*cdf0e10cSrcweir         virtual ::com::sun::star::uno::Any SAL_CALL getDirectElement( const ::rtl::OUString& key ) throw (::com::sun::star::uno::RuntimeException);
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir         // XNameAccess (base of XResourceBundle)
223*cdf0e10cSrcweir         virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
224*cdf0e10cSrcweir         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw (::com::sun::star::uno::RuntimeException);
225*cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (::com::sun::star::uno::RuntimeException);
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir         // XElementAccess (base of XNameAccess)
228*cdf0e10cSrcweir         virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  ) throw (::com::sun::star::uno::RuntimeException);
229*cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL hasElements(  ) throw (::com::sun::star::uno::RuntimeException);
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir     private:
232*cdf0e10cSrcweir         /** retrievs the element with the given key, without asking our parent bundle
233*cdf0e10cSrcweir             @param  _key
234*cdf0e10cSrcweir                 the key of the element to retrieve
235*cdf0e10cSrcweir             @param  _out_Element
236*cdf0e10cSrcweir                 will contained the retrieved element upon successful return. If the method is unsuccessful, the
237*cdf0e10cSrcweir                 value will not be touched.
238*cdf0e10cSrcweir             @return
239*cdf0e10cSrcweir                 <TRUE/> if and only if the element could be retrieved
240*cdf0e10cSrcweir             @precond
241*cdf0e10cSrcweir                 our mutex is locked
242*cdf0e10cSrcweir         */
243*cdf0e10cSrcweir         bool    impl_getDirectElement_nothrow( const ::rtl::OUString& _key, Any& _out_Element ) const;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir         /** retrieves the resource type and id from a given resource key, which assembles those two
246*cdf0e10cSrcweir             @param  _key
247*cdf0e10cSrcweir                 the resource key as got via a public API call
248*cdf0e10cSrcweir             @param  _out_resourceType
249*cdf0e10cSrcweir                 the resource type, if successful
250*cdf0e10cSrcweir             @param  _out_resourceId
251*cdf0e10cSrcweir                 the resource id, if successful
252*cdf0e10cSrcweir             @return
253*cdf0e10cSrcweir                 <TRUE/> if and only if the given key specifies a known resource type, and contains a valid
254*cdf0e10cSrcweir                 resource id
255*cdf0e10cSrcweir         */
256*cdf0e10cSrcweir         bool    impl_getResourceTypeAndId_nothrow( const ::rtl::OUString& _key, ResourceTypePtr& _out_resourceType, sal_Int32& _out_resourceId ) const;
257*cdf0e10cSrcweir     };
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 	//====================================================================
260*cdf0e10cSrcweir 	//= OpenOfficeResourceLoader
261*cdf0e10cSrcweir 	//====================================================================
262*cdf0e10cSrcweir 	//--------------------------------------------------------------------
263*cdf0e10cSrcweir     OpenOfficeResourceLoader::OpenOfficeResourceLoader( const Reference< XComponentContext >& _rxContext )
264*cdf0e10cSrcweir         :m_xContext( _rxContext )
265*cdf0e10cSrcweir     {
266*cdf0e10cSrcweir     }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 	//--------------------------------------------------------------------
269*cdf0e10cSrcweir     Sequence< ::rtl::OUString > OpenOfficeResourceLoader::getSupportedServiceNames_static()
270*cdf0e10cSrcweir     {
271*cdf0e10cSrcweir         Sequence< ::rtl::OUString > aServices( 1 );
272*cdf0e10cSrcweir         aServices[ 0 ] = getSingletonName_static();
273*cdf0e10cSrcweir         return aServices;
274*cdf0e10cSrcweir     }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir 	//--------------------------------------------------------------------
277*cdf0e10cSrcweir     ::rtl::OUString OpenOfficeResourceLoader::getImplementationName_static()
278*cdf0e10cSrcweir     {
279*cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.resource.OpenOfficeResourceLoader" ) );
280*cdf0e10cSrcweir     }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 	//--------------------------------------------------------------------
283*cdf0e10cSrcweir     ::rtl::OUString OpenOfficeResourceLoader::getSingletonName_static()
284*cdf0e10cSrcweir     {
285*cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.resource.OfficeResourceLoader" ) );
286*cdf0e10cSrcweir     }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir 	//--------------------------------------------------------------------
289*cdf0e10cSrcweir     Reference< XInterface > OpenOfficeResourceLoader::Create( const Reference< XComponentContext >& _rxContext )
290*cdf0e10cSrcweir     {
291*cdf0e10cSrcweir         return *( new OpenOfficeResourceLoader( _rxContext ) );
292*cdf0e10cSrcweir     }
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir     //--------------------------------------------------------------------
295*cdf0e10cSrcweir     Reference< XResourceBundle > SAL_CALL OpenOfficeResourceLoader::loadBundle_Default( const ::rtl::OUString& _baseName ) throw (MissingResourceException, RuntimeException)
296*cdf0e10cSrcweir     {
297*cdf0e10cSrcweir         return loadBundle( _baseName, Application::GetSettings().GetUILocale() );
298*cdf0e10cSrcweir     }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir     //--------------------------------------------------------------------
301*cdf0e10cSrcweir     Reference< XResourceBundle > SAL_CALL OpenOfficeResourceLoader::loadBundle( const ::rtl::OUString& _baseName, const Locale& _locale ) throw (MissingResourceException, RuntimeException)
302*cdf0e10cSrcweir     {
303*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir         Reference< XResourceBundle > xBundle;
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir         ResourceBundleDescriptor resourceDescriptor( _baseName, _locale );
308*cdf0e10cSrcweir         ResourceBundleCache::iterator cachePos = m_aBundleCache.find( resourceDescriptor );
309*cdf0e10cSrcweir         if ( cachePos != m_aBundleCache.end() )
310*cdf0e10cSrcweir             xBundle = cachePos->second;
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir         if ( !xBundle.is() )
313*cdf0e10cSrcweir         {   // not in the cache, or already died
314*cdf0e10cSrcweir             xBundle = new OpenOfficeResourceBundle( m_xContext, _baseName, _locale );
315*cdf0e10cSrcweir             m_aBundleCache.insert( ResourceBundleCache::value_type( resourceDescriptor, xBundle ) );
316*cdf0e10cSrcweir         }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir         return xBundle;
319*cdf0e10cSrcweir     }
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir     //--------------------------------------------------------------------
322*cdf0e10cSrcweir     ComponentInfo getComponentInfo_OpenOfficeResourceLoader()
323*cdf0e10cSrcweir     {
324*cdf0e10cSrcweir         ComponentInfo aInfo;
325*cdf0e10cSrcweir         aInfo.aSupportedServices = OpenOfficeResourceLoader::getSupportedServiceNames_static();
326*cdf0e10cSrcweir         aInfo.sImplementationName = OpenOfficeResourceLoader::getImplementationName_static();
327*cdf0e10cSrcweir         aInfo.sSingletonName = OpenOfficeResourceLoader::getSingletonName_static();
328*cdf0e10cSrcweir         aInfo.pFactory = &OpenOfficeResourceLoader::Create;
329*cdf0e10cSrcweir         return aInfo;
330*cdf0e10cSrcweir     }
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 	//====================================================================
333*cdf0e10cSrcweir 	//= OpenOfficeResourceBundle
334*cdf0e10cSrcweir 	//====================================================================
335*cdf0e10cSrcweir     //--------------------------------------------------------------------
336*cdf0e10cSrcweir     OpenOfficeResourceBundle::OpenOfficeResourceBundle( const Reference< XComponentContext >& /*_rxContext*/, const ::rtl::OUString& _rBaseName, const Locale& _rLocale )
337*cdf0e10cSrcweir         :m_aLocale( _rLocale )
338*cdf0e10cSrcweir         ,m_pResourceManager( NULL )
339*cdf0e10cSrcweir     {
340*cdf0e10cSrcweir         ::rtl::OUString sBaseName( _rBaseName );
341*cdf0e10cSrcweir         m_pResourceManager = new SimpleResMgr( sBaseName, m_aLocale );
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir         if ( !m_pResourceManager->IsValid() )
344*cdf0e10cSrcweir         {
345*cdf0e10cSrcweir             delete m_pResourceManager, m_pResourceManager = NULL;
346*cdf0e10cSrcweir             throw MissingResourceException();
347*cdf0e10cSrcweir         }
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir         // supported resource types so far: strings
350*cdf0e10cSrcweir         m_aResourceTypes[ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "string" ) ) ] =
351*cdf0e10cSrcweir             ResourceTypePtr( new StringResourceAccess );
352*cdf0e10cSrcweir     }
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir     //--------------------------------------------------------------------
355*cdf0e10cSrcweir     OpenOfficeResourceBundle::~OpenOfficeResourceBundle()
356*cdf0e10cSrcweir     {
357*cdf0e10cSrcweir         delete m_pResourceManager;
358*cdf0e10cSrcweir     }
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir     //--------------------------------------------------------------------
361*cdf0e10cSrcweir     Reference< XResourceBundle > SAL_CALL OpenOfficeResourceBundle::getParent() throw (RuntimeException)
362*cdf0e10cSrcweir     {
363*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
364*cdf0e10cSrcweir         return m_xParent;
365*cdf0e10cSrcweir     }
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir     //--------------------------------------------------------------------
368*cdf0e10cSrcweir     void SAL_CALL OpenOfficeResourceBundle::setParent( const Reference< XResourceBundle >& _parent ) throw (RuntimeException)
369*cdf0e10cSrcweir     {
370*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
371*cdf0e10cSrcweir         m_xParent = _parent;
372*cdf0e10cSrcweir     }
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir     //--------------------------------------------------------------------
375*cdf0e10cSrcweir     Locale SAL_CALL OpenOfficeResourceBundle::getLocale(  ) throw (RuntimeException)
376*cdf0e10cSrcweir     {
377*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
378*cdf0e10cSrcweir         return m_aLocale;
379*cdf0e10cSrcweir     }
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir     //--------------------------------------------------------------------
382*cdf0e10cSrcweir     bool OpenOfficeResourceBundle::impl_getResourceTypeAndId_nothrow( const ::rtl::OUString& _key, ResourceTypePtr& _out_resourceType, sal_Int32& _out_resourceId ) const
383*cdf0e10cSrcweir     {
384*cdf0e10cSrcweir         sal_Int32 typeSeparatorPos = _key.indexOf( ':' );
385*cdf0e10cSrcweir         if ( typeSeparatorPos == -1 )
386*cdf0e10cSrcweir             // invalid key
387*cdf0e10cSrcweir             return false;
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir         ::rtl::OUString resourceType = _key.copy( 0, typeSeparatorPos );
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir         ResourceTypes::const_iterator typePos = m_aResourceTypes.find( resourceType );
392*cdf0e10cSrcweir         if ( typePos == m_aResourceTypes.end() )
393*cdf0e10cSrcweir             // don't know this resource type
394*cdf0e10cSrcweir             return false;
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir         _out_resourceType = typePos->second;
397*cdf0e10cSrcweir         _out_resourceId = _key.copy( typeSeparatorPos + 1 ).toInt32();
398*cdf0e10cSrcweir         return true;
399*cdf0e10cSrcweir     }
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir     //--------------------------------------------------------------------
402*cdf0e10cSrcweir     bool OpenOfficeResourceBundle::impl_getDirectElement_nothrow( const ::rtl::OUString& _key, Any& _out_Element ) const
403*cdf0e10cSrcweir     {
404*cdf0e10cSrcweir         ResourceTypePtr resourceType;
405*cdf0e10cSrcweir         sal_Int32 resourceId( 0 );
406*cdf0e10cSrcweir         if ( !impl_getResourceTypeAndId_nothrow( _key, resourceType, resourceId ) )
407*cdf0e10cSrcweir             return false;
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir         if ( !m_pResourceManager->IsAvailable( resourceType->getResourceType(), resourceId ) )
410*cdf0e10cSrcweir             // no such resource with the given type/id
411*cdf0e10cSrcweir             return false;
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir         _out_Element = resourceType->getResource( *m_pResourceManager, resourceId );
414*cdf0e10cSrcweir         return _out_Element.hasValue();
415*cdf0e10cSrcweir     }
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir     //--------------------------------------------------------------------
418*cdf0e10cSrcweir     Any SAL_CALL OpenOfficeResourceBundle::getDirectElement( const ::rtl::OUString& _key ) throw (RuntimeException)
419*cdf0e10cSrcweir     {
420*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir         Any aElement;
423*cdf0e10cSrcweir         impl_getDirectElement_nothrow( _key, aElement );
424*cdf0e10cSrcweir         return aElement;
425*cdf0e10cSrcweir     }
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir     //--------------------------------------------------------------------
428*cdf0e10cSrcweir     Any SAL_CALL OpenOfficeResourceBundle::getByName( const ::rtl::OUString& _key ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
429*cdf0e10cSrcweir     {
430*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir         Any aElement;
433*cdf0e10cSrcweir         if ( !impl_getDirectElement_nothrow( _key, aElement ) )
434*cdf0e10cSrcweir         {
435*cdf0e10cSrcweir             if ( m_xParent.is() )
436*cdf0e10cSrcweir                 aElement = m_xParent->getByName( _key );
437*cdf0e10cSrcweir         }
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir         if ( !aElement.hasValue() )
440*cdf0e10cSrcweir             throw NoSuchElementException( ::rtl::OUString(), *this );
441*cdf0e10cSrcweir 
442*cdf0e10cSrcweir         return aElement;
443*cdf0e10cSrcweir     }
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir     //--------------------------------------------------------------------
446*cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL OpenOfficeResourceBundle::getElementNames(  ) throw (RuntimeException)
447*cdf0e10cSrcweir     {
448*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
449*cdf0e10cSrcweir         OSL_ENSURE( false, "OpenOfficeResourceBundle::getElementNames: not implemented!" );
450*cdf0e10cSrcweir             // the (Simple)ResManager does not provide an API to enumerate the resources
451*cdf0e10cSrcweir         return Sequence< ::rtl::OUString >( );
452*cdf0e10cSrcweir     }
453*cdf0e10cSrcweir 
454*cdf0e10cSrcweir     //--------------------------------------------------------------------
455*cdf0e10cSrcweir     ::sal_Bool SAL_CALL OpenOfficeResourceBundle::hasByName( const ::rtl::OUString& _key ) throw (RuntimeException)
456*cdf0e10cSrcweir     {
457*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir         ResourceTypePtr resourceType;
460*cdf0e10cSrcweir         sal_Int32 resourceId( 0 );
461*cdf0e10cSrcweir         if ( !impl_getResourceTypeAndId_nothrow( _key, resourceType, resourceId ) )
462*cdf0e10cSrcweir             return sal_False;
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir         if ( !m_pResourceManager->IsAvailable( resourceType->getResourceType(), resourceId ) )
465*cdf0e10cSrcweir             return sal_False;
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir         return sal_True;
468*cdf0e10cSrcweir     }
469*cdf0e10cSrcweir 
470*cdf0e10cSrcweir     //--------------------------------------------------------------------
471*cdf0e10cSrcweir     Type SAL_CALL OpenOfficeResourceBundle::getElementType(  ) throw (RuntimeException)
472*cdf0e10cSrcweir     {
473*cdf0e10cSrcweir         return ::cppu::UnoType< Any >::get();
474*cdf0e10cSrcweir     }
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir     //--------------------------------------------------------------------
477*cdf0e10cSrcweir     ::sal_Bool SAL_CALL OpenOfficeResourceBundle::hasElements(  ) throw (RuntimeException)
478*cdf0e10cSrcweir     {
479*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
480*cdf0e10cSrcweir         OSL_ENSURE( false, "OpenOfficeResourceBundle::hasElements: not implemented!" );
481*cdf0e10cSrcweir             // the (Simple)ResManager does not provide an API to enumerate the resources
482*cdf0e10cSrcweir         return ::sal_Bool( );
483*cdf0e10cSrcweir     }
484*cdf0e10cSrcweir 
485*cdf0e10cSrcweir //........................................................................
486*cdf0e10cSrcweir } // namespace res
487*cdf0e10cSrcweir //........................................................................
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir #endif // EXTENSIONS_SOURCE_RESOURCE_OOORESOURCELOADER_CXX
490