1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "ocompinstream.hxx"
21 #include <com/sun/star/embed/StorageFormats.hpp>
22 #include <com/sun/star/io/IOException.hpp>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <comphelper/sequence.hxx>
25 #include <cppuhelper/queryinterface.hxx>
26 #include <osl/diagnose.h>
27 #include <sal/log.hxx>
30 #include "owriteablestream.hxx"
32 using namespace ::com::sun::star
;
34 OInputCompStream::OInputCompStream( OWriteStream_Impl
& aImpl
,
35 uno::Reference
< io::XInputStream
> xStream
,
36 const uno::Sequence
< beans::PropertyValue
>& aProps
,
37 sal_Int32 nStorageType
)
39 , m_xMutex( m_pImpl
->m_xMutex
)
40 , m_xStream(std::move( xStream
))
41 , m_aProperties( aProps
)
42 , m_bDisposed( false )
43 , m_nStorageType( nStorageType
)
45 OSL_ENSURE( m_pImpl
->m_xMutex
.is(), "No mutex is provided!" );
46 if ( !m_pImpl
->m_xMutex
.is() )
47 throw uno::RuntimeException(); // just a disaster
49 assert(m_xStream
.is());
52 OInputCompStream::OInputCompStream( uno::Reference
< io::XInputStream
> xStream
,
53 const uno::Sequence
< beans::PropertyValue
>& aProps
,
54 sal_Int32 nStorageType
)
56 , m_xMutex( new comphelper::RefCountedMutex
)
57 , m_xStream(std::move( xStream
))
58 , m_aProperties( aProps
)
59 , m_bDisposed( false )
60 , m_nStorageType( nStorageType
)
62 assert(m_xStream
.is());
65 OInputCompStream::~OInputCompStream()
67 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
71 osl_atomic_increment(&m_refCount
);
76 uno::Any SAL_CALL
OInputCompStream::queryInterface( const uno::Type
& rType
)
79 uno::Any aReturn
= ::cppu::queryInterface
81 , static_cast<io::XInputStream
*> ( this )
82 , static_cast<io::XStream
*> ( this )
83 , static_cast<lang::XComponent
*> ( this )
84 , static_cast<beans::XPropertySet
*> ( this )
85 , static_cast<embed::XExtendedStorageStream
*> ( this ) );
87 if ( aReturn
.hasValue() )
90 if ( m_nStorageType
== embed::StorageFormats::OFOPXML
)
92 aReturn
= ::cppu::queryInterface
94 , static_cast<embed::XRelationshipAccess
*> ( this ) );
96 if ( aReturn
.hasValue() )
100 return OWeakObject::queryInterface( rType
);
103 sal_Int32 SAL_CALL
OInputCompStream::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
105 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
108 SAL_INFO("package.xstor", "Disposed!");
109 throw lang::DisposedException();
112 return m_xStream
->readBytes( aData
, nBytesToRead
);
115 sal_Int32 SAL_CALL
OInputCompStream::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
117 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
120 SAL_INFO("package.xstor", "Disposed!");
121 throw lang::DisposedException();
124 return m_xStream
->readSomeBytes( aData
, nMaxBytesToRead
);
128 void SAL_CALL
OInputCompStream::skipBytes( sal_Int32 nBytesToSkip
)
130 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
133 SAL_INFO("package.xstor", "Disposed!");
134 throw lang::DisposedException();
137 m_xStream
->skipBytes( nBytesToSkip
);
141 sal_Int32 SAL_CALL
OInputCompStream::available( )
143 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
146 SAL_INFO("package.xstor", "Disposed!");
147 throw lang::DisposedException();
150 return m_xStream
->available();
154 void SAL_CALL
OInputCompStream::closeInput( )
159 uno::Reference
< io::XInputStream
> SAL_CALL
OInputCompStream::getInputStream()
161 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
164 SAL_INFO("package.xstor", "Disposed!");
165 throw lang::DisposedException();
171 uno::Reference
< io::XOutputStream
> SAL_CALL
OInputCompStream::getOutputStream()
173 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
176 SAL_INFO("package.xstor", "Disposed!");
177 throw lang::DisposedException();
180 return uno::Reference
< io::XOutputStream
>();
183 void OInputCompStream::InternalDispose()
185 // can be called only by OWriteStream_Impl
186 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
190 // the source object is also a kind of locker for the current object
191 // since the listeners could dispose the object while being notified
192 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
*>( this ) );
194 if ( m_pInterfaceContainer
)
195 m_pInterfaceContainer
->disposeAndClear( aSource
);
199 m_xStream
->closeInput();
201 catch( uno::Exception
& )
208 void SAL_CALL
OInputCompStream::dispose( )
210 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
214 if ( m_pInterfaceContainer
)
216 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
*>( this ) );
217 m_pInterfaceContainer
->disposeAndClear( aSource
);
220 m_xStream
->closeInput();
224 m_pImpl
->InputStreamDisposed( this );
231 void SAL_CALL
OInputCompStream::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
233 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
236 SAL_INFO("package.xstor", "Disposed!");
237 throw lang::DisposedException();
240 if ( !m_pInterfaceContainer
)
241 m_pInterfaceContainer
.reset( new ::comphelper::OInterfaceContainerHelper3
<css::lang::XEventListener
>( m_xMutex
->GetMutex() ) );
243 m_pInterfaceContainer
->addInterface( xListener
);
246 void SAL_CALL
OInputCompStream::removeEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
248 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
251 SAL_INFO("package.xstor", "Disposed!");
252 throw lang::DisposedException();
255 if ( m_pInterfaceContainer
)
256 m_pInterfaceContainer
->removeInterface( xListener
);
259 sal_Bool SAL_CALL
OInputCompStream::hasByID( const OUString
& sID
)
261 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
265 SAL_INFO("package.xstor", "Disposed!");
266 throw lang::DisposedException();
269 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
270 throw uno::RuntimeException();
274 getRelationshipByID( sID
);
277 catch( container::NoSuchElementException
& )
286 const beans::StringPair
* lcl_findPairByName(const uno::Sequence
<beans::StringPair
>& rSeq
, const OUString
& rName
)
288 return std::find_if(rSeq
.begin(), rSeq
.end(),
289 [&rName
](const beans::StringPair
& rPair
) { return rPair
.First
== rName
; });
294 OUString SAL_CALL
OInputCompStream::getTargetByID( const OUString
& sID
)
296 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
300 SAL_INFO("package.xstor", "Disposed!");
301 throw lang::DisposedException();
304 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
305 throw uno::RuntimeException();
307 const uno::Sequence
< beans::StringPair
> aSeq
= getRelationshipByID( sID
);
308 auto pRel
= lcl_findPairByName(aSeq
, "Target");
309 if (pRel
!= aSeq
.end())
315 OUString SAL_CALL
OInputCompStream::getTypeByID( const OUString
& sID
)
317 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
321 SAL_INFO("package.xstor", "Disposed!");
322 throw lang::DisposedException();
325 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
326 throw uno::RuntimeException();
328 const uno::Sequence
< beans::StringPair
> aSeq
= getRelationshipByID( sID
);
329 auto pRel
= lcl_findPairByName(aSeq
, "Type");
330 if (pRel
!= aSeq
.end())
336 uno::Sequence
< beans::StringPair
> SAL_CALL
OInputCompStream::getRelationshipByID( const OUString
& sID
)
338 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
342 SAL_INFO("package.xstor", "Disposed!");
343 throw lang::DisposedException();
346 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
347 throw uno::RuntimeException();
349 // TODO/LATER: in future the unification of the ID could be checked
350 const uno::Sequence
< uno::Sequence
< beans::StringPair
> > aSeq
= getAllRelationships();
351 const beans::StringPair
aIDRel("Id", sID
);
352 auto pRel
= std::find_if(aSeq
.begin(), aSeq
.end(),
353 [&aIDRel
](const uno::Sequence
<beans::StringPair
>& rRel
){
354 return std::find(rRel
.begin(), rRel
.end(), aIDRel
) != rRel
.end(); });
355 if (pRel
!= aSeq
.end())
358 throw container::NoSuchElementException();
361 uno::Sequence
< uno::Sequence
< beans::StringPair
> > SAL_CALL
OInputCompStream::getRelationshipsByType( const OUString
& sType
)
363 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
367 SAL_INFO("package.xstor", "Disposed!");
368 throw lang::DisposedException();
371 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
372 throw uno::RuntimeException();
374 // TODO/LATER: in future the unification of the ID could be checked
375 const uno::Sequence
< uno::Sequence
< beans::StringPair
> > aSeq
= getAllRelationships();
376 const beans::StringPair
aTypeRel("Type", sType
);
377 std::vector
< uno::Sequence
<beans::StringPair
> > aResult
;
378 aResult
.reserve(aSeq
.getLength());
380 std::copy_if(aSeq
.begin(), aSeq
.end(), std::back_inserter(aResult
),
381 [&aTypeRel
](const uno::Sequence
<beans::StringPair
>& rRel
) {
382 return std::find(rRel
.begin(), rRel
.end(), aTypeRel
) != rRel
.end(); });
384 return comphelper::containerToSequence(aResult
);
387 uno::Sequence
< uno::Sequence
< beans::StringPair
> > SAL_CALL
OInputCompStream::getAllRelationships()
389 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
393 SAL_INFO("package.xstor", "Disposed!");
394 throw lang::DisposedException();
397 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
398 throw uno::RuntimeException();
400 // TODO/LATER: in future the information could be taken directly from m_pImpl when possible
401 auto pProp
= std::find_if(std::cbegin(m_aProperties
), std::cend(m_aProperties
),
402 [](const beans::PropertyValue
& rProp
) { return rProp
.Name
== "RelationsInfo"; });
403 if (pProp
!= std::cend(m_aProperties
))
405 uno::Sequence
< uno::Sequence
< beans::StringPair
> > aResult
;
406 if (pProp
->Value
>>= aResult
)
410 throw io::IOException("relations info could not be read"); // the relations info could not be read
413 void SAL_CALL
OInputCompStream::insertRelationshipByID( const OUString
& /*sID*/, const uno::Sequence
< beans::StringPair
>& /*aEntry*/, sal_Bool
/*bReplace*/ )
415 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
419 SAL_INFO("package.xstor", "Disposed!");
420 throw lang::DisposedException();
423 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
424 throw uno::RuntimeException();
426 throw io::IOException(); // TODO: Access denied
429 void SAL_CALL
OInputCompStream::removeRelationshipByID( const OUString
& /*sID*/ )
431 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
435 SAL_INFO("package.xstor", "Disposed!");
436 throw lang::DisposedException();
439 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
440 throw uno::RuntimeException();
442 throw io::IOException(); // TODO: Access denied
445 void SAL_CALL
OInputCompStream::insertRelationships( const uno::Sequence
< uno::Sequence
< beans::StringPair
> >& /*aEntries*/, sal_Bool
/*bReplace*/ )
447 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
451 SAL_INFO("package.xstor", "Disposed!");
452 throw lang::DisposedException();
455 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
456 throw uno::RuntimeException();
458 throw io::IOException(); // TODO: Access denied
461 void SAL_CALL
OInputCompStream::clearRelationships()
463 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
467 SAL_INFO("package.xstor", "Disposed!");
468 throw lang::DisposedException();
471 if ( m_nStorageType
!= embed::StorageFormats::OFOPXML
)
472 throw uno::RuntimeException();
474 throw io::IOException(); // TODO: Access denied
477 uno::Reference
< beans::XPropertySetInfo
> SAL_CALL
OInputCompStream::getPropertySetInfo()
479 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
483 SAL_INFO("package.xstor", "Disposed!");
484 throw lang::DisposedException();
488 return uno::Reference
< beans::XPropertySetInfo
>();
491 void SAL_CALL
OInputCompStream::setPropertyValue( const OUString
& aPropertyName
, const uno::Any
& /*aValue*/ )
493 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
497 SAL_INFO("package.xstor", "Disposed!");
498 throw lang::DisposedException();
501 // all the provided properties are accessible
502 if (std::any_of(std::cbegin(m_aProperties
), std::cend(m_aProperties
),
503 [&aPropertyName
](const beans::PropertyValue
& rProp
) { return rProp
.Name
== aPropertyName
; }))
504 throw beans::PropertyVetoException(); // TODO
506 throw beans::UnknownPropertyException(aPropertyName
); // TODO
509 uno::Any SAL_CALL
OInputCompStream::getPropertyValue( const OUString
& aProp
)
511 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
515 SAL_INFO("package.xstor", "Disposed!");
516 throw lang::DisposedException();
519 OUString aPropertyName
;
520 if ( aProp
== "IsEncrypted" )
521 aPropertyName
= "Encrypted";
523 aPropertyName
= aProp
;
525 if ( aPropertyName
== "RelationsInfo" )
526 throw beans::UnknownPropertyException(aPropertyName
); // TODO
528 // all the provided properties are accessible
529 auto pProp
= std::find_if(std::cbegin(m_aProperties
), std::cend(m_aProperties
),
530 [&aPropertyName
](const beans::PropertyValue
& rProp
) { return rProp
.Name
== aPropertyName
; });
531 if (pProp
!= std::cend(m_aProperties
))
534 throw beans::UnknownPropertyException(aPropertyName
); // TODO
537 void SAL_CALL
OInputCompStream::addPropertyChangeListener(
538 const OUString
& /*aPropertyName*/,
539 const uno::Reference
< beans::XPropertyChangeListener
>& /*xListener*/ )
541 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
545 SAL_INFO("package.xstor", "Disposed!");
546 throw lang::DisposedException();
552 void SAL_CALL
OInputCompStream::removePropertyChangeListener(
553 const OUString
& /*aPropertyName*/,
554 const uno::Reference
< beans::XPropertyChangeListener
>& /*aListener*/ )
556 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
560 SAL_INFO("package.xstor", "Disposed!");
561 throw lang::DisposedException();
567 void SAL_CALL
OInputCompStream::addVetoableChangeListener(
568 const OUString
& /*PropertyName*/,
569 const uno::Reference
< beans::XVetoableChangeListener
>& /*aListener*/ )
571 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
575 SAL_INFO("package.xstor", "Disposed!");
576 throw lang::DisposedException();
582 void SAL_CALL
OInputCompStream::removeVetoableChangeListener(
583 const OUString
& /*PropertyName*/,
584 const uno::Reference
< beans::XVetoableChangeListener
>& /*aListener*/ )
586 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
590 SAL_INFO("package.xstor", "Disposed!");
591 throw lang::DisposedException();
597 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */