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