xref: /aoo41x/main/oox/source/helper/propertymap.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "oox/helper/propertymap.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySetInfo.hpp>
33*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
34*cdf0e10cSrcweir #include <osl/mutex.hxx>
35*cdf0e10cSrcweir #include "oox/token/propertynames.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace oox {
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // ============================================================================
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
42*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
43*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using ::rtl::OString;
46*cdf0e10cSrcweir using ::rtl::OUString;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir // ============================================================================
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir namespace {
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper2< XPropertySet, XPropertySetInfo > GenericPropertySetBase;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir /** This class implements a generic XPropertySet.
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir     Properties of all names and types can be set and later retrieved.
57*cdf0e10cSrcweir     TODO: move this to comphelper or better find an existing implementation
58*cdf0e10cSrcweir  */
59*cdf0e10cSrcweir class GenericPropertySet : public GenericPropertySetBase, private ::osl::Mutex
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir public:
62*cdf0e10cSrcweir     explicit            GenericPropertySet();
63*cdf0e10cSrcweir     explicit            GenericPropertySet( const PropertyMap& rPropMap );
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     // XPropertySet
66*cdf0e10cSrcweir     virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo() throw (RuntimeException);
67*cdf0e10cSrcweir     virtual void SAL_CALL setPropertyValue( const OUString& aPropertyName, const Any& aValue ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException);
68*cdf0e10cSrcweir     virtual Any SAL_CALL getPropertyValue( const OUString& PropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
69*cdf0e10cSrcweir     virtual void SAL_CALL addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
70*cdf0e10cSrcweir     virtual void SAL_CALL removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& aListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
71*cdf0e10cSrcweir     virtual void SAL_CALL addVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
72*cdf0e10cSrcweir     virtual void SAL_CALL removeVetoableChangeListener( const OUString& PropertyName, const Reference< XVetoableChangeListener >& aListener ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException);
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     // XPropertySetInfo
75*cdf0e10cSrcweir     virtual Sequence< Property > SAL_CALL getProperties() throw (RuntimeException);
76*cdf0e10cSrcweir     virtual Property SAL_CALL getPropertyByName( const OUString& aName ) throw (UnknownPropertyException, RuntimeException);
77*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasPropertyByName( const OUString& Name ) throw (RuntimeException);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir private:
80*cdf0e10cSrcweir     typedef ::std::map< OUString, Any > PropertyNameMap;
81*cdf0e10cSrcweir     PropertyNameMap     maPropMap;
82*cdf0e10cSrcweir };
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir // ----------------------------------------------------------------------------
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir GenericPropertySet::GenericPropertySet()
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir GenericPropertySet::GenericPropertySet( const PropertyMap& rPropMap )
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir     const PropertyNameVector& rPropNames = StaticPropertyNameVector::get();
93*cdf0e10cSrcweir     for( PropertyMap::const_iterator aIt = rPropMap.begin(), aEnd = rPropMap.end(); aIt != aEnd; ++aIt )
94*cdf0e10cSrcweir         maPropMap[ rPropNames[ aIt->first ] ] = aIt->second;
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir Reference< XPropertySetInfo > SAL_CALL GenericPropertySet::getPropertySetInfo() throw (RuntimeException)
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir     return this;
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir void SAL_CALL GenericPropertySet::setPropertyValue( const OUString& rPropertyName, const Any& rValue ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( *this );
105*cdf0e10cSrcweir     maPropMap[ rPropertyName ] = rValue;
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir Any SAL_CALL GenericPropertySet::getPropertyValue( const OUString& rPropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir     PropertyNameMap::iterator aIt = maPropMap.find( rPropertyName );
111*cdf0e10cSrcweir     if( aIt == maPropMap.end() )
112*cdf0e10cSrcweir         throw UnknownPropertyException();
113*cdf0e10cSrcweir     return aIt->second;
114*cdf0e10cSrcweir }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir // listeners are not supported by this implementation
117*cdf0e10cSrcweir void SAL_CALL GenericPropertySet::addPropertyChangeListener( const OUString& , const Reference< XPropertyChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
118*cdf0e10cSrcweir void SAL_CALL GenericPropertySet::removePropertyChangeListener( const OUString& , const Reference< XPropertyChangeListener >&  ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
119*cdf0e10cSrcweir void SAL_CALL GenericPropertySet::addVetoableChangeListener( const OUString& , const Reference< XVetoableChangeListener >&  ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
120*cdf0e10cSrcweir void SAL_CALL GenericPropertySet::removeVetoableChangeListener( const OUString& , const Reference< XVetoableChangeListener >&  ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) {}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir // XPropertySetInfo
123*cdf0e10cSrcweir Sequence< Property > SAL_CALL GenericPropertySet::getProperties() throw (RuntimeException)
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir     Sequence< Property > aSeq( static_cast< sal_Int32 >( maPropMap.size() ) );
126*cdf0e10cSrcweir     Property* pProperty = aSeq.getArray();
127*cdf0e10cSrcweir     for( PropertyNameMap::iterator aIt = maPropMap.begin(), aEnd = maPropMap.end(); aIt != aEnd; ++aIt, ++pProperty )
128*cdf0e10cSrcweir     {
129*cdf0e10cSrcweir         pProperty->Name = aIt->first;
130*cdf0e10cSrcweir         pProperty->Handle = 0;
131*cdf0e10cSrcweir         pProperty->Type = aIt->second.getValueType();
132*cdf0e10cSrcweir         pProperty->Attributes = 0;
133*cdf0e10cSrcweir     }
134*cdf0e10cSrcweir     return aSeq;
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir Property SAL_CALL GenericPropertySet::getPropertyByName( const OUString& rPropertyName ) throw (UnknownPropertyException, RuntimeException)
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir     PropertyNameMap::iterator aIt = maPropMap.find( rPropertyName );
140*cdf0e10cSrcweir     if( aIt == maPropMap.end() )
141*cdf0e10cSrcweir         throw UnknownPropertyException();
142*cdf0e10cSrcweir     Property aProperty;
143*cdf0e10cSrcweir     aProperty.Name = aIt->first;
144*cdf0e10cSrcweir     aProperty.Handle = 0;
145*cdf0e10cSrcweir     aProperty.Type = aIt->second.getValueType();
146*cdf0e10cSrcweir     aProperty.Attributes = 0;
147*cdf0e10cSrcweir     return aProperty;
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir sal_Bool SAL_CALL GenericPropertySet::hasPropertyByName( const OUString& rPropertyName ) throw (RuntimeException)
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir     return maPropMap.find( rPropertyName ) != maPropMap.end();
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir } // namespace
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir // ============================================================================
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir PropertyMap::PropertyMap() :
160*cdf0e10cSrcweir     mpPropNames( &StaticPropertyNameVector::get() ) // pointer instead reference to get compiler generated copy c'tor and operator=
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir /*static*/ const OUString& PropertyMap::getPropertyName( sal_Int32 nPropId )
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir     OSL_ENSURE( (0 <= nPropId) && (nPropId < PROP_COUNT), "PropertyMap::getPropertyName - invalid property identifier" );
167*cdf0e10cSrcweir     return StaticPropertyNameVector::get()[ nPropId ];
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir const Any* PropertyMap::getProperty( sal_Int32 nPropId ) const
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir     const_iterator aIt = find( nPropId );
173*cdf0e10cSrcweir     return (aIt == end()) ? 0 : &aIt->second;
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir Sequence< PropertyValue > PropertyMap::makePropertyValueSequence() const
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir     Sequence< PropertyValue > aSeq( static_cast< sal_Int32 >( size() ) );
179*cdf0e10cSrcweir     if( !empty() )
180*cdf0e10cSrcweir     {
181*cdf0e10cSrcweir         PropertyValue* pValues = aSeq.getArray();
182*cdf0e10cSrcweir         for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt, ++pValues )
183*cdf0e10cSrcweir         {
184*cdf0e10cSrcweir             OSL_ENSURE( (0 <= aIt->first) && (aIt->first < PROP_COUNT), "PropertyMap::makePropertyValueSequence - invalid property identifier" );
185*cdf0e10cSrcweir             pValues->Name = (*mpPropNames)[ aIt->first ];
186*cdf0e10cSrcweir             pValues->Value = aIt->second;
187*cdf0e10cSrcweir             pValues->State = ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
188*cdf0e10cSrcweir         }
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir     return aSeq;
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir void PropertyMap::fillSequences( Sequence< OUString >& rNames, Sequence< Any >& rValues ) const
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     rNames.realloc( static_cast< sal_Int32 >( size() ) );
196*cdf0e10cSrcweir     rValues.realloc( static_cast< sal_Int32 >( size() ) );
197*cdf0e10cSrcweir     if( !empty() )
198*cdf0e10cSrcweir     {
199*cdf0e10cSrcweir         OUString* pNames = rNames.getArray();
200*cdf0e10cSrcweir         Any* pValues = rValues.getArray();
201*cdf0e10cSrcweir         for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt, ++pNames, ++pValues )
202*cdf0e10cSrcweir         {
203*cdf0e10cSrcweir             OSL_ENSURE( (0 <= aIt->first) && (aIt->first < PROP_COUNT), "PropertyMap::fillSequences - invalid property identifier" );
204*cdf0e10cSrcweir             *pNames = (*mpPropNames)[ aIt->first ];
205*cdf0e10cSrcweir             *pValues = aIt->second;
206*cdf0e10cSrcweir         }
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir Reference< XPropertySet > PropertyMap::makePropertySet() const
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir     return new GenericPropertySet( *this );
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir // ============================================================================
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir } // namespace oox
218