xref: /aoo41x/main/oox/source/helper/propertyset.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/propertyset.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <osl/diagnose.h>
27cdf0e10cSrcweir #include <rtl/strbuf.hxx>
28cdf0e10cSrcweir #include "oox/helper/propertymap.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir namespace oox {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // ============================================================================
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace ::com::sun::star::beans;
35cdf0e10cSrcweir using namespace ::com::sun::star::uno;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using ::rtl::OStringBuffer;
38cdf0e10cSrcweir using ::rtl::OUString;
39cdf0e10cSrcweir using ::rtl::OUStringToOString;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // ============================================================================
42cdf0e10cSrcweir 
set(const Reference<XPropertySet> & rxPropSet)43cdf0e10cSrcweir void PropertySet::set( const Reference< XPropertySet >& rxPropSet )
44cdf0e10cSrcweir {
45cdf0e10cSrcweir     mxPropSet = rxPropSet;
46cdf0e10cSrcweir     mxMultiPropSet.set( mxPropSet, UNO_QUERY );
47cdf0e10cSrcweir     if( mxPropSet.is() ) try
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         mxPropSetInfo = mxPropSet->getPropertySetInfo();
50cdf0e10cSrcweir     }
51cdf0e10cSrcweir     catch( Exception& )
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir     }
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
hasProperty(sal_Int32 nPropId) const56cdf0e10cSrcweir bool PropertySet::hasProperty( sal_Int32 nPropId ) const
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     if( mxPropSetInfo.is() ) try
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         const OUString& rPropName = PropertyMap::getPropertyName( nPropId );
61cdf0e10cSrcweir         return mxPropSetInfo->hasPropertyByName( rPropName );
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir     catch( Exception& )
64cdf0e10cSrcweir     {
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir     return false;
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir // Get properties -------------------------------------------------------------
70cdf0e10cSrcweir 
getAnyProperty(sal_Int32 nPropId) const71cdf0e10cSrcweir Any PropertySet::getAnyProperty( sal_Int32 nPropId ) const
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     Any aValue;
74cdf0e10cSrcweir     return implGetPropertyValue( aValue, PropertyMap::getPropertyName( nPropId ) ) ? aValue : Any();
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
getProperties(Sequence<Any> & orValues,const Sequence<OUString> & rPropNames) const77cdf0e10cSrcweir void PropertySet::getProperties( Sequence< Any >& orValues, const Sequence< OUString >& rPropNames ) const
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     if( mxMultiPropSet.is() ) try
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         orValues = mxMultiPropSet->getPropertyValues( rPropNames );
82cdf0e10cSrcweir         return;
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir     catch( Exception& )
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         OSL_ENSURE( false, "PropertySet::getProperties - cannot get all property values - fallback to single mode" );
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     if( mxPropSet.is() )
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir         sal_Int32 nLen = rPropNames.getLength();
92cdf0e10cSrcweir         const OUString* pPropName = rPropNames.getConstArray();
93cdf0e10cSrcweir         const OUString* pPropNameEnd = pPropName + nLen;
94cdf0e10cSrcweir         orValues.realloc( nLen );
95cdf0e10cSrcweir         Any* pValue = orValues.getArray();
96cdf0e10cSrcweir         for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
97cdf0e10cSrcweir             implGetPropertyValue( *pValue, *pPropName );
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir // Set properties -------------------------------------------------------------
102cdf0e10cSrcweir 
setAnyProperty(sal_Int32 nPropId,const Any & rValue)103cdf0e10cSrcweir bool PropertySet::setAnyProperty( sal_Int32 nPropId, const Any& rValue )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     return implSetPropertyValue( PropertyMap::getPropertyName( nPropId ), rValue );
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
setProperties(const Sequence<OUString> & rPropNames,const Sequence<Any> & rValues)108cdf0e10cSrcweir void PropertySet::setProperties( const Sequence< OUString >& rPropNames, const Sequence< Any >& rValues )
109cdf0e10cSrcweir {
110cdf0e10cSrcweir     OSL_ENSURE( rPropNames.getLength() == rValues.getLength(),
111cdf0e10cSrcweir         "PropertySet::setProperties - length of sequences different" );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     if( mxMultiPropSet.is() ) try
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         mxMultiPropSet->setPropertyValues( rPropNames, rValues );
116cdf0e10cSrcweir         return;
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir     catch( Exception& )
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         OSL_ENSURE( false, "PropertySet::setProperties - cannot set all property values, fallback to single mode" );
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     if( mxPropSet.is() )
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         const OUString* pPropName = rPropNames.getConstArray();
126cdf0e10cSrcweir         const OUString* pPropNameEnd = pPropName + rPropNames.getLength();
127cdf0e10cSrcweir         const Any* pValue = rValues.getConstArray();
128cdf0e10cSrcweir         for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
129cdf0e10cSrcweir             implSetPropertyValue( *pPropName, *pValue );
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
setProperties(const PropertyMap & rPropertyMap)133cdf0e10cSrcweir void PropertySet::setProperties( const PropertyMap& rPropertyMap )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     if( !rPropertyMap.empty() )
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         Sequence< OUString > aPropNames;
138cdf0e10cSrcweir         Sequence< Any > aValues;
139cdf0e10cSrcweir         rPropertyMap.fillSequences( aPropNames, aValues );
140cdf0e10cSrcweir         setProperties( aPropNames, aValues );
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir // private --------------------------------------------------------------------
145cdf0e10cSrcweir 
implGetPropertyValue(Any & orValue,const OUString & rPropName) const146cdf0e10cSrcweir bool PropertySet::implGetPropertyValue( Any& orValue, const OUString& rPropName ) const
147cdf0e10cSrcweir {
148cdf0e10cSrcweir     if( mxPropSet.is() ) try
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         orValue = mxPropSet->getPropertyValue( rPropName );
151cdf0e10cSrcweir         return true;
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir     catch( Exception& )
154cdf0e10cSrcweir     {
155cdf0e10cSrcweir         OSL_ENSURE( false, OStringBuffer( "PropertySet::implGetPropertyValue - cannot get property \"" ).
156cdf0e10cSrcweir             append( OUStringToOString( rPropName, RTL_TEXTENCODING_ASCII_US ) ).append( '"' ).getStr() );
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir     return false;
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
implSetPropertyValue(const OUString & rPropName,const Any & rValue)161cdf0e10cSrcweir bool PropertySet::implSetPropertyValue( const OUString& rPropName, const Any& rValue )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir     if( mxPropSet.is() ) try
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir         mxPropSet->setPropertyValue( rPropName, rValue );
166cdf0e10cSrcweir         return true;
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir     catch( Exception& )
169cdf0e10cSrcweir     {
170cdf0e10cSrcweir         OSL_ENSURE( false, OStringBuffer( "PropertySet::implSetPropertyValue - cannot set property \"" ).
171cdf0e10cSrcweir             append( OUStringToOString( rPropName, RTL_TEXTENCODING_ASCII_US ) ).append( '"' ).getStr() );
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir     return false;
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir // ============================================================================
177cdf0e10cSrcweir 
178cdf0e10cSrcweir } // namespace oox
179