xref: /aoo41x/main/oox/source/helper/propertymap.cxx (revision ca5ec200)
1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/helper/propertymap.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
27cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
28cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySetInfo.hpp>
29cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
30cdf0e10cSrcweir #include <osl/mutex.hxx>
31cdf0e10cSrcweir #include "oox/token/propertynames.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir namespace oox {
34cdf0e10cSrcweir 
35cdf0e10cSrcweir // ============================================================================
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace ::com::sun::star::beans;
38cdf0e10cSrcweir using namespace ::com::sun::star::lang;
39cdf0e10cSrcweir using namespace ::com::sun::star::uno;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using ::rtl::OString;
42cdf0e10cSrcweir using ::rtl::OUString;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // ============================================================================
45cdf0e10cSrcweir 
46cdf0e10cSrcweir namespace {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir typedef ::cppu::WeakImplHelper2< XPropertySet, XPropertySetInfo > GenericPropertySetBase;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir /** This class implements a generic XPropertySet.
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     Properties of all names and types can be set and later retrieved.
53cdf0e10cSrcweir     TODO: move this to comphelper or better find an existing implementation
54cdf0e10cSrcweir  */
55cdf0e10cSrcweir class GenericPropertySet : public GenericPropertySetBase, private ::osl::Mutex
56cdf0e10cSrcweir {
57cdf0e10cSrcweir public:
58cdf0e10cSrcweir     explicit            GenericPropertySet();
59cdf0e10cSrcweir     explicit            GenericPropertySet( const PropertyMap& rPropMap );
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     // XPropertySet
62cdf0e10cSrcweir     virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo() throw (RuntimeException);
63cdf0e10cSrcweir     virtual void SAL_CALL setPropertyValue( const OUString& aPropertyName, const Any& aValue ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException);
64cdf0e10cSrcweir     virtual Any SAL_CALL getPropertyValue( const OUString& PropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
65cdf0e10cSrcweir     virtual void SAL_CALL addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
66cdf0e10cSrcweir     virtual void SAL_CALL removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& aListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
67cdf0e10cSrcweir     virtual void SAL_CALL addVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
68cdf0e10cSrcweir     virtual void SAL_CALL removeVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     // XPropertySetInfo
71cdf0e10cSrcweir     virtual Sequence< Property > SAL_CALL getProperties() throw (RuntimeException);
72cdf0e10cSrcweir     virtual Property SAL_CALL getPropertyByName( const OUString& aName ) throw (UnknownPropertyException, RuntimeException);
73cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasPropertyByName( const OUString& Name ) throw (RuntimeException);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir private:
76cdf0e10cSrcweir     typedef ::std::map< OUString, Any > PropertyNameMap;
77cdf0e10cSrcweir     PropertyNameMap     maPropMap;
78cdf0e10cSrcweir };
79cdf0e10cSrcweir 
80cdf0e10cSrcweir // ----------------------------------------------------------------------------
81cdf0e10cSrcweir 
GenericPropertySet()82cdf0e10cSrcweir GenericPropertySet::GenericPropertySet()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
GenericPropertySet(const PropertyMap & rPropMap)86cdf0e10cSrcweir GenericPropertySet::GenericPropertySet( const PropertyMap& rPropMap )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     const PropertyNameVector& rPropNames = StaticPropertyNameVector::get();
89cdf0e10cSrcweir     for( PropertyMap::const_iterator aIt = rPropMap.begin(), aEnd = rPropMap.end(); aIt != aEnd; ++aIt )
90cdf0e10cSrcweir         maPropMap[ rPropNames[ aIt->first ] ] = aIt->second;
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
getPropertySetInfo()93cdf0e10cSrcweir Reference< XPropertySetInfo > SAL_CALL GenericPropertySet::getPropertySetInfo() throw (RuntimeException)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir     return this;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
setPropertyValue(const OUString & rPropertyName,const Any & rValue)98cdf0e10cSrcweir void SAL_CALL GenericPropertySet::setPropertyValue( const OUString& rPropertyName, const Any& rValue ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     ::osl::MutexGuard aGuard( *this );
101cdf0e10cSrcweir     maPropMap[ rPropertyName ] = rValue;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
getPropertyValue(const OUString & rPropertyName)104cdf0e10cSrcweir Any SAL_CALL GenericPropertySet::getPropertyValue( const OUString& rPropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     PropertyNameMap::iterator aIt = maPropMap.find( rPropertyName );
107cdf0e10cSrcweir     if( aIt == maPropMap.end() )
108cdf0e10cSrcweir         throw UnknownPropertyException();
109cdf0e10cSrcweir     return aIt->second;
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir // listeners are not supported by this implementation
addPropertyChangeListener(const OUString &,const Reference<XPropertyChangeListener> &)113cdf0e10cSrcweir void SAL_CALL GenericPropertySet::addPropertyChangeListener( const OUString& , const Reference< XPropertyChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
removePropertyChangeListener(const OUString &,const Reference<XPropertyChangeListener> &)114cdf0e10cSrcweir void SAL_CALL GenericPropertySet::removePropertyChangeListener( const OUString& , const Reference< XPropertyChangeListener >&  ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
addVetoableChangeListener(const OUString &,const Reference<XVetoableChangeListener> &)115cdf0e10cSrcweir void SAL_CALL GenericPropertySet::addVetoableChangeListener( const OUString& , const Reference< XVetoableChangeListener >&  ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
removeVetoableChangeListener(const OUString &,const Reference<XVetoableChangeListener> &)116cdf0e10cSrcweir void SAL_CALL GenericPropertySet::removeVetoableChangeListener( const OUString& , const Reference< XVetoableChangeListener >&  ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
117cdf0e10cSrcweir 
118cdf0e10cSrcweir // XPropertySetInfo
getProperties()119cdf0e10cSrcweir Sequence< Property > SAL_CALL GenericPropertySet::getProperties() throw (RuntimeException)
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     Sequence< Property > aSeq( static_cast< sal_Int32 >( maPropMap.size() ) );
122cdf0e10cSrcweir     Property* pProperty = aSeq.getArray();
123cdf0e10cSrcweir     for( PropertyNameMap::iterator aIt = maPropMap.begin(), aEnd = maPropMap.end(); aIt != aEnd; ++aIt, ++pProperty )
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         pProperty->Name = aIt->first;
126cdf0e10cSrcweir         pProperty->Handle = 0;
127cdf0e10cSrcweir         pProperty->Type = aIt->second.getValueType();
128cdf0e10cSrcweir         pProperty->Attributes = 0;
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir     return aSeq;
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
getPropertyByName(const OUString & rPropertyName)133cdf0e10cSrcweir Property SAL_CALL GenericPropertySet::getPropertyByName( const OUString& rPropertyName ) throw (UnknownPropertyException, RuntimeException)
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     PropertyNameMap::iterator aIt = maPropMap.find( rPropertyName );
136cdf0e10cSrcweir     if( aIt == maPropMap.end() )
137cdf0e10cSrcweir         throw UnknownPropertyException();
138cdf0e10cSrcweir     Property aProperty;
139cdf0e10cSrcweir     aProperty.Name = aIt->first;
140cdf0e10cSrcweir     aProperty.Handle = 0;
141cdf0e10cSrcweir     aProperty.Type = aIt->second.getValueType();
142cdf0e10cSrcweir     aProperty.Attributes = 0;
143cdf0e10cSrcweir     return aProperty;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
hasPropertyByName(const OUString & rPropertyName)146cdf0e10cSrcweir sal_Bool SAL_CALL GenericPropertySet::hasPropertyByName( const OUString& rPropertyName ) throw (RuntimeException)
147cdf0e10cSrcweir {
148cdf0e10cSrcweir     return maPropMap.find( rPropertyName ) != maPropMap.end();
149cdf0e10cSrcweir }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir } // namespace
152cdf0e10cSrcweir 
153cdf0e10cSrcweir // ============================================================================
154cdf0e10cSrcweir 
PropertyMap()155cdf0e10cSrcweir PropertyMap::PropertyMap() :
156cdf0e10cSrcweir     mpPropNames( &StaticPropertyNameVector::get() ) // pointer instead reference to get compiler generated copy c'tor and operator=
157cdf0e10cSrcweir {
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
getPropertyName(sal_Int32 nPropId)160cdf0e10cSrcweir /*static*/ const OUString& PropertyMap::getPropertyName( sal_Int32 nPropId )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir     OSL_ENSURE( (0 <= nPropId) && (nPropId < PROP_COUNT), "PropertyMap::getPropertyName - invalid property identifier" );
163cdf0e10cSrcweir     return StaticPropertyNameVector::get()[ nPropId ];
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
getProperty(sal_Int32 nPropId) const166cdf0e10cSrcweir const Any* PropertyMap::getProperty( sal_Int32 nPropId ) const
167cdf0e10cSrcweir {
168cdf0e10cSrcweir     const_iterator aIt = find( nPropId );
169cdf0e10cSrcweir     return (aIt == end()) ? 0 : &aIt->second;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
makePropertyValueSequence() const172cdf0e10cSrcweir Sequence< PropertyValue > PropertyMap::makePropertyValueSequence() const
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     Sequence< PropertyValue > aSeq( static_cast< sal_Int32 >( size() ) );
175cdf0e10cSrcweir     if( !empty() )
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         PropertyValue* pValues = aSeq.getArray();
178cdf0e10cSrcweir         for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt, ++pValues )
179cdf0e10cSrcweir         {
180cdf0e10cSrcweir             OSL_ENSURE( (0 <= aIt->first) && (aIt->first < PROP_COUNT), "PropertyMap::makePropertyValueSequence - invalid property identifier" );
181cdf0e10cSrcweir             pValues->Name = (*mpPropNames)[ aIt->first ];
182cdf0e10cSrcweir             pValues->Value = aIt->second;
183cdf0e10cSrcweir             pValues->State = ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
184cdf0e10cSrcweir         }
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir     return aSeq;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
fillSequences(Sequence<OUString> & rNames,Sequence<Any> & rValues) const189cdf0e10cSrcweir void PropertyMap::fillSequences( Sequence< OUString >& rNames, Sequence< Any >& rValues ) const
190cdf0e10cSrcweir {
191cdf0e10cSrcweir     rNames.realloc( static_cast< sal_Int32 >( size() ) );
192cdf0e10cSrcweir     rValues.realloc( static_cast< sal_Int32 >( size() ) );
193cdf0e10cSrcweir     if( !empty() )
194cdf0e10cSrcweir     {
195cdf0e10cSrcweir         OUString* pNames = rNames.getArray();
196cdf0e10cSrcweir         Any* pValues = rValues.getArray();
197cdf0e10cSrcweir         for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt, ++pNames, ++pValues )
198cdf0e10cSrcweir         {
199cdf0e10cSrcweir             OSL_ENSURE( (0 <= aIt->first) && (aIt->first < PROP_COUNT), "PropertyMap::fillSequences - invalid property identifier" );
200cdf0e10cSrcweir             *pNames = (*mpPropNames)[ aIt->first ];
201cdf0e10cSrcweir             *pValues = aIt->second;
202cdf0e10cSrcweir         }
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
makePropertySet() const206cdf0e10cSrcweir Reference< XPropertySet > PropertyMap::makePropertySet() const
207cdf0e10cSrcweir {
208cdf0e10cSrcweir     return new GenericPropertySet( *this );
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir // ============================================================================
212cdf0e10cSrcweir 
213cdf0e10cSrcweir } // namespace oox
214