xref: /aoo4110/main/oox/inc/oox/helper/propertyset.hxx (revision b1cdbd2c)
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 #ifndef OOX_HELPER_PROPERTYSET_HXX
25 #define OOX_HELPER_PROPERTYSET_HXX
26 
27 #include <com/sun/star/beans/XMultiPropertySet.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/beans/XPropertySetInfo.hpp>
30 #include "oox/token/properties.hxx"
31 
32 namespace oox {
33 
34 class PropertyMap;
35 
36 // ============================================================================
37 
38 /** A wrapper for a UNO property set.
39 
40     This class provides functions to silently get and set properties (without
41     exceptions, without the need to check validity of the UNO property set).
42 
43     An instance is constructed with the reference to a UNO property set or any
44     other interface (the constructor will query for the
45     com.sun.star.beans.XPropertySet interface then). The reference to the
46     property set will be kept as long as the instance of this class is alive.
47 
48     The functions getProperties() and setProperties() try to handle all passed
49     values at once, using the com.sun.star.beans.XMultiPropertySet interface.
50     If the implementation does not support the XMultiPropertySet interface, all
51     properties are handled separately in a loop.
52  */
53 class PropertySet
54 {
55 public:
PropertySet()56     inline explicit     PropertySet() {}
57 
58     /** Constructs a property set wrapper with the passed UNO property set. */
PropertySet(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> & rxPropSet)59     inline explicit     PropertySet(
60                             const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& rxPropSet )
61                                 { set( rxPropSet ); }
62 
63     /** Constructs a property set wrapper after querying the XPropertySet interface. */
64     template< typename Type >
PropertySet(const Type & rObject)65     inline explicit     PropertySet( const Type& rObject ) { set( rObject ); }
66 
67     /** Sets the passed UNO property set and releases the old UNO property set. */
68     void                set( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& rxPropSet );
69 
70     /** Queries the passed object (interface or any) for an XPropertySet and releases the old UNO property set. */
71     template< typename Type >
set(const Type & rObject)72     inline void         set( const Type& rObject )
73                             { set( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >( rObject, ::com::sun::star::uno::UNO_QUERY ) ); }
74 
75     /** Returns true, if the contained XPropertySet interface is valid. */
is() const76     inline bool         is() const { return mxPropSet.is(); }
77 
78     /** Returns the contained XPropertySet interface. */
79     inline ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >
getXPropertySet() const80                         getXPropertySet() const { return mxPropSet; }
81 
82     /** Returns true, if the specified property is supported by the property set. */
83     bool                hasProperty( sal_Int32 nPropId ) const;
84 
85     // Get properties ---------------------------------------------------------
86 
87     /** Gets the specified property from the property set.
88         @return  the property value, or an empty Any, if the property is missing. */
89     ::com::sun::star::uno::Any getAnyProperty( sal_Int32 nPropId ) const;
90 
91     /** Gets the specified property from the property set.
92         @return  true, if the passed variable could be filled with the property value. */
93     template< typename Type >
getProperty(Type & orValue,sal_Int32 nPropId) const94     inline bool         getProperty( Type& orValue, sal_Int32 nPropId ) const
95                             { return getAnyProperty( nPropId ) >>= orValue; }
96 
97     /** Gets the specified boolean property from the property set.
98         @return  true = property contains true; false = property contains false or error occured. */
getBoolProperty(sal_Int32 nPropId) const99     inline bool         getBoolProperty( sal_Int32 nPropId ) const
100                             { bool bValue = false; return getProperty( bValue, nPropId ) && bValue; }
101 
102     /** Gets the specified properties from the property set. Tries to use the XMultiPropertySet interface.
103         @param orValues  (out-parameter) The related property values.
104         @param rPropNames  The property names. MUST be ordered alphabetically. */
105     void                getProperties(
106                             ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& orValues,
107                             const ::com::sun::star::uno::Sequence< ::rtl::OUString >& rPropNames ) const;
108 
109     // Set properties ---------------------------------------------------------
110 
111     /** Puts the passed any into the property set. */
112     bool                setAnyProperty( sal_Int32 nPropId, const ::com::sun::star::uno::Any& rValue );
113 
114     /** Puts the passed value into the property set. */
115     template< typename Type >
setProperty(sal_Int32 nPropId,const Type & rValue)116     inline bool         setProperty( sal_Int32 nPropId, const Type& rValue )
117                             { return setAnyProperty( nPropId, ::com::sun::star::uno::Any( rValue ) ); }
118 
119     /** Puts the passed properties into the property set. Tries to use the XMultiPropertySet interface.
120         @param rPropNames  The property names. MUST be ordered alphabetically.
121         @param rValues  The related property values. */
122     void                setProperties(
123                             const ::com::sun::star::uno::Sequence< ::rtl::OUString >& rPropNames,
124                             const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& rValues );
125 
126     /** Puts the passed property map into the property set. Tries to use the XMultiPropertySet interface.
127         @param rPropertyMap  The property map. */
128     void                setProperties( const PropertyMap& rPropertyMap );
129 
130     // ------------------------------------------------------------------------
131 private:
132     /** Gets the specified property from the property set.
133         @return  true, if the any could be filled with the property value. */
134     bool                implGetPropertyValue( ::com::sun::star::uno::Any& orValue, const ::rtl::OUString& rPropName ) const;
135 
136     /** Puts the passed any into the property set. */
137     bool                implSetPropertyValue( const ::rtl::OUString& rPropName, const ::com::sun::star::uno::Any& rValue );
138 
139 private:
140     ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >
141                         mxPropSet;          /// The mandatory property set interface.
142     ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet >
143                         mxMultiPropSet;     /// The optional multi property set interface.
144     ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo >
145                         mxPropSetInfo;      /// Property information.
146 };
147 
148 // ============================================================================
149 
150 } // namespace oox
151 
152 #endif
153