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