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: xfactory.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_embeddedobj.hxx"
33 #include <com/sun/star/embed/ElementModes.hpp>
34 #include <com/sun/star/embed/EntryInitModes.hpp>
35 #include <com/sun/star/document/XTypeDetection.hpp>
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <com/sun/star/container/XNameAccess.hpp>
40 #include <rtl/logfile.hxx>
43 #include "xfactory.hxx"
44 #include "commonembobj.hxx"
45 #include "specialobject.hxx"
46 #include "oleembobj.hxx"
49 using namespace ::com::sun::star
;
51 //-------------------------------------------------------------------------
52 uno::Sequence
< ::rtl::OUString
> SAL_CALL
OOoEmbeddedObjectFactory::impl_staticGetSupportedServiceNames()
54 uno::Sequence
< ::rtl::OUString
> aRet(2);
55 aRet
[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.OOoEmbeddedObjectFactory");
56 aRet
[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.OOoEmbeddedObjectFactory");
60 //-------------------------------------------------------------------------
61 ::rtl::OUString SAL_CALL
OOoEmbeddedObjectFactory::impl_staticGetImplementationName()
63 return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.OOoEmbeddedObjectFactory");
66 //-------------------------------------------------------------------------
67 uno::Reference
< uno::XInterface
> SAL_CALL
OOoEmbeddedObjectFactory::impl_staticCreateSelfInstance(
68 const uno::Reference
< lang::XMultiServiceFactory
>& xServiceManager
)
70 return uno::Reference
< uno::XInterface
>( *new OOoEmbeddedObjectFactory( xServiceManager
) );
73 //-------------------------------------------------------------------------
74 uno::Reference
< uno::XInterface
> SAL_CALL
OOoEmbeddedObjectFactory::createInstanceInitFromEntry(
75 const uno::Reference
< embed::XStorage
>& xStorage
,
76 const ::rtl::OUString
& sEntName
,
77 const uno::Sequence
< beans::PropertyValue
>& aMediaDescr
,
78 const uno::Sequence
< beans::PropertyValue
>& lObjArgs
)
79 throw ( lang::IllegalArgumentException
,
80 container::NoSuchElementException
,
83 uno::RuntimeException
)
85 RTL_LOGFILE_CONTEXT( aLog
, "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceInitFromEntry" );
88 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
89 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
92 if ( !sEntName
.getLength() )
93 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
94 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
97 uno::Reference
< container::XNameAccess
> xNameAccess( xStorage
, uno::UNO_QUERY
);
98 if ( !xNameAccess
.is() )
99 throw uno::RuntimeException(); //TODO
101 // detect entry existence
102 if ( !xNameAccess
->hasByName( sEntName
) )
103 throw container::NoSuchElementException();
105 uno::Reference
< uno::XInterface
> xResult
;
106 if ( xStorage
->isStorageElement( sEntName
) )
108 // the object must be based on storage
109 uno::Reference
< embed::XStorage
> xSubStorage
=
110 xStorage
->openStorageElement( sEntName
, embed::ElementModes::READ
);
112 uno::Reference
< beans::XPropertySet
> xPropSet( xSubStorage
, uno::UNO_QUERY
);
113 if ( !xPropSet
.is() )
114 throw uno::RuntimeException();
116 ::rtl::OUString aMediaType
;
118 uno::Any aAny
= xPropSet
->getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) );
121 catch ( uno::Exception
& )
126 uno::Reference
< lang::XComponent
> xComp( xSubStorage
, uno::UNO_QUERY
);
130 catch ( uno::Exception
& )
133 xSubStorage
= uno::Reference
< embed::XStorage
>();
136 ::rtl::OUString aDocServiceName
= m_aConfigHelper
.GetDocumentServiceFromMediaType( aMediaType
);
137 if ( !aDocServiceName
.getLength() )
139 // only own document can be based on storage
140 // in case it is not possible to find related
141 // document service name the storage entry is invalid
143 throw io::IOException(); // unexpected mimetype of the storage
147 uno::Sequence
< beans::NamedValue
> aObject
= m_aConfigHelper
.GetObjectPropsByMediaType( aMediaType
);
148 if ( !aObject
.getLength() )
149 throw io::IOException(); // unexpected mimetype of the storage
151 xResult
= uno::Reference
< uno::XInterface
>(
152 static_cast< ::cppu::OWeakObject
* > ( new OCommonEmbeddedObject(
156 GetClassIDFromServName( aDocServiceName
),
157 GetClassNameFromServName( aDocServiceName
),
164 // the object must be OOo embedded object, if it is not an exception must be thrown
165 throw io::IOException(); // TODO:
168 uno::Reference
< embed::XEmbedPersist
> xPersist( xResult
, uno::UNO_QUERY
);
170 if ( !xPersist
.is() )
171 throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
173 xPersist
->setPersistentEntry( xStorage
,
175 embed::EntryInitModes::DEFAULT_INIT
,
182 //-------------------------------------------------------------------------
183 uno::Reference
< uno::XInterface
> SAL_CALL
OOoEmbeddedObjectFactory::createInstanceInitFromMediaDescriptor(
184 const uno::Reference
< embed::XStorage
>& xStorage
,
185 const ::rtl::OUString
& sEntName
,
186 const uno::Sequence
< beans::PropertyValue
>& aMediaDescr
,
187 const uno::Sequence
< beans::PropertyValue
>& lObjArgs
)
188 throw ( lang::IllegalArgumentException
,
191 uno::RuntimeException
)
193 RTL_LOGFILE_CONTEXT( aLog
, "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceInitFromMediaDescriptor" );
195 if ( !xStorage
.is() )
196 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
197 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
200 if ( !sEntName
.getLength() )
201 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
202 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
205 uno::Sequence
< beans::PropertyValue
> aTempMedDescr( aMediaDescr
);
207 // check if there is FilterName
208 ::rtl::OUString aFilterName
= m_aConfigHelper
.UpdateMediaDescriptorWithFilterName( aTempMedDescr
, sal_False
);
210 uno::Reference
< uno::XInterface
> xResult
;
212 // find document service name
213 if ( aFilterName
.getLength() )
215 uno::Sequence
< beans::NamedValue
> aObject
= m_aConfigHelper
.GetObjectPropsByFilter( aFilterName
);
216 if ( !aObject
.getLength() )
217 throw io::IOException(); // unexpected mimetype of the storage
220 xResult
= uno::Reference
< uno::XInterface
>(
221 static_cast< ::cppu::OWeakObject
* > ( new OCommonEmbeddedObject(
228 // the object must be OOo embedded object, if it is not an exception must be thrown
229 throw io::IOException(); // TODO:
232 uno::Reference
< embed::XEmbedPersist
> xPersist( xResult
, uno::UNO_QUERY
);
234 if ( !xPersist
.is() )
235 throw uno::RuntimeException(); // TODO: the interface must be supported ( what about applets? )
237 xPersist
->setPersistentEntry( xStorage
,
239 embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT
,
246 //-------------------------------------------------------------------------
247 uno::Reference
< uno::XInterface
> SAL_CALL
OOoEmbeddedObjectFactory::createInstanceInitNew(
248 const uno::Sequence
< sal_Int8
>& aClassID
,
249 const ::rtl::OUString
& /*aClassName*/,
250 const uno::Reference
< embed::XStorage
>& xStorage
,
251 const ::rtl::OUString
& sEntName
,
252 const uno::Sequence
< beans::PropertyValue
>& lObjArgs
)
253 throw ( lang::IllegalArgumentException
,
256 uno::RuntimeException
)
258 RTL_LOGFILE_CONTEXT( aLog
, "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceInitNew" );
260 uno::Reference
< uno::XInterface
> xResult
;
262 if ( !xStorage
.is() )
263 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
264 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
267 if ( !sEntName
.getLength() )
268 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
269 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
272 uno::Sequence
< beans::NamedValue
> aObject
= m_aConfigHelper
.GetObjectPropsByClassID( aClassID
);
273 if ( !aObject
.getLength() )
274 throw io::IOException(); // unexpected mimetype of the storage
276 xResult
= uno::Reference
< uno::XInterface
>(
277 static_cast< ::cppu::OWeakObject
* > ( new OCommonEmbeddedObject(
283 uno::Reference
< embed::XEmbedPersist
> xPersist( xResult
, uno::UNO_QUERY
);
285 if ( !xPersist
.is() )
286 throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
288 xPersist
->setPersistentEntry( xStorage
,
290 embed::EntryInitModes::TRUNCATE_INIT
,
291 uno::Sequence
< beans::PropertyValue
>(),
297 //-------------------------------------------------------------------------
298 uno::Reference
< uno::XInterface
> SAL_CALL
OOoEmbeddedObjectFactory::createInstanceUserInit(
299 const uno::Sequence
< sal_Int8
>& aClassID
,
300 const ::rtl::OUString
& /*aClassName*/,
301 const uno::Reference
< embed::XStorage
>& xStorage
,
302 const ::rtl::OUString
& sEntName
,
303 sal_Int32 nEntryConnectionMode
,
304 const uno::Sequence
< beans::PropertyValue
>& lArguments
,
305 const uno::Sequence
< beans::PropertyValue
>& lObjArgs
)
306 throw ( lang::IllegalArgumentException
,
309 uno::RuntimeException
)
311 RTL_LOGFILE_CONTEXT( aLog
, "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceUserInit" );
313 // the initialization is completelly controlled by user
314 if ( !xStorage
.is() )
315 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
316 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
319 if ( !sEntName
.getLength() )
320 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
321 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
324 uno::Sequence
< beans::NamedValue
> aObject
= m_aConfigHelper
.GetObjectPropsByClassID( aClassID
);
325 if ( !aObject
.getLength() )
326 throw io::IOException(); // unexpected mimetype of the storage
328 uno::Sequence
< beans::PropertyValue
> aTempMedDescr( lArguments
);
329 if ( nEntryConnectionMode
== embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT
)
331 ::rtl::OUString aFilterName
= m_aConfigHelper
.UpdateMediaDescriptorWithFilterName( aTempMedDescr
, aObject
);
332 if ( !aFilterName
.getLength() )
333 // the object must be OOo embedded object, if it is not an exception must be thrown
334 throw io::IOException(); // TODO:
337 uno::Reference
< uno::XInterface
> xResult
= uno::Reference
< uno::XInterface
> (
338 static_cast< ::cppu::OWeakObject
* > ( new OCommonEmbeddedObject(
343 uno::Reference
< embed::XEmbedPersist
> xPersist( xResult
, uno::UNO_QUERY
);
346 xPersist
->setPersistentEntry( xStorage
,
348 nEntryConnectionMode
,
354 throw uno::RuntimeException(); // TODO:
360 //-------------------------------------------------------------------------
361 uno::Reference
< uno::XInterface
> SAL_CALL
OOoEmbeddedObjectFactory::createInstanceLink(
362 const uno::Reference
< embed::XStorage
>& /*xStorage*/,
363 const ::rtl::OUString
& /*sEntName*/,
364 const uno::Sequence
< beans::PropertyValue
>& aMediaDescr
,
365 const uno::Sequence
< beans::PropertyValue
>& lObjArgs
)
366 throw ( lang::IllegalArgumentException
,
369 uno::RuntimeException
)
371 RTL_LOGFILE_CONTEXT( aLog
, "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceLink" );
373 uno::Reference
< uno::XInterface
> xResult
;
375 uno::Sequence
< beans::PropertyValue
> aTempMedDescr( aMediaDescr
);
377 // check if there is URL, URL must exist
378 ::rtl::OUString aURL
;
379 for ( sal_Int32 nInd
= 0; nInd
< aTempMedDescr
.getLength(); nInd
++ )
380 if ( aTempMedDescr
[nInd
].Name
.equalsAscii( "URL" ) )
381 aTempMedDescr
[nInd
].Value
>>= aURL
;
383 if ( !aURL
.getLength() )
384 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No URL for the link is provided!\n" ),
385 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
388 ::rtl::OUString aFilterName
= m_aConfigHelper
.UpdateMediaDescriptorWithFilterName( aTempMedDescr
, sal_False
);
390 if ( aFilterName
.getLength() )
392 uno::Sequence
< beans::NamedValue
> aObject
= m_aConfigHelper
.GetObjectPropsByFilter( aFilterName
);
393 if ( !aObject
.getLength() )
394 throw io::IOException(); // unexpected mimetype of the storage
397 xResult
= uno::Reference
< uno::XInterface
>(
398 static_cast< ::cppu::OWeakObject
* > ( new OCommonEmbeddedObject(
407 // the object must be OOo embedded object, if it is not an exception must be thrown
408 throw io::IOException(); // TODO:
414 //-------------------------------------------------------------------------
415 uno::Reference
< uno::XInterface
> SAL_CALL
OOoEmbeddedObjectFactory::createInstanceLinkUserInit(
416 const uno::Sequence
< sal_Int8
>& aClassID
,
417 const ::rtl::OUString
& /*aClassName*/,
418 const uno::Reference
< embed::XStorage
>& xStorage
,
419 const ::rtl::OUString
& sEntName
,
420 const uno::Sequence
< beans::PropertyValue
>& lArguments
,
421 const uno::Sequence
< beans::PropertyValue
>& lObjArgs
)
422 throw ( lang::IllegalArgumentException
,
425 uno::RuntimeException
)
427 RTL_LOGFILE_CONTEXT( aLog
, "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceLinkUserInit" );
429 uno::Reference
< uno::XInterface
> xResult
;
431 // the initialization is completelly controlled by user
432 if ( !xStorage
.is() )
433 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
434 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
437 if ( !sEntName
.getLength() )
438 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
439 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
442 uno::Sequence
< beans::PropertyValue
> aTempMedDescr( lArguments
);
444 ::rtl::OUString aURL
;
445 for ( sal_Int32 nInd
= 0; nInd
< aTempMedDescr
.getLength(); nInd
++ )
446 if ( aTempMedDescr
[nInd
].Name
.equalsAscii( "URL" ) )
447 aTempMedDescr
[nInd
].Value
>>= aURL
;
449 if ( !aURL
.getLength() )
450 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No URL for the link is provided!\n" ),
451 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
454 uno::Sequence
< beans::NamedValue
> aObject
= m_aConfigHelper
.GetObjectPropsByClassID( aClassID
);
455 if ( !aObject
.getLength() )
456 throw io::IOException(); // unexpected mimetype of the storage
458 ::rtl::OUString aFilterName
= m_aConfigHelper
.UpdateMediaDescriptorWithFilterName( aTempMedDescr
, aObject
);
460 if ( aFilterName
.getLength() )
463 xResult
= uno::Reference
< uno::XInterface
>(
464 static_cast< ::cppu::OWeakObject
* > ( new OCommonEmbeddedObject(
473 // the object must be OOo embedded object, if it is not an exception must be thrown
474 throw io::IOException(); // TODO:
480 //-------------------------------------------------------------------------
481 ::rtl::OUString SAL_CALL
OOoEmbeddedObjectFactory::getImplementationName()
482 throw ( uno::RuntimeException
)
484 return impl_staticGetImplementationName();
487 //-------------------------------------------------------------------------
488 sal_Bool SAL_CALL
OOoEmbeddedObjectFactory::supportsService( const ::rtl::OUString
& ServiceName
)
489 throw ( uno::RuntimeException
)
491 uno::Sequence
< ::rtl::OUString
> aSeq
= impl_staticGetSupportedServiceNames();
493 for ( sal_Int32 nInd
= 0; nInd
< aSeq
.getLength(); nInd
++ )
494 if ( ServiceName
.compareTo( aSeq
[nInd
] ) == 0 )
500 //-------------------------------------------------------------------------
501 uno::Sequence
< ::rtl::OUString
> SAL_CALL
OOoEmbeddedObjectFactory::getSupportedServiceNames()
502 throw ( uno::RuntimeException
)
504 return impl_staticGetSupportedServiceNames();
507 //-------------------------------------------------------------------------
508 uno::Sequence
< ::rtl::OUString
> SAL_CALL
OOoSpecialEmbeddedObjectFactory::impl_staticGetSupportedServiceNames()
510 uno::Sequence
< ::rtl::OUString
> aRet(2);
511 aRet
[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.OOoSpecialEmbeddedObjectFactory");
512 aRet
[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory");
516 //-------------------------------------------------------------------------
517 ::rtl::OUString SAL_CALL
OOoSpecialEmbeddedObjectFactory::impl_staticGetImplementationName()
519 return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory");
522 //-------------------------------------------------------------------------
523 uno::Reference
< uno::XInterface
> SAL_CALL
OOoSpecialEmbeddedObjectFactory::impl_staticCreateSelfInstance(
524 const uno::Reference
< lang::XMultiServiceFactory
>& xServiceManager
)
526 return uno::Reference
< uno::XInterface
>( *new OOoSpecialEmbeddedObjectFactory( xServiceManager
) );
529 //-------------------------------------------------------------------------
530 uno::Reference
< uno::XInterface
> SAL_CALL
OOoSpecialEmbeddedObjectFactory::createInstanceUserInit(
531 const uno::Sequence
< sal_Int8
>& aClassID
,
532 const ::rtl::OUString
& /*aClassName*/,
533 const uno::Reference
< embed::XStorage
>& /*xStorage*/,
534 const ::rtl::OUString
& /*sEntName*/,
535 sal_Int32
/*nEntryConnectionMode*/,
536 const uno::Sequence
< beans::PropertyValue
>& /*lArguments*/,
537 const uno::Sequence
< beans::PropertyValue
>& /*lObjArgs*/ )
538 throw ( lang::IllegalArgumentException
,
541 uno::RuntimeException
)
543 uno::Sequence
< beans::NamedValue
> aObject
= m_aConfigHelper
.GetObjectPropsByClassID( aClassID
);
544 if ( !aObject
.getLength() )
545 throw io::IOException(); // unexpected mimetype of the storage
547 uno::Reference
< uno::XInterface
> xResult(
548 static_cast< ::cppu::OWeakObject
* > ( new OSpecialEmbeddedObject(
555 //-------------------------------------------------------------------------
556 ::rtl::OUString SAL_CALL
OOoSpecialEmbeddedObjectFactory::getImplementationName()
557 throw ( uno::RuntimeException
)
559 return impl_staticGetImplementationName();
562 //-------------------------------------------------------------------------
563 sal_Bool SAL_CALL
OOoSpecialEmbeddedObjectFactory::supportsService( const ::rtl::OUString
& ServiceName
)
564 throw ( uno::RuntimeException
)
566 uno::Sequence
< ::rtl::OUString
> aSeq
= impl_staticGetSupportedServiceNames();
568 for ( sal_Int32 nInd
= 0; nInd
< aSeq
.getLength(); nInd
++ )
569 if ( ServiceName
.compareTo( aSeq
[nInd
] ) == 0 )
575 //-------------------------------------------------------------------------
576 uno::Sequence
< ::rtl::OUString
> SAL_CALL
OOoSpecialEmbeddedObjectFactory::getSupportedServiceNames()
577 throw ( uno::RuntimeException
)
579 return impl_staticGetSupportedServiceNames();