diff options
Diffstat (limited to 'extensions/source/resource')
-rw-r--r-- | extensions/source/resource/exports.dxp | 2 | ||||
-rw-r--r-- | extensions/source/resource/makefile.mk | 84 | ||||
-rw-r--r-- | extensions/source/resource/oooresourceloader.cxx | 489 | ||||
-rw-r--r-- | extensions/source/resource/res.component | 38 | ||||
-rw-r--r-- | extensions/source/resource/res_services.cxx | 86 | ||||
-rw-r--r-- | extensions/source/resource/res_services.hxx | 64 | ||||
-rw-r--r-- | extensions/source/resource/resource.cxx | 471 |
7 files changed, 1234 insertions, 0 deletions
diff --git a/extensions/source/resource/exports.dxp b/extensions/source/resource/exports.dxp new file mode 100644 index 000000000000..f0e1c69934bc --- /dev/null +++ b/extensions/source/resource/exports.dxp @@ -0,0 +1,2 @@ +component_getImplementationEnvironment +component_getFactory diff --git a/extensions/source/resource/makefile.mk b/extensions/source/resource/makefile.mk new file mode 100644 index 000000000000..adeec5fd09dc --- /dev/null +++ b/extensions/source/resource/makefile.mk @@ -0,0 +1,84 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* +PRJ=..$/.. + +PRJNAME=extensions +TARGET=res +LIBTARGET=NO +USE_LDUMP2=TRUE +USE_DEFFILE=TRUE +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : $(PRJ)$/util$/makefile.pmk + +# --- Files -------------------------------------------------------- + +SLOFILES= $(SLO)$/resource.obj \ + $(SLO)$/oooresourceloader.obj \ + $(SLO)$/res_services.obj + +LIB1TARGET= $(SLB)$/$(TARGET).lib +LIB1OBJFILES= $(SLOFILES) + +SHL1TARGET= $(TARGET)$(DLLPOSTFIX) + +SHL1STDLIBS= \ + $(CPPULIB) \ + $(CPPUHELPERLIB) \ + $(SALLIB) \ + $(VCLLIB) \ + $(TOOLSLIB) + +SHL1DEPN= +SHL1IMPLIB= ires +SHL1LIBS= $(LIB1TARGET) +SHL1DEF= $(MISC)$/$(SHL1TARGET).def + +SHL1DEF=$(MISC)$/$(SHL1TARGET).def +SHL1IMPLIB=i$(TARGET) +SHL1LIBS=$(SLB)$/$(TARGET).lib +SHL1VERSIONMAP=$(SOLARENV)/src/component.map + +DEF1NAME= $(SHL1TARGET) +DEF1EXPORTFILE= exports.dxp + + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk +.INCLUDE : $(PRJ)$/util$/target.pmk + + +ALLTAR : $(MISC)/res.component + +$(MISC)/res.component .ERRREMOVE : $(SOLARENV)/bin/createcomponent.xslt \ + res.component + $(XSLTPROC) --nonet --stringparam uri \ + '$(COMPONENTPREFIX_BASIS_NATIVE)$(SHL1TARGETN:f)' -o $@ \ + $(SOLARENV)/bin/createcomponent.xslt res.component diff --git a/extensions/source/resource/oooresourceloader.cxx b/extensions/source/resource/oooresourceloader.cxx new file mode 100644 index 000000000000..6a9d6f338c06 --- /dev/null +++ b/extensions/source/resource/oooresourceloader.cxx @@ -0,0 +1,489 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_extensions.hxx" + +#ifndef EXTENSIONS_SOURCE_RESOURCE_OOORESOURCELOADER_CXX +#define EXTENSIONS_SOURCE_RESOURCE_OOORESOURCELOADER_CXX +#include "res_services.hxx" + +/** === begin UNO includes === **/ +#include <com/sun/star/resource/XResourceBundleLoader.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +/** === end UNO includes === **/ +#include <vcl/svapp.hxx> +#include <tools/simplerm.hxx> +#include <tools/rcid.h> +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/weakref.hxx> + +#include <boost/shared_ptr.hpp> +#include <map> + +//........................................................................ +namespace res +{ +//........................................................................ + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::resource::XResourceBundleLoader; + using ::com::sun::star::resource::XResourceBundle; + using ::com::sun::star::resource::MissingResourceException; + using ::com::sun::star::uno::XComponentContext; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::lang::Locale; + using ::com::sun::star::uno::Any; + using ::com::sun::star::container::NoSuchElementException; + using ::com::sun::star::lang::WrappedTargetException; + using ::com::sun::star::uno::Type; + using ::com::sun::star::uno::WeakReference; + /** === end UNO using === **/ + + //==================================================================== + //= helper + //==================================================================== + typedef ::std::pair< ::rtl::OUString, Locale > ResourceBundleDescriptor; + + struct ResourceBundleDescriptorLess : public ::std::binary_function< ResourceBundleDescriptor, ResourceBundleDescriptor, bool > + { + bool operator()( const ResourceBundleDescriptor& _lhs, const ResourceBundleDescriptor& _rhs ) const + { + if ( _lhs.first < _rhs.first ) + return true; + if ( _lhs.second.Language < _rhs.second.Language ) + return true; + if ( _lhs.second.Country < _rhs.second.Country ) + return true; + if ( _lhs.second.Variant < _rhs.second.Variant ) + return true; + return false; + } + }; + + //==================================================================== + //= OpenOfficeResourceLoader + //==================================================================== + typedef ::cppu::WeakImplHelper1 < XResourceBundleLoader + > OpenOfficeResourceLoader_Base; + class OpenOfficeResourceLoader : public OpenOfficeResourceLoader_Base + { + private: + typedef ::std::map< ResourceBundleDescriptor, WeakReference< XResourceBundle >, ResourceBundleDescriptorLess > + ResourceBundleCache; + + private: + Reference< XComponentContext > m_xContext; + ::osl::Mutex m_aMutex; + ResourceBundleCache m_aBundleCache; + + protected: + OpenOfficeResourceLoader( const Reference< XComponentContext >& _rxContext ); + + public: + static Sequence< ::rtl::OUString > getSupportedServiceNames_static(); + static ::rtl::OUString getImplementationName_static(); + static ::rtl::OUString getSingletonName_static(); + static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext ); + + // XResourceBundleLoader + virtual Reference< XResourceBundle > SAL_CALL loadBundle_Default( const ::rtl::OUString& aBaseName ) throw (MissingResourceException, RuntimeException); + virtual Reference< XResourceBundle > SAL_CALL loadBundle( const ::rtl::OUString& abaseName, const Locale& aLocale ) throw (MissingResourceException, RuntimeException); + + private: + OpenOfficeResourceLoader(); // never implemented + OpenOfficeResourceLoader( const OpenOfficeResourceLoader& ); // never implemented + OpenOfficeResourceLoader& operator=( const OpenOfficeResourceLoader& ); // never implemented + }; + + //==================================================================== + //= IResourceType + //==================================================================== + /** encapsulates access to a fixed resource type + */ + class IResourceType + { + public: + /** returns the RESOURCE_TYPE associated with this instance + */ + virtual RESOURCE_TYPE getResourceType() const = 0; + + /** reads a single resource from the given resource manager + @param _resourceManager + the resource manager to read from + @param _resourceId + the id of the resource to read + @return + the required resource + @precond + the caler checked via <code>_resourceManager.IsAvailable( getResourceType(), _resourceId )</code> + that the required resource really exists + */ + virtual Any getResource( SimpleResMgr& _resourceManager, sal_Int32 _resourceId ) const = 0; + + virtual ~IResourceType() { }; + }; + + //==================================================================== + //= StringResourceAccess + //==================================================================== + class StringResourceAccess : public IResourceType + { + public: + StringResourceAccess(); + + // IResourceType + virtual RESOURCE_TYPE getResourceType() const; + virtual Any getResource( SimpleResMgr& _resourceManager, sal_Int32 _resourceId ) const; + }; + + //-------------------------------------------------------------------- + StringResourceAccess::StringResourceAccess() + { + } + + //-------------------------------------------------------------------- + RESOURCE_TYPE StringResourceAccess::getResourceType() const + { + return RSC_STRING; + } + + //-------------------------------------------------------------------- + Any StringResourceAccess::getResource( SimpleResMgr& _resourceManager, sal_Int32 _resourceId ) const + { + OSL_PRECOND( _resourceManager.IsAvailable( getResourceType(), _resourceId ), "StringResourceAccess::getResource: precondition not met!" ); + Any aResource; + aResource <<= ::rtl::OUString( _resourceManager.ReadString( _resourceId ) ); + return aResource; + } + + //==================================================================== + //= OpenOfficeResourceBundle + //==================================================================== + typedef ::cppu::WeakImplHelper1 < XResourceBundle + > OpenOfficeResourceBundle_Base; + class OpenOfficeResourceBundle : public OpenOfficeResourceBundle_Base + { + private: + typedef ::boost::shared_ptr< IResourceType > ResourceTypePtr; + typedef ::std::map< ::rtl::OUString, ResourceTypePtr > ResourceTypes; + + ::osl::Mutex m_aMutex; + Reference< XResourceBundle > m_xParent; + Locale m_aLocale; + SimpleResMgr* m_pResourceManager; + ResourceTypes m_aResourceTypes; + + public: + OpenOfficeResourceBundle( + const Reference< XComponentContext >& _rxContext, + const ::rtl::OUString& _rBaseName, + const Locale& _rLocale + ); + + protected: + ~OpenOfficeResourceBundle(); + + public: + // XResourceBundle + virtual ::com::sun::star::uno::Reference< ::com::sun::star::resource::XResourceBundle > SAL_CALL getParent() throw (::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL setParent( const ::com::sun::star::uno::Reference< ::com::sun::star::resource::XResourceBundle >& _parent ) throw (::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::lang::Locale SAL_CALL getLocale( ) throw (::com::sun::star::uno::RuntimeException); + virtual ::com::sun::star::uno::Any SAL_CALL getDirectElement( const ::rtl::OUString& key ) throw (::com::sun::star::uno::RuntimeException); + + // XNameAccess (base of XResourceBundle) + 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); + virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw (::com::sun::star::uno::RuntimeException); + virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (::com::sun::star::uno::RuntimeException); + + // XElementAccess (base of XNameAccess) + virtual ::com::sun::star::uno::Type SAL_CALL getElementType( ) throw (::com::sun::star::uno::RuntimeException); + virtual ::sal_Bool SAL_CALL hasElements( ) throw (::com::sun::star::uno::RuntimeException); + + private: + /** retrievs the element with the given key, without asking our parent bundle + @param _key + the key of the element to retrieve + @param _out_Element + will contained the retrieved element upon successful return. If the method is unsuccessful, the + value will not be touched. + @return + <TRUE/> if and only if the element could be retrieved + @precond + our mutex is locked + */ + bool impl_getDirectElement_nothrow( const ::rtl::OUString& _key, Any& _out_Element ) const; + + /** retrieves the resource type and id from a given resource key, which assembles those two + @param _key + the resource key as got via a public API call + @param _out_resourceType + the resource type, if successful + @param _out_resourceId + the resource id, if successful + @return + <TRUE/> if and only if the given key specifies a known resource type, and contains a valid + resource id + */ + bool impl_getResourceTypeAndId_nothrow( const ::rtl::OUString& _key, ResourceTypePtr& _out_resourceType, sal_Int32& _out_resourceId ) const; + }; + + //==================================================================== + //= OpenOfficeResourceLoader + //==================================================================== + //-------------------------------------------------------------------- + OpenOfficeResourceLoader::OpenOfficeResourceLoader( const Reference< XComponentContext >& _rxContext ) + :m_xContext( _rxContext ) + { + } + + //-------------------------------------------------------------------- + Sequence< ::rtl::OUString > OpenOfficeResourceLoader::getSupportedServiceNames_static() + { + Sequence< ::rtl::OUString > aServices( 1 ); + aServices[ 0 ] = getSingletonName_static(); + return aServices; + } + + //-------------------------------------------------------------------- + ::rtl::OUString OpenOfficeResourceLoader::getImplementationName_static() + { + return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.resource.OpenOfficeResourceLoader" ) ); + } + + //-------------------------------------------------------------------- + ::rtl::OUString OpenOfficeResourceLoader::getSingletonName_static() + { + return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.resource.OfficeResourceLoader" ) ); + } + + //-------------------------------------------------------------------- + Reference< XInterface > OpenOfficeResourceLoader::Create( const Reference< XComponentContext >& _rxContext ) + { + return *( new OpenOfficeResourceLoader( _rxContext ) ); + } + + //-------------------------------------------------------------------- + Reference< XResourceBundle > SAL_CALL OpenOfficeResourceLoader::loadBundle_Default( const ::rtl::OUString& _baseName ) throw (MissingResourceException, RuntimeException) + { + return loadBundle( _baseName, Application::GetSettings().GetUILocale() ); + } + + //-------------------------------------------------------------------- + Reference< XResourceBundle > SAL_CALL OpenOfficeResourceLoader::loadBundle( const ::rtl::OUString& _baseName, const Locale& _locale ) throw (MissingResourceException, RuntimeException) + { + ::osl::MutexGuard aGuard( m_aMutex ); + + Reference< XResourceBundle > xBundle; + + ResourceBundleDescriptor resourceDescriptor( _baseName, _locale ); + ResourceBundleCache::iterator cachePos = m_aBundleCache.find( resourceDescriptor ); + if ( cachePos != m_aBundleCache.end() ) + xBundle = cachePos->second; + + if ( !xBundle.is() ) + { // not in the cache, or already died + xBundle = new OpenOfficeResourceBundle( m_xContext, _baseName, _locale ); + m_aBundleCache.insert( ResourceBundleCache::value_type( resourceDescriptor, xBundle ) ); + } + + return xBundle; + } + + //-------------------------------------------------------------------- + ComponentInfo getComponentInfo_OpenOfficeResourceLoader() + { + ComponentInfo aInfo; + aInfo.aSupportedServices = OpenOfficeResourceLoader::getSupportedServiceNames_static(); + aInfo.sImplementationName = OpenOfficeResourceLoader::getImplementationName_static(); + aInfo.sSingletonName = OpenOfficeResourceLoader::getSingletonName_static(); + aInfo.pFactory = &OpenOfficeResourceLoader::Create; + return aInfo; + } + + //==================================================================== + //= OpenOfficeResourceBundle + //==================================================================== + //-------------------------------------------------------------------- + OpenOfficeResourceBundle::OpenOfficeResourceBundle( const Reference< XComponentContext >& /*_rxContext*/, const ::rtl::OUString& _rBaseName, const Locale& _rLocale ) + :m_aLocale( _rLocale ) + ,m_pResourceManager( NULL ) + { + ::rtl::OUString sBaseName( _rBaseName ); + m_pResourceManager = new SimpleResMgr( sBaseName, m_aLocale ); + + if ( !m_pResourceManager->IsValid() ) + { + delete m_pResourceManager, m_pResourceManager = NULL; + throw MissingResourceException(); + } + + // supported resource types so far: strings + m_aResourceTypes[ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "string" ) ) ] = + ResourceTypePtr( new StringResourceAccess ); + } + + //-------------------------------------------------------------------- + OpenOfficeResourceBundle::~OpenOfficeResourceBundle() + { + delete m_pResourceManager; + } + + //-------------------------------------------------------------------- + Reference< XResourceBundle > SAL_CALL OpenOfficeResourceBundle::getParent() throw (RuntimeException) + { + ::osl::MutexGuard aGuard( m_aMutex ); + return m_xParent; + } + + //-------------------------------------------------------------------- + void SAL_CALL OpenOfficeResourceBundle::setParent( const Reference< XResourceBundle >& _parent ) throw (RuntimeException) + { + ::osl::MutexGuard aGuard( m_aMutex ); + m_xParent = _parent; + } + + //-------------------------------------------------------------------- + Locale SAL_CALL OpenOfficeResourceBundle::getLocale( ) throw (RuntimeException) + { + ::osl::MutexGuard aGuard( m_aMutex ); + return m_aLocale; + } + + //-------------------------------------------------------------------- + bool OpenOfficeResourceBundle::impl_getResourceTypeAndId_nothrow( const ::rtl::OUString& _key, ResourceTypePtr& _out_resourceType, sal_Int32& _out_resourceId ) const + { + sal_Int32 typeSeparatorPos = _key.indexOf( ':' ); + if ( typeSeparatorPos == -1 ) + // invalid key + return false; + + ::rtl::OUString resourceType = _key.copy( 0, typeSeparatorPos ); + + ResourceTypes::const_iterator typePos = m_aResourceTypes.find( resourceType ); + if ( typePos == m_aResourceTypes.end() ) + // don't know this resource type + return false; + + _out_resourceType = typePos->second; + _out_resourceId = _key.copy( typeSeparatorPos + 1 ).toInt32(); + return true; + } + + //-------------------------------------------------------------------- + bool OpenOfficeResourceBundle::impl_getDirectElement_nothrow( const ::rtl::OUString& _key, Any& _out_Element ) const + { + ResourceTypePtr resourceType; + sal_Int32 resourceId( 0 ); + if ( !impl_getResourceTypeAndId_nothrow( _key, resourceType, resourceId ) ) + return false; + + if ( !m_pResourceManager->IsAvailable( resourceType->getResourceType(), resourceId ) ) + // no such resource with the given type/id + return false; + + _out_Element = resourceType->getResource( *m_pResourceManager, resourceId ); + return _out_Element.hasValue(); + } + + //-------------------------------------------------------------------- + Any SAL_CALL OpenOfficeResourceBundle::getDirectElement( const ::rtl::OUString& _key ) throw (RuntimeException) + { + ::osl::MutexGuard aGuard( m_aMutex ); + + Any aElement; + impl_getDirectElement_nothrow( _key, aElement ); + return aElement; + } + + //-------------------------------------------------------------------- + Any SAL_CALL OpenOfficeResourceBundle::getByName( const ::rtl::OUString& _key ) throw (NoSuchElementException, WrappedTargetException, RuntimeException) + { + ::osl::MutexGuard aGuard( m_aMutex ); + + Any aElement; + if ( !impl_getDirectElement_nothrow( _key, aElement ) ) + { + if ( m_xParent.is() ) + aElement = m_xParent->getByName( _key ); + } + + if ( !aElement.hasValue() ) + throw NoSuchElementException( ::rtl::OUString(), *this ); + + return aElement; + } + + //-------------------------------------------------------------------- + Sequence< ::rtl::OUString > SAL_CALL OpenOfficeResourceBundle::getElementNames( ) throw (RuntimeException) + { + ::osl::MutexGuard aGuard( m_aMutex ); + OSL_ENSURE( false, "OpenOfficeResourceBundle::getElementNames: not implemented!" ); + // the (Simple)ResManager does not provide an API to enumerate the resources + return Sequence< ::rtl::OUString >( ); + } + + //-------------------------------------------------------------------- + ::sal_Bool SAL_CALL OpenOfficeResourceBundle::hasByName( const ::rtl::OUString& _key ) throw (RuntimeException) + { + ::osl::MutexGuard aGuard( m_aMutex ); + + ResourceTypePtr resourceType; + sal_Int32 resourceId( 0 ); + if ( !impl_getResourceTypeAndId_nothrow( _key, resourceType, resourceId ) ) + return sal_False; + + if ( !m_pResourceManager->IsAvailable( resourceType->getResourceType(), resourceId ) ) + return sal_False; + + return sal_True; + } + + //-------------------------------------------------------------------- + Type SAL_CALL OpenOfficeResourceBundle::getElementType( ) throw (RuntimeException) + { + return ::cppu::UnoType< Any >::get(); + } + + //-------------------------------------------------------------------- + ::sal_Bool SAL_CALL OpenOfficeResourceBundle::hasElements( ) throw (RuntimeException) + { + ::osl::MutexGuard aGuard( m_aMutex ); + OSL_ENSURE( false, "OpenOfficeResourceBundle::hasElements: not implemented!" ); + // the (Simple)ResManager does not provide an API to enumerate the resources + return ::sal_Bool( ); + } + +//........................................................................ +} // namespace res +//........................................................................ + +#endif // EXTENSIONS_SOURCE_RESOURCE_OOORESOURCELOADER_CXX diff --git a/extensions/source/resource/res.component b/extensions/source/resource/res.component new file mode 100644 index 000000000000..57f96609d5df --- /dev/null +++ b/extensions/source/resource/res.component @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!--********************************************************************** +* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2000, 2010 Oracle and/or its affiliates. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +* +**********************************************************************--> + +<component loader="com.sun.star.loader.SharedLibrary" + xmlns="http://openoffice.org/2010/uno-components"> + <implementation name="com.sun.star.comp.extensions.ResourceService"> + <service name="com.sun.star.resource.VclStringResourceLoader"/> + </implementation> + <implementation name="com.sun.star.comp.resource.OpenOfficeResourceLoader"> + <service name="com.sun.star.resource.OfficeResourceLoader"/> + <singleton name="com.sun.star.resource.OfficeResourceLoader"/> + </implementation> +</component> diff --git a/extensions/source/resource/res_services.cxx b/extensions/source/resource/res_services.cxx new file mode 100644 index 000000000000..7bdc684294ea --- /dev/null +++ b/extensions/source/resource/res_services.cxx @@ -0,0 +1,86 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_extensions.hxx" +#include "res_services.hxx" + +/** === begin UNO using === **/ +using ::com::sun::star::uno::Reference; +using ::com::sun::star::uno::Sequence; +using ::com::sun::star::uno::Exception; +using ::com::sun::star::lang::XMultiServiceFactory; +using ::com::sun::star::lang::XSingleServiceFactory; +using ::com::sun::star::uno::UNO_QUERY; +/** === end UNO using === **/ + +#include <vector> + +namespace res +{ + ::std::vector< ComponentInfo > getComponentInfos() + { + ::std::vector< ::res::ComponentInfo > aComponentInfos; + aComponentInfos.push_back( getComponentInfo_VclStringResourceLoader() ); + aComponentInfos.push_back( getComponentInfo_OpenOfficeResourceLoader() ); + return aComponentInfos; + } +} + +extern "C" { + +void SAL_CALL component_getImplementationEnvironment( + const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ ) +{ + *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; +} + +void * SAL_CALL component_getFactory( + const sal_Char * pImplName, XMultiServiceFactory * /*pServiceManager*/, void * /*pRegistryKey*/ ) +{ + void * pRet = 0; + ::std::vector< ::res::ComponentInfo > aComponentInfos( ::res::getComponentInfos() ); + for ( ::std::vector< ::res::ComponentInfo >::const_iterator loop = aComponentInfos.begin(); + loop != aComponentInfos.end(); + ++loop + ) + { + if ( 0 == loop->sImplementationName.compareToAscii( pImplName ) ) + { + // create the factory + Reference< XSingleServiceFactory > xFactory( ::cppu::createSingleComponentFactory( + loop->pFactory, loop->sImplementationName, loop->aSupportedServices ), + UNO_QUERY ); + // acquire, because we return an interface pointer instead of a reference + xFactory->acquire(); + pRet = xFactory.get(); + } + } + return pRet; +} + +} // extern "C" diff --git a/extensions/source/resource/res_services.hxx b/extensions/source/resource/res_services.hxx new file mode 100644 index 000000000000..c62d9e6e495c --- /dev/null +++ b/extensions/source/resource/res_services.hxx @@ -0,0 +1,64 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef EXTENSIONS_RESOURCE_SERVICES_HXX +#define EXTENSIONS_RESOURCE_SERVICES_HXX + +/** === begin UNO includes === **/ +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/uno/Reference.hxx> +#include <com/sun/star/uno/XInterface.hpp> +/** === end UNO includes === **/ +#include <cppuhelper/factory.hxx> + +//........................................................................ +namespace res +{ +//........................................................................ + + struct ComponentInfo + { + /// services supported by the component + ::com::sun::star::uno::Sequence< ::rtl::OUString > aSupportedServices; + /// implementation name of the component + ::rtl::OUString sImplementationName; + /** name of the singleton instance of the component, if it is a singleton, empty otherwise + If the component is a singleton, aSupportedServices must contain exactly one element. + */ + ::rtl::OUString sSingletonName; + /// factory for creating the component + ::cppu::ComponentFactoryFunc pFactory; + }; + + ComponentInfo getComponentInfo_VclStringResourceLoader(); + ComponentInfo getComponentInfo_OpenOfficeResourceLoader(); + +//........................................................................ +} // namespace res +//........................................................................ + +#endif // EXTENSIONS_RESOURCE_SERVICES_HXX diff --git a/extensions/source/resource/resource.cxx b/extensions/source/resource/resource.cxx new file mode 100644 index 000000000000..73cb48d479a9 --- /dev/null +++ b/extensions/source/resource/resource.cxx @@ -0,0 +1,471 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_extensions.hxx" +#include "res_services.hxx" + +#include <vos/mutex.hxx> +#include <uno/lbnames.h> // CPPU_CURRENT_LANGUAGE_BINDING_NAME macro, which specify the environment type +#include <cppuhelper/factory.hxx> // helper for factories +#include <cppuhelper/implbase3.hxx> // helper for implementations + +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/script/XInvocation.hpp> +#include <com/sun/star/script/XTypeConverter.hpp> +#include <com/sun/star/reflection/InvocationTargetException.hpp> +#include <com/sun/star/beans/XExactName.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/beans/PropertyState.hpp> + +#include <tools/resmgr.hxx> +#include <tools/rcid.h> +#include <tools/resary.hxx> +#include <vcl/svapp.hxx> + +#include <rtl/ustring.hxx> +#include <rtl/strbuf.hxx> + +using namespace vos; +using namespace rtl; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::registry; +using namespace com::sun::star::script; +using namespace com::sun::star::beans; +using namespace com::sun::star::reflection; + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ +class ResourceService : public cppu::WeakImplHelper3< XInvocation, XExactName, XServiceInfo > +{ +public: + ResourceService( const Reference< XMultiServiceFactory > & ); + ~ResourceService(); + + // XServiceInfo + OUString SAL_CALL getImplementationName() throw(); + BOOL SAL_CALL supportsService(const OUString& ServiceName) throw(); + Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw(); + + static Sequence< OUString > getSupportedServiceNames_Static(void) throw(); + static OUString getImplementationName_Static() throw() + { + return OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.extensions.ResourceService")); + } + static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext ); + + // XExactName + OUString SAL_CALL getExactName( const OUString & ApproximateName ) throw(RuntimeException); + + // XInvokation + Reference< XIntrospectionAccess > SAL_CALL getIntrospection(void) throw(RuntimeException); + Any SAL_CALL invoke(const OUString& FunctionName, const Sequence< Any >& Params, Sequence< sal_Int16 >& OutParamIndex, Sequence< Any >& OutParam) throw(IllegalArgumentException, CannotConvertException, InvocationTargetException, RuntimeException); + void SAL_CALL setValue(const OUString& PropertyName, const Any& Value) throw(UnknownPropertyException, CannotConvertException, InvocationTargetException, RuntimeException); + Any SAL_CALL getValue(const OUString& PropertyName) throw(UnknownPropertyException, RuntimeException); + BOOL SAL_CALL hasMethod(const OUString& Name) throw(RuntimeException); + BOOL SAL_CALL hasProperty(const OUString& Name) throw(RuntimeException); +private: + Reference< XTypeConverter > getTypeConverter() const; + Reference< XInvocation > getDefaultInvocation() const; + + Reference< XMultiServiceFactory > xSMgr; + Reference< XInvocation > xDefaultInvocation; + Reference< XTypeConverter > xTypeConverter; + OUString aFileName; + ResMgr * pResMgr; +}; + + +//----------------------------------------------------------------------------- +ResourceService::ResourceService( const Reference< XMultiServiceFactory > & rSMgr ) + : xSMgr( rSMgr ) + , pResMgr( NULL ) +{ +} + +//----------------------------------------------------------------------------- +Reference< XInterface > ResourceService::Create( const Reference< XComponentContext >& _rxContext ) +{ + Reference< XMultiServiceFactory > xFactory( _rxContext->getServiceManager(), UNO_QUERY_THROW ); + return *( new ResourceService( xFactory ) ); +} + +//----------------------------------------------------------------------------- +ResourceService::~ResourceService() +{ + delete pResMgr; +} + +// XServiceInfo +OUString ResourceService::getImplementationName() throw() +{ + return getImplementationName_Static(); +} + +// XServiceInfo +BOOL SAL_CALL ResourceService::supportsService(const OUString& ServiceName) throw() +{ + Sequence< OUString > aSNL = getSupportedServiceNames(); + const OUString * pArray = aSNL.getConstArray(); + for( sal_Int32 i = 0; i < aSNL.getLength(); i++ ) + if( pArray[i] == ServiceName ) + return TRUE; + return FALSE; +} + +// XServiceInfo +Sequence< OUString > SAL_CALL ResourceService::getSupportedServiceNames(void) throw() +{ + return getSupportedServiceNames_Static(); +} + +// ResourceService +Sequence< OUString > ResourceService::getSupportedServiceNames_Static(void) throw() +{ + Sequence< OUString > aSNS( 1 ); + aSNS.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.resource.VclStringResourceLoader")); + return aSNS; +} + +// ResourceService +Reference< XTypeConverter > ResourceService::getTypeConverter() const +{ + OGuard aGuard( Application::GetSolarMutex() ); + if( xSMgr.is() ) + { + Reference< XTypeConverter > xConv( xSMgr->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.script.Converter" ))), UNO_QUERY ); + ((ResourceService*)this)->xTypeConverter = xConv; + } + return xTypeConverter; +} + +// ResourceService +Reference< XInvocation > ResourceService::getDefaultInvocation() const +{ + OGuard aGuard( Application::GetSolarMutex() ); + /* f�hrt zur Zeit noch zu einer rekursion + if( xSMgr.is() ) + { + Reference< XSingleServiceFactory > xFact( xSMgr->createInstance( OUString::createFromAscii("com.sun.star.script.Invocation") ), UNO_QUERY ); + if( xFact.is() ) + { + Sequence< Any > aArgs( 1 ); + Reference< XInterface > xThis( *this ); + aArgs.getArray()[0].set( &xThis, XInterface_Reference< get >lection() ); + Reference< XInvokation > xI( xFact->createInstanceWithArguments( aArgs ), UNO_QUERY ); + ((ResourceService*)this)->xDefaultInvocation = xI; + } + } + */ + return xDefaultInvocation; +} + +// XExactName +OUString SAL_CALL ResourceService::getExactName( const OUString & ApproximateName ) throw(RuntimeException) +{ + OUString aName( ApproximateName ); + aName = aName.toAsciiLowerCase(); + if( aName.equalsAscii("filename") ) + return OUString(RTL_CONSTASCII_USTRINGPARAM("FileName")); + else if( aName.equalsAscii("getstring" )) + return OUString(RTL_CONSTASCII_USTRINGPARAM("getString")); + else if( aName.equalsAscii("getstrings" )) + return OUString(RTL_CONSTASCII_USTRINGPARAM("getStrings")); + else if( aName.equalsAscii("hasstring") ) + return OUString(RTL_CONSTASCII_USTRINGPARAM("hasString")); + else if( aName.equalsAscii("hasstrings") ) + return OUString(RTL_CONSTASCII_USTRINGPARAM("hasStrings")); + else if( aName.equalsAscii("getstringlist") ) + return OUString(RTL_CONSTASCII_USTRINGPARAM("getStringList")); + else if( aName.equalsAscii("hasStringList") ) + return OUString(RTL_CONSTASCII_USTRINGPARAM("hasStringList")); + Reference< XExactName > xEN( getDefaultInvocation(), UNO_QUERY ); + if( xEN.is() ) + return xEN->getExactName( ApproximateName ); + return OUString(); +} + +// XInvokation +Reference< XIntrospectionAccess > SAL_CALL ResourceService::getIntrospection(void) + throw(RuntimeException) +{ + Reference< XInvocation > xI = getDefaultInvocation(); + if( xI.is() ) + return xI->getIntrospection(); + return Reference< XIntrospectionAccess >(); +} + +// XInvokation +Any SAL_CALL ResourceService::invoke +( + const OUString& FunctionName, + const Sequence< Any >& Params, + Sequence< sal_Int16 >& OutParamIndex, + Sequence< Any >& OutParam +) + throw(IllegalArgumentException, CannotConvertException, InvocationTargetException, RuntimeException) +{ + Any aRet; + if( FunctionName.equalsAscii("getString") + || FunctionName.equalsAscii("getStrings" ) + || FunctionName.equalsAscii("hasString" ) + || FunctionName.equalsAscii("hasStrings" ) + ) + { + sal_Int32 nElements = Params.getLength(); + if( nElements < 1 ) + throw IllegalArgumentException(); + if( nElements > 1 && (FunctionName.equalsAscii("getString") || FunctionName.equalsAscii("hasString") ) ) + throw IllegalArgumentException(); + if( !pResMgr ) + throw IllegalArgumentException(); + + Sequence< OUString > aStrings( Params.getLength() ); + Sequence< sal_Bool > aBools( Params.getLength() ); + const Any* pIn = Params.getConstArray(); + OUString* pOutString = aStrings.getArray(); + sal_Bool* pOutBool = aBools.getArray(); + + Reference< XTypeConverter > xC = getTypeConverter(); + bool bGetBranch = FunctionName.equalsAscii( "getString" ) || FunctionName.equalsAscii( "getStrings" ); + + OGuard aGuard( Application::GetSolarMutex() ); + for( sal_Int32 n = 0; n < nElements; n++ ) + { + sal_Int32 nId = 0; + if( !(pIn[n] >>= nId) ) + { + if( xC.is() ) + { + xC->convertToSimpleType( pIn[n], TypeClass_LONG ) >>= nId; + } + else + throw CannotConvertException(); + } + if( nId > 0xFFFF || nId < 0 ) + throw IllegalArgumentException(); + + if( bGetBranch ) + { + ResId aId( (USHORT)nId, *pResMgr ); + aId.SetRT( RSC_STRING ); + if( pResMgr->IsAvailable( aId ) ) + { + String aStr( aId ); + pOutString[n] = aStr; + } + else + throw IllegalArgumentException(); + } + else // hasString(s) + { + sal_Bool bRet = sal_False; + if( pResMgr ) + { + ResId aId( (USHORT)nId, *pResMgr ); + aId.SetRT( RSC_STRING ); + bRet = pResMgr->IsAvailable( aId ); + } + pOutBool[n] = bRet; + } + } + if( FunctionName.equalsAscii("getString") ) + aRet <<= pOutString[0]; + else if( FunctionName.equalsAscii("getStrings" ) ) + aRet <<= aStrings; + else if( FunctionName.equalsAscii("hasString" ) ) + aRet <<= pOutBool[0]; + else + aRet <<= aBools; + } + else if( FunctionName.equalsAscii("getStringList") || FunctionName.equalsAscii("hasStringList" ) ) + { + if( Params.getLength() != 1 ) + throw IllegalArgumentException(); + Reference< XTypeConverter > xC = getTypeConverter(); + OGuard aGuard( Application::GetSolarMutex() ); + + sal_Int32 nId = 0; + if( !(Params.getConstArray()[0] >>= nId) ) + { + if( xC.is() ) + { + xC->convertToSimpleType( Params.getConstArray()[0], TypeClass_LONG ) >>= nId; + } + else + throw CannotConvertException(); + } + + if( FunctionName.equalsAscii("getStringList") ) + { + ResId aId( (USHORT)nId, *pResMgr ); + aId.SetRT( RSC_STRINGARRAY ); + if( pResMgr->IsAvailable( aId ) ) + { + ResStringArray aStr( aId ); + int nEntries = aStr.Count(); + Sequence< PropertyValue > aPropSeq( nEntries ); + PropertyValue* pOut = aPropSeq.getArray(); + for( int i = 0; i < nEntries; i++ ) + { + pOut[i].Name = aStr.GetString( i ); + pOut[i].Handle = -1; + pOut[i].Value <<= aStr.GetValue( i ); + pOut[i].State = PropertyState_DIRECT_VALUE; + } + aRet <<= aPropSeq; + } + else + throw IllegalArgumentException(); + } + else // hasStringList + { + sal_Bool bRet = sal_False; + if( pResMgr ) + { + ResId aId( (USHORT)nId, *pResMgr ); + aId.SetRT( RSC_STRINGARRAY ); + bRet = pResMgr->IsAvailable( aId ); + } + aRet <<= bRet; + } + } + else + { + Reference< XInvocation > xI = getDefaultInvocation(); + if( xI.is() ) + return xI->invoke( FunctionName, Params, OutParamIndex, OutParam ); + else + throw IllegalArgumentException(); + } + return aRet; +} + +// XInvokation +void SAL_CALL ResourceService::setValue(const OUString& PropertyName, const Any& Value) + throw(UnknownPropertyException, CannotConvertException, InvocationTargetException, RuntimeException) +{ + if( PropertyName.equalsAscii("FileName") ) + { + OUString aName; + if( !(Value >>= aName) ) + { + Reference< XTypeConverter > xC = getTypeConverter(); + if( xC.is() ) + xC->convertToSimpleType( Value, TypeClass_STRING ) >>= aName; + else + throw CannotConvertException(); + } + + OGuard aGuard( Application::GetSolarMutex() ); + OStringBuffer aBuf( aName.getLength()+8 ); + aBuf.append( OUStringToOString( aName, osl_getThreadTextEncoding() ) ); + ResMgr * pRM = ResMgr::CreateResMgr( aBuf.getStr() ); + if( !pRM ) + throw InvocationTargetException(); + if( pResMgr ) + delete pResMgr; + pResMgr = pRM; + aFileName = OStringToOUString( aBuf.makeStringAndClear(), osl_getThreadTextEncoding() ); + } + else + { + Reference< XInvocation > xI = getDefaultInvocation(); + if( xI.is() ) + xI->setValue( PropertyName, Value ); + else + throw UnknownPropertyException(); + } +} + +// XInvokation +Any SAL_CALL ResourceService::getValue(const OUString& PropertyName) + throw(UnknownPropertyException, RuntimeException) +{ + OGuard aGuard( Application::GetSolarMutex() ); + if( PropertyName.equalsAscii("FileName" )) + return makeAny( aFileName ); + + Reference< XInvocation > xI = getDefaultInvocation(); + if( xI.is() ) + return xI->getValue( PropertyName ); + + throw UnknownPropertyException(); +} + +// XInvokation +BOOL SAL_CALL ResourceService::hasMethod(const OUString& Name) + throw(RuntimeException) +{ + if( Name.equalsAscii("getString") || + Name.equalsAscii("getStrings") || + Name.equalsAscii("hasString") || + Name.equalsAscii("hasStrings") || + Name.equalsAscii("getStringList") || + Name.equalsAscii("hasStringList") + ) + return TRUE; + else + { + Reference< XInvocation > xI = getDefaultInvocation(); + if( xI.is() ) + return xI->hasMethod( Name ); + else + return FALSE; + } +} + +// XInvokation +BOOL SAL_CALL ResourceService::hasProperty(const OUString& Name) + throw(RuntimeException) +{ + if( Name.equalsAscii("FileName") ) + return TRUE; + else + { + Reference< XInvocation > xI = getDefaultInvocation(); + if( xI.is() ) + return xI->hasProperty( Name ); + else + return FALSE; + } +} + +namespace res +{ + ComponentInfo getComponentInfo_VclStringResourceLoader() + { + ComponentInfo aInfo; + aInfo.aSupportedServices = ResourceService::getSupportedServiceNames_Static(); + aInfo.sImplementationName = ResourceService::getImplementationName_Static(); + aInfo.pFactory = &ResourceService::Create; + return aInfo; + } +} + |