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