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