20#include <com/sun/star/beans/XPropertySet.hpp>
21#include <com/sun/star/embed/ElementModes.hpp>
22#include <com/sun/star/embed/InvalidStorageException.hpp>
23#include <com/sun/star/embed/StorageFactory.hpp>
24#include <com/sun/star/embed/StorageWrappedTargetException.hpp>
25#include <com/sun/star/io/IOException.hpp>
26#include <com/sun/star/packages/NoEncryptionException.hpp>
29#include <osl/diagnose.h>
44StorageElementFactory::StorageElementFactory(
45 uno::Reference< uno::XComponentContext > xContext,
47: m_xDocsMgr(
std::move( xDocsMgr )),
55 OSL_ENSURE(
m_aMap.empty(),
56 "StorageElementFactory::~StorageElementFactory - Dangling storages!" );
60uno::Reference< embed::XStorage >
63 uno::Reference< embed::XStorage > xStorage;
64 uno::Reference< lang::XSingleServiceFactory > xStorageFac;
67 xStorageFac = embed::StorageFactory::create(
m_xContext );
70 OSL_ENSURE( xStorageFac.is(),
"Can't create storage factory!" );
71 if ( xStorageFac.is() )
72 xStorage.set( xStorageFac->createInstance(), uno::UNO_QUERY );
75 throw uno::RuntimeException();
81uno::Reference< embed::XStorage >
90 throw lang::IllegalArgumentException(
92 uno::Reference< uno::XInterface >(),
98 throw lang::IllegalArgumentException(
99 "Root never has a storage!",
100 uno::Reference< uno::XInterface >(),
106 ? rUri.copy( 0, rUri.getLength() - 1 )
109 StorageMap::iterator aIt (
m_aMap.begin() );
110 StorageMap::iterator aEnd(
m_aMap.end() );
112 while ( aIt != aEnd )
114 if ( (*aIt).first.first == aUriKey )
129 bMatch = (*aIt).first.second;
141 uno::Reference< embed::XStorage > xParentStorage;
148 if ( !xParentStorage.is() )
152 "Unable to create parent storage!" );
153 return xParentStorage;
157 uno::Reference< embed::XStorage > xStorage
160 if ( !xStorage.is() )
164 "Unable to create storage!" );
175 std::pair< OUString, bool >( aUriKey, bWritable ),
176 xElement.get() ).first;
178 aIt->second->m_aContainerIt = aIt;
181 else if ( osl_atomic_increment( &aIt->second->m_refCount ) > 1 )
183 uno::Reference< embed::XStorage > xElement( aIt->second );
184 osl_atomic_decrement( &aIt->second->m_refCount );
189 osl_atomic_decrement( &aIt->second->m_refCount );
190 aIt->second->m_aContainerIt =
m_aMap.end();
192 uno::Reference< embed::XStorage > xParentStorage;
199 if ( !xParentStorage.is() )
203 "Unable to create parent storage!" );
204 return xParentStorage;
208 uno::Reference< embed::XStorage > xStorage
211 if ( !xStorage.is() )
215 "Unable to create storage!" );
220 aIt->second = pNewStorage.get();
221 aIt->second->m_aContainerIt = aIt;
227uno::Reference< io::XInputStream >
229 const OUString & rPassword )
233 uno::Reference< embed::XStorage > xParentStorage
237 if ( !xParentStorage.is() )
238 return uno::Reference< io::XInputStream >();
240 uno::Reference< io::XStream >
xStream
244 return uno::Reference< io::XInputStream >();
246 return xStream->getInputStream();
250uno::Reference< io::XOutputStream >
252 const OUString & rPassword,
257 uno::Reference< embed::XStorage > xParentStorage
261 if ( !xParentStorage.is() )
263 OSL_FAIL(
"StorageElementFactory::createOutputStream - "
264 "Unable to create parent storage!" );
265 return uno::Reference< io::XOutputStream >();
268 uno::Reference< io::XStream >
xStream
274 OSL_FAIL(
"StorageElementFactory::createOutputStream - "
275 "Unable to create stream!" );
276 return uno::Reference< io::XOutputStream >();
284 return uno::Reference< io::XOutputStream >(
289uno::Reference< io::XStream >
291 const OUString & rPassword,
296 uno::Reference< embed::XStorage > xParentStorage
300 if ( !xParentStorage.is() )
302 OSL_FAIL(
"StorageElementFactory::createStream - "
303 "Unable to create parent storage!" );
304 return uno::Reference< io::XStream >();
307 uno::Reference< io::XStream >
xStream
313 OSL_FAIL(
"StorageElementFactory::createStream - "
314 "Unable to create stream!" );
315 return uno::Reference< io::XStream >();
318 return uno::Reference< io::XStream >(
325 OSL_ASSERT( pElement );
338 uno::Reference< embed::XStorage > xParentStorage;
342 if ( !aParentUri.
isRoot() )
345 OSL_ENSURE( xParentStorage.is()
348 "StorageElementFactory::queryParentStorage - No storage!" );
350 return xParentStorage;
355 const uno::Reference< embed::XStorage > & xParentStorage,
356 const OUString & rUri,
359 uno::Reference< embed::XStorage > xStorage;
363 if ( !xParentStorage.is() )
369 if ( !xStorage.is() )
372 throw lang::IllegalArgumentException(
373 "Invalid open mode: document storages cannot be created!",
374 uno::Reference< uno::XInterface >(),
377 throw embed::InvalidStorageException(
378 "Invalid document id!",
379 uno::Reference< uno::XInterface >() );
384 uno::Reference< beans::XPropertySet > xPropSet(
385 xStorage, uno::UNO_QUERY );
386 OSL_ENSURE( xPropSet.is(),
387 "StorageElementFactory::queryStorage - "
388 "No XPropertySet interface!" );
391 uno::Any aPropValue = xPropSet->getPropertyValue(
"OpenMode");
393 sal_Int32 nOpenMode = 0;
394 if ( aPropValue >>= nOpenMode )
402 throw embed::InvalidStorageException(
403 "Storage is open, but not readable!" );
410 if ( !( nOpenMode & embed::ElementModes::WRITE ) )
413 throw embed::InvalidStorageException(
414 "Storage is open, but not writable!" );
423 "Bug! Value of property OpenMode has wrong type!" );
425 throw uno::RuntimeException(
426 "Bug! Value of property OpenMode has wrong type!" );
429 catch ( beans::UnknownPropertyException
const & )
432 OSL_FAIL(
"Property OpenMode not supported!" );
434 throw embed::StorageWrappedTargetException(
435 "Bug! Value of property OpenMode has wrong type!",
436 uno::Reference< uno::XInterface >(),
439 catch ( lang::WrappedTargetException
const & )
442 OSL_FAIL(
"Caught WrappedTargetException!" );
444 throw embed::StorageWrappedTargetException(
445 "WrappedTargetException during getPropertyValue!",
446 uno::Reference< uno::XInterface >(),
461 | embed::ElementModes::NOCREATE;
463 = xParentStorage->openStorageElement( rName, nOpenMode );
465 catch ( io::IOException
const & )
469 xParentStorage->copyStorageElementLastCommitTo( rName,
475 sal_Int32 nOpenMode = embed::ElementModes::READWRITE;
477 nOpenMode |= embed::ElementModes::NOCREATE;
479 xStorage = xParentStorage->openStorageElement( rName, nOpenMode );
484 "StorageElementFactory::queryStorage - No storage!" );
489uno::Reference< io::XStream >
491 const uno::Reference< embed::XStorage > & xParentStorage,
492 const OUString & rUri,
493 const OUString & rPassword,
499 if ( !xParentStorage.is() )
501 throw lang::IllegalArgumentException(
502 "No parent storage!",
503 uno::Reference< uno::XInterface >(),
510 throw lang::IllegalArgumentException(
511 "Root never is a stream!",
512 uno::Reference< uno::XInterface >(),
517 throw lang::IllegalArgumentException(
518 "A document never is a stream!",
519 uno::Reference< uno::XInterface >(),
528 | embed::ElementModes::NOCREATE
529 | embed::ElementModes::SEEKABLE;
533 nOpenMode = embed::ElementModes::READWRITE
534 | embed::ElementModes::NOCREATE
535 | embed::ElementModes::SEEKABLE;
538 nOpenMode |= embed::ElementModes::TRUNCATE;
543 nOpenMode = embed::ElementModes::READWRITE
544 | embed::ElementModes::SEEKABLE;
547 nOpenMode |= embed::ElementModes::TRUNCATE;
552 OSL_FAIL(
"StorageElementFactory::queryStream : Unknown open mode!" );
554 throw embed::InvalidStorageException(
555 "Unknown open mode!",
556 uno::Reference< uno::XInterface >() );
561 uno::Reference< io::XStream >
xStream;
562 if ( !rPassword.isEmpty() )
568 xStream = xParentStorage->cloneEncryptedStreamElement(
572 catch ( packages::NoEncryptionException
const & )
582 xStream = xParentStorage->openEncryptedStreamElement(
587 catch ( packages::NoEncryptionException
const & )
610 throw embed::InvalidStorageException(
612 uno::Reference< uno::XInterface >() );
Reference< XComponentContext > m_xContext
css::uno::Reference< css::embed::XStorage > createStorage(const OUString &rUri, StorageAccessMode eMode)
rtl::Reference< OfficeDocumentsManager > m_xDocsMgr
css::uno::Reference< css::embed::XStorage > queryParentStorage(const OUString &rUri, StorageAccessMode eMode)
css::uno::Reference< css::io::XInputStream > createInputStream(const OUString &rUri, const OUString &rPassword)
css::uno::Reference< css::io::XStream > queryStream(const css::uno::Reference< css::embed::XStorage > &xParentStorage, const OUString &rUri, const OUString &rPassword, StorageAccessMode eMode, bool bTruncate)
css::uno::Reference< css::io::XOutputStream > createOutputStream(const OUString &rUri, const OUString &rPassword, bool bTruncate)
css::uno::Reference< css::io::XStream > createStream(const OUString &rUri, const OUString &rPassword, bool bTruncate)
virtual ~StorageElementFactory() override
void releaseElement(Storage const *pElement)
css::uno::Reference< css::uno::XComponentContext > m_xContext
css::uno::Reference< css::embed::XStorage > queryStorage(const css::uno::Reference< css::embed::XStorage > &xParentStorage, const OUString &rUri, StorageAccessMode eMode)
css::uno::Reference< css::embed::XStorage > createTemporaryStorage()
StorageElementFactory::StorageMap::iterator m_aContainerIt
const OUString & getDecodedName() const
const OUString & getDocumentId() const
const OUString & getParentUri() const
Any SAL_CALL getCaughtException()