xref: /AOO41X/main/xmloff/inc/xmloff/xmluconv.hxx (revision ecfe53c5d1886e1e0d215b0d140d05282ab1c477)
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 _XMLOFF_XMLUCONV_HXX
25 #define _XMLOFF_XMLUCONV_HXX
26 
27 #include "sal/config.h"
28 #include "xmloff/dllapi.h"
29 #include "sal/types.h"
30 
31 #include <limits.h>
32 #include <tools/solar.h>
33 #include <tools/color.hxx>
34 #include <tools/mapunit.hxx>
35 #include <xmloff/xmlement.hxx>
36 #include <xmloff/xmltoken.hxx>
37 #include <com/sun/star/util/Date.hpp>
38 #include <com/sun/star/frame/XModel.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/drawing/Position3D.hpp>
41 
42 // #110680#
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 
45 //////////////////////////////////////////////////////////////////////////////
46 // predeclarations
47 
48 class Time;
49 namespace rtl
50 {
51 class OUString;
52 class OUStringBuffer;
53 }
54 
55 namespace com { namespace sun { namespace star {
56     namespace util { struct DateTime; }
57     namespace text { class XNumberingTypeInfo; }
58     namespace i18n { class XCharacterClassification; }
59 }}}
60 
61 namespace basegfx
62 {
63     class B3DVector;
64 } // end of namespace basegfx
65 
66 class XMLOFF_DLLPUBLIC SvXMLTokenEnumerator
67 {
68 private:
69     const ::rtl::OUString&  maTokenString;
70     sal_Int32               mnNextTokenPos;
71     sal_Unicode             mcSeperator;
72 
73 public:
74     SvXMLTokenEnumerator( const ::rtl::OUString& rString, sal_Unicode cSeperator = sal_Unicode(' ') );
75 
76     sal_Bool getNextToken( ::rtl::OUString& rToken );
77 };
78 
79 /** the SvXMLTypeConverter converts values of various types from
80     their internal represantation to the textual form used in xml
81     and back.
82     Most of the methods are static but the SvXMLTypeConverter can
83     also store default units for both numerical and textual measures.
84 */
85 
86 class XMLOFF_DLLPUBLIC SvXMLUnitConverter
87 {
88 private:
89     MapUnit meCoreMeasureUnit;
90     MapUnit meXMLMeasureUnit;
91     com::sun::star::util::Date aNullDate;
92     ::com::sun::star::uno::Reference<
93         ::com::sun::star::text::XNumberingTypeInfo > xNumTypeInfo;
94     ::com::sun::star::uno::Reference<
95         ::com::sun::star::i18n::XCharacterClassification > xCharClass;
96     // #110680#
97     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > mxServiceFactory;
98 
99     SAL_DLLPRIVATE static ::rtl::OUString msXML_false;
100     SAL_DLLPRIVATE static ::rtl::OUString msXML_true;
101 
102     SAL_DLLPRIVATE static void initXMLStrings();
103 
104     SAL_DLLPRIVATE void createNumTypeInfo() const;
105 
106 public:
107     /** constructs a SvXMLUnitConverter. The core measure unit is the
108         default unit for numerical measures, the XML measure unit is
109         the default unit for textual measures */
110     // #110680#
111     // SvXMLUnitConverter( MapUnit eCoreMeasureUnit, MapUnit eXMLMeasureUnit );
112     SvXMLUnitConverter(
113         MapUnit eCoreMeasureUnit,
114         MapUnit eXMLMeasureUnit,
115         const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory );
116 
117     virtual ~SvXMLUnitConverter();
118 
119     static MapUnit GetMapUnit(sal_Int16 nFieldUnit);
120 
121     /** sets the default unit for numerical measures */
122     inline void setCoreMeasureUnit( MapUnit eCoreMeasureUnit );
123 
124     /** gets the default unit for numerical measures */
125     inline MapUnit getCoreMeasureUnit() const;
126 
127     /** sets the default unit for textual measures */
128     void setXMLMeasureUnit( MapUnit eXMLMeasureUnit );
129 
130     /** gets the default unit for textual measures */
131     MapUnit getXMLMeasureUnit() const;
132 
133     /** gets XNumberingTypeInfo */
134     const ::com::sun::star::uno::Reference<
getNumTypeInfo() const135         ::com::sun::star::text::XNumberingTypeInfo >& getNumTypeInfo() const
136     {
137         if( !xNumTypeInfo.is() )
138             createNumTypeInfo();
139         return xNumTypeInfo;
140     }
141 
142     /** convert string to measure using optional min and max values*/
143     sal_Bool convertMeasure( sal_Int32& rValue,
144                          const ::rtl::OUString& rString,
145                          sal_Int32 nMin = SAL_MIN_INT32,
146                          sal_Int32 nMax = SAL_MAX_INT32) const;
147 
148     /** convert measure to string */
149     void convertMeasure( ::rtl::OUStringBuffer& rBuffer,
150                          sal_Int32 nMeasure ) const;
151 
152     /** convert measure with given unit to string */
153     void convertMeasure( ::rtl::OUStringBuffer&,
154                          sal_Int32 nMeasure,
155                          MapUnit eSrcUnit ) const;
156 
157     /** convert string to measure in given unit
158         using optional min and max values */
159     static sal_Bool convertMeasure( sal_Int32& rVal,
160                                 const ::rtl::OUString& rString,
161                                 MapUnit eDstUnit,
162                                 sal_Int32 nMin = SAL_MIN_INT32,
163                                 sal_Int32 nMax = SAL_MAX_INT32);
164 
165     /** convert measure in given unit to string with given unit */
166     static void convertMeasure( ::rtl::OUStringBuffer& rBuffer,
167                                 sal_Int32 nMeasure,
168                                 MapUnit eSrcUnit,
169                                 MapUnit eDstUnit );
170 
171     /** convert string to boolean */
172     static sal_Bool convertBool( sal_Bool& rBool,
173                              const ::rtl::OUString& rString );
174 
175     /** convert boolean to string */
176     static void convertBool( ::rtl::OUStringBuffer& rBuffer,
177                              sal_Bool bValue );
178 
179     /** convert string to percent */
180     static sal_Bool convertPercent( sal_Int32& rValue,
181                                 const ::rtl::OUString& rString );
182 
183     /** convert percent to string */
184     static void convertPercent( ::rtl::OUStringBuffer& rBuffer,
185                                 sal_Int32 nValue );
186 
187     /** convert string to pixel measure unite */
188     static sal_Bool convertMeasurePx( sal_Int32& rValue,
189                                 const ::rtl::OUString& rString );
190 
191     /** convert pixel measure unit to string */
192     static void convertMeasurePx( ::rtl::OUStringBuffer& rBuffer,
193                                 sal_Int32 nValue );
194 
195     /** convert string to enum using given enum map, if the enum is
196         not found in the map, this method will return false */
197     static sal_Bool convertEnum( sal_uInt16& rEnum,
198                              const ::rtl::OUString& rValue,
199                              const SvXMLEnumMapEntry *pMap );
200 
201     /** convert string to enum using given token map, if the enum is
202         not found in the map, this method will return false */
203     static sal_Bool convertEnum( sal_uInt16& rEnum,
204                              const ::rtl::OUString& rValue,
205                              const SvXMLEnumStringMapEntry *pMap );
206 
207     /** convert enum to string using given enum map with an optional
208         default token. If the enum is not found in the map,
209         this method will either use the given default or return
210         false if not default is set */
211     static sal_Bool convertEnum( ::rtl::OUStringBuffer& rBuffer,
212                                  unsigned int nValue,
213                                  const SvXMLEnumMapEntry *pMap,
214                                  enum ::xmloff::token::XMLTokenEnum eDefault =
215                                          ::xmloff::token::XML_TOKEN_INVALID );
216 
217     /** convert enum to string using given token map with an optional
218         default token. If the enum is not found in the map,
219         this method will either use the given default or return
220         false if not default is set */
221     static sal_Bool convertEnum( ::rtl::OUStringBuffer& rBuffer,
222                                  sal_uInt16 nValue,
223                                  const SvXMLEnumStringMapEntry *pMap,
224                                  sal_Char* pDefault = NULL );
225 
226     /** convert string to color */
227     static sal_Bool convertColor( Color& rColor,
228                               const ::rtl::OUString&rValue );
229 
230     /** convert color to string */
231     static void convertColor( ::rtl::OUStringBuffer &rBuffer,
232                               const Color& rCol );
233 
234     /** convert number to string */
235     static void convertNumber( ::rtl::OUStringBuffer& rBuffer,
236                                sal_Int32 nNumber );
237 
238     /** convert string to number with optional min and max values */
239     static sal_Bool convertNumber( sal_Int32& rValue,
240                                const ::rtl::OUString& rString,
241                                sal_Int32 nMin = SAL_MIN_INT32,
242                                sal_Int32 nMax = SAL_MAX_INT32);
243 
244     /** convert number to string */
245     static void convertNumber64( ::rtl::OUStringBuffer& rBuffer,
246                                sal_Int64 nNumber );
247 
248     /** convert string to number with optional min and max values */
249     static sal_Bool convertNumber64( sal_Int64& rValue,
250                                const ::rtl::OUString& rString,
251                                sal_Int64 nMin = SAL_MIN_INT64,
252                                sal_Int64 nMax = SAL_MAX_INT64);
253 
254     /** convert double number to string (using ::rtl::math) and DO
255         convert to export MapUnit */
256     void convertDouble(::rtl::OUStringBuffer& rBuffer,
257         double fNumber, sal_Bool bWriteUnits) const;
258 
259     /** convert double number to string (using ::rtl::math) and
260         DO convert from eSrcUnit to export MapUnit */
261     static void convertDouble( ::rtl::OUStringBuffer& rBuffer,
262         double fNumber, sal_Bool bWriteUnits, MapUnit eCoreUnit, MapUnit eDstUnit);
263 
264     /** convert double number to string (using ::rtl::math) without unit conversion */
265     static void convertDouble( ::rtl::OUStringBuffer& rBuffer, double fNumber);
266 
267     /** convert string to double number (using ::rtl::math) and DO convert. */
268     sal_Bool convertDouble(double& rValue, const ::rtl::OUString& rString, sal_Bool bLookForUnits) const;
269 
270     /** convert string to double number (using ::rtl::math) and DO convert from
271         SrcUnit to DstUnit. */
272     static sal_Bool convertDouble(double& rValue,
273         const ::rtl::OUString& rString, MapUnit eSrcUnit, MapUnit eCoreUnit);
274 
275     /** convert string to double number (using ::rtl::math) without unit conversion */
276     static sal_Bool convertDouble(double& rValue, const ::rtl::OUString& rString);
277 
278     /** get the Null Date of the XModel and set it to the UnitConverter */
279     sal_Bool setNullDate (
280         const com::sun::star::uno::Reference <com::sun::star::frame::XModel>& xModel);
281 
282     /** Set the Null Date of the UnitConverter */
setNullDate(const com::sun::star::util::Date & aTempNullDate)283     void setNullDate ( const com::sun::star::util::Date& aTempNullDate ) { aNullDate = aTempNullDate; }
284 
285     /** convert double to ISO Time String */
286     static void convertTime( ::rtl::OUStringBuffer& rBuffer,
287                                 const double& fTime);
288 
289     /** convert util::DateTime to ISO Time String */
290     static void convertTime( ::rtl::OUStringBuffer& rBuffer,
291                                 const ::com::sun::star::util::DateTime& rDateTime );
292 
293     /** convert ISO Time String to double */
294     static sal_Bool convertTime( double& fTime,
295                                 const ::rtl::OUString& rString);
296 
297     /** convert ISO Time String to util::DateTime */
298     static sal_Bool convertTime( ::com::sun::star::util::DateTime& rDateTime,
299                                  const ::rtl::OUString& rString );
300 
301     /** convert double to ISO Date Time String */
convertDateTime(::rtl::OUStringBuffer & rBuffer,const double & fDateTime,sal_Bool bAddTimeIf0AM=sal_False)302     void convertDateTime( ::rtl::OUStringBuffer& rBuffer,
303                                 const double& fDateTime,
304                                 sal_Bool bAddTimeIf0AM=sal_False) { convertDateTime(rBuffer, fDateTime, aNullDate, bAddTimeIf0AM); }
305 
306     /** convert ISO Date Time String to double */
convertDateTime(double & fDateTime,const::rtl::OUString & rString)307     sal_Bool convertDateTime( double& fDateTime,
308                                 const ::rtl::OUString& rString) { return convertDateTime(fDateTime, rString, aNullDate); }
309 
310     /** convert double to ISO Date Time String */
311     static void convertDateTime( ::rtl::OUStringBuffer& rBuffer,
312                                 const double& fDateTime,
313                                 const com::sun::star::util::Date& aNullDate,
314                                 sal_Bool bAddTimeIf0AM=sal_False);
315     /** convert ISO Date Time String to double */
316     static sal_Bool convertDateTime( double& fDateTime,
317                                 const ::rtl::OUString& rString,
318                                 const com::sun::star::util::Date& aNullDate);
319 
320     /** converts the given time value into an ISO-conform duration string
321 
322         @param rTime
323             the time value to convert. This parameter is evaluated only down to the seconds - in particular,
324             "100th seconds" are ignored.
325         @param nSecondsFraction
326             Additional milleseconds to add to the time. Must be smaller than 1000.
327             This parameter is necessary since neither <type>Time</type> nor <type scope="com::sun::star::util">Time</type>
328             have a sufficient resolution to transport milliseconds.
329         @see http://www.w3.org/TR/xmlschema-2/#duration
330     */
331     static ::rtl::OUString convertTimeDuration( const ::Time& rTime, sal_Int32 nSecondsFraction = 0 );
332 
333     /** converts the given ISO-conform duration string into a time value
334 
335         @param rTime
336             the converted time value. Fractions of seconds of this object are not filled, even if present in
337             the string. See <arg>nSecondsFraction</arg>
338 
339         @param pSecondsFraction
340             recieves fractions of whole seconds, in milliseconds. May be <NULL/>
341             This parameter is necessary since neither <type>Time</type> nor <type scope="com::sun::star::util">Time</type>
342             have a sufficient resolution to transport milliseconds.
343         @see http://www.w3.org/TR/xmlschema-2/#duration
344     */
345     static bool convertTimeDuration( const rtl::OUString& rString, ::Time& rTime, sal_Int32* pSecondsFraction = NULL );
346 
347     /** convert string to ::basegfx::B3DVector */
348     static sal_Bool convertB3DVector( ::basegfx::B3DVector& rVector,
349                               const ::rtl::OUString& rValue );
350 
351     /** convert B3DVector to string */
352     static void convertB3DVector( ::rtl::OUStringBuffer &rBuffer,
353         const ::basegfx::B3DVector& rVector );
354 
355     /** convert string to Position3D */
356     sal_Bool convertPosition3D( com::sun::star::drawing::Position3D& rPosition,
357                               const ::rtl::OUString& rValue );
358 
359     /** convert Position3D to string */
360     void convertPosition3D( ::rtl::OUStringBuffer &rBuffer,
361                               const com::sun::star::drawing::Position3D& rVector );
362 
363     /** convert util::DateTime to ISO Date String */
364     static void convertDateTime( ::rtl::OUStringBuffer& rBuffer,
365                                 const com::sun::star::util::DateTime& rDateTime,
366                                 sal_Bool bAddTimeIf0AM=sal_False );
367 
368     /** convert ISO Date String to util::DateTime */
369     static sal_Bool convertDateTime( com::sun::star::util::DateTime& rDateTime,
370                                      const ::rtl::OUString& rString );
371 
372     /** gets the position of the first comma after npos in the string
373         rStr. Commas inside '"' pairs are not matched */
374     static sal_Int32 indexOfComma( const ::rtl::OUString& rStr,
375                                    sal_Int32 nPos );
376 
377     static void encodeBase64(rtl::OUStringBuffer& aStrBuffer, const com::sun::star::uno::Sequence<sal_Int8>& aPass);
378 
379     // Decode a base 64 encoded string into a sequence of bytes. The first
380     // version can be used for attribute values only, bacause it does not
381     // return any chars left from conversion.
382     // For text submitted throgh the SAX characters call, the later method
383     // must be used!
384     static void decodeBase64(com::sun::star::uno::Sequence<sal_Int8>& aPass, const rtl::OUString& sBuffer);
385 
386     static sal_Int32 decodeBase64SomeChars(com::sun::star::uno::Sequence<sal_Int8>& aPass, const rtl::OUString& sBuffer);
387 
388     /** convert num-forat and num-letter-sync values to NumberingType */
389     sal_Bool convertNumFormat( sal_Int16& rType,
390                                const ::rtl::OUString& rNumFormat,
391                                const ::rtl::OUString& rNumLetterSync,
392                                sal_Bool bNumberNone = sal_False ) const;
393 
394     /** convert NumberingType to num-forat and num-letter-sync values */
395     void convertNumFormat( ::rtl::OUStringBuffer& rBuffer,
396                            sal_Int16 nType ) const;
397     void convertNumLetterSync( ::rtl::OUStringBuffer& rBuffer,
398                                sal_Int16 nType ) const;
399 
400     static void convertPropertySet(com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue>& rProps,
401                         const com::sun::star::uno::Reference<com::sun::star::beans::XPropertySet>& aProperties);
402     static void convertPropertySet(com::sun::star::uno::Reference<com::sun::star::beans::XPropertySet>& rProperties,
403                         const com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue>& aProps);
404 
405     static void clearUndefinedChars(rtl::OUString& rTarget, const rtl::OUString& rSource);
406 
407     ::rtl::OUString encodeStyleName( const ::rtl::OUString& rName,
408                                      sal_Bool *pEncoded=0 ) const;
409 
410     /** convert an Any to string (typesafe) */
411     static sal_Bool convertAny(      ::rtl::OUStringBuffer&    sValue,
412                                      ::rtl::OUStringBuffer&    sType ,
413                                const com::sun::star::uno::Any& aValue);
414 
415     /** convert a string to Any (typesafe) */
416     static sal_Bool convertAny(      com::sun::star::uno::Any& aValue,
417                                const ::rtl::OUString&          sType ,
418                                const ::rtl::OUString&          sValue);
419 };
420 
setCoreMeasureUnit(MapUnit eCoreMeasureUnit)421 inline void SvXMLUnitConverter::setCoreMeasureUnit( MapUnit eCoreMeasureUnit )
422 {
423     meCoreMeasureUnit = eCoreMeasureUnit;
424 }
425 
getCoreMeasureUnit() const426 inline MapUnit SvXMLUnitConverter::getCoreMeasureUnit() const
427 {
428     return meCoreMeasureUnit;
429 }
430 
setXMLMeasureUnit(MapUnit eXMLMeasureUnit)431 inline void SvXMLUnitConverter::setXMLMeasureUnit( MapUnit eXMLMeasureUnit )
432 {
433     meXMLMeasureUnit = eXMLMeasureUnit;
434 }
435 
getXMLMeasureUnit() const436 inline MapUnit SvXMLUnitConverter::getXMLMeasureUnit() const
437 {
438     return meXMLMeasureUnit;
439 }
440 
441 #endif  //  _XMLOFF_XMLUCONV_HXX
442