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