1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sfxmodelfactory.cxx,v $
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_sfx2.hxx"
33 #include "sfx2/sfxmodelfactory.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/beans/NamedValue.hpp>
38 #include <com/sun/star/lang/XInitialization.hpp>
39 /** === end UNO includes === **/
41 #include <comphelper/namedvaluecollection.hxx>
42 #include <cppuhelper/implbase2.hxx>
47 //........................................................................
50 //........................................................................
52 /** === begin UNO using === **/
53 using ::com::sun::star::uno::Reference
;
54 using ::com::sun::star::uno::XInterface
;
55 using ::com::sun::star::uno::UNO_QUERY
;
56 using ::com::sun::star::uno::UNO_QUERY_THROW
;
57 using ::com::sun::star::uno::UNO_SET_THROW
;
58 using ::com::sun::star::uno::Exception
;
59 using ::com::sun::star::uno::RuntimeException
;
60 using ::com::sun::star::uno::Any
;
61 using ::com::sun::star::uno::makeAny
;
62 using ::com::sun::star::lang::XMultiServiceFactory
;
63 using ::com::sun::star::uno::Sequence
;
64 using ::com::sun::star::lang::XSingleServiceFactory
;
65 using ::com::sun::star::lang::XServiceInfo
;
66 using ::com::sun::star::beans::NamedValue
;
67 using ::com::sun::star::beans::PropertyValue
;
68 using ::com::sun::star::lang::XInitialization
;
69 /** === end UNO using === **/
71 //====================================================================
72 //= SfxModelFactory - declaration
73 //====================================================================
74 typedef ::cppu::WeakImplHelper2
< XSingleServiceFactory
76 > SfxModelFactory_Base
;
77 /** implements a XSingleServiceFactory which can be used to created instances
78 of classes derived from SfxBaseModel
80 In opposite to the default implementations from module cppuhelper, this
81 factory evaluates certain creation arguments (passed to createInstanceWithArguments)
82 and passes them to the factory function of the derived class.
84 class SfxModelFactory
: public SfxModelFactory_Base
88 const Reference
< XMultiServiceFactory
>& _rxServiceFactory
,
89 const ::rtl::OUString
& _rImplementationName
,
90 const SfxModelFactoryFunc _pComponentFactoryFunc
,
91 const Sequence
< ::rtl::OUString
>& _rServiceNames
94 // XSingleServiceFactory
95 virtual Reference
< XInterface
> SAL_CALL
createInstance( ) throw (Exception
, RuntimeException
);
96 virtual Reference
< XInterface
> SAL_CALL
createInstanceWithArguments( const Sequence
< Any
>& aArguments
) throw (Exception
, RuntimeException
);
99 virtual ::rtl::OUString SAL_CALL
getImplementationName( ) throw (RuntimeException
);
100 virtual ::sal_Bool SAL_CALL
supportsService( const ::rtl::OUString
& ServiceName
) throw (RuntimeException
);
101 virtual Sequence
< ::rtl::OUString
> SAL_CALL
getSupportedServiceNames( ) throw (RuntimeException
);
104 virtual ~SfxModelFactory();
107 Reference
< XInterface
> impl_createInstance( const sal_uInt64 _nCreationFlags
) const;
110 const Reference
< XMultiServiceFactory
> m_xServiceFactory
;
111 const ::rtl::OUString m_sImplementationName
;
112 const Sequence
< ::rtl::OUString
> m_aServiceNames
;
113 const SfxModelFactoryFunc m_pComponentFactoryFunc
;
116 //====================================================================
117 //= SfxModelFactory - implementation
118 //====================================================================
119 //--------------------------------------------------------------------
120 SfxModelFactory::SfxModelFactory( const Reference
< XMultiServiceFactory
>& _rxServiceFactory
,
121 const ::rtl::OUString
& _rImplementationName
, const SfxModelFactoryFunc _pComponentFactoryFunc
,
122 const Sequence
< ::rtl::OUString
>& _rServiceNames
)
123 :m_xServiceFactory( _rxServiceFactory
)
124 ,m_sImplementationName( _rImplementationName
)
125 ,m_aServiceNames( _rServiceNames
)
126 ,m_pComponentFactoryFunc( _pComponentFactoryFunc
)
130 //--------------------------------------------------------------------
131 SfxModelFactory::~SfxModelFactory()
135 //--------------------------------------------------------------------
136 Reference
< XInterface
> SfxModelFactory::impl_createInstance( const sal_uInt64 _nCreationFlags
) const
138 return (*m_pComponentFactoryFunc
)( m_xServiceFactory
, _nCreationFlags
);
141 //--------------------------------------------------------------------
142 Reference
< XInterface
> SAL_CALL
SfxModelFactory::createInstance( ) throw (Exception
, RuntimeException
)
144 return createInstanceWithArguments( Sequence
< Any
>() );
147 //--------------------------------------------------------------------
150 struct IsSpecialArgument
: public ::std::unary_function
< Any
, bool >
152 static bool isSpecialArgumentName( const ::rtl::OUString
& _rValueName
)
154 return _rValueName
.equalsAscii( "EmbeddedObject" )
155 || _rValueName
.equalsAscii( "EmbeddedScriptSupport" );
158 bool operator()( const Any
& _rArgument
) const
160 NamedValue aNamedValue
;
161 if ( ( _rArgument
>>= aNamedValue
) && isSpecialArgumentName( aNamedValue
.Name
) )
163 PropertyValue aPropertyValue
;
164 if ( ( _rArgument
>>= aPropertyValue
) && isSpecialArgumentName( aPropertyValue
.Name
) )
171 //--------------------------------------------------------------------
172 Reference
< XInterface
> SAL_CALL
SfxModelFactory::createInstanceWithArguments( const Sequence
< Any
>& _rArguments
) throw (Exception
, RuntimeException
)
174 ::comphelper::NamedValueCollection
aArgs( _rArguments
);
175 const sal_Bool bEmbeddedObject
= aArgs
.getOrDefault( "EmbeddedObject", sal_False
);
176 const sal_Bool bScriptSupport
= aArgs
.getOrDefault( "EmbeddedScriptSupport", sal_True
);
178 sal_uInt64 nCreationFlags
=
179 ( bEmbeddedObject
? SFXMODEL_EMBEDDED_OBJECT
: 0 )
180 | ( bScriptSupport
? 0 : SFXMODEL_DISABLE_EMBEDDED_SCRIPTS
);
182 Reference
< XInterface
> xInstance( impl_createInstance( nCreationFlags
) );
184 // to mimic the bahaviour of the default factory's createInstanceWithArguments, we initialize
185 // the object with the given arguments, stripped by the two special ones
186 Sequence
< Any
> aStrippedArguments( _rArguments
.getLength() );
187 Any
* pStrippedArgs
= aStrippedArguments
.getArray();
188 Any
* pStrippedArgsEnd
= ::std::remove_copy_if(
189 _rArguments
.getConstArray(),
190 _rArguments
.getConstArray() + _rArguments
.getLength(),
194 aStrippedArguments
.realloc( pStrippedArgsEnd
- pStrippedArgs
);
196 if ( aStrippedArguments
.getLength() )
198 Reference
< XInitialization
> xModelInit( xInstance
, UNO_QUERY
);
199 OSL_ENSURE( xModelInit
.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" );
200 if ( xModelInit
.is() )
201 xModelInit
->initialize( aStrippedArguments
);
207 //--------------------------------------------------------------------
208 ::rtl::OUString SAL_CALL
SfxModelFactory::getImplementationName( ) throw (RuntimeException
)
210 return m_sImplementationName
;
213 //--------------------------------------------------------------------
214 ::sal_Bool SAL_CALL
SfxModelFactory::supportsService( const ::rtl::OUString
& _rServiceName
) throw (RuntimeException
)
217 m_aServiceNames
.getConstArray(),
218 m_aServiceNames
.getConstArray() + m_aServiceNames
.getLength(),
220 ) != m_aServiceNames
.getConstArray() + m_aServiceNames
.getLength();
223 //--------------------------------------------------------------------
224 Sequence
< ::rtl::OUString
> SAL_CALL
SfxModelFactory::getSupportedServiceNames( ) throw (RuntimeException
)
226 return m_aServiceNames
;
229 //--------------------------------------------------------------------
230 Reference
< XSingleServiceFactory
> createSfxModelFactory( const Reference
< XMultiServiceFactory
>& _rxServiceFactory
,
231 const ::rtl::OUString
& _rImplementationName
, const SfxModelFactoryFunc _pComponentFactoryFunc
,
232 const Sequence
< ::rtl::OUString
>& _rServiceNames
)
234 return new SfxModelFactory( _rxServiceFactory
, _rImplementationName
, _pComponentFactoryFunc
, _rServiceNames
);
237 //........................................................................
239 //........................................................................