Update ooo320-m1
[ooovba.git] / oox / source / helper / propertymap.cxx
bloba40b4a737bd33ac12199e6e5093b887bbeec715d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propertymap.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/helper/propertymap.hxx"
32 #include <osl/mutex.hxx>
33 #include <cppuhelper/implbase2.hxx>
34 #include <com/sun/star/beans/PropertyValue.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/beans/XPropertySetInfo.hpp>
37 #include "properties.hxx"
38 #include "oox/token/propertylist.hxx"
40 using ::rtl::OUString;
41 using ::com::sun::star::uno::Any;
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::RuntimeException;
44 using ::com::sun::star::uno::Sequence;
45 using ::com::sun::star::lang::IllegalArgumentException;
46 using ::com::sun::star::lang::WrappedTargetException;
47 using ::com::sun::star::beans::Property;
48 using ::com::sun::star::beans::PropertyValue;
49 using ::com::sun::star::beans::PropertyVetoException;
50 using ::com::sun::star::beans::UnknownPropertyException;
51 using ::com::sun::star::beans::XPropertyChangeListener;
52 using ::com::sun::star::beans::XPropertySet;
53 using ::com::sun::star::beans::XPropertySetInfo;
54 using ::com::sun::star::beans::XVetoableChangeListener;
56 namespace oox {
58 // ============================================================================
60 namespace {
62 /** Thread-save singleton of a vector of all supported property names. */
63 struct StaticPropertyList : public ::rtl::Static< PropertyList, StaticPropertyList > {};
65 // ----------------------------------------------------------------------------
67 typedef ::cppu::WeakImplHelper2< XPropertySet, XPropertySetInfo > GenericPropertySetImplBase;
69 /** This class implements a generic XPropertySet.
71 Properties of all names and types can be set and later retrieved.
72 TODO: move this to comphelper or better find an existing implementation
74 class GenericPropertySet : public GenericPropertySetImplBase, private ::osl::Mutex
76 public:
77 explicit GenericPropertySet();
78 explicit GenericPropertySet( const PropertyMap& rPropMap );
80 // XPropertySet
81 virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo() throw (RuntimeException);
82 virtual void SAL_CALL setPropertyValue( const OUString& aPropertyName, const Any& aValue ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException);
83 virtual Any SAL_CALL getPropertyValue( const OUString& PropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
84 virtual void SAL_CALL addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
85 virtual void SAL_CALL removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& aListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
86 virtual void SAL_CALL addVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
87 virtual void SAL_CALL removeVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
89 // XPropertySetInfo
90 virtual Sequence< Property > SAL_CALL getProperties() throw (RuntimeException);
91 virtual Property SAL_CALL getPropertyByName( const OUString& aName ) throw (UnknownPropertyException, RuntimeException);
92 virtual sal_Bool SAL_CALL hasPropertyByName( const OUString& Name ) throw (RuntimeException);
94 private:
95 typedef ::std::map< OUString, Any > PropertyNameMap;
96 PropertyNameMap maPropMap;
99 // ----------------------------------------------------------------------------
101 GenericPropertySet::GenericPropertySet()
105 GenericPropertySet::GenericPropertySet( const PropertyMap& rPropMap )
107 const PropertyList& rPropNames = StaticPropertyList::get();
108 for( PropertyMap::const_iterator aIt = rPropMap.begin(), aEnd = rPropMap.end(); aIt != aEnd; ++aIt )
109 maPropMap[ rPropNames[ aIt->first ] ] = aIt->second;
112 Reference< XPropertySetInfo > SAL_CALL GenericPropertySet::getPropertySetInfo() throw (RuntimeException)
114 return this;
117 void SAL_CALL GenericPropertySet::setPropertyValue( const OUString& rPropertyName, const Any& rValue ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
119 ::osl::MutexGuard aGuard( *this );
120 maPropMap[ rPropertyName ] = rValue;
123 Any SAL_CALL GenericPropertySet::getPropertyValue( const OUString& rPropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
125 PropertyNameMap::iterator aIt = maPropMap.find( rPropertyName );
126 if( aIt == maPropMap.end() )
127 throw UnknownPropertyException();
128 return aIt->second;
131 // listeners are not supported by this implementation
132 void SAL_CALL GenericPropertySet::addPropertyChangeListener( const OUString& , const Reference< XPropertyChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
133 void SAL_CALL GenericPropertySet::removePropertyChangeListener( const OUString& , const Reference< XPropertyChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
134 void SAL_CALL GenericPropertySet::addVetoableChangeListener( const OUString& , const Reference< XVetoableChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
135 void SAL_CALL GenericPropertySet::removeVetoableChangeListener( const OUString& , const Reference< XVetoableChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
137 // XPropertySetInfo
138 Sequence< Property > SAL_CALL GenericPropertySet::getProperties() throw (RuntimeException)
140 Sequence< Property > aSeq( static_cast< sal_Int32 >( maPropMap.size() ) );
141 Property* pProperty = aSeq.getArray();
142 for( PropertyNameMap::iterator aIt = maPropMap.begin(), aEnd = maPropMap.end(); aIt != aEnd; ++aIt, ++pProperty )
144 pProperty->Name = aIt->first;
145 pProperty->Handle = 0;
146 pProperty->Type = aIt->second.getValueType();
147 pProperty->Attributes = 0;
149 return aSeq;
152 Property SAL_CALL GenericPropertySet::getPropertyByName( const OUString& rPropertyName ) throw (UnknownPropertyException, RuntimeException)
154 PropertyNameMap::iterator aIt = maPropMap.find( rPropertyName );
155 if( aIt == maPropMap.end() )
156 throw UnknownPropertyException();
157 Property aProperty;
158 aProperty.Name = aIt->first;
159 aProperty.Handle = 0;
160 aProperty.Type = aIt->second.getValueType();
161 aProperty.Attributes = 0;
162 return aProperty;
165 sal_Bool SAL_CALL GenericPropertySet::hasPropertyByName( const OUString& rPropertyName ) throw (RuntimeException)
167 return maPropMap.find( rPropertyName ) != maPropMap.end();
170 } // namespace
172 // ============================================================================
174 PropertyMap::PropertyMap() :
175 mpPropNames( &StaticPropertyList::get() )
179 PropertyMap::~PropertyMap()
183 /*static*/ const OUString& PropertyMap::getPropertyName( sal_Int32 nPropId )
185 OSL_ENSURE( (0 <= nPropId) && (nPropId < PROP_COUNT), "PropertyMap::getPropertyName - invalid property identifier" );
186 return StaticPropertyList::get()[ nPropId ];
189 const Any* PropertyMap::getProperty( sal_Int32 nPropId ) const
191 const_iterator aIt = find( nPropId );
192 return (aIt == end()) ? 0 : &aIt->second;
195 Sequence< PropertyValue > PropertyMap::makePropertyValueSequence() const
197 Sequence< PropertyValue > aSeq( static_cast< sal_Int32 >( size() ) );
198 if( !empty() )
200 PropertyValue* pValues = aSeq.getArray();
201 for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt, ++pValues )
203 OSL_ENSURE( (0 <= aIt->first) && (aIt->first < PROP_COUNT), "PropertyMap::makePropertyValueSequence - invalid property identifier" );
204 pValues->Name = (*mpPropNames)[ aIt->first ];
205 pValues->Value = aIt->second;
206 pValues->State = ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
209 return aSeq;
212 void PropertyMap::fillSequences( Sequence< OUString >& rNames, Sequence< Any >& rValues ) const
214 rNames.realloc( static_cast< sal_Int32 >( size() ) );
215 rValues.realloc( static_cast< sal_Int32 >( size() ) );
216 if( !empty() )
218 OUString* pNames = rNames.getArray();
219 Any* pValues = rValues.getArray();
220 for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt, ++pNames, ++pValues )
222 OSL_ENSURE( (0 <= aIt->first) && (aIt->first < PROP_COUNT), "PropertyMap::fillSequences - invalid property identifier" );
223 *pNames = (*mpPropNames)[ aIt->first ];
224 *pValues = aIt->second;
229 Reference< XPropertySet > PropertyMap::makePropertySet() const
231 return new GenericPropertySet( *this );
234 // ============================================================================
236 } // namespace oox