22#include <com/sun/star/container/XNameContainer.hpp>
23#include <com/sun/star/embed/XTransactedObject.hpp>
24#include <com/sun/star/io/IOException.hpp>
25#include <com/sun/star/io/NotConnectedException.hpp>
26#include <com/sun/star/io/TempFile.hpp>
27#include <com/sun/star/io/XInputStream.hpp>
28#include <com/sun/star/io/XOutputStream.hpp>
29#include <com/sun/star/io/XSeekable.hpp>
30#include <com/sun/star/io/XStream.hpp>
31#include <com/sun/star/lang/XMultiServiceFactory.hpp>
32#include <com/sun/star/uno/XComponentContext.hpp>
34#include <osl/diagnose.h>
53class OleOutputStream :
public ::cppu::WeakImplHelper< XSeekable, XOutputStream >
56 explicit OleOutputStream(
57 const Reference< XComponentContext >& rxContext,
58 const Reference< XNameContainer >& rxStorage,
59 OUString aElementName );
61 virtual void SAL_CALL seek( sal_Int64 nPos )
override;
62 virtual sal_Int64 SAL_CALL getPosition()
override;
63 virtual sal_Int64 SAL_CALL
getLength()
override;
66 virtual void SAL_CALL flush()
override;
67 virtual void SAL_CALL closeOutput()
override;
71 void ensureSeekable()
const;
73 void ensureConnected()
const;
83OleOutputStream::OleOutputStream(
const Reference< XComponentContext >& rxContext,
84 const Reference< XNameContainer >& rxStorage, OUString aElementName ) :
90 mxTempFile.set( TempFile::create(rxContext), UNO_QUERY_THROW );
94 catch(
const Exception& )
99void SAL_CALL OleOutputStream::seek( sal_Int64 nPos )
105sal_Int64 SAL_CALL OleOutputStream::getPosition()
111sal_Int64 SAL_CALL OleOutputStream::getLength()
123void SAL_CALL OleOutputStream::flush()
129void SAL_CALL OleOutputStream::closeOutput()
134 Reference< XOutputStream > xOutStrm =
mxOutStrm;
135 Reference< XSeekable > xSeekable =
mxSeekable;
140 xOutStrm->closeOutput();
142 xSeekable->seek( 0 );
147void OleOutputStream::ensureSeekable()
const
153void OleOutputStream::ensureConnected()
const
156 throw NotConnectedException();
161OleStorage::OleStorage(
const Reference< XComponentContext >& rxContext,
162 const Reference< XInputStream >& rxInStream,
bool bBaseStreamAccess ) :
165 mpParentStorage( nullptr )
167 OSL_ENSURE(
mxContext.is(),
"OleStorage::OleStorage - missing component context" );
168 initStorage( rxInStream );
171OleStorage::OleStorage(
const Reference< XComponentContext >& rxContext,
172 const Reference< XStream >& rxOutStream,
bool bBaseStreamAccess ) :
175 mpParentStorage( nullptr )
177 OSL_ENSURE(
mxContext.is(),
"OleStorage::OleStorage - missing component context" );
178 initStorage( rxOutStream );
181OleStorage::OleStorage(
const OleStorage& rParentStorage,
182 const Reference< XNameContainer >& rxStorage,
const OUString& rElementName,
bool bReadOnly ) :
186 mpParentStorage( &rParentStorage )
188 OSL_ENSURE(
mxStorage.is(),
"OleStorage::OleStorage - missing substorage elements" );
191OleStorage::OleStorage(
const OleStorage& rParentStorage,
192 const Reference< XStream >& rxOutStream,
const OUString& rElementName ) :
193 StorageBase( rParentStorage, rElementName, false ),
195 mpParentStorage( &rParentStorage )
197 initStorage( rxOutStream );
200OleStorage::~OleStorage()
204void OleStorage::initStorage(
const Reference< XInputStream >& rxInStream )
207 Reference< XInputStream > xInStrm = rxInStream;
208 if( !Reference< XSeekable >( xInStrm, UNO_QUERY ).is() )
try
210 Reference< XStream > xTempFile( TempFile::create(mxContext), UNO_QUERY_THROW );
212 Reference< XOutputStream > xOutStrm( xTempFile->getOutputStream(), UNO_SET_THROW );
218 aInStrm.copyToStream( aOutStrm );
220 xInStrm = xTempFile->getInputStream();
222 catch(
const Exception& )
224 OSL_FAIL(
"OleStorage::initStorage - cannot create temporary copy of input stream" );
228 if( xInStrm.is() )
try
230 Reference< XMultiServiceFactory >
xFactory(
mxContext->getServiceManager(), UNO_QUERY_THROW );
231 Sequence< Any > aArgs{
Any(xInStrm),
233 mxStorage.set(
xFactory->createInstanceWithArguments(
"com.sun.star.embed.OLESimpleStorage", aArgs ), UNO_QUERY_THROW );
235 catch(
const Exception& )
240void OleStorage::initStorage(
const Reference< XStream >& rxOutStream )
243 if( rxOutStream.is() )
try
245 Reference< XMultiServiceFactory >
xFactory(
mxContext->getServiceManager(), UNO_QUERY_THROW );
246 Sequence< Any > aArgs{
Any(rxOutStream),
248 mxStorage.set(
xFactory->createInstanceWithArguments(
"com.sun.star.embed.OLESimpleStorage", aArgs ), UNO_QUERY_THROW );
250 catch(
const Exception& )
257bool OleStorage::implIsStorage()
const
273Reference< XStorage > OleStorage::implGetXStorage()
const
275 OSL_FAIL(
"OleStorage::getXStorage - not implemented" );
276 return Reference< XStorage >();
279void OleStorage::implGetElementNames( ::std::vector< OUString >& orElementNames )
const
283 const Sequence<OUString> aNames =
mxStorage->getElementNames();
284 if( aNames.hasElements() )
285 orElementNames.insert( orElementNames.end(), aNames.begin(), aNames.end() );
292StorageRef OleStorage::implOpenSubStorage(
const OUString& rElementName,
bool bCreateMissing )
295 if(
mxStorage.is() && !rElementName.isEmpty() )
299 Reference< XNameContainer > xSubElements(
mxStorage->getByName( rElementName ), UNO_QUERY_THROW );
300 xSubStorage.reset(
new OleStorage( *
this, xSubElements, rElementName,
true ) );
312 if( !isReadOnly() && (bCreateMissing || xSubStorage) )
try
315 Reference< XStream > xTempFile( TempFile::create(
mxContext), UNO_QUERY_THROW );
319 xSubStorage->copyStorageToStorage( *xTempStorage );
321 xSubStorage = xTempStorage;
330Reference< XInputStream > OleStorage::implOpenInputStream(
const OUString& rElementName )
332 Reference< XInputStream > xInStream;
335 xInStream.set(
mxStorage->getByName( rElementName ), UNO_QUERY );
343Reference< XOutputStream > OleStorage::implOpenOutputStream(
const OUString& rElementName )
345 Reference< XOutputStream > xOutStream;
346 if(
mxStorage.is() && !rElementName.isEmpty() )
351void OleStorage::implCommit()
const
356 Reference< XTransactedObject >(
mxStorage, UNO_QUERY_THROW )->commit();
358 if( mpParentStorage )
360 if( mpParentStorage->mxStorage->hasByName( getName() ) )
363 mpParentStorage->mxStorage->removeByName( getName() );
364 Reference< XTransactedObject >( mpParentStorage->mxStorage, UNO_QUERY_THROW )->commit();
366 mpParentStorage->mxStorage->insertByName( getName(),
Any(
mxStorage ) );
Wraps a UNO output stream and provides convenient access functions.
Implements stream access for binary OLE storages.
Reference< XSingleServiceFactory > xFactory
double getLength(const B2DPolygon &rCandidate)
std::shared_ptr< StorageBase > StorageRef
Reference< XNameContainer > mxStorage
Reference< XStream > mxTempFile
Reference< XSeekable > mxSeekable
Reference< XOutputStream > mxOutStrm