1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_XMLOFF_SOURCE_FORMS_PROPERTYEXPORT_HXX
21 #define INCLUDED_XMLOFF_SOURCE_FORMS_PROPERTYEXPORT_HXX
22 
23 #include <sal/config.h>
24 
25 #include <set>
26 
27 #include "formattributes.hxx"
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/beans/XPropertyState.hpp>
30 #include <xmloff/xmltoken.hxx>
31 #include <xmloff/xmlexp.hxx>
32 #include "callbacks.hxx"
33 #include "strings.hxx"
34 
35 enum class BoolAttrFlags {
36     DefaultFalse          = 0x00,
37     DefaultTrue           = 0x01,
38     DefaultVoid           = 0x02,
39     InverseSemantics      = 0x04,
40 };
41 namespace o3tl {
42     template<> struct typed_flags<BoolAttrFlags> : is_typed_flags<BoolAttrFlags, 0x07> {};
43 }
44 
45 namespace xmloff
46 {
47 
48     // if sal_True, indicates that the semantic of the property referred by <arg>_pPropertyName</arg>
49     // is inverse to the semantic of the XML attribute.<br/>
50     // I.e. if the property value is <TRUE/>, <FALSE/> has to be written and vice versa.
51     // <p>Be careful with <arg>_bDefault</arg> and <arg>_bInverseSemantics</arg>: if <arg>_bInverseSemantics</arg>
52     // is <TRUE/>, the current property value is inverted <em>before</em> comparing it to the default.</p>
53 
54     class IFormsExportContext;
55     //= OPropertyExport
56     /** provides export related tools for attribute handling
57 
58         <p>(The name is somewhat misleading. It's not only a PropertyExport, but in real an ElementExport.
59         Anyway.)</p>
60     */
61     class OPropertyExport
62     {
63     private:
64         typedef std::set<OUString> StringSet;
65         StringSet       m_aRemainingProps;
66             // see examinePersistence
67 
68         void exportRelativeTargetLocation(const OUString& _sPropertyName, CCAFlags _nProperty,bool _bAddType);
69 
70     protected:
71         IFormsExportContext&    m_rContext;
72 
73         const css::uno::Reference< css::beans::XPropertySet >
74                                 m_xProps;
75         const css::uno::Reference< css::beans::XPropertySetInfo >
76                                 m_xPropertyInfo;
77         const css::uno::Reference< css::beans::XPropertyState >
78                                 m_xPropertyState;
79 
80         // caching
81         OUString     m_sValueTrue;
82         OUString     m_sValueFalse;
83 
84     public:
85         /** constructs an object capable of handling attributes for export
86             @param  _rContext
87                 the export context to which's attribute list the property translation should be added
88             @param  m_xControl
89                 the property set to be exported
90         */
91         OPropertyExport(IFormsExportContext& _rContext,
92             const css::uno::Reference< css::beans::XPropertySet >& _rxProps);
93 
94     protected:
95         /** examines a property set given for all properties which's value are to made persistent
96 
97             <p>upon return the <method>m_aRemainingProps</method> will be filled with the names of all properties
98             which need to be stored</p>
99         */
100         void examinePersistence();
101 
102         template< typename T > void exportRemainingPropertiesSequence(
103             css::uno::Any const & value,
104             token::XMLTokenEnum eValueAttName);
105 
106         void exportRemainingProperties();
107 
108         /** indicates that a property has been handled by a derived class, without using the helper methods of this
109             class.
110 
111             <p>Calling this method is necessary in case you use the suggested mechanism for the generic export of
112             properties. This means that you want to use <method>exportRemainingProperties</method>, which exports
113             all properties which need to ('cause they haven't been exported with one of the other type-specific
114             methods).</p>
115 
116             <p>In this case you should call exportedProperty for every property you export yourself, so the property
117             will be flagged as <em>already handled</em></p>
118         */
exportedProperty(const OUString & _rPropertyName)119         void exportedProperty(const OUString& _rPropertyName)
120             { m_aRemainingProps.erase(_rPropertyName); }
121 
122         /** add an attribute which is represented by a string property to the export context
123 
124             @param _nNamespaceKey
125                 the key of the namespace to use for the attribute name. Is used with the namespace map
126                 provided by the export context.
127             @param _pAttributeName
128                 the name of the attribute to add. Must not contain any namespace
129             @param _pPropertyName
130                 the name of the property to ask the control for
131         */
132         void exportStringPropertyAttribute(
133             const sal_uInt16 _nNamespaceKey,
134             const sal_Char* _pAttributeName,
135             const OUString& _rPropertyName
136         );
137 
138         /** add an attribute which is represented by a boolean property to the export context
139 
140             @param _nNamespaceKey
141                 the key of the namespace to use for the attribute name. Is used with the namespace map
142                 provided by the export context.
143             @param _pAttributeName
144                 the name of the attribute to add. Must not contain any namespace (it's added automatically)
145             @param _pPropertyName
146                 the name of the property to ask the control for
147             @param _nBooleanAttributeFlags
148                 specifies the default and the "alignment" (inverse semantics) of the boolean property
149         */
150         void exportBooleanPropertyAttribute(
151             const sal_uInt16 _nNamespaceKey,
152             const sal_Char* _pAttributeName,
153             const OUString& _rPropertyName,
154             const BoolAttrFlags _nBooleanAttributeFlags);
155 
156         /** add an attribute which is represented by a sal_Int16 property to the export context
157 
158             @param _nNamespaceKey
159                 the key of the namespace to use for the attribute name. Is used with the namespace map
160                 provided by the export context.
161             @param _pAttributeName
162                 the name of the attribute to add. Must not contain any namespace (it's added automatically)
163             @param _pPropertyName
164                 the name of the property to ask the control for
165             @param _nDefault
166                 the default of the attribute. See force parameter.
167             @param force
168                 if true and the property is not set or does not contain a sal_Int16,
169                 then _nDefault is written out.
170                 if false and the current property value equals _nDefault,
171                 then no attribute is added.
172         */
173         void exportInt16PropertyAttribute(
174             const sal_uInt16 _nNamespaceKey,
175             const sal_Char* _pAttributeName,
176             const OUString& _rPropertyName,
177             const sal_Int16 _nDefault,
178             const bool force = false);
179 
180         /** add an attribute which is represented by a sal_Int32 property to the export context
181 
182             @param _nNamespaceKey
183                 the key of the namespace to use for the attribute name. Is used with the namespace map
184                 provided by the export context.
185             @param _pAttributeName
186                 the name of the attribute to add. Must not contain any namespace (it's added automatically)
187             @param _pPropertyName
188                 the name of the property to ask the control for
189             @param _nDefault
190                 the default of the attribute. If the current property value equals this default, no
191                 attribute is added.
192         */
193         void exportInt32PropertyAttribute(
194             const sal_uInt16 _nNamespaceKey,
195             const sal_Char* _pAttributeName,
196             const OUString& _rPropertyName,
197             const sal_Int32 _nDefault);
198 
199         /** add an attribute which is represented by an enum property to the export context
200 
201             @param _nNamespaceKey
202                 the key of the namespace to use for the attribute name. Is used with the namespace map
203                 provided by the export context.
204             @param _pAttributeName
205                 the name of the attribute to add. Must not contain any namespace (it's added automatically)
206             @param _pPropertyName
207                 the name of the property to ask the control for
208             @param _pValueMap
209                 the map to use when converting the property value to an attribute value
210             @param _nDefault
211                 the default of the attribute. If the current property value equals this default, no
212                 attribute is added.
213         */
214         template<typename EnumT>
exportEnumPropertyAttribute(const sal_uInt16 _nNamespaceKey,const sal_Char * _pAttributeName,const OUString & _rPropertyName,const SvXMLEnumMapEntry<EnumT> * _pValueMap,const EnumT _nDefault,const bool _bVoidDefault=false)215         void exportEnumPropertyAttribute(
216             const sal_uInt16 _nNamespaceKey,
217             const sal_Char* _pAttributeName,
218             const OUString& _rPropertyName,
219             const SvXMLEnumMapEntry<EnumT>* _pValueMap,
220             const EnumT _nDefault,
221             const bool _bVoidDefault = false)
222         {
223             exportEnumPropertyAttributeImpl(_nNamespaceKey, _pAttributeName, _rPropertyName,
224                             reinterpret_cast<const SvXMLEnumMapEntry<sal_uInt16>*>(_pValueMap),
225                             static_cast<sal_Int16>(_nDefault), _bVoidDefault);
226         }
227         void exportEnumPropertyAttributeImpl(
228             const sal_uInt16 _nNamespaceKey,
229             const sal_Char* _pAttributeName,
230             const OUString& _rPropertyName,
231             const SvXMLEnumMapEntry<sal_uInt16>* _pValueMap,
232             const sal_uInt16 _nDefault,
233             const bool _bVoidDefault);
234 
235         // some very special methods for some very special attribute/property pairs
236 
237         /** add the hlink:target-frame attribute to the export context.
238 
239             <p>The value of this attribute is extracted from the TargetFrame property of the object given.</p>
240 
241             <p>The property needs a special handling because conflicts between the default values for the attribute
242             and the property.</p>
243         */
244         void exportTargetFrameAttribute();
245 
246         /** add the form:href attribute to the export context.
247 
248             <p>The value of this attribute is extracted from the TargetURL property of the object given.</p>
249 
250             <p>The property needs a special handling because the URL's need to be made relative</p>
251 
252             <p>If _bAddType is set, an additional xlink:type="simple" attribute is also added.</p>
253         */
exportTargetLocationAttribute(bool _bAddType)254         void exportTargetLocationAttribute(bool _bAddType) { exportRelativeTargetLocation(PROPERTY_TARGETURL,CCAFlags::TargetLocation,_bAddType); }
255 
256         /** add the form:image attribute to the export context.
257 
258             <p>The value of this attribute is extracted from the ImageURL property of the object given.</p>
259 
260             <p>The property needs a special handling because the URL's need to be made relative</p>
261         */
exportImageDataAttribute()262         void exportImageDataAttribute() { exportRelativeTargetLocation(PROPERTY_GRAPHIC, CCAFlags::ImageData, false); }
263 
264         /** flag the style properties as 'already exported'
265 
266             <p>We don't have style support right now, so the only thing the method does is removing the style-relevant
267             properties from the list of yet-to-be-exported properties (<member>m_aRemainingProps</member>)</p>
268         */
269         void flagStyleProperties();
270 
271         /** add an arbitrary attribute extracted from an arbitrary property to the export context
272 
273             <p>The current value of the property specified with <arg>_pPropertyName</arg> is taken and converted
274             into a string, no matter what type it has. (Okay, there are the usual limitations: We know Date, Datetime,
275             double, integer ... to name just a few).</p>
276 
277             <p>In case the property value is <NULL/> (void), no attribute is added</p>
278 
279             <p>In case the property value is an empty string, and the property is a not allowed to be <NULL/> (void),
280             no attribute is added</p>
281 
282             <p>In case the property value is a sequence of any type, no attribute is added, 'cause sequences can't be
283             transported as attribute. In the debug version, an additional assertion will occur if you nonetheless try
284             to do this.</p>
285 
286             @param _nNamespaceKey
287                 the key of the namespace to use for the attribute name. Is used with the namespace map
288                 provided by the export context.
289             @param _pAttributeName
290                 the name of the attribute to add. Must not contain any namespace (it's added automatically)
291             @param _pPropertyName
292                 the name of the property to ask the object for
293         */
294         void exportGenericPropertyAttribute(
295             const sal_uInt16 _nAttributeNamespaceKey,
296             const sal_Char* _pAttributeName,
297             const sal_Char* _pPropertyName);
298 
299         /** exports a property value, which is a string sequence, as attribute
300 
301             <p>The elements of the string sequence given are quoted and concatenated, with the characters used for
302             this to be chosen by the caller</p>
303 
304             <p>If you use the quote character, no check (except assertions) is made if one of the list items
305             contains the quote character</p>
306 
307             <p>If you don't use the quote character, no check (except assertions) is made if one of the list items
308             contains the separator character (which would be deadly when reimporting the string)</p>
309 
310             @param _nNamespaceKey
311                 the key of the namespace to use for the attribute name. Is used with the namespace map
312                 provided by the export context.
313             @param _pAttributeName
314                 the name of the attribute to add. Must not contain any namespace (it's added automatically)
315             @param _pPropertyName
316                 the name of the property to ask the object for
317         */
318         void exportStringSequenceAttribute(
319             const sal_uInt16 _nAttributeNamespaceKey,
320             const sal_Char* _pAttributeName,
321             const OUString& _rPropertyName);
322 
323         /** determines whether the given property is to be exported
324 
325             <p>Currently, the method simply checks whether the property's state is <em>not</em> PropertyState.DEFAULT,
326             or whether the property is a dynamic property (i.e. added via an <code>XPropertyContainer</code>).
327             So, take care when using the method - the heuristics is not applicable for all properties.</p>
328         */
329         bool shouldExportProperty( const OUString& i_propertyName ) const;
330 
331         /** tries to convert an arbitrary <type scope="com.sun:star.uno">Any</type> into an string
332 
333             <p>If the type contained in the Any is not supported, the returned string will be empty. In the
334             debug version, an additional assertion occurs.</p>
335 
336             @param  _rValue
337                 the value to convert
338         */
339         OUString implConvertAny(
340             const css::uno::Any& _rValue);
341 
342         /**
343             @return
344                 token which can be used in the <code>form:property</code> element's <code>type</code> attribute
345                 to describe the type of a value.<br/>
346                 Possible types returned are
347                 <ul>
348                     <li><b>boolean</b>: <arg>_rValue</arg> was interpreted as boolean value before converting
349                         it into a string</li>
350                     <li><b>float</b>: <arg>_rValue</arg> was interpreted as 64 bit floating point 16bit integer, 32bit integer or 64 bit integer value before
351                         converting it into a string</li>
352                     <li><b>string</b>: <arg>_rValue</arg> did not need any conversion as it already was a string</li>
353                 </ul>
354                 If the type is not convertible, float is returned
355         */
356         static ::xmloff::token::XMLTokenEnum implGetPropertyXMLType(const css::uno::Type& _rType);
357 
358 #ifdef DBG_UTIL
359                 void AddAttribute(sal_uInt16 _nPrefix, const sal_Char* _pName, const OUString& _rValue);
360                 void AddAttribute( sal_uInt16 _nPrefix, const OUString& _rName, const OUString& _rValue );
361                 void AddAttribute(sal_uInt16 _nPrefix, ::xmloff::token::XMLTokenEnum _eName, const OUString& _rValue);
362                 void AddAttribute(sal_uInt16 _nPrefix, ::xmloff::token::XMLTokenEnum _eName, ::xmloff::token::XMLTokenEnum _eValue );
363 #else
364         //  in the product version, inline this, so it does not cost us extra time calling into our method
AddAttribute(sal_uInt16 _nPrefix,const sal_Char * _pName,const OUString & _rValue)365         void AddAttribute(sal_uInt16 _nPrefix, const sal_Char* _pName, const OUString& _rValue)
366             { m_rContext.getGlobalContext().AddAttribute(_nPrefix, _pName, _rValue); }
AddAttribute(sal_uInt16 _nPrefix,const OUString & _rName,const OUString & _rValue)367         void AddAttribute( sal_uInt16 _nPrefix, const OUString& _rName, const OUString& _rValue )
368             { m_rContext.getGlobalContext().AddAttribute( _nPrefix, _rName, _rValue ); }
AddAttribute(sal_uInt16 _nPrefix,::xmloff::token::XMLTokenEnum _eName,const OUString & _rValue)369         void AddAttribute(sal_uInt16 _nPrefix, ::xmloff::token::XMLTokenEnum _eName, const OUString& _rValue)
370             { m_rContext.getGlobalContext().AddAttribute(_nPrefix, _eName, _rValue); }
AddAttribute(sal_uInt16 _nPrefix,::xmloff::token::XMLTokenEnum _eName,::xmloff::token::XMLTokenEnum _eValue)371         void AddAttribute(sal_uInt16 _nPrefix, ::xmloff::token::XMLTokenEnum _eName, ::xmloff::token::XMLTokenEnum _eValue )
372             { m_rContext.getGlobalContext().AddAttribute(_nPrefix, _eName, _eValue); }
373 #endif
374 
375 #ifdef DBG_UTIL
376     protected:
377         /** check a given property set for the existence and type correctness of a given property
378 
379             <p>This method is available in the non-product version only.</p>
380 
381             @param _rPropertyName
382                 the name of the property to ask the control model for
383             @param _pType
384                 the expected type of the property. May be NULL, in this case no type check is made.
385             @return sal_True, if the property exists and is of the correct type
386         */
387         void dbg_implCheckProperty(
388             const OUString& _rPropertyName,
389             const css::uno::Type* _pType);
390 
391 //      void dbg_implCheckProperty(
392 //          const sal_Char* _rPropertyName,
393 //          const css::uno::Type* _pType)
394 //      {
395 //          dbg_implCheckProperty(OUString::createFromAscii(_rPropertyName), _pType);
396 //      }
397 #endif
398     };
399 
400     //= helper
401 #ifdef DBG_UTIL
402     #define DBG_CHECK_PROPERTY(name, type)  \
403         dbg_implCheckProperty(name, &cppu::UnoType<type>::get())
404 
405     #define DBG_CHECK_PROPERTY_NO_TYPE(name)    \
406         dbg_implCheckProperty(name, nullptr)
407 
408     #define DBG_CHECK_PROPERTY_ASCII_NO_TYPE( name ) \
409         dbg_implCheckProperty( OUString::createFromAscii( name ), nullptr )
410 #else
411     #define DBG_CHECK_PROPERTY(name, type)
412     #define DBG_CHECK_PROPERTY_NO_TYPE(name)
413     #define DBG_CHECK_PROPERTY_ASCII_NO_TYPE( name )
414 #endif
415 
416 }   // namespace xmloff
417 
418 #endif // INCLUDED_XMLOFF_SOURCE_FORMS_PROPERTYEXPORT_HXX
419 
420 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
421