Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <comphelper/officeresourcebundle.hxx>
21 :
22 : #include <com/sun/star/resource/XResourceBundle.hpp>
23 : #include <com/sun/star/resource/XResourceBundleLoader.hpp>
24 : #include <com/sun/star/lang/NullPointerException.hpp>
25 : #include <osl/mutex.hxx>
26 : #include <osl/diagnose.h>
27 : #include <rtl/ustrbuf.hxx>
28 :
29 :
30 : namespace comphelper
31 : {
32 :
33 :
34 : using ::com::sun::star::uno::Reference;
35 : using com::sun::star::resource::XResourceBundle;
36 : using com::sun::star::resource::XResourceBundleLoader;
37 : using com::sun::star::resource::MissingResourceException;
38 : using ::com::sun::star::uno::XComponentContext;
39 : using ::com::sun::star::lang::NullPointerException;
40 : using ::com::sun::star::uno::UNO_QUERY;
41 : using ::com::sun::star::uno::Exception;
42 : using ::com::sun::star::uno::Any;
43 :
44 :
45 : //= ResourceBundle_Impl
46 :
47 0 : class ResourceBundle_Impl
48 : {
49 : private:
50 : Reference< XComponentContext > m_xContext;
51 : OUString m_sBaseName;
52 : Reference< XResourceBundle > m_xBundle;
53 : bool m_bAttemptedCreate;
54 : mutable ::osl::Mutex m_aMutex;
55 :
56 : public:
57 0 : ResourceBundle_Impl( const Reference< XComponentContext >& _context, const OUString& _baseName )
58 : :m_xContext( _context )
59 : ,m_sBaseName( _baseName )
60 0 : ,m_bAttemptedCreate( false )
61 : {
62 0 : }
63 :
64 : public:
65 : /** loads the string with the given resource id from the resource bundle
66 : @param _resourceId
67 : the id of the string to load
68 : @return
69 : the requested resource string. If no string with the given id exists in the resource bundle,
70 : an empty string is returned. In a non-product version, an OSL_ENSURE will notify you of this
71 : then.
72 : */
73 : OUString loadString( sal_Int32 _resourceId ) const;
74 :
75 : /** determines whether the resource bundle has a string with the given id
76 : @param _resourceId
77 : the id of the string whose existence is to be checked
78 : @return
79 : <TRUE/> if and only if a string with the given ID exists in the resource
80 : bundle.
81 : */
82 : bool hasString( sal_Int32 _resourceId ) const;
83 :
84 : private:
85 : /** loads the bundle represented by the instance
86 :
87 : The method is safe against multiple calls: If a previous call succeeded or failed, the
88 : previous result will be returned, without any other processing.
89 :
90 : @precond
91 : Our mutex is locked.
92 : */
93 : bool impl_loadBundle_nothrow();
94 :
95 : /** returns the resource bundle key for a string with a given resource id
96 : */
97 : static OUString
98 : impl_getStringResourceKey( sal_Int32 _resourceId );
99 : };
100 :
101 :
102 0 : OUString ResourceBundle_Impl::impl_getStringResourceKey( sal_Int32 _resourceId )
103 : {
104 0 : OUStringBuffer key;
105 0 : key.appendAscii( "string:" );
106 0 : key.append( _resourceId );
107 0 : return key.makeStringAndClear();
108 : }
109 :
110 :
111 0 : OUString ResourceBundle_Impl::loadString( sal_Int32 _resourceId ) const
112 : {
113 0 : ::osl::MutexGuard aGuard( m_aMutex );
114 :
115 0 : OUString sString;
116 :
117 0 : if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() )
118 : {
119 : try
120 : {
121 0 : OSL_VERIFY( m_xBundle->getByName( impl_getStringResourceKey( _resourceId ) ) >>= sString );
122 : }
123 0 : catch( const Exception& )
124 : {
125 : OSL_FAIL( "ResourceBundle_Impl::loadString: caught an exception!" );
126 : }
127 : }
128 0 : return sString;
129 : }
130 :
131 :
132 0 : bool ResourceBundle_Impl::hasString( sal_Int32 _resourceId ) const
133 : {
134 0 : ::osl::MutexGuard aGuard( m_aMutex );
135 :
136 0 : bool has = false;
137 :
138 0 : if ( const_cast< ResourceBundle_Impl* >( this )->impl_loadBundle_nothrow() )
139 : {
140 : try
141 : {
142 0 : has = m_xBundle->hasByName( impl_getStringResourceKey( _resourceId ) );
143 : }
144 0 : catch( const Exception& )
145 : {
146 : OSL_FAIL( "ResourceBundle_Impl::hasString: caught an exception!" );
147 : }
148 : }
149 0 : return has;
150 : }
151 :
152 :
153 0 : bool ResourceBundle_Impl::impl_loadBundle_nothrow()
154 : {
155 0 : if ( m_bAttemptedCreate )
156 0 : return m_xBundle.is();
157 :
158 0 : m_bAttemptedCreate = true;
159 :
160 0 : Reference< XResourceBundleLoader > xLoader;
161 : try
162 : {
163 0 : Any aValue( m_xContext->getValueByName(
164 0 : OUString( "/singletons/com.sun.star.resource.OfficeResourceLoader" ) ) );
165 0 : OSL_VERIFY( aValue >>= xLoader );
166 : }
167 0 : catch( const Exception& )
168 : {
169 : OSL_FAIL( "ResourceBundle_Impl::impl_loadBundle_nopthrow: could not create the resource loader!" );
170 : }
171 :
172 0 : if ( !xLoader.is() )
173 0 : return false;
174 :
175 : try
176 : {
177 0 : m_xBundle = xLoader->loadBundle_Default( m_sBaseName );
178 : }
179 0 : catch( const MissingResourceException& )
180 : {
181 : OSL_FAIL( "ResourceBundle_Impl::impl_loadBundle_nopthrow: missing the given resource bundle!" );
182 : }
183 :
184 0 : return m_xBundle.is();
185 : }
186 :
187 :
188 : //= OfficeResourceBundle
189 :
190 :
191 0 : OfficeResourceBundle::OfficeResourceBundle( const Reference< XComponentContext >& _context, const sal_Char* _bundleBaseAsciiName )
192 0 : :m_pImpl( new ResourceBundle_Impl( _context, OUString::createFromAscii( _bundleBaseAsciiName ) ) )
193 : {
194 0 : if ( !_context.is() )
195 0 : throw NullPointerException();
196 0 : }
197 :
198 :
199 0 : OfficeResourceBundle::~OfficeResourceBundle()
200 : {
201 0 : }
202 :
203 :
204 0 : OUString OfficeResourceBundle::loadString( sal_Int32 _resourceId ) const
205 : {
206 0 : return m_pImpl->loadString( _resourceId );
207 : }
208 :
209 :
210 0 : bool OfficeResourceBundle::hasString( sal_Int32 _resourceId ) const
211 : {
212 0 : return m_pImpl->hasString( _resourceId );
213 : }
214 :
215 :
216 : } // namespace comphelper
217 :
218 :
219 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|