Branch libreoffice-5-0-4
[LibreOffice.git] / include / sax / tools / converter.hxx
blob2f46c3b087ff5121cc7346a476838d9a315188ae
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 {
35 namespace uno {
36 class Any;
38 namespace util {
39 struct Date;
40 struct DateTime;
41 struct DateWithTimezone;
42 struct DateTimeWithTimezone;
43 struct Duration;
45 } } }
47 namespace sax {
49 /** the Converter converts values of various types from
50 their internal representation to the textual form used in xml
51 and back.
53 All unit types are expressed as com::sun::star::util::MeasureUnit
57 class SAX_DLLPUBLIC Converter
59 public:
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,
69 sal_Int32 nMeasure,
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,
79 bool bValue );
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,
87 sal_Int32 nValue );
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,
95 sal_Int32 nValue );
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,
103 sal_Int32 nColor );
105 /** convert number to string */
106 static void convertNumber( OUStringBuffer& rBuffer,
107 sal_Int32 nNumber );
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,
124 double fNumber,
125 bool bWriteUnits,
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,
150 const double fTime);
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
191 util::Date */
192 static bool parseDateOrDateTime(
193 com::sun::star::util::Date * pDate,
194 com::sun::star::util::DateTime & rDateTime,
195 bool & rbDateTime,
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,
202 sal_Int32 nPos );
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
211 // must be used!
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: */