xref: /trunk/main/forms/source/xforms/datatypes.hxx (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 #ifndef FORMS_SOURCE_XFORMS_DATATYPES_HXX
29 #define FORMS_SOURCE_XFORMS_DATATYPES_HXX
30 
31 /** === begin UNO includes === **/
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 #include <com/sun/star/util/Date.hpp>
34 #include <com/sun/star/util/Time.hpp>
35 #include <com/sun/star/util/DateTime.hpp>
36 #include <com/sun/star/xsd/XDataType.hpp>
37 #include <com/sun/star/xsd/DataTypeClass.hpp>
38 /** === end UNO includes === **/
39 #include <cppuhelper/implbase1.hxx>
40 #include <comphelper/uno3.hxx>
41 #include <comphelper/propertycontainer.hxx>
42 #include <comphelper/proparrhlp.hxx>
43 #include <comphelper/broadcasthelper.hxx>
44 #include "warnings_guard_unicode_regex.h"
45 
46 #include <memory>
47 
48 //........................................................................
49 namespace xforms
50 {
51 //........................................................................
52 
53 	//====================================================================
54 	//= OXSDDataType
55 	//====================================================================
56     typedef ::cppu::WeakImplHelper1             <   ::com::sun::star::xsd::XDataType
57                                                 >   OXSDDataType_Base;
58     typedef ::comphelper::OMutexAndBroadcastHelper  OXSDDataType_BBase;
59     typedef ::comphelper::OPropertyContainer        OXSDDataType_PBase;
60 
61     class OXSDDataType  :public OXSDDataType_Base
62                         ,public OXSDDataType_BBase         // order matters: OMutexAndBroadcastHelper before
63                         ,public OXSDDataType_PBase         // OPropertyContainer
64     {
65     private:
66         // <properties>
67         sal_Bool        m_bIsBasic;
68         sal_Int16       m_nTypeClass;
69         ::rtl::OUString m_sName;
70         ::rtl::OUString m_sPattern;
71         sal_uInt16      m_nWST;
72         // </properties>
73 
74         ::std::auto_ptr< U_NAMESPACE_QUALIFIER RegexMatcher >
75                         m_pPatternMatcher;
76         bool            m_bPatternMatcherDirty;
77 
78     protected:
79 
80         sal_Bool    isBasic() const         { return m_bIsBasic; }
81         sal_Int16   getTypeClass() const    { return m_nTypeClass; }
82         const ::rtl::OUString&
83                     getName() const         { return m_sName; }
84 
85     private:
86         OXSDDataType( );                                    // never implemented
87         OXSDDataType( const OXSDDataType& );                // never implemented
88         OXSDDataType& operator=( const OXSDDataType& );     // never implemented
89 
90     protected:
91         // create basic data type
92         OXSDDataType( const ::rtl::OUString& _rName, sal_Int16 _nTypeClass );
93         ~OXSDDataType();
94 
95     public:
96         DECLARE_XINTERFACE()
97         DECLARE_XTYPEPROVIDER()
98 
99         virtual ::rtl::OUString SAL_CALL getName(  ) throw (::com::sun::star::uno::RuntimeException);
100         virtual void SAL_CALL setName( const ::rtl::OUString& aName ) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::util::VetoException);
101         virtual ::rtl::OUString SAL_CALL getPattern() throw (::com::sun::star::uno::RuntimeException);
102         virtual void SAL_CALL setPattern( const ::rtl::OUString& _pattern ) throw (::com::sun::star::uno::RuntimeException);
103         virtual sal_Int16 SAL_CALL getWhiteSpaceTreatment() throw (::com::sun::star::uno::RuntimeException);
104         virtual void SAL_CALL setWhiteSpaceTreatment( sal_Int16 _whitespacetreatment ) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::IllegalArgumentException);
105         virtual sal_Bool SAL_CALL getIsBasic() throw (::com::sun::star::uno::RuntimeException);
106         virtual sal_Int16 SAL_CALL getTypeClass() throw (::com::sun::star::uno::RuntimeException);
107 
108         virtual sal_Bool SAL_CALL validate( const ::rtl::OUString& value ) throw (::com::sun::star::uno::RuntimeException);
109         virtual ::rtl::OUString SAL_CALL explainInvalid( const ::rtl::OUString& value ) throw (::com::sun::star::uno::RuntimeException);
110 
111         // XPropertySet - is a base of XDataType and needs to be disambiguated
112         virtual void SAL_CALL setPropertyValue( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Any& aValue ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
113         virtual ::com::sun::star::uno::Any SAL_CALL getPropertyValue( const ::rtl::OUString& PropertyName ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
114         virtual void SAL_CALL addPropertyChangeListener( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
115         virtual void SAL_CALL removePropertyChangeListener( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& aListener ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
116         virtual void SAL_CALL addVetoableChangeListener( const ::rtl::OUString& PropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& aListener ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
117         virtual void SAL_CALL removeVetoableChangeListener( const ::rtl::OUString& PropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& aListener ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
118 
119     public:
120         OXSDDataType* clone( const ::rtl::OUString& _rNewName ) const;
121 
122     protected:
123         // XPropertySet and friends
124 	    virtual sal_Bool SAL_CALL   convertFastPropertyValue( ::com::sun::star::uno::Any& _rConvertedValue, ::com::sun::star::uno::Any& _rOldValue, sal_Int32 _nHandle, const ::com::sun::star::uno::Any& _rValue ) throw(::com::sun::star::lang::IllegalArgumentException);
125 	    virtual void SAL_CALL       setFastPropertyValue_NoBroadcast(
126 							            sal_Int32 nHandle,
127 							            const ::com::sun::star::uno::Any& rValue
128                                     )
129                                     throw (::com::sun::star::uno::Exception);
130 
131         // --- own overridables ---
132         // helper for implementing cloning of data types
133         virtual OXSDDataType*   createClone( const ::rtl::OUString& _rName ) const = 0;
134         virtual void            initializeClone( const OXSDDataType& _rCloneSource );
135 
136         // helper method for validate and explainInvalid
137         virtual sal_uInt16  _validate( const ::rtl::OUString& value );
138         virtual ::rtl::OUString _explainInvalid( sal_uInt16 nReason );
139 
140         // helper method for checking properties values which are to be set
141         virtual bool        checkPropertySanity( sal_Int32 _nHandle, const ::com::sun::star::uno::Any& _rNewValue, ::rtl::OUString& _rErrorMessage );
142 
143         // register properties implemented by this instance - call the base class when overriding
144         virtual void        registerProperties();
145     };
146 
147 	//====================================================================
148 	//= helper for deriving from OXSDDataType
149 	//====================================================================
150 #define DECLARE_DEFAULT_CLONING( classname )        \
151     virtual OXSDDataType* createClone( const ::rtl::OUString& _rName ) const;    \
152     virtual void       initializeClone( const OXSDDataType& _rCloneSource ); \
153             void       initializeTypedClone( const classname& _rCloneSource );
154 
155 #define IMPLEMENT_DEFAULT_CLONING( classname, baseclass )   \
156     OXSDDataType* classname::createClone( const ::rtl::OUString& _rName ) const              \
157     {                                                       \
158         return new classname( _rName );                     \
159     }                                                       \
160     void classname::initializeClone( const OXSDDataType& _rCloneSource ) \
161     { \
162         baseclass::initializeClone( _rCloneSource );        \
163         initializeTypedClone( static_cast< const classname& >( _rCloneSource ) ); \
164     } \
165 
166 #define IMPLEMENT_DEFAULT_TYPED_CLONING( classname, baseclass )   \
167     OXSDDataType* classname::createClone( const ::rtl::OUString& _rName ) const              \
168     {                                                       \
169         return new classname( _rName, getTypeClass() );     \
170     }                                                       \
171     void classname::initializeClone( const OXSDDataType& _rCloneSource ) \
172     { \
173         baseclass::initializeClone( _rCloneSource );        \
174         initializeTypedClone( static_cast< const classname& >( _rCloneSource ) ); \
175     } \
176 
177 #define REGISTER_VOID_PROP( prop, memberAny, type ) \
178     registerMayBeVoidProperty( PROPERTY_##prop, PROPERTY_ID_##prop, ::com::sun::star::beans::PropertyAttribute::BOUND | ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, \
179         &memberAny, ::getCppuType( static_cast< type* >( NULL ) ) );
180 
181     //====================================================================
182 	//= OValueLimitedType_Base
183 	//====================================================================
184     class OValueLimitedType_Base : public OXSDDataType
185     {
186     protected:
187         ::com::sun::star::uno::Any m_aMaxInclusive;
188         ::com::sun::star::uno::Any m_aMaxExclusive;
189         ::com::sun::star::uno::Any m_aMinInclusive;
190         ::com::sun::star::uno::Any m_aMinExclusive;
191 
192         double  m_fCachedMaxInclusive;
193         double  m_fCachedMaxExclusive;
194         double  m_fCachedMinInclusive;
195         double  m_fCachedMinExclusive;
196 
197     protected:
198         OValueLimitedType_Base( const ::rtl::OUString& _rName, sal_Int16 _nTypeClass );
199 
200         virtual void       initializeClone( const OXSDDataType& _rCloneSource );
201                 void       initializeTypedClone( const OValueLimitedType_Base& _rCloneSource );
202 
203         // XPropertySet and friends
204         virtual void SAL_CALL       setFastPropertyValue_NoBroadcast(
205 							            sal_Int32 nHandle,
206 							            const ::com::sun::star::uno::Any& rValue
207                                     )
208                                     throw (::com::sun::star::uno::Exception);
209 
210         // OXSDDataType overridables
211         virtual bool            _getValue( const ::rtl::OUString& value, double& fValue );
212         virtual sal_uInt16      _validate( const ::rtl::OUString& value );
213         virtual ::rtl::OUString _explainInvalid( sal_uInt16 nReason );
214 
215         // own overridables
216         /** translate a given value into a human-readable string
217 
218             The value is guaranteed to be not <NULL/>, and is of type <member>ValueType</member>
219         */
220         virtual ::rtl::OUString typedValueAsHumanReadableString( const ::com::sun::star::uno::Any& _rValue ) const = 0;
221 
222         /** translates a <member>ValueType</member> value into a double value
223 
224             The normalization must respect the "<" and "==" relations on the value
225             space. That is, if two values are equal, their normalizations must be equal, too.
226             Similarily, if <code>foo</code> is less than <code>bar</code>, the same
227             must hold for their normalizations.
228 
229             @param _rValue
230                 the value to translate. Guranteed to be not <NULL/>, and of type <member>ValueType</member>
231             @param _rDoubleValue
232                 output parameter to hold the resulting double value
233         */
234         virtual void normalizeValue( const ::com::sun::star::uno::Any& _rValue, double& _rDoubleValue ) const = 0;
235     };
236 
237     //====================================================================
238 	//= OValueLimitedType
239 	//====================================================================
240     template < typename VALUE_TYPE >
241     class OValueLimitedType : public OValueLimitedType_Base
242     {
243     protected:
244         typedef     VALUE_TYPE  ValueType;
245         inline const ::com::sun::star::uno::Type&
246             getCppuType() const { return ::getCppuType( static_cast< ValueType* >( NULL ) ); }
247 
248     protected:
249         OValueLimitedType( const ::rtl::OUString& _rName, sal_Int16 _nTypeClass );
250 
251         // OXSDDataType overridables
252         virtual void            registerProperties();
253     };
254 
255 	//====================================================================
256 	//= ODerivedDataType
257 	//====================================================================
258     /** helper class for implementing interfaces derived from XDataType
259     */
260     template< typename CONCRETE_DATA_TYPE_IMPL, typename SUPERCLASS = OXSDDataType >
261     class ODerivedDataType  :public SUPERCLASS
262                             ,::comphelper::OPropertyArrayUsageHelper< CONCRETE_DATA_TYPE_IMPL >
263     {
264     private:
265         bool    m_bPropertiesRegistered;
266 
267     protected:
268         ODerivedDataType( const ::rtl::OUString& _rName, sal_Int16 _nTypeClass );
269 
270     protected:
271 		// OPropertyArrayUsageHelper
272 		virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
273 
274 		// XPropertySet
275 		virtual com::sun::star::uno::Reference<com::sun::star::beans::XPropertySetInfo>  SAL_CALL getPropertySetInfo() throw(com::sun::star::uno::RuntimeException);
276 		virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper();
277     };
278 
279     //====================================================================
280 	//= OBooleanType
281 	//====================================================================
282     class OBooleanType;
283     typedef ODerivedDataType< OBooleanType > OBooleanType_Base;
284     class OBooleanType : public OBooleanType_Base
285     {
286     public:
287         OBooleanType( const ::rtl::OUString& _rName );
288 
289     protected:
290         DECLARE_DEFAULT_CLONING( OBooleanType )
291 
292         // OXSDDataType overridables
293         virtual sal_uInt16      _validate( const ::rtl::OUString& value );
294         virtual ::rtl::OUString _explainInvalid( sal_uInt16 nReason );
295     };
296 
297 	//====================================================================
298 	//= OStringType
299 	//====================================================================
300     class OStringType;
301     typedef ODerivedDataType< OStringType > OStringType_Base;
302     class OStringType   :public OStringType_Base
303     {
304     protected:
305         // <properties>
306         ::com::sun::star::uno::Any m_aLength;
307         ::com::sun::star::uno::Any m_aMinLength;
308         ::com::sun::star::uno::Any m_aMaxLength;
309         // </properties>
310 
311     public:
312         OStringType( const ::rtl::OUString& _rName, sal_Int16 _nTypeClass /* = ::com::sun::star::xsd::DataTypeClass::STRING */ );
313 
314     protected:
315         DECLARE_DEFAULT_CLONING( OStringType )
316 
317         // OXSDDataType overridables
318         virtual sal_uInt16      _validate( const ::rtl::OUString& value );
319         virtual ::rtl::OUString _explainInvalid( sal_uInt16 nReason );
320         virtual bool            checkPropertySanity( sal_Int32 _nHandle, const ::com::sun::star::uno::Any& _rNewValue, ::rtl::OUString& _rErrorMessage );
321         virtual void            registerProperties();
322     };
323 
324 	//====================================================================
325 	//= ODecimalType
326 	//====================================================================
327     class ODecimalType;
328     typedef ODerivedDataType< ODecimalType, OValueLimitedType< double > > ODecimalType_Base;
329     class ODecimalType : public ODecimalType_Base
330     {
331     protected:
332         ::com::sun::star::uno::Any m_aTotalDigits;
333         ::com::sun::star::uno::Any m_aFractionDigits;
334 
335     public:
336         ODecimalType( const ::rtl::OUString& _rName, sal_Int16 _nTypeClass /* = ::com::sun::star::xsd::DataTypeClass::DECIMAL */ );
337 
338     protected:
339         DECLARE_DEFAULT_CLONING( ODecimalType )
340 
341         // OXSDDataType overridables
342         virtual sal_uInt16      _validate( const ::rtl::OUString& value );
343         virtual ::rtl::OUString _explainInvalid( sal_uInt16 nReason );
344         virtual void            registerProperties();
345 
346         // OValueLimitedType overridables
347         virtual ::rtl::OUString typedValueAsHumanReadableString( const ::com::sun::star::uno::Any& _rValue ) const;
348         virtual void normalizeValue( const ::com::sun::star::uno::Any& _rValue, double& _rDoubleValue ) const;
349 
350     private:
351         using ODecimalType_Base::initializeTypedClone;
352     };
353 
354 	//====================================================================
355 	//=
356 	//====================================================================
357 #define DEFAULT_DECLARE_SUBTYPE( classname, valuetype )         \
358     class classname;                                            \
359     typedef ODerivedDataType< classname, OValueLimitedType< valuetype > > classname##_Base;  \
360     class classname : public classname##_Base                   \
361     {                                                           \
362     public:                                                     \
363         classname( const ::rtl::OUString& _rName );             \
364                                                                 \
365     protected:                                                  \
366         DECLARE_DEFAULT_CLONING( classname )                    \
367                                                                 \
368         /* OXSDDataType overridables */                         \
369         virtual sal_uInt16          _validate( const ::rtl::OUString& value );  \
370         virtual bool                _getValue( const ::rtl::OUString& value, double& fValue );  \
371                                                                 \
372         /* OValueLimitedType overridables */                    \
373         virtual ::rtl::OUString     typedValueAsHumanReadableString( const ::com::sun::star::uno::Any& _rValue ) const;  \
374         virtual void normalizeValue( const ::com::sun::star::uno::Any& _rValue, double& _rDoubleValue ) const; \
375                                                                 \
376     private:                                                    \
377         using classname##_Base::initializeTypedClone;          \
378     };
379 
380 	//====================================================================
381 	//= ODateType
382 	//====================================================================
383     DEFAULT_DECLARE_SUBTYPE( ODateType, ::com::sun::star::util::Date )
384 
385     //====================================================================
386 	//= OTimeType
387 	//====================================================================
388     DEFAULT_DECLARE_SUBTYPE( OTimeType, ::com::sun::star::util::Time )
389 
390     //====================================================================
391 	//= ODateTimeType
392 	//====================================================================
393     DEFAULT_DECLARE_SUBTYPE( ODateTimeType, ::com::sun::star::util::DateTime )
394 
395     //====================================================================
396 	//= OShortIntegerType
397 	//====================================================================
398     class OShortIntegerType;
399     typedef ODerivedDataType< OShortIntegerType, OValueLimitedType< sal_Int16 > > OShortIntegerType_Base;
400     class OShortIntegerType : public OShortIntegerType_Base
401     {
402     public:
403         OShortIntegerType( const ::rtl::OUString& _rName, sal_Int16 _nTypeClass );
404 
405     protected:
406         DECLARE_DEFAULT_CLONING( OShortIntegerType )
407 
408         // OXSDDataType overridables
409         virtual bool            _getValue( const ::rtl::OUString& value, double& fValue );
410 
411         // OValueLimitedType overridables
412         virtual ::rtl::OUString typedValueAsHumanReadableString( const ::com::sun::star::uno::Any& _rValue ) const;
413         virtual void normalizeValue( const ::com::sun::star::uno::Any& _rValue, double& _rDoubleValue ) const;
414 
415     private:
416         using OShortIntegerType_Base::initializeTypedClone;
417     };
418 
419 //........................................................................
420 } // namespace xforms
421 //........................................................................
422 
423 #endif // FORMS_SOURCE_XFORMS_DATATYPES_HXX
424 
425