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