merge the formfield patch from ooo-build
[ooovba.git] / svx / source / xml / xmleohlp.cxx
blob8b94cf3085f325b88664b10648804412dac3b1f1
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 //eliminate 'superfluous' slashes at start and end
330 //#i103076# load objects with all allowed xlink:href syntaxes
332 //eliminate './' at start
333 sal_Int32 nStart = 0;
334 sal_Int32 nCount = aURLNoPar.getLength();
335 if( 0 == aURLNoPar.compareToAscii( "./", 2 ) )
337 nStart = 2;
338 nCount -= 2;
341 //eliminate '/' at end
342 sal_Int32 nEnd = aURLNoPar.lastIndexOf( '/' );
343 if( nEnd == aURLNoPar.getLength()-1 && nEnd != (nStart-1) )
344 nCount--;
346 aURLNoPar = aURLNoPar.copy( nStart, nCount );
349 _nPos = aURLNoPar.lastIndexOf( '/' );
350 if( _nPos >= 0 )
351 rContainerStorageName = aURLNoPar.copy( 0, _nPos );
352 rObjectStorageName = aURLNoPar.copy( _nPos+1 );
356 if( -1 != rContainerStorageName.indexOf( '/' ) )
358 DBG_ERROR( "SvXMLEmbeddedObjectHelper: invalid path name" );
359 return sal_False;
362 return sal_True;
366 // -----------------------------------------------------------------------------
368 uno::Reference < embed::XStorage > SvXMLEmbeddedObjectHelper::ImplGetContainerStorage(
369 const ::rtl::OUString& rStorageName )
371 DBG_ASSERT( -1 == rStorageName.indexOf( '/' ) &&
372 -1 == rStorageName.indexOf( '\\' ),
373 "nested embedded storages aren't supported" );
374 if( !mxContainerStorage.is() ||
375 ( rStorageName != maCurContainerStorageName ) )
377 if( mxContainerStorage.is() &&
378 maCurContainerStorageName.getLength() > 0 &&
379 EMBEDDEDOBJECTHELPER_MODE_WRITE == meCreateMode )
381 uno::Reference < embed::XTransactedObject > xTrans( mxContainerStorage, uno::UNO_QUERY );
382 if ( xTrans.is() )
383 xTrans->commit();
386 if( rStorageName.getLength() > 0 && mxRootStorage.is() )
388 sal_Int32 nMode = EMBEDDEDOBJECTHELPER_MODE_WRITE == meCreateMode
389 ? ::embed::ElementModes::READWRITE
390 : ::embed::ElementModes::READ;
391 mxContainerStorage = mxRootStorage->openStorageElement( rStorageName,
392 nMode );
394 else
396 mxContainerStorage = mxRootStorage;
398 maCurContainerStorageName = rStorageName;
401 return mxContainerStorage;
404 // -----------------------------------------------------------------------------
406 sal_Bool SvXMLEmbeddedObjectHelper::ImplReadObject(
407 const ::rtl::OUString& rContainerStorageName,
408 ::rtl::OUString& rObjName,
409 const SvGlobalName *pClassId,
410 SvStream* pTemp )
412 (void)pClassId;
414 uno::Reference < embed::XStorage > xDocStor( mpDocPersist->getStorage() );
415 uno::Reference < embed::XStorage > xCntnrStor( ImplGetContainerStorage( rContainerStorageName ) );
417 if( !xCntnrStor.is() && !pTemp )
418 return sal_False;
420 String aSrcObjName( rObjName );
421 comphelper::EmbeddedObjectContainer& rContainer = mpDocPersist->getEmbeddedObjectContainer();
423 // Is the object name unique?
424 // if the object is already instantiated by GetEmbeddedObject
425 // that means that the duplication is being loaded
426 sal_Bool bDuplicate = rContainer.HasInstantiatedEmbeddedObject( rObjName );
427 DBG_ASSERT( !bDuplicate, "An object in the document is referenced twice!" );
429 if( xDocStor != xCntnrStor || pTemp || bDuplicate )
431 // TODO/LATER: make this alltogether a method in the EmbeddedObjectContainer
433 // create a unique name for the duplicate object
434 if( bDuplicate )
435 rObjName = rContainer.CreateUniqueObjectName();
437 if( pTemp )
441 pTemp->Seek( 0 );
442 uno::Reference < io::XStream > xStm = xDocStor->openStreamElement( rObjName,
443 embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
444 SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( xStm );
445 *pTemp >> *pStream;
446 delete pStream;
448 // TODO/LATER: what to do when other types of objects are based on substream persistence?
449 // This is an ole object
450 uno::Reference< beans::XPropertySet > xProps( xStm, uno::UNO_QUERY_THROW );
451 xProps->setPropertyValue(
452 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ),
453 uno::makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/vnd.sun.star.oleobject" ) ) ) );
455 xStm->getOutputStream()->closeOutput();
457 catch ( uno::Exception& )
459 return sal_False;
462 else
466 xCntnrStor->copyElementTo( aSrcObjName, xDocStor, rObjName );
468 catch ( uno::Exception& )
470 return sal_False;
475 // make object known to the container
476 // TODO/LATER: could be done a little bit more efficient!
477 ::rtl::OUString aName( rObjName );
479 // TODO/LATER: The provided pClassId is ignored for now.
480 // The stream contains OLE storage internally and this storage already has a class id specifying the
481 // server that was used to create the object. pClassId could be used to specify the server that should
482 // be used for the next opening, but this information seems to be out of the file format responsibility
483 // area.
484 rContainer.GetEmbeddedObject( aName );
486 return sal_True;
489 // -----------------------------------------------------------------------------
491 ::rtl::OUString SvXMLEmbeddedObjectHelper::ImplInsertEmbeddedObjectURL(
492 const ::rtl::OUString& rURLStr )
494 ::rtl::OUString sRetURL;
496 ::rtl::OUString aContainerStorageName, aObjectStorageName;
497 if( !ImplGetStorageNames( rURLStr, aContainerStorageName,
498 aObjectStorageName,
499 EMBEDDEDOBJECTHELPER_MODE_WRITE == meCreateMode ) )
500 return sRetURL;
502 if( EMBEDDEDOBJECTHELPER_MODE_READ == meCreateMode )
504 OutputStorageWrapper_Impl *pOut = 0;
505 SvXMLEmbeddedObjectHelper_Impl::iterator aIter;
507 if( mpStreamMap )
509 aIter = mpStreamMap->find( rURLStr );
510 if( aIter != mpStreamMap->end() && aIter->second )
511 pOut = aIter->second;
514 SvGlobalName aClassId, *pClassId = 0;
515 sal_Int32 nPos = aObjectStorageName.lastIndexOf( '!' );
516 if( -1 != nPos && aClassId.MakeId( aObjectStorageName.copy( nPos+1 ) ) )
518 aObjectStorageName = aObjectStorageName.copy( 0, nPos );
519 pClassId = &aClassId;
522 ImplReadObject( aContainerStorageName, aObjectStorageName, pClassId, pOut ? pOut->GetStream() : 0 );
523 sRetURL = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(XML_EMBEDDEDOBJECT_URL_BASE) );
524 sRetURL += aObjectStorageName;
526 if( pOut )
528 mpStreamMap->erase( aIter );
529 pOut->release();
532 else
534 // Objects are written using ::comphelper::IEmbeddedHelper::SaveAs
535 sRetURL = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("./") );
536 if( aContainerStorageName.getLength() )
538 sRetURL += aContainerStorageName;
539 sRetURL += ::rtl::OUString( '/' );
541 sRetURL += aObjectStorageName;
544 return sRetURL;
547 // -----------------------------------------------------------------------------
549 uno::Reference< io::XInputStream > SvXMLEmbeddedObjectHelper::ImplGetReplacementImage(
550 const uno::Reference< embed::XEmbeddedObject >& xObj )
552 uno::Reference< io::XInputStream > xStream;
554 if( xObj.is() )
558 sal_Bool bSwitchBackToLoaded = sal_False;
559 sal_Int32 nCurState = xObj->getCurrentState();
560 if ( nCurState == embed::EmbedStates::LOADED || nCurState == embed::EmbedStates::RUNNING )
562 // means that the object is not active
563 // copy replacement image from old to new container
564 ::rtl::OUString aMediaType;
565 xStream = mpDocPersist->getEmbeddedObjectContainer().GetGraphicStream( xObj, &aMediaType );
568 if ( !xStream.is() )
570 // the image must be regenerated
571 // TODO/LATER: another aspect could be used
572 if ( nCurState == embed::EmbedStates::LOADED )
573 bSwitchBackToLoaded = sal_True;
575 ::rtl::OUString aMediaType;
576 xStream = svt::EmbeddedObjectRef::GetGraphicReplacementStream(
577 embed::Aspects::MSOLE_CONTENT,
578 xObj,
579 &aMediaType );
582 if ( bSwitchBackToLoaded )
583 // switch back to loaded state; that way we have a minimum cache confusion
584 xObj->changeState( embed::EmbedStates::LOADED );
586 catch( uno::Exception& )
590 return xStream;
593 // -----------------------------------------------------------------------------
595 void SvXMLEmbeddedObjectHelper::Init(
596 const uno::Reference < embed::XStorage >& rRootStorage,
597 ::comphelper::IEmbeddedHelper& rPersist,
598 SvXMLEmbeddedObjectHelperMode eCreateMode )
600 mxRootStorage = rRootStorage;
601 mpDocPersist = &rPersist;
602 meCreateMode = eCreateMode;
605 // -----------------------------------------------------------------------------
607 SvXMLEmbeddedObjectHelper* SvXMLEmbeddedObjectHelper::Create(
608 const uno::Reference < embed::XStorage >& rRootStorage,
609 ::comphelper::IEmbeddedHelper& rDocPersist,
610 SvXMLEmbeddedObjectHelperMode eCreateMode,
611 sal_Bool bDirect )
613 (void)bDirect;
615 SvXMLEmbeddedObjectHelper* pThis = new SvXMLEmbeddedObjectHelper;
617 pThis->acquire();
618 pThis->Init( rRootStorage, rDocPersist, eCreateMode );
620 return pThis;
623 SvXMLEmbeddedObjectHelper* SvXMLEmbeddedObjectHelper::Create(
624 ::comphelper::IEmbeddedHelper& rDocPersist,
625 SvXMLEmbeddedObjectHelperMode eCreateMode )
627 SvXMLEmbeddedObjectHelper* pThis = new SvXMLEmbeddedObjectHelper;
629 pThis->acquire();
630 pThis->Init( 0, rDocPersist, eCreateMode );
632 return pThis;
635 // -----------------------------------------------------------------------------
637 void SvXMLEmbeddedObjectHelper::Destroy(
638 SvXMLEmbeddedObjectHelper* pSvXMLEmbeddedObjectHelper )
640 if( pSvXMLEmbeddedObjectHelper )
642 pSvXMLEmbeddedObjectHelper->dispose();
643 pSvXMLEmbeddedObjectHelper->release();
647 // -----------------------------------------------------------------------------
649 void SvXMLEmbeddedObjectHelper::Flush()
651 if( mxTempStorage.is() )
653 Reference < XComponent > xComp( mxTempStorage, UNO_QUERY );
654 xComp->dispose();
658 // XGraphicObjectResolver: alien objects!
659 ::rtl::OUString SAL_CALL SvXMLEmbeddedObjectHelper::resolveEmbeddedObjectURL( const ::rtl::OUString& aURL )
660 throw(RuntimeException)
662 MutexGuard aGuard( maMutex );
664 return ImplInsertEmbeddedObjectURL( aURL );
667 // XNameAccess: alien objects!
668 Any SAL_CALL SvXMLEmbeddedObjectHelper::getByName(
669 const ::rtl::OUString& rURLStr )
670 throw (NoSuchElementException, WrappedTargetException, RuntimeException)
672 MutexGuard aGuard( maMutex );
673 Any aRet;
674 if( EMBEDDEDOBJECTHELPER_MODE_READ == meCreateMode )
676 Reference < XOutputStream > xStrm;
677 if( mpStreamMap )
679 SvXMLEmbeddedObjectHelper_Impl::iterator aIter =
680 mpStreamMap->find( rURLStr );
681 if( aIter != mpStreamMap->end() && aIter->second )
682 xStrm = aIter->second;
684 if( !xStrm.is() )
686 OutputStorageWrapper_Impl *pOut = new OutputStorageWrapper_Impl;
687 pOut->acquire();
688 if( !mpStreamMap )
689 mpStreamMap = new SvXMLEmbeddedObjectHelper_Impl;
690 (*mpStreamMap)[rURLStr] = pOut;
691 xStrm = pOut;
694 aRet <<= xStrm;
696 else
698 sal_Bool bGraphicRepl = sal_False;
699 sal_Bool bOasisFormat = sal_True;
700 Reference < XInputStream > xStrm;
701 ::rtl::OUString aContainerStorageName, aObjectStorageName;
702 if( ImplGetStorageNames( rURLStr, aContainerStorageName,
703 aObjectStorageName,
704 sal_True,
705 &bGraphicRepl,
706 &bOasisFormat ) )
710 comphelper::EmbeddedObjectContainer& rContainer =
711 mpDocPersist->getEmbeddedObjectContainer();
713 Reference < embed::XEmbeddedObject > xObj = rContainer.GetEmbeddedObject( aObjectStorageName );
714 DBG_ASSERT( xObj.is(), "Didn't get object" );
716 if( xObj.is() )
718 if( bGraphicRepl )
720 xStrm = ImplGetReplacementImage( xObj );
722 else
724 Reference < embed::XEmbedPersist > xPersist( xObj, UNO_QUERY );
725 if( xPersist.is() )
727 if( !mxTempStorage.is() )
728 mxTempStorage =
729 comphelper::OStorageHelper::GetTemporaryStorage();
730 Sequence < beans::PropertyValue > aDummy( 0 ), aEmbDescr( 1 );
731 aEmbDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StoreVisualReplacement" ) );
732 aEmbDescr[0].Value <<= (sal_Bool)(!bOasisFormat);
733 if ( !bOasisFormat )
735 ::rtl::OUString aMimeType;
736 uno::Reference< io::XInputStream > xGrInStream = ImplGetReplacementImage( xObj );
737 if ( xGrInStream.is() )
739 aEmbDescr.realloc( 2 );
740 aEmbDescr[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "VisualReplacement" ) );
741 aEmbDescr[1].Value <<= xGrInStream;
745 xPersist->storeToEntry( mxTempStorage, aObjectStorageName,
746 aDummy, aEmbDescr );
747 Reference < io::XStream > xStream =
748 mxTempStorage->openStreamElement(
749 aObjectStorageName,
750 embed::ElementModes::READ);
751 if( xStream.is() )
752 xStrm = xStream->getInputStream();
757 catch ( uno::Exception& )
762 aRet <<= xStrm;
765 return aRet;
768 Sequence< ::rtl::OUString > SAL_CALL SvXMLEmbeddedObjectHelper::getElementNames()
769 throw (RuntimeException)
771 MutexGuard aGuard( maMutex );
772 return Sequence< ::rtl::OUString >(0);
775 sal_Bool SAL_CALL SvXMLEmbeddedObjectHelper::hasByName( const ::rtl::OUString& rURLStr )
776 throw (RuntimeException)
778 MutexGuard aGuard( maMutex );
779 if( EMBEDDEDOBJECTHELPER_MODE_READ == meCreateMode )
781 return sal_True;
783 else
785 ::rtl::OUString aContainerStorageName, aObjectStorageName;
786 if( !ImplGetStorageNames( rURLStr, aContainerStorageName,
787 aObjectStorageName,
788 sal_True ) )
789 return sal_False;
791 comphelper::EmbeddedObjectContainer& rContainer = mpDocPersist->getEmbeddedObjectContainer();
792 return aObjectStorageName.getLength() > 0 &&
793 rContainer.HasEmbeddedObject( aObjectStorageName );
797 // XNameAccess
798 Type SAL_CALL SvXMLEmbeddedObjectHelper::getElementType()
799 throw (RuntimeException)
801 MutexGuard aGuard( maMutex );
802 if( EMBEDDEDOBJECTHELPER_MODE_READ == meCreateMode )
803 return ::getCppuType((const Reference<XOutputStream>*)0);
804 else
805 return ::getCppuType((const Reference<XInputStream>*)0);
808 sal_Bool SAL_CALL SvXMLEmbeddedObjectHelper::hasElements()
809 throw (RuntimeException)
811 MutexGuard aGuard( maMutex );
812 if( EMBEDDEDOBJECTHELPER_MODE_READ == meCreateMode )
814 return sal_True;
816 else
818 comphelper::EmbeddedObjectContainer& rContainer = mpDocPersist->getEmbeddedObjectContainer();
819 return rContainer.HasEmbeddedObjects();