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