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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_comphelper.hxx"
26 #include "comphelper/propertybag.hxx"
27 
28 /** === begin UNO includes === **/
29 #include <com/sun/star/beans/IllegalTypeException.hpp>
30 #include <com/sun/star/beans/PropertyExistException.hpp>
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 #include <com/sun/star/beans/NotRemoveableException.hpp>
34 /** === end UNO includes === **/
35 
36 #include <map>
37 
38 //........................................................................
39 namespace comphelper
40 {
41 //........................................................................
42 
43 	/** === begin UNO using === **/
44     using ::com::sun::star::uno::Any;
45     using ::com::sun::star::uno::Type;
46     using ::com::sun::star::uno::TypeClass_VOID;
47     using ::com::sun::star::beans::IllegalTypeException;
48     using ::com::sun::star::beans::PropertyExistException;
49     using ::com::sun::star::lang::IllegalArgumentException;
50     using ::com::sun::star::beans::Property;
51     using ::com::sun::star::beans::NotRemoveableException;
52     using ::com::sun::star::beans::UnknownPropertyException;
53 	/** === end UNO using === **/
54     namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
55 
56 	//====================================================================
57 	//= PropertyBag_Impl
58 	//====================================================================
59     typedef ::std::map< sal_Int32, Any >    MapInt2Any;
60     struct PropertyBag_Impl
61     {
62         PropertyBag_Impl() : m_bAllowEmptyPropertyName(false) { }
63         MapInt2Any  aDefaults;
64         bool m_bAllowEmptyPropertyName;
65     };
66 
67 	//====================================================================
68 	//= PropertyBag
69 	//====================================================================
70 	//--------------------------------------------------------------------
71     PropertyBag::PropertyBag()
72         :m_pImpl( new PropertyBag_Impl )
73     {
74     }
75 
76     PropertyBag::~PropertyBag()
77     {
78     }
79 
80 	//--------------------------------------------------------------------
81     void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed )
82     {
83         m_pImpl->m_bAllowEmptyPropertyName = i_isAllowed;
84     }
85 
86 	//--------------------------------------------------------------------
87     namespace
88     {
89         void    lcl_checkForEmptyName( const bool _allowEmpty, const ::rtl::OUString& _name )
90         {
91             if ( !_allowEmpty && !_name.getLength() )
92                 throw IllegalArgumentException(
93                         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The property name must not be empty." ) ),
94                         // TODO: resource
95                         NULL,
96                         1
97                       );
98         }
99 
100         void    lcl_checkNameAndHandle( const ::rtl::OUString& _name, const sal_Int32 _handle, const PropertyBag& _container )
101         {
102             if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
103                 throw PropertyExistException(
104                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Property name or handle already used." ) ),
105                     // TODO: resource
106                     NULL );
107 
108         }
109     }
110 
111 	//--------------------------------------------------------------------
112     void PropertyBag::addVoidProperty( const ::rtl::OUString& _rName, const Type& _rType, sal_Int32 _nHandle, sal_Int32 _nAttributes )
113     {
114         if ( _rType.getTypeClass() == TypeClass_VOID )
115             throw IllegalArgumentException(
116                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Illegal property type: VOID" ) ),
117                         // TODO: resource
118                     NULL,
119                     1
120                   );
121 
122         // check name/handle sanity
123         lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
124         lcl_checkNameAndHandle( _rName, _nHandle, *this );
125 
126         // register the property
127         OSL_ENSURE( _nAttributes & PropertyAttribute::MAYBEVOID, "PropertyBag::addVoidProperty: this is for default-void properties only!" );
128         registerPropertyNoMember( _rName, _nHandle, _nAttributes | PropertyAttribute::MAYBEVOID, _rType, NULL );
129 
130         // remember the default
131         m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, Any() ) );
132     }
133 
134 	//--------------------------------------------------------------------
135     void PropertyBag::addProperty( const ::rtl::OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const Any& _rInitialValue )
136     {
137         // check type sanity
138         Type aPropertyType = _rInitialValue.getValueType();
139         if ( aPropertyType.getTypeClass() == TypeClass_VOID )
140             throw IllegalTypeException(
141                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The initial value must be non-NULL to determine the property type." ) ),
142                 // TODO: resource
143                 NULL );
144 
145         // check name/handle sanity
146         lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
147         lcl_checkNameAndHandle( _rName, _nHandle, *this );
148 
149         // register the property
150         registerPropertyNoMember( _rName, _nHandle, _nAttributes, aPropertyType,
151             _rInitialValue.hasValue() ? _rInitialValue.getValue() : NULL );
152 
153         // remember the default
154         m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, _rInitialValue ) );
155     }
156 
157 	//--------------------------------------------------------------------
158     void PropertyBag::removeProperty( const ::rtl::OUString& _rName )
159     {
160         const Property& rProp = getProperty( _rName );
161             // will throw an UnknownPropertyException if necessary
162         if ( ( rProp.Attributes & PropertyAttribute::REMOVEABLE ) == 0 )
163             throw NotRemoveableException( ::rtl::OUString(), NULL );
164         const sal_Int32 nHandle = rProp.Handle;
165 
166         revokeProperty( nHandle );
167 
168         m_pImpl->aDefaults.erase( nHandle );
169     }
170 
171 	//--------------------------------------------------------------------
172     void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle, Any& _out_rValue ) const
173     {
174         if ( !hasPropertyByHandle( _nHandle ) )
175             throw UnknownPropertyException();
176 
177         OPropertyContainerHelper::getFastPropertyValue( _out_rValue, _nHandle );
178     }
179 
180 	//--------------------------------------------------------------------
181 	bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle, const Any& _rNewValue, Any& _out_rConvertedValue, Any& _out_rCurrentValue ) const
182     {
183         if ( !hasPropertyByHandle( _nHandle ) )
184             throw UnknownPropertyException();
185 
186         return const_cast< PropertyBag*  >( this )->OPropertyContainerHelper::convertFastPropertyValue(
187             _out_rConvertedValue, _out_rCurrentValue, _nHandle, _rNewValue );
188     }
189 
190 	//--------------------------------------------------------------------
191 	void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle, const Any& _rValue )
192     {
193         if ( !hasPropertyByHandle( _nHandle ) )
194             throw UnknownPropertyException();
195 
196         OPropertyContainerHelper::setFastPropertyValue( _nHandle, _rValue );
197     }
198 
199 	//--------------------------------------------------------------------
200 	void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _out_rValue ) const
201     {
202         if ( !hasPropertyByHandle( _nHandle ) )
203             throw UnknownPropertyException();
204 
205         MapInt2Any::const_iterator pos = m_pImpl->aDefaults.find( _nHandle );
206         OSL_ENSURE( pos != m_pImpl->aDefaults.end(), "PropertyBag::getPropertyDefaultByHandle: inconsistency!" );
207         if ( pos != m_pImpl->aDefaults.end() )
208             _out_rValue = pos->second;
209         else
210             _out_rValue.clear();
211     }
212 
213 
214 //........................................................................
215 } // namespace comphelper
216 //........................................................................
217 
218