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>
52 class OleOutputStream :
public ::cppu::WeakImplHelper< XSeekable, XOutputStream >
55 explicit OleOutputStream(
57 const Reference< XNameContainer >& rxStorage,
58 const OUString& rElementName );
60 virtual void SAL_CALL seek( sal_Int64 nPos )
override;
61 virtual sal_Int64 SAL_CALL getPosition()
override;
62 virtual sal_Int64 SAL_CALL
getLength()
override;
65 virtual void SAL_CALL flush()
override;
66 virtual void SAL_CALL closeOutput()
override;
70 void ensureSeekable()
const;
72 void ensureConnected()
const;
83 const Reference< XNameContainer >& rxStorage,
const OUString& rElementName ) :
89 mxTempFile.set( TempFile::create(rxContext), UNO_QUERY_THROW );
93 catch(
const Exception& )
98 void SAL_CALL OleOutputStream::seek( sal_Int64 nPos )
104 sal_Int64 SAL_CALL OleOutputStream::getPosition()
110 sal_Int64 SAL_CALL OleOutputStream::getLength()
122 void SAL_CALL OleOutputStream::flush()
128 void SAL_CALL OleOutputStream::closeOutput()
133 Reference< XOutputStream > xOutStrm =
mxOutStrm;
134 Reference< XSeekable > xSeekable =
mxSeekable;
139 xOutStrm->closeOutput();
141 xSeekable->seek( 0 );
146 void OleOutputStream::ensureSeekable()
const
152 void OleOutputStream::ensureConnected()
const
155 throw NotConnectedException();
164 mpParentStorage( nullptr )
166 OSL_ENSURE(
mxContext.is(),
"OleStorage::OleStorage - missing component context" );
167 initStorage( rxInStream );
171 const Reference< XStream >& rxOutStream,
bool bBaseStreamAccess ) :
174 mpParentStorage( nullptr )
176 OSL_ENSURE(
mxContext.is(),
"OleStorage::OleStorage - missing component context" );
177 initStorage( rxOutStream );
180 OleStorage::OleStorage(
const OleStorage& rParentStorage,
181 const Reference< XNameContainer >& rxStorage,
const OUString& rElementName,
bool bReadOnly ) :
182 StorageBase( rParentStorage, rElementName, bReadOnly ),
185 mpParentStorage( &rParentStorage )
187 OSL_ENSURE(
mxStorage.is(),
"OleStorage::OleStorage - missing substorage elements" );
190 OleStorage::OleStorage(
const OleStorage& rParentStorage,
191 const Reference< XStream >& rxOutStream,
const OUString& rElementName ) :
194 mpParentStorage( &rParentStorage )
196 initStorage( rxOutStream );
199 OleStorage::~OleStorage()
207 if( !Reference< XSeekable >( xInStrm, UNO_QUERY ).is() )
try
209 Reference< XStream > xTempFile( TempFile::create(mxContext), UNO_QUERY_THROW );
211 Reference< XOutputStream > xOutStrm( xTempFile->getOutputStream(), UNO_SET_THROW );
217 aInStrm.copyToStream( aOutStrm );
219 xInStrm = xTempFile->getInputStream();
221 catch(
const Exception& )
223 OSL_FAIL(
"OleStorage::initStorage - cannot create temporary copy of input stream" );
227 if( xInStrm.is() )
try
229 Reference< XMultiServiceFactory >
xFactory(
mxContext->getServiceManager(), UNO_QUERY_THROW );
230 Sequence< Any > aArgs{
Any(xInStrm),
232 mxStorage.set(
xFactory->createInstanceWithArguments(
"com.sun.star.embed.OLESimpleStorage", aArgs ), UNO_QUERY_THROW );
234 catch(
const Exception& )
239 void OleStorage::initStorage(
const Reference< XStream >& rxOutStream )
242 if( rxOutStream.is() )
try
244 Reference< XMultiServiceFactory >
xFactory(
mxContext->getServiceManager(), UNO_QUERY_THROW );
245 Sequence< Any > aArgs{
Any(rxOutStream),
247 mxStorage.set(
xFactory->createInstanceWithArguments(
"com.sun.star.embed.OLESimpleStorage", aArgs ), UNO_QUERY_THROW );
249 catch(
const Exception& )
256 bool OleStorage::implIsStorage()
const
272 Reference< XStorage > OleStorage::implGetXStorage()
const
274 OSL_FAIL(
"OleStorage::getXStorage - not implemented" );
275 return Reference< XStorage >();
278 void OleStorage::implGetElementNames( ::std::vector< OUString >& orElementNames )
const
283 if( aNames.hasElements() )
284 orElementNames.insert( orElementNames.end(), aNames.begin(), aNames.end() );
291 StorageRef OleStorage::implOpenSubStorage(
const OUString& rElementName,
bool bCreateMissing )
294 if(
mxStorage.is() && !rElementName.isEmpty() )
298 Reference< XNameContainer > xSubElements(
mxStorage->getByName( rElementName ), UNO_QUERY_THROW );
299 xSubStorage.reset(
new OleStorage( *
this, xSubElements, rElementName,
true ) );
311 if( !isReadOnly() && (bCreateMissing || xSubStorage) )
try
314 Reference< XStream > xTempFile( TempFile::create(
mxContext), UNO_QUERY_THROW );
318 xSubStorage->copyStorageToStorage( *xTempStorage );
320 xSubStorage = xTempStorage;
334 xInStream.set(
mxStorage->getByName( rElementName ), UNO_QUERY );
342 Reference< XOutputStream > OleStorage::implOpenOutputStream(
const OUString& rElementName )
344 Reference< XOutputStream > xOutStream;
345 if(
mxStorage.is() && !rElementName.isEmpty() )
350 void OleStorage::implCommit()
const
355 Reference< XTransactedObject >(
mxStorage, UNO_QUERY_THROW )->commit();
357 if( mpParentStorage )
359 if( mpParentStorage->mxStorage->hasByName( getName() ) )
362 mpParentStorage->mxStorage->removeByName( getName() );
363 Reference< XTransactedObject >( mpParentStorage->mxStorage, UNO_QUERY_THROW )->commit();
365 mpParentStorage->mxStorage->insertByName( getName(),
Any( mxStorage ) );
Reference< XOutputStream > mxOutStrm
std::shared_ptr< StorageBase > StorageRef
Implements stream access for binary OLE storages.
Wraps a UNO output stream and provides convenient access functions.
Reference< XNameContainer > mxStorage
Reference< XSeekable > mxSeekable
Reference< XStream > mxTempFile
double getLength(const B2DPolygon &rCandidate)
Reference< XSingleServiceFactory > xFactory
bool m_bDetectedRangeSegmentation false