xref: /trunk/main/oox/source/ole/olestorage.cxx (revision 79aad27f)
1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/ole/olestorage.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
27cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
28cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
29cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
30cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
31cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
32cdf0e10cSrcweir #include <com/sun/star/io/XStream.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
35cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
36cdf0e10cSrcweir #include "oox/helper/binaryinputstream.hxx"
37cdf0e10cSrcweir #include "oox/helper/binaryoutputstream.hxx"
38cdf0e10cSrcweir #include "oox/helper/containerhelper.hxx"
39cdf0e10cSrcweir #include "oox/helper/helper.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace oox {
42cdf0e10cSrcweir namespace ole {
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // ============================================================================
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace ::com::sun::star::beans;
47cdf0e10cSrcweir using namespace ::com::sun::star::container;
48cdf0e10cSrcweir using namespace ::com::sun::star::embed;
49cdf0e10cSrcweir using namespace ::com::sun::star::io;
50cdf0e10cSrcweir using namespace ::com::sun::star::lang;
51cdf0e10cSrcweir using namespace ::com::sun::star::uno;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir using ::rtl::OUString;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir // ============================================================================
56cdf0e10cSrcweir 
57cdf0e10cSrcweir namespace {
58cdf0e10cSrcweir 
59cdf0e10cSrcweir typedef ::cppu::WeakImplHelper2< XSeekable, XOutputStream > OleOutputStreamBase;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir /** Implementation of an OLE storage output stream that inserts itself into the
62cdf0e10cSrcweir     storage when it is closed.
63cdf0e10cSrcweir  */
64cdf0e10cSrcweir class OleOutputStream : public OleOutputStreamBase
65cdf0e10cSrcweir {
66cdf0e10cSrcweir public:
67cdf0e10cSrcweir     explicit            OleOutputStream(
68cdf0e10cSrcweir                             const Reference< XComponentContext >& rxContext,
69cdf0e10cSrcweir                             const Reference< XNameContainer >& rxStorage,
70cdf0e10cSrcweir                             const OUString& rElementName );
71cdf0e10cSrcweir     virtual             ~OleOutputStream();
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     virtual void SAL_CALL seek( sal_Int64 nPos ) throw( IllegalArgumentException, IOException, RuntimeException );
74cdf0e10cSrcweir     virtual sal_Int64 SAL_CALL getPosition() throw( IOException, RuntimeException );
75cdf0e10cSrcweir     virtual sal_Int64 SAL_CALL getLength() throw( IOException, RuntimeException );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& rData ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException );
78cdf0e10cSrcweir     virtual void SAL_CALL flush() throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException );
79cdf0e10cSrcweir     virtual void SAL_CALL closeOutput() throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir private:
82cdf0e10cSrcweir     void                ensureSeekable() const throw( IOException );
83cdf0e10cSrcweir     void                ensureConnected() const throw( NotConnectedException );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir private:
86cdf0e10cSrcweir     Reference< XNameContainer > mxStorage;
87cdf0e10cSrcweir     Reference< XStream > mxTempFile;
88cdf0e10cSrcweir     Reference< XOutputStream > mxOutStrm;
89cdf0e10cSrcweir     Reference< XSeekable > mxSeekable;
90cdf0e10cSrcweir     OUString            maElementName;
91cdf0e10cSrcweir };
92cdf0e10cSrcweir 
93cdf0e10cSrcweir // ----------------------------------------------------------------------------
94cdf0e10cSrcweir 
OleOutputStream(const Reference<XComponentContext> & rxContext,const Reference<XNameContainer> & rxStorage,const OUString & rElementName)95cdf0e10cSrcweir OleOutputStream::OleOutputStream( const Reference< XComponentContext >& rxContext,
96cdf0e10cSrcweir         const Reference< XNameContainer >& rxStorage, const OUString& rElementName ) :
97cdf0e10cSrcweir     mxStorage( rxStorage ),
98cdf0e10cSrcweir     maElementName( rElementName )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     try
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
103cdf0e10cSrcweir         mxTempFile.set( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.io.TempFile" ) ), UNO_QUERY_THROW );
104cdf0e10cSrcweir         mxOutStrm = mxTempFile->getOutputStream();
105cdf0e10cSrcweir         mxSeekable.set( mxOutStrm, UNO_QUERY );
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir     catch( Exception& )
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
~OleOutputStream()112cdf0e10cSrcweir OleOutputStream::~OleOutputStream()
113cdf0e10cSrcweir {
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
seek(sal_Int64 nPos)116cdf0e10cSrcweir void SAL_CALL OleOutputStream::seek( sal_Int64 nPos ) throw( IllegalArgumentException, IOException, RuntimeException )
117cdf0e10cSrcweir {
118cdf0e10cSrcweir     ensureSeekable();
119cdf0e10cSrcweir     mxSeekable->seek( nPos );
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
getPosition()122cdf0e10cSrcweir sal_Int64 SAL_CALL OleOutputStream::getPosition() throw( IOException, RuntimeException )
123cdf0e10cSrcweir {
124cdf0e10cSrcweir     ensureSeekable();
125cdf0e10cSrcweir     return mxSeekable->getPosition();
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
getLength()128cdf0e10cSrcweir sal_Int64 SAL_CALL OleOutputStream::getLength() throw( IOException, RuntimeException )
129cdf0e10cSrcweir {
130cdf0e10cSrcweir     ensureSeekable();
131cdf0e10cSrcweir     return mxSeekable->getLength();
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
writeBytes(const Sequence<sal_Int8> & rData)134cdf0e10cSrcweir void SAL_CALL OleOutputStream::writeBytes( const Sequence< sal_Int8 >& rData ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     ensureConnected();
137cdf0e10cSrcweir     mxOutStrm->writeBytes( rData );
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
flush()140cdf0e10cSrcweir void SAL_CALL OleOutputStream::flush() throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir     ensureConnected();
143cdf0e10cSrcweir     mxOutStrm->flush();
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
closeOutput()146cdf0e10cSrcweir void SAL_CALL OleOutputStream::closeOutput() throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir     ensureConnected();
149cdf0e10cSrcweir     ensureSeekable();
150cdf0e10cSrcweir     // remember the class members
151cdf0e10cSrcweir     Reference< XOutputStream > xOutStrm = mxOutStrm;
152cdf0e10cSrcweir     Reference< XSeekable > xSeekable = mxSeekable;
153cdf0e10cSrcweir     // reset all class members
154cdf0e10cSrcweir     mxOutStrm.clear();
155cdf0e10cSrcweir     mxSeekable.clear();
156cdf0e10cSrcweir     // close stream (and let it throw something if needed)
157cdf0e10cSrcweir     xOutStrm->closeOutput();
158cdf0e10cSrcweir     // on success, insert the stream into the OLE storage (must be seeked back before)
159cdf0e10cSrcweir     xSeekable->seek( 0 );
160cdf0e10cSrcweir     if( !ContainerHelper::insertByName( mxStorage, maElementName, Any( mxTempFile ) ) )
161cdf0e10cSrcweir         throw IOException();
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
ensureSeekable() const164cdf0e10cSrcweir void OleOutputStream::ensureSeekable() const throw( IOException )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir     if( !mxSeekable.is() )
167cdf0e10cSrcweir         throw IOException();
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
ensureConnected() const170cdf0e10cSrcweir void OleOutputStream::ensureConnected() const throw( NotConnectedException )
171cdf0e10cSrcweir {
172cdf0e10cSrcweir     if( !mxOutStrm.is() )
173cdf0e10cSrcweir         throw NotConnectedException();
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir } // namespace
177cdf0e10cSrcweir 
178cdf0e10cSrcweir // ============================================================================
179cdf0e10cSrcweir 
OleStorage(const Reference<XComponentContext> & rxContext,const Reference<XInputStream> & rxInStream,bool bBaseStreamAccess)180cdf0e10cSrcweir OleStorage::OleStorage( const Reference< XComponentContext >& rxContext,
181cdf0e10cSrcweir         const Reference< XInputStream >& rxInStream, bool bBaseStreamAccess ) :
182cdf0e10cSrcweir     StorageBase( rxInStream, bBaseStreamAccess ),
183cdf0e10cSrcweir     mxContext( rxContext ),
184cdf0e10cSrcweir     mpParentStorage( 0 )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir     OSL_ENSURE( mxContext.is(), "OleStorage::OleStorage - missing component context" );
187cdf0e10cSrcweir     initStorage( rxInStream );
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
OleStorage(const Reference<XComponentContext> & rxContext,const Reference<XStream> & rxOutStream,bool bBaseStreamAccess)190cdf0e10cSrcweir OleStorage::OleStorage( const Reference< XComponentContext >& rxContext,
191cdf0e10cSrcweir         const Reference< XStream >& rxOutStream, bool bBaseStreamAccess ) :
192cdf0e10cSrcweir     StorageBase( rxOutStream, bBaseStreamAccess ),
193cdf0e10cSrcweir     mxContext( rxContext ),
194cdf0e10cSrcweir     mpParentStorage( 0 )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     OSL_ENSURE( mxContext.is(), "OleStorage::OleStorage - missing component context" );
197cdf0e10cSrcweir     initStorage( rxOutStream );
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
OleStorage(const OleStorage & rParentStorage,const Reference<XNameContainer> & rxStorage,const OUString & rElementName,bool bReadOnly)200cdf0e10cSrcweir OleStorage::OleStorage( const OleStorage& rParentStorage,
201cdf0e10cSrcweir         const Reference< XNameContainer >& rxStorage, const OUString& rElementName, bool bReadOnly ) :
202cdf0e10cSrcweir     StorageBase( rParentStorage, rElementName, bReadOnly ),
203cdf0e10cSrcweir     mxContext( rParentStorage.mxContext ),
204cdf0e10cSrcweir     mxStorage( rxStorage ),
205cdf0e10cSrcweir     mpParentStorage( &rParentStorage )
206cdf0e10cSrcweir {
207cdf0e10cSrcweir     OSL_ENSURE( mxStorage.is(), "OleStorage::OleStorage - missing substorage elements" );
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
OleStorage(const OleStorage & rParentStorage,const Reference<XStream> & rxOutStream,const OUString & rElementName)210cdf0e10cSrcweir OleStorage::OleStorage( const OleStorage& rParentStorage,
211cdf0e10cSrcweir         const Reference< XStream >& rxOutStream, const OUString& rElementName ) :
212cdf0e10cSrcweir     StorageBase( rParentStorage, rElementName, false ),
213cdf0e10cSrcweir     mxContext( rParentStorage.mxContext ),
214cdf0e10cSrcweir     mpParentStorage( &rParentStorage )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir     initStorage( rxOutStream );
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
~OleStorage()219cdf0e10cSrcweir OleStorage::~OleStorage()
220cdf0e10cSrcweir {
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir // ----------------------------------------------------------------------------
224cdf0e10cSrcweir 
initStorage(const Reference<XInputStream> & rxInStream)225cdf0e10cSrcweir void OleStorage::initStorage( const Reference< XInputStream >& rxInStream )
226cdf0e10cSrcweir {
227cdf0e10cSrcweir     // if stream is not seekable, create temporary copy
228cdf0e10cSrcweir     Reference< XInputStream > xInStrm = rxInStream;
229cdf0e10cSrcweir     if( !Reference< XSeekable >( xInStrm, UNO_QUERY ).is() ) try
230cdf0e10cSrcweir     {
231cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory( mxContext->getServiceManager(), UNO_QUERY_THROW );
232cdf0e10cSrcweir         Reference< XStream > xTempFile( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.io.TempFile" ) ), UNO_QUERY_THROW );
233cdf0e10cSrcweir         {
234cdf0e10cSrcweir             Reference< XOutputStream > xOutStrm( xTempFile->getOutputStream(), UNO_SET_THROW );
235cdf0e10cSrcweir             /*  Pass false to both binary stream objects to keep the UNO
236cdf0e10cSrcweir                 streams alive. Life time of these streams is controlled by the
237cdf0e10cSrcweir                 tempfile implementation. */
238cdf0e10cSrcweir             BinaryXOutputStream aOutStrm( xOutStrm, false );
239cdf0e10cSrcweir             BinaryXInputStream aInStrm( xInStrm, false );
240cdf0e10cSrcweir             aInStrm.copyToStream( aOutStrm );
241cdf0e10cSrcweir         } // scope closes output stream of tempfile
242cdf0e10cSrcweir         xInStrm = xTempFile->getInputStream();
243cdf0e10cSrcweir     }
244cdf0e10cSrcweir     catch( Exception& )
245cdf0e10cSrcweir     {
246cdf0e10cSrcweir         OSL_ENSURE( false, "OleStorage::initStorage - cannot create temporary copy of input stream" );
247cdf0e10cSrcweir     }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir     // create base storage object
250cdf0e10cSrcweir     if( xInStrm.is() ) try
251cdf0e10cSrcweir     {
252cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory( mxContext->getServiceManager(), UNO_QUERY_THROW );
253cdf0e10cSrcweir         Sequence< Any > aArgs( 2 );
254cdf0e10cSrcweir         aArgs[ 0 ] <<= xInStrm;
255cdf0e10cSrcweir         aArgs[ 1 ] <<= true;        // true = do not create a copy of the input stream
256cdf0e10cSrcweir         mxStorage.set( xFactory->createInstanceWithArguments(
257cdf0e10cSrcweir             CREATE_OUSTRING( "com.sun.star.embed.OLESimpleStorage" ), aArgs ), UNO_QUERY_THROW );
258cdf0e10cSrcweir     }
259cdf0e10cSrcweir     catch( Exception& )
260cdf0e10cSrcweir     {
261cdf0e10cSrcweir     }
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
initStorage(const Reference<XStream> & rxOutStream)264cdf0e10cSrcweir void OleStorage::initStorage( const Reference< XStream >& rxOutStream )
265cdf0e10cSrcweir {
266cdf0e10cSrcweir     // create base storage object
267cdf0e10cSrcweir     if( rxOutStream.is() ) try
268cdf0e10cSrcweir     {
269cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory( mxContext->getServiceManager(), UNO_QUERY_THROW );
270cdf0e10cSrcweir         Sequence< Any > aArgs( 2 );
271cdf0e10cSrcweir         aArgs[ 0 ] <<= rxOutStream;
272cdf0e10cSrcweir         aArgs[ 1 ] <<= true;        // true = do not create a copy of the stream
273cdf0e10cSrcweir         mxStorage.set( xFactory->createInstanceWithArguments(
274cdf0e10cSrcweir             CREATE_OUSTRING( "com.sun.star.embed.OLESimpleStorage" ), aArgs ), UNO_QUERY_THROW );
275cdf0e10cSrcweir     }
276cdf0e10cSrcweir     catch( Exception& )
277cdf0e10cSrcweir     {
278cdf0e10cSrcweir     }
279cdf0e10cSrcweir }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir // StorageBase interface ------------------------------------------------------
282cdf0e10cSrcweir 
implIsStorage() const283cdf0e10cSrcweir bool OleStorage::implIsStorage() const
284cdf0e10cSrcweir {
285cdf0e10cSrcweir     if( mxStorage.is() ) try
286cdf0e10cSrcweir     {
287cdf0e10cSrcweir         /*  If this is not an OLE storage, hasElements() of the OLESimpleStorage
288cdf0e10cSrcweir             implementation throws an exception. But we do not return the result
289cdf0e10cSrcweir             of hasElements(), because an empty storage is a valid storage too. */
290cdf0e10cSrcweir         mxStorage->hasElements();
291cdf0e10cSrcweir         return true;
292cdf0e10cSrcweir     }
293cdf0e10cSrcweir     catch( Exception& )
294cdf0e10cSrcweir     {
295cdf0e10cSrcweir     }
296cdf0e10cSrcweir     return false;
297cdf0e10cSrcweir }
298cdf0e10cSrcweir 
implGetXStorage() const299cdf0e10cSrcweir Reference< XStorage > OleStorage::implGetXStorage() const
300cdf0e10cSrcweir {
301cdf0e10cSrcweir     OSL_ENSURE( false, "OleStorage::getXStorage - not implemented" );
302cdf0e10cSrcweir     return Reference< XStorage >();
303cdf0e10cSrcweir }
304cdf0e10cSrcweir 
implGetElementNames(::std::vector<OUString> & orElementNames) const305cdf0e10cSrcweir void OleStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
306cdf0e10cSrcweir {
307cdf0e10cSrcweir     Sequence< OUString > aNames;
308cdf0e10cSrcweir     if( mxStorage.is() ) try
309cdf0e10cSrcweir     {
310cdf0e10cSrcweir         aNames = mxStorage->getElementNames();
311cdf0e10cSrcweir         if( aNames.getLength() > 0 )
312cdf0e10cSrcweir             orElementNames.insert( orElementNames.end(), aNames.getConstArray(), aNames.getConstArray() + aNames.getLength() );
313cdf0e10cSrcweir     }
314cdf0e10cSrcweir     catch( Exception& )
315cdf0e10cSrcweir     {
316cdf0e10cSrcweir     }
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
implOpenSubStorage(const OUString & rElementName,bool bCreateMissing)319cdf0e10cSrcweir StorageRef OleStorage::implOpenSubStorage( const OUString& rElementName, bool bCreateMissing )
320cdf0e10cSrcweir {
321cdf0e10cSrcweir     StorageRef xSubStorage;
322cdf0e10cSrcweir     if( mxStorage.is() && (rElementName.getLength() > 0) )
323cdf0e10cSrcweir     {
324cdf0e10cSrcweir         try
325cdf0e10cSrcweir         {
326cdf0e10cSrcweir             Reference< XNameContainer > xSubElements( mxStorage->getByName( rElementName ), UNO_QUERY_THROW );
327cdf0e10cSrcweir             xSubStorage.reset( new OleStorage( *this, xSubElements, rElementName, true ) );
328cdf0e10cSrcweir         }
329cdf0e10cSrcweir         catch( Exception& )
330cdf0e10cSrcweir         {
331cdf0e10cSrcweir         }
332cdf0e10cSrcweir 
333cdf0e10cSrcweir         /*  The OLESimpleStorage API implementation seems to be buggy in the
334cdf0e10cSrcweir             area of writable inplace substorage (sometimes it overwrites other
335cdf0e10cSrcweir             unrelated streams with zero bytes). We go the save way and create a
336cdf0e10cSrcweir             new OLE storage based on a temporary file. All operations are
337cdf0e10cSrcweir             performed on this clean storage. On committing, the storage will be
338cdf0e10cSrcweir             completely re-inserted into the parent storage. */
339cdf0e10cSrcweir         if( !isReadOnly() && (bCreateMissing || xSubStorage.get()) ) try
340cdf0e10cSrcweir         {
341cdf0e10cSrcweir             // create new storage based on a temp file
342cdf0e10cSrcweir             Reference< XMultiServiceFactory > xFactory( mxContext->getServiceManager(), UNO_QUERY_THROW );
343cdf0e10cSrcweir             Reference< XStream > xTempFile( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.io.TempFile" ) ), UNO_QUERY_THROW );
344cdf0e10cSrcweir             StorageRef xTempStorage( new OleStorage( *this, xTempFile, rElementName ) );
345cdf0e10cSrcweir             // copy existing substorage into temp storage
346cdf0e10cSrcweir             if( xSubStorage.get() )
347cdf0e10cSrcweir                 xSubStorage->copyStorageToStorage( *xTempStorage );
348cdf0e10cSrcweir             // return the temp storage to caller
349cdf0e10cSrcweir             xSubStorage = xTempStorage;
350cdf0e10cSrcweir         }
351cdf0e10cSrcweir         catch( Exception& )
352cdf0e10cSrcweir         {
353cdf0e10cSrcweir         }
354cdf0e10cSrcweir     }
355cdf0e10cSrcweir     return xSubStorage;
356cdf0e10cSrcweir }
357cdf0e10cSrcweir 
implOpenInputStream(const OUString & rElementName)358cdf0e10cSrcweir Reference< XInputStream > OleStorage::implOpenInputStream( const OUString& rElementName )
359cdf0e10cSrcweir {
360cdf0e10cSrcweir     Reference< XInputStream > xInStream;
361cdf0e10cSrcweir     if( mxStorage.is() ) try
362cdf0e10cSrcweir     {
363cdf0e10cSrcweir         xInStream.set( mxStorage->getByName( rElementName ), UNO_QUERY );
364cdf0e10cSrcweir     }
365cdf0e10cSrcweir     catch( Exception& )
366cdf0e10cSrcweir     {
367cdf0e10cSrcweir     }
368cdf0e10cSrcweir     return xInStream;
369cdf0e10cSrcweir }
370cdf0e10cSrcweir 
implOpenOutputStream(const OUString & rElementName)371cdf0e10cSrcweir Reference< XOutputStream > OleStorage::implOpenOutputStream( const OUString& rElementName )
372cdf0e10cSrcweir {
373cdf0e10cSrcweir     Reference< XOutputStream > xOutStream;
374cdf0e10cSrcweir     if( mxStorage.is() && (rElementName.getLength() > 0) )
375cdf0e10cSrcweir         xOutStream.set( new OleOutputStream( mxContext, mxStorage, rElementName ) );
376cdf0e10cSrcweir     return xOutStream;
377cdf0e10cSrcweir }
378cdf0e10cSrcweir 
implCommit() const379cdf0e10cSrcweir void OleStorage::implCommit() const
380cdf0e10cSrcweir {
381cdf0e10cSrcweir     try
382cdf0e10cSrcweir     {
383cdf0e10cSrcweir         // commit this storage (finalizes the file this storage is based on)
384cdf0e10cSrcweir         Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit();
385cdf0e10cSrcweir         // re-insert this storage into the parent storage
386cdf0e10cSrcweir         if( mpParentStorage )
387cdf0e10cSrcweir         {
388cdf0e10cSrcweir             if( mpParentStorage->mxStorage->hasByName( getName() ) )
389cdf0e10cSrcweir             {
390cdf0e10cSrcweir                 // replaceByName() does not work (#i109539#)
391cdf0e10cSrcweir                 mpParentStorage->mxStorage->removeByName( getName() );
392cdf0e10cSrcweir                 Reference< XTransactedObject >( mpParentStorage->mxStorage, UNO_QUERY_THROW )->commit();
393cdf0e10cSrcweir             }
394cdf0e10cSrcweir             mpParentStorage->mxStorage->insertByName( getName(), Any( mxStorage ) );
395cdf0e10cSrcweir             // this requires another commit(), which will be performed by the parent storage
396cdf0e10cSrcweir         }
397cdf0e10cSrcweir     }
398cdf0e10cSrcweir     catch( Exception& )
399cdf0e10cSrcweir     {
400cdf0e10cSrcweir     }
401cdf0e10cSrcweir }
402cdf0e10cSrcweir 
403cdf0e10cSrcweir // ============================================================================
404cdf0e10cSrcweir 
405cdf0e10cSrcweir } // namespace ole
406cdf0e10cSrcweir } // namespace oox
407