merge the formfield patch from ooo-build
[ooovba.git] / connectivity / source / resource / sharedresources.cxx
blob7d2a5a0002d7a54412dfc3ad753494f966bcf5c6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sharedresources.cxx,v $
10 * $Revision: 1.4.56.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "resource/sharedresources.hxx"
35 #include <comphelper/processfactory.hxx>
36 #include <comphelper/officeresourcebundle.hxx>
38 /** === begin UNO includes === **/
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/uno/XComponentContext.hpp>
41 /** === end UNO includes === **/
43 #include <tools/diagnose_ex.h>
44 #include <osl/diagnose.h>
46 //........................................................................
47 namespace connectivity
49 //........................................................................
51 /** === begin UNO using === **/
52 using ::com::sun::star::uno::Reference;
53 using ::com::sun::star::beans::XPropertySet;
54 using ::com::sun::star::uno::UNO_QUERY_THROW;
55 using ::com::sun::star::uno::XComponentContext;
56 using ::com::sun::star::uno::Exception;
57 /** === end UNO using === **/
59 //====================================================================
60 //= SharedResources_Impl
61 //====================================================================
62 class SharedResources_Impl
64 private:
65 static SharedResources_Impl* s_pInstance;
66 static oslInterlockedCount s_nClients;
68 private:
69 ::std::auto_ptr< ::comphelper::OfficeResourceBundle >
70 m_pResourceBundle;
72 public:
73 static void registerClient();
74 static void revokeClient();
76 static SharedResources_Impl&
77 getInstance();
79 ::rtl::OUString getResourceString( ResourceId _nId );
81 private:
82 SharedResources_Impl();
84 static ::osl::Mutex& getMutex()
86 static ::osl::Mutex s_aMutex;
87 return s_aMutex;
91 //--------------------------------------------------------------------
92 SharedResources_Impl* SharedResources_Impl::s_pInstance( NULL );
93 oslInterlockedCount SharedResources_Impl::s_nClients( 0 );
95 //--------------------------------------------------------------------
96 SharedResources_Impl::SharedResources_Impl()
98 try
100 Reference< XPropertySet > xFactoryProps(
101 ::comphelper::getProcessServiceFactory(), UNO_QUERY_THROW );
102 Reference< XComponentContext > xContext(
103 xFactoryProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) ),
104 UNO_QUERY_THROW
106 m_pResourceBundle.reset( new ::comphelper::OfficeResourceBundle( xContext, "cnr" ) );
108 catch( const Exception& )
110 DBG_UNHANDLED_EXCEPTION();
114 //--------------------------------------------------------------------
115 ::rtl::OUString SharedResources_Impl::getResourceString( ResourceId _nId )
117 if ( m_pResourceBundle.get() == NULL )
118 // this should never happen, but we gracefully ignore it. It has been reported
119 // in the constructor in non-product builds.
120 return ::rtl::OUString();
122 return m_pResourceBundle->loadString( _nId );
125 //--------------------------------------------------------------------
126 void SharedResources_Impl::registerClient()
128 osl_incrementInterlockedCount( &s_nClients );
131 //--------------------------------------------------------------------
132 void SharedResources_Impl::revokeClient()
134 ::osl::MutexGuard aGuard( getMutex() );
135 if ( 0 == osl_decrementInterlockedCount( &s_nClients ) )
137 delete s_pInstance;
138 s_pInstance = NULL;
142 //--------------------------------------------------------------------
143 SharedResources_Impl& SharedResources_Impl::getInstance()
145 ::osl::MutexGuard aGuard( getMutex() );
146 OSL_ENSURE( s_nClients > 0, "SharedResources_Impl::getInstance: no active clients!" );
148 if ( !s_pInstance )
149 s_pInstance = new SharedResources_Impl;
151 return *s_pInstance;
154 //====================================================================
155 //= helpers
156 //====================================================================
157 namespace
159 size_t lcl_substitute( ::rtl::OUString& _inout_rString,
160 const sal_Char* _pAsciiPattern, const ::rtl::OUString& _rReplace )
162 size_t nOccurences = 0;
164 ::rtl::OUString sPattern( ::rtl::OUString::createFromAscii( _pAsciiPattern ) );
165 sal_Int32 nIndex = 0;
166 while ( ( nIndex = _inout_rString.indexOf( sPattern ) ) > -1 )
168 ++nOccurences;
169 _inout_rString = _inout_rString.replaceAt( nIndex, sPattern.getLength(), _rReplace );
172 return nOccurences;
176 //====================================================================
177 //= SharedResources
178 //====================================================================
179 //--------------------------------------------------------------------
180 SharedResources::SharedResources()
182 SharedResources_Impl::registerClient();
185 //--------------------------------------------------------------------
186 SharedResources::~SharedResources()
188 SharedResources_Impl::revokeClient();
191 //--------------------------------------------------------------------
192 ::rtl::OUString SharedResources::getResourceString( ResourceId _nResId ) const
194 return SharedResources_Impl::getInstance().getResourceString( _nResId );
197 //--------------------------------------------------------------------
198 ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
199 const sal_Char* _pAsciiPatternToReplace, const ::rtl::OUString& _rStringToSubstitute ) const
201 ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
202 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace, _rStringToSubstitute ) );
203 return sString;
206 //--------------------------------------------------------------------
207 ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
208 const sal_Char* _pAsciiPatternToReplace1, const ::rtl::OUString& _rStringToSubstitute1,
209 const sal_Char* _pAsciiPatternToReplace2, const ::rtl::OUString& _rStringToSubstitute2 ) const
211 ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
212 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) );
213 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) );
214 return sString;
217 //--------------------------------------------------------------------
218 ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
219 const sal_Char* _pAsciiPatternToReplace1, const ::rtl::OUString& _rStringToSubstitute1,
220 const sal_Char* _pAsciiPatternToReplace2, const ::rtl::OUString& _rStringToSubstitute2,
221 const sal_Char* _pAsciiPatternToReplace3, const ::rtl::OUString& _rStringToSubstitute3 ) const
223 ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
224 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) );
225 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) );
226 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace3, _rStringToSubstitute3 ) );
227 return sString;
229 //--------------------------------------------------------------------
230 ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
231 const ::std::list< ::std::pair<const sal_Char* , ::rtl::OUString > > _aStringToSubstitutes) const
233 ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
234 ::std::list< ::std::pair<const sal_Char* , ::rtl::OUString > >::const_iterator aIter = _aStringToSubstitutes.begin();
235 ::std::list< ::std::pair<const sal_Char* , ::rtl::OUString > >::const_iterator aEnd = _aStringToSubstitutes.end();
236 for(;aIter != aEnd; ++aIter)
237 OSL_VERIFY( lcl_substitute( sString, aIter->first, aIter->second ) );
239 return sString;
242 //........................................................................
243 } // namespace connectivity
244 //........................................................................