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