2006-07-10 15:15:46 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-11 10:13:50 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2006-07-10 15:15:46 +00:00
|
|
|
*
|
2010-02-12 15:01:35 +01:00
|
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
2006-07-10 15:15:46 +00:00
|
|
|
*
|
2008-04-11 10:13:50 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2006-07-10 15:15:46 +00:00
|
|
|
*
|
2008-04-11 10:13:50 +00:00
|
|
|
* This file is part of OpenOffice.org.
|
2006-07-10 15:15:46 +00:00
|
|
|
*
|
2008-04-11 10:13:50 +00:00
|
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
* only, as published by the Free Software Foundation.
|
2006-07-10 15:15:46 +00:00
|
|
|
*
|
2008-04-11 10:13:50 +00:00
|
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License version 3 for more details
|
|
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
2006-07-10 15:15:46 +00:00
|
|
|
*
|
2008-04-11 10:13:50 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
|
|
* <http://www.openoffice.org/license.html>
|
|
|
|
* for a copy of the LGPLv3 License.
|
2006-07-10 15:15:46 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
2006-09-17 16:12:43 +00:00
|
|
|
// MARKER(update_precomp.py): autogen include statement, do not remove
|
|
|
|
#include "precompiled_comphelper.hxx"
|
2006-07-10 15:15:46 +00:00
|
|
|
#include <comphelper/namedvaluecollection.hxx>
|
|
|
|
|
|
|
|
/** === begin UNO includes === **/
|
|
|
|
#include <com/sun/star/beans/NamedValue.hpp>
|
2007-01-15 13:44:35 +00:00
|
|
|
#include <com/sun/star/lang/IllegalArgumentException.hpp>
|
2007-11-21 15:53:08 +00:00
|
|
|
#include <com/sun/star/beans/PropertyState.hpp>
|
2006-07-10 15:15:46 +00:00
|
|
|
/** === end UNO includes === **/
|
|
|
|
|
2007-01-15 13:44:35 +00:00
|
|
|
#include <rtl/ustrbuf.hxx>
|
2008-08-19 09:35:34 +00:00
|
|
|
#include <rtl/strbuf.hxx>
|
2006-07-10 15:15:46 +00:00
|
|
|
#include <osl/diagnose.h>
|
|
|
|
|
|
|
|
#include <hash_map>
|
2007-11-21 15:53:08 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <algorithm>
|
2006-07-10 15:15:46 +00:00
|
|
|
|
|
|
|
//........................................................................
|
|
|
|
namespace comphelper
|
|
|
|
{
|
|
|
|
//........................................................................
|
|
|
|
|
|
|
|
/** === begin UNO using === **/
|
|
|
|
using ::com::sun::star::uno::Any;
|
|
|
|
using ::com::sun::star::uno::Sequence;
|
|
|
|
using ::com::sun::star::beans::PropertyValue;
|
|
|
|
using ::com::sun::star::beans::NamedValue;
|
|
|
|
using ::com::sun::star::uno::Type;
|
|
|
|
using ::com::sun::star::uno::cpp_acquire;
|
|
|
|
using ::com::sun::star::uno::cpp_release;
|
|
|
|
using ::com::sun::star::uno::cpp_queryInterface;
|
2007-01-15 13:44:35 +00:00
|
|
|
using ::com::sun::star::lang::IllegalArgumentException;
|
2006-12-01 16:32:40 +00:00
|
|
|
using ::com::sun::star::beans::NamedValue;
|
2007-11-21 15:53:08 +00:00
|
|
|
using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
|
2006-07-10 15:15:46 +00:00
|
|
|
/** === end UNO using === **/
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
//= NamedValueCollection_Impl
|
|
|
|
//====================================================================
|
|
|
|
typedef ::std::hash_map< ::rtl::OUString, Any, ::rtl::OUStringHash > NamedValueRepository;
|
|
|
|
|
|
|
|
struct NamedValueCollection_Impl
|
|
|
|
{
|
|
|
|
NamedValueRepository aValues;
|
|
|
|
};
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
//= NamedValueCollection
|
|
|
|
//====================================================================
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
NamedValueCollection::NamedValueCollection()
|
|
|
|
:m_pImpl( new NamedValueCollection_Impl )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-05 15:19:57 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
NamedValueCollection::NamedValueCollection( const NamedValueCollection& _rCopySource )
|
|
|
|
:m_pImpl( new NamedValueCollection_Impl )
|
|
|
|
{
|
2010-02-02 09:53:26 +01:00
|
|
|
*this = _rCopySource;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
NamedValueCollection& NamedValueCollection::operator=( const NamedValueCollection& i_rCopySource )
|
|
|
|
{
|
|
|
|
m_pImpl->aValues = i_rCopySource.m_pImpl->aValues;
|
|
|
|
return *this;
|
2008-05-05 15:19:57 +00:00
|
|
|
}
|
|
|
|
|
2008-03-06 19:00:01 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
NamedValueCollection::NamedValueCollection( const Any& _rElements )
|
|
|
|
:m_pImpl( new NamedValueCollection_Impl )
|
|
|
|
{
|
|
|
|
Sequence< NamedValue > aNamedValues;
|
|
|
|
Sequence< PropertyValue > aPropertyValues;
|
|
|
|
NamedValue aNamedValue;
|
|
|
|
PropertyValue aPropertyValue;
|
|
|
|
|
|
|
|
if ( _rElements >>= aNamedValues )
|
|
|
|
impl_assign( aNamedValues );
|
|
|
|
else if ( _rElements >>= aPropertyValues )
|
|
|
|
impl_assign( aPropertyValues );
|
|
|
|
else if ( _rElements >>= aNamedValue )
|
|
|
|
impl_assign( Sequence< NamedValue >( &aNamedValue, 1 ) );
|
|
|
|
else if ( _rElements >>= aPropertyValue )
|
|
|
|
impl_assign( Sequence< PropertyValue >( &aPropertyValue, 1 ) );
|
|
|
|
else
|
2008-10-16 06:57:26 +00:00
|
|
|
OSL_ENSURE( !_rElements.hasValue(), "NamedValueCollection::NamedValueCollection(Any): unsupported type!" );
|
2008-03-06 19:00:01 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 15:15:46 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
NamedValueCollection::NamedValueCollection( const Sequence< Any >& _rArguments )
|
|
|
|
:m_pImpl( new NamedValueCollection_Impl )
|
|
|
|
{
|
|
|
|
impl_assign( _rArguments );
|
|
|
|
}
|
|
|
|
|
2006-08-04 12:58:47 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
NamedValueCollection::NamedValueCollection( const Sequence< PropertyValue >& _rArguments )
|
|
|
|
:m_pImpl( new NamedValueCollection_Impl )
|
|
|
|
{
|
|
|
|
impl_assign( _rArguments );
|
|
|
|
}
|
|
|
|
|
2006-12-01 16:32:40 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
NamedValueCollection::NamedValueCollection( const Sequence< NamedValue >& _rArguments )
|
|
|
|
:m_pImpl( new NamedValueCollection_Impl )
|
|
|
|
{
|
|
|
|
impl_assign( _rArguments );
|
|
|
|
}
|
|
|
|
|
2006-07-10 15:15:46 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
NamedValueCollection::~NamedValueCollection()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-06-27 13:55:20 +00:00
|
|
|
//--------------------------------------------------------------------
|
2008-05-05 15:19:57 +00:00
|
|
|
NamedValueCollection& NamedValueCollection::merge( const NamedValueCollection& _rAdditionalValues, bool _bOverwriteExisting )
|
|
|
|
{
|
|
|
|
for ( NamedValueRepository::const_iterator namedValue = _rAdditionalValues.m_pImpl->aValues.begin();
|
|
|
|
namedValue != _rAdditionalValues.m_pImpl->aValues.end();
|
|
|
|
++namedValue
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if ( _bOverwriteExisting || !impl_has( namedValue->first ) )
|
|
|
|
impl_put( namedValue->first, namedValue->second );
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2007-06-27 13:55:20 +00:00
|
|
|
size_t NamedValueCollection::size() const
|
|
|
|
{
|
|
|
|
return m_pImpl->aValues.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
bool NamedValueCollection::empty() const
|
|
|
|
{
|
|
|
|
return m_pImpl->aValues.empty();
|
|
|
|
}
|
|
|
|
|
2006-07-10 15:15:46 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
void NamedValueCollection::impl_assign( const Sequence< Any >& _rArguments )
|
|
|
|
{
|
|
|
|
{
|
2007-06-27 13:55:20 +00:00
|
|
|
NamedValueRepository aEmpty;
|
|
|
|
m_pImpl->aValues.swap( aEmpty );
|
2006-07-10 15:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PropertyValue aPropertyValue;
|
|
|
|
NamedValue aNamedValue;
|
|
|
|
|
|
|
|
const Any* pArgument = _rArguments.getConstArray();
|
|
|
|
const Any* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
|
|
|
|
for ( ; pArgument != pArgumentEnd; ++pArgument )
|
|
|
|
{
|
|
|
|
if ( *pArgument >>= aPropertyValue )
|
|
|
|
m_pImpl->aValues[ aPropertyValue.Name ] = aPropertyValue.Value;
|
|
|
|
else if ( *pArgument >>= aNamedValue )
|
|
|
|
m_pImpl->aValues[ aNamedValue.Name ] = aNamedValue.Value;
|
2008-08-19 09:35:34 +00:00
|
|
|
#if OSL_DEBUG_LEVEL > 0
|
|
|
|
else if ( pArgument->hasValue() )
|
|
|
|
{
|
|
|
|
::rtl::OStringBuffer message;
|
|
|
|
message.append( "NamedValueCollection::impl_assign: encountered a value type which I cannot handle:\n" );
|
|
|
|
message.append( ::rtl::OUStringToOString( pArgument->getValueTypeName(), RTL_TEXTENCODING_ASCII_US ) );
|
|
|
|
OSL_ENSURE( false, message.makeStringAndClear() );
|
|
|
|
}
|
|
|
|
#endif
|
2006-07-10 15:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-04 12:58:47 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
void NamedValueCollection::impl_assign( const Sequence< PropertyValue >& _rArguments )
|
|
|
|
{
|
|
|
|
{
|
2007-06-27 13:55:20 +00:00
|
|
|
NamedValueRepository aEmpty;
|
|
|
|
m_pImpl->aValues.swap( aEmpty );
|
2006-08-04 12:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PropertyValue* pArgument = _rArguments.getConstArray();
|
|
|
|
const PropertyValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
|
|
|
|
for ( ; pArgument != pArgumentEnd; ++pArgument )
|
|
|
|
m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
|
|
|
|
}
|
|
|
|
|
2006-12-01 16:32:40 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
void NamedValueCollection::impl_assign( const Sequence< NamedValue >& _rArguments )
|
|
|
|
{
|
|
|
|
{
|
2007-06-27 13:55:20 +00:00
|
|
|
NamedValueRepository aEmpty;
|
|
|
|
m_pImpl->aValues.swap( aEmpty );
|
2006-12-01 16:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const NamedValue* pArgument = _rArguments.getConstArray();
|
|
|
|
const NamedValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
|
|
|
|
for ( ; pArgument != pArgumentEnd; ++pArgument )
|
|
|
|
m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
|
|
|
|
}
|
|
|
|
|
2006-07-10 15:15:46 +00:00
|
|
|
//--------------------------------------------------------------------
|
2007-01-15 13:44:35 +00:00
|
|
|
bool NamedValueCollection::get_ensureType( const ::rtl::OUString& _rValueName, void* _pValueLocation, const Type& _rExpectedValueType ) const
|
2006-07-10 15:15:46 +00:00
|
|
|
{
|
|
|
|
NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
|
|
|
|
if ( pos != m_pImpl->aValues.end() )
|
|
|
|
{
|
2007-01-15 13:44:35 +00:00
|
|
|
if ( uno_type_assignData(
|
|
|
|
_pValueLocation, _rExpectedValueType.getTypeLibType(),
|
|
|
|
const_cast< void* >( pos->second.getValue() ), pos->second.getValueType().getTypeLibType(),
|
|
|
|
reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
|
|
|
|
reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
|
|
|
|
reinterpret_cast< uno_ReleaseFunc >( cpp_release )
|
|
|
|
) )
|
|
|
|
// argument exists, and could be extracted
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// argument exists, but is of wrong type
|
|
|
|
::rtl::OUStringBuffer aBuffer;
|
|
|
|
aBuffer.appendAscii( "Invalid value type for '" );
|
|
|
|
aBuffer.append ( _rValueName );
|
|
|
|
aBuffer.appendAscii( "'.\nExpected: " );
|
|
|
|
aBuffer.append ( _rExpectedValueType.getTypeName() );
|
|
|
|
aBuffer.appendAscii( "\nFound: " );
|
|
|
|
aBuffer.append ( pos->second.getValueType().getTypeName() );
|
|
|
|
throw IllegalArgumentException( aBuffer.makeStringAndClear(), NULL, 0 );
|
2006-07-10 15:15:46 +00:00
|
|
|
}
|
2007-01-15 13:44:35 +00:00
|
|
|
|
|
|
|
// argument does not exist
|
|
|
|
return false;
|
2006-07-10 15:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
const Any& NamedValueCollection::impl_get( const ::rtl::OUString& _rValueName ) const
|
|
|
|
{
|
|
|
|
NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
|
|
|
|
if ( pos != m_pImpl->aValues.end() )
|
|
|
|
return pos->second;
|
|
|
|
|
|
|
|
static Any aEmptyDefault;
|
|
|
|
return aEmptyDefault;
|
|
|
|
}
|
|
|
|
|
2007-01-15 13:44:35 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
bool NamedValueCollection::impl_has( const ::rtl::OUString& _rValueName ) const
|
|
|
|
{
|
|
|
|
NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
|
|
|
|
return ( pos != m_pImpl->aValues.end() );
|
|
|
|
}
|
|
|
|
|
2007-11-21 15:53:08 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
bool NamedValueCollection::impl_put( const ::rtl::OUString& _rValueName, const Any& _rValue )
|
|
|
|
{
|
|
|
|
bool bHas = impl_has( _rValueName );
|
|
|
|
m_pImpl->aValues[ _rValueName ] = _rValue;
|
|
|
|
return bHas;
|
|
|
|
}
|
|
|
|
|
2007-06-27 13:55:20 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
bool NamedValueCollection::impl_remove( const ::rtl::OUString& _rValueName )
|
|
|
|
{
|
|
|
|
NamedValueRepository::iterator pos = m_pImpl->aValues.find( _rValueName );
|
|
|
|
if ( pos == m_pImpl->aValues.end() )
|
|
|
|
return false;
|
|
|
|
m_pImpl->aValues.erase( pos );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-11-21 15:53:08 +00:00
|
|
|
//--------------------------------------------------------------------
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct Value2PropertyValue : public ::std::unary_function< NamedValueRepository::value_type, PropertyValue >
|
|
|
|
{
|
|
|
|
PropertyValue operator()( const NamedValueRepository::value_type& _rValue )
|
|
|
|
{
|
|
|
|
return PropertyValue(
|
|
|
|
_rValue.first, 0, _rValue.second, PropertyState_DIRECT_VALUE );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Value2NamedValue : public ::std::unary_function< NamedValueRepository::value_type, NamedValue >
|
|
|
|
{
|
|
|
|
NamedValue operator()( const NamedValueRepository::value_type& _rValue )
|
|
|
|
{
|
|
|
|
return NamedValue( _rValue.first, _rValue.second );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2008-03-06 19:00:01 +00:00
|
|
|
sal_Int32 NamedValueCollection::operator >>= ( Sequence< PropertyValue >& _out_rValues ) const
|
2007-11-21 15:53:08 +00:00
|
|
|
{
|
|
|
|
_out_rValues.realloc( m_pImpl->aValues.size() );
|
|
|
|
::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2PropertyValue() );
|
|
|
|
return _out_rValues.getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2008-03-06 19:00:01 +00:00
|
|
|
sal_Int32 NamedValueCollection::operator >>= ( Sequence< NamedValue >& _out_rValues ) const
|
2007-11-21 15:53:08 +00:00
|
|
|
{
|
|
|
|
_out_rValues.realloc( m_pImpl->aValues.size() );
|
|
|
|
::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2NamedValue() );
|
|
|
|
return _out_rValues.getLength();
|
|
|
|
}
|
|
|
|
|
2006-07-10 15:15:46 +00:00
|
|
|
//........................................................................
|
|
|
|
} // namespace comphelper
|
|
|
|
//........................................................................
|
|
|
|
|