update dev300-m58
[ooovba.git] / svx / source / xml / xmleohlp.cxx
blob47056a247f7bb487c37c84452fe42ea9ba6ed4cf
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: xmleohlp.cxx,v $
10 * $Revision: 1.27 $
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_svx.hxx"
34 #include <stdio.h>
35 #include <com/sun/star/io/XStream.hpp>
36 #include <com/sun/star/embed/XTransactedObject.hpp>
37 #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
38 #include <com/sun/star/embed/XEmbedObjectFactory.hpp>
39 #include <com/sun/star/embed/ElementModes.hpp>
40 #include <com/sun/star/embed/XEmbeddedObject.hpp>
41 #ifndef _COM_SUN_STAR_EMBED_XEMBED_PERSIST_HPP_
42 #include <com/sun/star/embed/XEmbedPersist.hpp>
43 #endif
44 #include <com/sun/star/embed/EntryInitModes.hpp>
45 #include <com/sun/star/embed/EmbedStates.hpp>
46 #include <com/sun/star/embed/Aspects.hpp>
47 #include <com/sun/star/beans/XPropertySet.hpp>
48 #include <tools/debug.hxx>
49 #include <unotools/streamwrap.hxx>
50 #include <unotools/tempfile.hxx>
52 #include <svtools/embedhlp.hxx>
53 #include <unotools/ucbstreamhelper.hxx>
54 #include <comphelper/processfactory.hxx>
55 #include <comphelper/storagehelper.hxx>
56 #include <comphelper/embeddedobjectcontainer.hxx>
58 #ifndef _SO_CLSIDS_HXX
59 #include <sot/clsids.hxx>
60 #endif
61 #include <map>
62 #include "xmleohlp.hxx"
64 // -----------
65 // - Defines -
66 // -----------
68 using namespace ::osl;
69 using namespace ::cppu;
70 using namespace ::utl;
71 using namespace ::com::sun::star;
72 using namespace ::com::sun::star::document;
73 using namespace ::com::sun::star::uno;
74 using namespace ::com::sun::star::container;
75 using namespace ::com::sun::star::io;
76 using namespace ::com::sun::star::lang;
78 #define XML_CONTAINERSTORAGE_NAME_60 "Pictures"
79 #define XML_CONTAINERSTORAGE_NAME "ObjectReplacements"
80 #define XML_EMBEDDEDOBJECT_URL_BASE "vnd.sun.star.EmbeddedObject:"
81 #define XML_EMBEDDEDOBJECTGRAPHIC_URL_BASE "vnd.sun.star.GraphicObject:"
83 // -----------------------------------------------------------------------------
85 // -----------------------------------------------------------------------------
87 class OutputStorageWrapper_Impl : public ::cppu::WeakImplHelper1<XOutputStream>
89 ::osl::Mutex maMutex;
90 Reference < XOutputStream > xOut;
91 TempFile aTempFile;
92 sal_Bool bStreamClosed : 1;
93 SvStream* pStream;
95 public:
96 OutputStorageWrapper_Impl();
97 virtual ~OutputStorageWrapper_Impl();
99 // stario::XOutputStream
100 virtual void SAL_CALL writeBytes(const Sequence< sal_Int8 >& aData) throw(NotConnectedException, BufferSizeExceededException, RuntimeException);
101 virtual void SAL_CALL flush() throw(NotConnectedException, BufferSizeExceededException, RuntimeException);
102 virtual void SAL_CALL closeOutput() throw(NotConnectedException, BufferSizeExceededException, RuntimeException);
104 SvStream* GetStream();
107 OutputStorageWrapper_Impl::OutputStorageWrapper_Impl()
108 : bStreamClosed( sal_False )
109 , pStream(0)
111 aTempFile.EnableKillingFile();
112 pStream = aTempFile.GetStream( STREAM_READWRITE );
113 xOut = new OOutputStreamWrapper( *pStream );
116 OutputStorageWrapper_Impl::~OutputStorageWrapper_Impl()
120 SvStream *OutputStorageWrapper_Impl::GetStream()
122 if( bStreamClosed )
123 return pStream;
124 return NULL;
127 void SAL_CALL OutputStorageWrapper_Impl::writeBytes(
128 const Sequence< sal_Int8 >& aData)
129 throw(NotConnectedException, BufferSizeExceededException, RuntimeException)
131 MutexGuard aGuard( maMutex );
132 xOut->writeBytes( aData );
135 void SAL_CALL OutputStorageWrapper_Impl::flush()
136 throw(NotConnectedException, BufferSizeExceededException, RuntimeException)
138 MutexGuard aGuard( maMutex );
139 xOut->flush();
142 void SAL_CALL OutputStorageWrapper_Impl::closeOutput()
143 throw(NotConnectedException, BufferSizeExceededException, RuntimeException)
145 MutexGuard aGuard( maMutex );
146 xOut->closeOutput();
147 bStreamClosed = sal_True;
150 // -----------------------------------------------------------------------------
152 struct OUStringLess
154 bool operator() ( const ::rtl::OUString& r1, const ::rtl::OUString& r2 ) const
156 return (r1 < r2) != sal_False;
160 // -----------------------------------------------------------------------------
162 // -----------------------------
163 // - SvXMLEmbeddedObjectHelper -
164 // -----------------------------
165 DBG_NAME(SvXMLEmbeddedObjectHelper)
166 SvXMLEmbeddedObjectHelper::SvXMLEmbeddedObjectHelper() :
167 WeakComponentImplHelper2< XEmbeddedObjectResolver, XNameAccess >( maMutex ),
168 maReplacementGraphicsContainerStorageName( RTL_CONSTASCII_USTRINGPARAM(XML_CONTAINERSTORAGE_NAME) ),
169 maReplacementGraphicsContainerStorageName60( RTL_CONSTASCII_USTRINGPARAM(XML_CONTAINERSTORAGE_NAME_60) ),
170 mpDocPersist( 0 ),
171 meCreateMode( EMBEDDEDOBJECTHELPER_MODE_READ ),
172 mpStreamMap( 0 )
174 DBG_CTOR(SvXMLEmbeddedObjectHelper,NULL);
177 SvXMLEmbeddedObjectHelper::SvXMLEmbeddedObjectHelper( ::comphelper::IEmbeddedHelper& rDocPersist, SvXMLEmbeddedObjectHelperMode eCreateMode ) :
178 WeakComponentImplHelper2< XEmbeddedObjectResolver, XNameAccess >( maMutex ),
179 maReplacementGraphicsContainerStorageName( RTL_CONSTASCII_USTRINGPARAM(XML_CONTAINERSTORAGE_NAME) ),
180 maReplacementGraphicsContainerStorageName60( RTL_CONSTASCII_USTRINGPARAM(XML_CONTAINERSTORAGE_NAME_60) ),
181 mpDocPersist( 0 ),
182 meCreateMode( EMBEDDEDOBJECTHELPER_MODE_READ ),
183 mpStreamMap( 0 )
185 DBG_CTOR(SvXMLEmbeddedObjectHelper,NULL);
186 Init( 0, rDocPersist, eCreateMode );
190 // -----------------------------------------------------------------------------
192 SvXMLEmbeddedObjectHelper::~SvXMLEmbeddedObjectHelper()
194 DBG_DTOR(SvXMLEmbeddedObjectHelper,NULL);
195 if( mpStreamMap )
197 SvXMLEmbeddedObjectHelper_Impl::iterator aIter = mpStreamMap->begin();
198 SvXMLEmbeddedObjectHelper_Impl::iterator aEnd = mpStreamMap->end();
199 for( ; aIter != aEnd; aIter++ )
201 if( aIter->second )
203 aIter->second->release();
204 aIter->second = 0;
210 // -----------------------------------------------------------------------------
212 void SAL_CALL SvXMLEmbeddedObjectHelper::disposing()
214 Flush();
217 // -----------------------------------------------------------------------------
219 sal_Bool SvXMLEmbeddedObjectHelper::ImplGetStorageNames(
220 const ::rtl::OUString& rURLStr,
221 ::rtl::OUString& rContainerStorageName,
222 ::rtl::OUString& rObjectStorageName,
223 sal_Bool bInternalToExternal,
224 sal_Bool *pGraphicRepl,
225 sal_Bool *pOasisFormat ) const
227 // internal URL: vnd.sun.star.EmbeddedObject:<object-name>
228 // or: vnd.sun.star.EmbeddedObject:<path>/<object-name>
229 // internal replacement images:
230 // vnd.sun.star.EmbeddedObjectGraphic:<object-name>
231 // or: vnd.sun.star.EmbeddedObjectGraphic:<path>/<object-name>
232 // external URL: ./<path>/<object-name>
233 // or: <path>/<object-name>
234 // or: <object-name>
235 // currently, path may only consist of a single directory name
236 // it is also possible to have additional arguments at the end of URL: <main URL>[?<name>=<value>[,<name>=<value>]*]
238 if( pGraphicRepl )
239 *pGraphicRepl = sal_False;
241 if( pOasisFormat )
242 *pOasisFormat = sal_True; // the default value
244 if( !rURLStr.getLength() )
245 return sal_False;
247 // get rid of arguments
248 sal_Int32 nPos = rURLStr.indexOf( '?' );
249 ::rtl::OUString aURLNoPar;
250 if ( nPos == -1 )
251 aURLNoPar = rURLStr;
252 else
254 aURLNoPar = rURLStr.copy( 0, nPos );
256 // check the arguments
257 nPos++;
258 while( nPos >= 0 && nPos < rURLStr.getLength() )
260 ::rtl::OUString aToken = rURLStr.getToken( 0, ',', nPos );
261 if ( aToken.equalsIgnoreAsciiCase( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "oasis=false" ) ) ) )
263 if ( pOasisFormat )
264 *pOasisFormat = sal_False;
265 break;
267 else
269 DBG_ASSERT( sal_False, "invalid arguments was found in URL!" );
274 if( bInternalToExternal )
276 nPos = aURLNoPar.indexOf( ':' );
277 if( -1 == nPos )
278 return sal_False;
279 sal_Bool bObjUrl =
280 0 == aURLNoPar.compareToAscii( XML_EMBEDDEDOBJECT_URL_BASE,
281 sizeof( XML_EMBEDDEDOBJECT_URL_BASE ) -1 );
282 sal_Bool bGrUrl = !bObjUrl &&
283 0 == aURLNoPar.compareToAscii( XML_EMBEDDEDOBJECTGRAPHIC_URL_BASE,
284 sizeof( XML_EMBEDDEDOBJECTGRAPHIC_URL_BASE ) -1 );
285 if( !(bObjUrl || bGrUrl) )
286 return sal_False;
288 sal_Int32 nPathStart = nPos + 1;
289 nPos = aURLNoPar.lastIndexOf( '/' );
290 if( -1 == nPos )
292 rContainerStorageName = ::rtl::OUString();
293 rObjectStorageName = aURLNoPar.copy( nPathStart );
295 else if( nPos > nPathStart )
297 rContainerStorageName = aURLNoPar.copy( nPathStart, nPos-nPathStart);
298 rObjectStorageName = aURLNoPar.copy( nPos+1 );
300 else
301 return sal_False;
303 if( bGrUrl )
305 sal_Bool bOASIS = mxRootStorage.is() &&
306 ( SotStorage::GetVersion( mxRootStorage ) > SOFFICE_FILEFORMAT_60 );
307 rContainerStorageName = bOASIS
308 ? maReplacementGraphicsContainerStorageName
309 : maReplacementGraphicsContainerStorageName60;
311 if( pGraphicRepl )
312 *pGraphicRepl = sal_True;
317 else
319 DBG_ASSERT( '#' != aURLNoPar[0], "invalid object URL" );
321 sal_Int32 _nPos = aURLNoPar.lastIndexOf( '/' );
322 if( -1 == _nPos )
324 rContainerStorageName = ::rtl::OUString();
325 rObjectStorageName = aURLNoPar;
327 else
329 sal_Int32 nPathStart = 0;
330 if( 0 == aURLNoPar.compareToAscii( "./", 2 ) )
331 nPathStart = 2;
332 if( _nPos >= nPathStart )
333 rContainerStorageName = aURLNoPar.copy( nPathStart, _nPos-nPathStart);
334 rObjectStorageName = aURLNoPar.copy( _nPos+1 );
338 if( -1 != rContainerStorageName.indexOf( '/' ) )
340 DBG_ERROR( "SvXMLEmbeddedObjectHelper: invalid path name" );
341 return sal_False;
344 return sal_True;
348 // -----------------------------------------------------------------------------
350 uno::Reference < embed::XStorage > SvXMLEmbeddedObjectHelper::ImplGetContainerStorage(
351 const ::rtl::OUString& rStorageName )
353 DBG_ASSERT( -1 == rStorageName.indexOf( '/' ) &&
354 -1 == rStorageName.indexOf( '\\' ),
355 "nested embedded storages aren't supported" );
356 if( !mxContainerStorage.is() ||
357 ( rStorageName != maCurContainerStorageName ) )
359 if( mxContainerStorage.is() &&
360 maCurContainerStorageName.getLength() > 0 &&
361 EMBEDDEDOBJECTHELPER_MODE_WRITE == meCreateMode )
363 uno::Reference < embed::XTransactedObject > xTrans( mxContainerStorage, uno::UNO_QUERY );
364 if ( xTrans.is() )
365 xTrans->commit();
368 if( rStorageName.getLength() > 0 && mxRootStorage.is() )
370 sal_Int32 nMode = EMBEDDEDOBJECTHELPER_MODE_WRITE == meCreateMode
371 ? ::embed::ElementModes::READWRITE
372 : ::embed::ElementModes::READ;
373 mxContainerStorage = mxRootStorage->openStorageElement( rStorageName,
374 nMode );
376 else
378 mxContainerStorage = mxRootStorage;
380 maCurContainerStorageName = rStorageName;
383 return mxContainerStorage;
386 // -----------------------------------------------------------------------------
388 sal_Bool SvXMLEmbeddedObjectHelper::ImplReadObject(
389 const ::rtl::OUString& rContainerStorageName,
390 ::rtl::OUString& rObjName,
391 const SvGlobalName *pClassId,
392 SvStream* pTemp )
394 (void)pClassId;
396 uno::Reference < embed::XStorage > xDocStor( mpDocPersist->getStorage() );
397 uno::Reference < embed::XStorage > xCntnrStor( ImplGetContainerStorage( rContainerStorageName ) );
399 if( !xCntnrStor.is() && !pTemp )
400 return sal_False;
402 String aSrcObjName( rObjName );
403 comphelper::EmbeddedObjectContainer& rContainer = mpDocPersist->getEmbeddedObjectContainer();
405 // Is the object name unique?
406 // if the object is already instantiated by GetEmbeddedObject
407 // that means that the duplication is being loaded
408 sal_Bool bDuplicate = rContainer.HasInstantiatedEmbeddedObject( rObjName );
409 DBG_ASSERT( !bDuplicate, "An object in the document is referenced twice!" );
411 if( xDocStor != xCntnrStor || pTemp || bDuplicate )
413 // TODO/LATER: make this alltogether a method in the EmbeddedObjectContainer
415 // create a unique name for the duplicate object
416 if( bDuplicate )
417 rObjName = rContainer.CreateUniqueObjectName();
419 if( pTemp )
423 pTemp->Seek( 0 );
424 uno::Reference < io::XStream > xStm = xDocStor->openStreamElement( rObjName,
425 embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
426 SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( xStm );
427 *pTemp >> *pStream;
428 delete pStream;
430 // TODO/LATER: what to do when other types of objects are based on substream persistence?
431 // This is an ole object
432 uno::Reference< beans::XPropertySet > xProps( xStm, uno::UNO_QUERY_THROW );
433 xProps->setPropertyValue(
434 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ),
435 uno::makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/vnd.sun.star.oleobject" ) ) ) );
437 xStm->getOutputStream()->closeOutput();
439 catch ( uno::Exception& )
441 return sal_False;
444 else
448 xCntnrStor->copyElementTo( aSrcObjName, xDocStor, rObjName );
450 catch ( uno::Exception& )
452 return sal_False;
457 // make object known to the container
458 // TODO/LATER: could be done a little bit more efficient!
459 ::rtl::OUString aName( rObjName );
461 // TODO/LATER: The provided pClassId is ignored for now.
462 // The stream contains OLE storage internally and this storage already has a class id specifying the
463 // server that was used to create the object. pClassId could be used to specify the server that should
464 // be used for the next opening, but this information seems to be out of the file format responsibility
465 // area.
466 rContainer.GetEmbeddedObject( aName );
468 return sal_True;
471 // -----------------------------------------------------------------------------
473 ::rtl::OUString SvXMLEmbeddedObjectHelper::ImplInsertEmbeddedObjectURL(
474 const ::rtl::OUString& rURLStr )
476 ::rtl::OUString sRetURL;
478 ::rtl::OUString aContainerStorageName, aObjectStorageName;
479 if( !ImplGetStorageNames( rURLStr, aContainerStorageName,
480 aObjectStorageName,
481 EMBEDDEDOBJECTHELPER_MODE_WRITE == meCreateMode ) )
482 return sRetURL;
484 if( EMBEDDEDOBJECTHELPER_MODE_READ == meCreateMode )
486 OutputStorageWrapper_Impl *pOut = 0;
487 SvXMLEmbeddedObjectHelper_Impl::iterator aIter;
489 if( mpStreamMap )
491 aIter = mpStreamMap->find( rURLStr );
492 if( aIter != mpStreamMap->end() && aIter->second )
493 pOut = aIter->second;
496 SvGlobalName aClassId, *pClassId = 0;
497 sal_Int32 nPos = aObjectStorageName.lastIndexOf( '!' );
498 if( -1 != nPos && aClassId.MakeId( aObjectStorageName.copy( nPos+1 ) ) )
500 aObjectStorageName = aObjectStorageName.copy( 0, nPos );
501 pClassId = &aClassId;
504 ImplReadObject( aContainerStorageName, aObjectStorageName, pClassId, pOut ? pOut->GetStream() : 0 );
505 sRetURL = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(XML_EMBEDDEDOBJECT_URL_BASE) );
506 sRetURL += aObjectStorageName;
508 if( pOut )
510 mpStreamMap->erase( aIter );
511 pOut->release();
514 else
516 // Objects are written using ::comphelper::IEmbeddedHelper::SaveAs
517 sRetURL = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("./") );
518 if( aContainerStorageName.getLength() )
520 sRetURL += aContainerStorageName;
521 sRetURL += ::rtl::OUString( '/' );
523 sRetURL += aObjectStorageName;
526 return sRetURL;
529 // -----------------------------------------------------------------------------
531 uno::Reference< io::XInputStream > SvXMLEmbeddedObjectHelper::ImplGetReplacementImage(
532 const uno::Reference< embed::XEmbeddedObject >& xObj )
534 uno::Reference< io::XInputStream > xStream;
536 if( xObj.is() )
540 sal_Bool bSwitchBackToLoaded = sal_False;
541 sal_Int32 nCurState = xObj->getCurrentState();
542 if ( nCurState == embed::EmbedStates::LOADED || nCurState == embed::EmbedStates::RUNNING )
544 // means that the object is not active
545 // copy replacement image from old to new container
546 ::rtl::OUString aMediaType;
547 xStream = mpDocPersist->getEmbeddedObjectContainer().GetGraphicStream( xObj, &aMediaType );
550 if ( !xStream.is() )
552 // the image must be regenerated
553 // TODO/LATER: another aspect could be used
554 if ( nCurState == embed::EmbedStates::LOADED )
555 bSwitchBackToLoaded = sal_True;
557 ::rtl::OUString aMediaType;
558 xStream = svt::EmbeddedObjectRef::GetGraphicReplacementStream(
559 embed::Aspects::MSOLE_CONTENT,
560 xObj,
561 &aMediaType );
564 if ( bSwitchBackToLoaded )
565 // switch back to loaded state; that way we have a minimum cache confusion
566 xObj->changeState( embed::EmbedStates::LOADED );
568 catch( uno::Exception& )
572 return xStream;
575 // -----------------------------------------------------------------------------
577 void SvXMLEmbeddedObjectHelper::Init(
578 const uno::Reference < embed::XStorage >& rRootStorage,
579 ::comphelper::IEmbeddedHelper& rPersist,
580 SvXMLEmbeddedObjectHelperMode eCreateMode )
582 mxRootStorage = rRootStorage;
583 mpDocPersist = &rPersist;
584 meCreateMode = eCreateMode;
587 // -----------------------------------------------------------------------------
589 SvXMLEmbeddedObjectHelper* SvXMLEmbeddedObjectHelper::Create(
590 const uno::Reference < embed::XStorage >& rRootStorage,
591 ::comphelper::IEmbeddedHelper& rDocPersist,
592 SvXMLEmbeddedObjectHelperMode eCreateMode,
593 sal_Bool bDirect )
595 (void)bDirect;
597 SvXMLEmbeddedObjectHelper* pThis = new SvXMLEmbeddedObjectHelper;
599 pThis->acquire();
600 pThis->Init( rRootStorage, rDocPersist, eCreateMode );
602 return pThis;
605 SvXMLEmbeddedObjectHelper* SvXMLEmbeddedObjectHelper::Create(
606 ::comphelper::IEmbeddedHelper& rDocPersist,
607 SvXMLEmbeddedObjectHelperMode eCreateMode )
609 SvXMLEmbeddedObjectHelper* pThis = new SvXMLEmbeddedObjectHelper;
611 pThis->acquire();
612 pThis->Init( 0, rDocPersist, eCreateMode );
614 return pThis;
617 // -----------------------------------------------------------------------------
619 void SvXMLEmbeddedObjectHelper::Destroy(
620 SvXMLEmbeddedObjectHelper* pSvXMLEmbeddedObjectHelper )
622 if( pSvXMLEmbeddedObjectHelper )
624 pSvXMLEmbeddedObjectHelper->dispose();
625 pSvXMLEmbeddedObjectHelper->release();
629 // -----------------------------------------------------------------------------
631 void SvXMLEmbeddedObjectHelper::Flush()
633 if( mxTempStorage.is() )
635 Reference < XComponent > xComp( mxTempStorage, UNO_QUERY );
636 xComp->dispose();
640 // XGraphicObjectResolver: alien objects!
641 ::rtl::OUString SAL_CALL SvXMLEmbeddedObjectHelper::resolveEmbeddedObjectURL( const ::rtl::OUString& aURL )
642 throw(RuntimeException)
644 MutexGuard aGuard( maMutex );
646 return ImplInsertEmbeddedObjectURL( aURL );
649 // XNameAccess: alien objects!
650 Any SAL_CALL SvXMLEmbeddedObjectHelper::getByName(
651 const ::rtl::OUString& rURLStr )
652 throw (NoSuchElementException, WrappedTargetException, RuntimeException)
654 MutexGuard aGuard( maMutex );
655 Any aRet;
656 if( EMBEDDEDOBJECTHELPER_MODE_READ == meCreateMode )
658 Reference < XOutputStream > xStrm;
659 if( mpStreamMap )
661 SvXMLEmbeddedObjectHelper_Impl::iterator aIter =
662 mpStreamMap->find( rURLStr );
663 if( aIter != mpStreamMap->end() && aIter->second )
664 xStrm = aIter->second;
666 if( !xStrm.is() )
668 OutputStorageWrapper_Impl *pOut = new OutputStorageWrapper_Impl;
669 pOut->acquire();
670 if( !mpStreamMap )
671 mpStreamMap = new SvXMLEmbeddedObjectHelper_Impl;
672 (*mpStreamMap)[rURLStr] = pOut;
673 xStrm = pOut;
676 aRet <<= xStrm;
678 else
680 sal_Bool bGraphicRepl = sal_False;
681 sal_Bool bOasisFormat = sal_True;
682 Reference < XInputStream > xStrm;
683 ::rtl::OUString aContainerStorageName, aObjectStorageName;
684 if( ImplGetStorageNames( rURLStr, aContainerStorageName,
685 aObjectStorageName,
686 sal_True,
687 &bGraphicRepl,
688 &bOasisFormat ) )
692 comphelper::EmbeddedObjectContainer& rContainer =
693 mpDocPersist->getEmbeddedObjectContainer();
695 Reference < embed::XEmbeddedObject > xObj = rContainer.GetEmbeddedObject( aObjectStorageName );
696 DBG_ASSERT( xObj.is(), "Didn't get object" );
698 if( xObj.is() )
700 if( bGraphicRepl )
702 xStrm = ImplGetReplacementImage( xObj );
704 else
706 Reference < embed::XEmbedPersist > xPersist( xObj, UNO_QUERY );
707 if( xPersist.is() )
709 if( !mxTempStorage.is() )
710 mxTempStorage =
711 comphelper::OStorageHelper::GetTemporaryStorage();
712 Sequence < beans::PropertyValue > aDummy( 0 ), aEmbDescr( 1 );
713 aEmbDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StoreVisualReplacement" ) );
714 aEmbDescr[0].Value <<= (sal_Bool)(!bOasisFormat);
715 if ( !bOasisFormat )
717 ::rtl::OUString aMimeType;
718 uno::Reference< io::XInputStream > xGrInStream = ImplGetReplacementImage( xObj );
719 if ( xGrInStream.is() )
721 aEmbDescr.realloc( 2 );
722 aEmbDescr[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "VisualReplacement" ) );
723 aEmbDescr[1].Value <<= xGrInStream;
727 xPersist->storeToEntry( mxTempStorage, aObjectStorageName,
728 aDummy, aEmbDescr );
729 Reference < io::XStream > xStream =
730 mxTempStorage->openStreamElement(
731 aObjectStorageName,
732 embed::ElementModes::READ);
733 if( xStream.is() )
734 xStrm = xStream->getInputStream();
739 catch ( uno::Exception& )
744 aRet <<= xStrm;
747 return aRet;
750 Sequence< ::rtl::OUString > SAL_CALL SvXMLEmbeddedObjectHelper::getElementNames()
751 throw (RuntimeException)
753 MutexGuard aGuard( maMutex );
754 return Sequence< ::rtl::OUString >(0);
757 sal_Bool SAL_CALL SvXMLEmbeddedObjectHelper::hasByName( const ::rtl::OUString& rURLStr )
758 throw (RuntimeException)
760 MutexGuard aGuard( maMutex );
761 if( EMBEDDEDOBJECTHELPER_MODE_READ == meCreateMode )
763 return sal_True;
765 else
767 ::rtl::OUString aContainerStorageName, aObjectStorageName;
768 if( !ImplGetStorageNames( rURLStr, aContainerStorageName,
769 aObjectStorageName,
770 sal_True ) )
771 return sal_False;
773 comphelper::EmbeddedObjectContainer& rContainer = mpDocPersist->getEmbeddedObjectContainer();
774 return aObjectStorageName.getLength() > 0 &&
775 rContainer.HasEmbeddedObject( aObjectStorageName );
779 // XNameAccess
780 Type SAL_CALL SvXMLEmbeddedObjectHelper::getElementType()
781 throw (RuntimeException)
783 MutexGuard aGuard( maMutex );
784 if( EMBEDDEDOBJECTHELPER_MODE_READ == meCreateMode )
785 return ::getCppuType((const Reference<XOutputStream>*)0);
786 else
787 return ::getCppuType((const Reference<XInputStream>*)0);
790 sal_Bool SAL_CALL SvXMLEmbeddedObjectHelper::hasElements()
791 throw (RuntimeException)
793 MutexGuard aGuard( maMutex );
794 if( EMBEDDEDOBJECTHELPER_MODE_READ == meCreateMode )
796 return sal_True;
798 else
800 comphelper::EmbeddedObjectContainer& rContainer = mpDocPersist->getEmbeddedObjectContainer();
801 return rContainer.HasEmbeddedObjects();