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