1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SAX_TOOLS_CONVERTER_HXX
21 #define INCLUDED_SAX_TOOLS_CONVERTER_HXX
23 #include <sax/saxdllapi.h>
25 #include <boost/optional/optional.hpp>
27 #include <sal/types.h>
28 #include <rtl/ustring.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <com/sun/star/uno/Sequence.h>
31 #include <com/sun/star/util/MeasureUnit.hpp>
34 namespace com
{ namespace sun
{ namespace star
{
41 struct DateWithTimezone
;
42 struct DateTimeWithTimezone
;
49 /** the Converter converts values of various types from
50 their internal representation to the textual form used in xml
53 All unit types are expressed as com::sun::star::util::MeasureUnit
57 class SAX_DLLPUBLIC Converter
60 /** convert string to measure using optional min and max values*/
61 static bool convertMeasure( sal_Int32
& rValue
,
62 const OUString
& rString
,
63 sal_Int16 nTargetUnit
= ::com::sun::star::util::MeasureUnit::MM_100TH
,
64 sal_Int32 nMin
= SAL_MIN_INT32
,
65 sal_Int32 nMax
= SAL_MAX_INT32
);
67 /** convert measure to string */
68 static void convertMeasure( OUStringBuffer
& rBuffer
,
70 sal_Int16 SourceUnit
= ::com::sun::star::util::MeasureUnit::MM_100TH
,
71 sal_Int16 nTargetUnit
= ::com::sun::star::util::MeasureUnit::INCH
);
73 /** convert string to boolean */
74 static bool convertBool( bool& rBool
,
75 const OUString
& rString
);
77 /** convert boolean to string */
78 static void convertBool( OUStringBuffer
& rBuffer
,
81 /** convert string to percent */
82 static bool convertPercent( sal_Int32
& rValue
,
83 const OUString
& rString
);
85 /** convert percent to string */
86 static void convertPercent( OUStringBuffer
& rBuffer
,
89 /** convert string to pixel measure unite */
90 static bool convertMeasurePx( sal_Int32
& rValue
,
91 const OUString
& rString
);
93 /** convert pixel measure unit to string */
94 static void convertMeasurePx( OUStringBuffer
& rBuffer
,
97 /** convert string to rgb color */
98 static bool convertColor( sal_Int32
& rColor
,
99 const OUString
&rValue
);
101 /** convert color to string */
102 static void convertColor( OUStringBuffer
&rBuffer
,
105 /** convert number to string */
106 static void convertNumber( OUStringBuffer
& rBuffer
,
109 /** convert string to number with optional min and max values */
110 static bool convertNumber( sal_Int32
& rValue
,
111 const OUString
& rString
,
112 sal_Int32 nMin
= SAL_MIN_INT32
,
113 sal_Int32 nMax
= SAL_MAX_INT32
);
115 /** convert string to number with optional min and max values */
116 static bool convertNumber64(sal_Int64
& rValue
,
117 const OUString
& rString
,
118 sal_Int64 nMin
= SAL_MIN_INT64
,
119 sal_Int64 nMax
= SAL_MAX_INT64
);
121 /** convert double number to string (using ::rtl::math) and
122 DO convert from source unit to target unit */
123 static void convertDouble( OUStringBuffer
& rBuffer
,
126 sal_Int16 nSourceUnit
,
127 sal_Int16 nTargetUnit
);
129 /** convert double number to string (using ::rtl::math) without unit conversion */
130 static void convertDouble( OUStringBuffer
& rBuffer
, double fNumber
);
132 /** convert string to double number (using ::rtl::math) and DO convert from
133 source unit to target unit. */
134 static bool convertDouble( double& rValue
,
135 const OUString
& rString
,
136 sal_Int16 nSourceUnit
,
137 sal_Int16 nTargetUnit
);
139 /** convert string to double number (using ::rtl::math) without unit conversion */
140 static bool convertDouble(double& rValue
, const OUString
& rString
);
142 /** convert number, 10th of degrees with range [0..3600] to SVG angle */
143 static void convertAngle(OUStringBuffer
& rBuffer
, sal_Int16 nAngle
);
145 /** convert SVG angle to number, 10th of degrees with range [0..3600] */
146 static bool convertAngle(sal_Int16
& rAngle
, OUString
const& rString
);
148 /** convert double to ISO "duration" string; negative durations allowed */
149 static void convertDuration(OUStringBuffer
& rBuffer
,
152 /** convert util::Duration to ISO "duration" string */
153 static void convertDuration(OUStringBuffer
& rBuffer
,
154 const ::com::sun::star::util::Duration
& rDuration
);
156 /** convert ISO "duration" string to double; negative durations allowed */
157 static bool convertDuration(double & rfTime
,
158 const OUString
& rString
);
160 /** convert ISO "duration" string to util::Duration */
161 static bool convertDuration(::com::sun::star::util::Duration
& rDuration
,
162 const OUString
& rString
);
164 /** convert util::Date to ISO "date" string */
165 static void convertDate( OUStringBuffer
& rBuffer
,
166 const com::sun::star::util::Date
& rDate
,
167 sal_Int16
const* pTimeZoneOffset
);
169 /** convert util::DateTime to ISO "date" or "dateTime" string */
170 static void convertDateTime( OUStringBuffer
& rBuffer
,
171 const com::sun::star::util::DateTime
& rDateTime
,
172 sal_Int16
const* pTimeZoneOffset
,
173 bool bAddTimeIf0AM
= false );
175 /** convert util::DateTime to ISO "time" or "dateTime" string */
176 static void convertTimeOrDateTime(OUStringBuffer
& rBuffer
,
177 const com::sun::star::util::DateTime
& rDateTime
,
178 sal_Int16
const* pTimeZoneOffset
);
180 /** convert ISO "date" or "dateTime" string to util::DateTime */
181 static bool parseDateTime( com::sun::star::util::DateTime
& rDateTime
,
182 boost::optional
<sal_Int16
> * pTimeZoneOffset
,
183 const OUString
& rString
);
185 /** convert ISO "time" or "dateTime" string to util::DateTime */
186 static bool parseTimeOrDateTime(com::sun::star::util::DateTime
& rDateTime
,
187 boost::optional
<sal_Int16
> * pTimeZoneOffset
,
188 const OUString
& rString
);
190 /** convert ISO "date" or "dateTime" string to util::DateTime or
192 static bool parseDateOrDateTime(
193 com::sun::star::util::Date
* pDate
,
194 com::sun::star::util::DateTime
& rDateTime
,
196 boost::optional
<sal_Int16
> * pTimeZoneOffset
,
197 const OUString
& rString
);
199 /** gets the position of the first comma after npos in the string
200 rStr. Commas inside '"' pairs are not matched */
201 static sal_Int32
indexOfComma( const OUString
& rStr
,
204 /** encodes the given byte sequence into Base64 */
205 static void encodeBase64(OUStringBuffer
& aStrBuffer
, const com::sun::star::uno::Sequence
<sal_Int8
>& aPass
);
207 // Decode a base 64 encoded string into a sequence of bytes. The first
208 // version can be used for attribute values only, bacause it does not
209 // return any chars left from conversion.
210 // For text submitted throgh the SAX characters call, the later method
212 static void decodeBase64(com::sun::star::uno::Sequence
<sal_Int8
>& aPass
, const OUString
& sBuffer
);
214 static sal_Int32
decodeBase64SomeChars(com::sun::star::uno::Sequence
<sal_Int8
>& aPass
, const OUString
& sBuffer
);
216 static double GetConversionFactor(OUStringBuffer
& rUnit
, sal_Int16 nSourceUnit
, sal_Int16 nTargetUnit
);
217 static sal_Int16
GetUnitFromString(const OUString
& rString
, sal_Int16 nDefaultUnit
);
219 /** convert an Any to string (typesafe) */
220 static bool convertAny(OUStringBuffer
& rsValue
,
221 OUStringBuffer
& rsType
,
222 const ::com::sun::star::uno::Any
& rValue
);
228 #endif // INCLUDED_SAX_TOOLS_CONVERTER_HXX
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */