xref: /trunk/main/sax/inc/sax/tools/converter.hxx (revision 8d192041)
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 _SAX_CONVERTER_HXX
25 #define _SAX_CONVERTER_HXX
26 
27 #include "sax/dllapi.h"
28 
29 #include <sal/types.h>
30 
31 #include <com/sun/star/uno/Sequence.h>
32 #include <com/sun/star/util/MeasureUnit.hpp>
33 
34 
35 namespace rtl
36 {
37 class OUString;
38 class OUStringBuffer;
39 }
40 
41 namespace com { namespace sun { namespace star {
42     namespace util {
43         struct Date;
44         struct DateTime;
45         struct Duration;
46     }
47 } } }
48 
49 namespace sax {
50 
51 /** the Converter converts values of various types from
52 	their internal represantation to the textual form used in xml
53 	and back.
54 
55 	All unit types are expressed as com::sun::star::util::MeasureUnit
56 */
57 
58 
59 class SAX_DLLPUBLIC Converter
60 {
61 public:
62 	/** convert string to measure using optional min and max values*/
63 	static bool convertMeasure(	sal_Int32& rValue,
64 								const ::rtl::OUString& rString,
65 								sal_Int16 nTargetUnit = ::com::sun::star::util::MeasureUnit::MM_100TH,
66 								sal_Int32 nMin = SAL_MIN_INT32,
67 								sal_Int32 nMax = SAL_MAX_INT32 );
68 
69 	/** convert measure to string */
70 	static void convertMeasure( ::rtl::OUStringBuffer& rBuffer,
71 								sal_Int32 nMeasure,
72 								sal_Int16 SourceUnit = ::com::sun::star::util::MeasureUnit::MM_100TH,
73 								sal_Int16 nTargetUnit = ::com::sun::star::util::MeasureUnit::INCH  );
74 
75 	/** convert string to boolean */
76 	static bool convertBool( bool& rBool,
77 							 const ::rtl::OUString& rString );
78 
79 	/** convert boolean to string */
80 	static void convertBool( ::rtl::OUStringBuffer& rBuffer,
81 							 bool bValue );
82 
83 	/** convert string to percent */
84 	static bool convertPercent( sal_Int32& rValue,
85 								const ::rtl::OUString& rString );
86 
87 	/** convert percent to string */
88 	static void convertPercent( ::rtl::OUStringBuffer& rBuffer,
89 								sal_Int32 nValue );
90 
91 	/** convert string to pixel measure unite */
92 	static bool convertMeasurePx( sal_Int32& rValue,
93 								  const ::rtl::OUString& rString );
94 
95 	/** convert pixel measure unit to string */
96 	static void convertMeasurePx( ::rtl::OUStringBuffer& rBuffer,
97 								  sal_Int32 nValue );
98 
99 	/** convert string to color */
100 	static bool convertColor( sal_Int32& rColor,
101 							  const ::rtl::OUString&rValue );
102 
103 	/** convert color to string */
104 	static void convertColor( ::rtl::OUStringBuffer &rBuffer,
105 							  sal_Int32 nColor );
106 
107 	/** convert number to string */
108 	static void convertNumber( ::rtl::OUStringBuffer& rBuffer,
109 							   sal_Int32 nNumber );
110 
111 	/** convert string to number with optional min and max values */
112 	static bool convertNumber( sal_Int32& rValue,
113 							   const ::rtl::OUString& rString,
114 							   sal_Int32 nMin = SAL_MIN_INT32,
115 							   sal_Int32 nMax = SAL_MAX_INT32 );
116 
117 	/** convert double number to string (using ::rtl::math) and
118 		DO convert from source unit to target unit */
119 	static void convertDouble( ::rtl::OUStringBuffer& rBuffer,
120 							   double fNumber,
121 							   bool bWriteUnits,
122 							   sal_Int16 nSourceUnit,
123 							   sal_Int16 nTargetUnit );
124 
125 	/** convert double number to string (using ::rtl::math) without unit conversion */
126 	static void convertDouble( ::rtl::OUStringBuffer& rBuffer, double fNumber);
127 
128 	/** convert string to double number (using ::rtl::math) and DO convert from
129 		source unit to target unit. */
130 	static bool convertDouble(	double& rValue,
131 								const ::rtl::OUString& rString,
132 								sal_Int16 nSourceUnit,
133 								sal_Int16 nTargetUnit );
134 
135 	/** convert string to double number (using ::rtl::math) without unit conversion */
136 	static bool convertDouble(double& rValue, const ::rtl::OUString& rString);
137 
138 	/** convert string to double number (using ::rtl::math) with unit conversion */
139 	static bool convertDouble(double& rValue, const ::rtl::OUString& rString, sal_Int16 nTargetUnit );
140 
141     /** convert double to ISO "duration" string; negative durations allowed */
142     static void convertDuration(::rtl::OUStringBuffer& rBuffer,
143                                 const double fTime);
144 
145     /** convert util::Duration to ISO "duration" string */
146     static void convertDuration(::rtl::OUStringBuffer& rBuffer,
147                         const ::com::sun::star::util::Duration& rDuration);
148 
149     /** convert ISO "duration" string to double; negative durations allowed */
150     static bool convertDuration(double & rfTime,
151                                 const ::rtl::OUString& rString);
152 
153     /** convert ISO "duration" string to util::Duration */
154     static bool convertDuration(::com::sun::star::util::Duration& rDuration,
155                         const ::rtl::OUString& rString);
156 
157     /** convert util::Date to ISO "date" string */
158     static void convertDate( ::rtl::OUStringBuffer& rBuffer,
159                     const com::sun::star::util::Date& rDate );
160 
161     /** convert util::DateTime to ISO "date" or "dateTime" string */
162 	static void convertDateTime( ::rtl::OUStringBuffer& rBuffer,
163 								const com::sun::star::util::DateTime& rDateTime,
164 		   						bool bAddTimeIf0AM = false );
165 
166     /** convert ISO "date" or "dateTime" string to util::DateTime */
167 	static bool convertDateTime( com::sun::star::util::DateTime& rDateTime,
168 								 const ::rtl::OUString& rString );
169 
170     /** convert ISO "date" or "dateTime" string to util::DateTime or
171         util::Date */
172     static bool convertDateOrDateTime(
173                     com::sun::star::util::Date & rDate,
174                     com::sun::star::util::DateTime & rDateTime,
175                     bool & rbDateTime,
176                     const ::rtl::OUString & rString );
177 
178 	/** gets the position of the first comma after npos in the string
179 		rStr. Commas inside '"' pairs are not matched */
180 	static sal_Int32 indexOfComma( const ::rtl::OUString& rStr,
181 								   sal_Int32 nPos );
182 
183 	/** encodes the given byte sequence into Base64 */
184 	static void encodeBase64(rtl::OUStringBuffer& aStrBuffer, const com::sun::star::uno::Sequence<sal_Int8>& aPass);
185 
186 	// Decode a base 64 encoded string into a sequence of bytes. The first
187 	// version can be used for attribute values only, bacause it does not
188 	// return any chars left from conversion.
189 	// For text submitted throgh the SAX characters call, the later method
190 	// must be used!
191 	static void decodeBase64(com::sun::star::uno::Sequence<sal_Int8>& aPass, const rtl::OUString& sBuffer);
192 
193 	static sal_Int32 decodeBase64SomeChars(com::sun::star::uno::Sequence<sal_Int8>& aPass, const rtl::OUString& sBuffer);
194 
195 	static void clearUndefinedChars(rtl::OUString& rTarget, const rtl::OUString& rSource);
196 
197 	static double GetConversionFactor(::rtl::OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit);
198 	static sal_Int16 GetUnitFromString(const ::rtl::OUString& rString, sal_Int16 nDefaultUnit);
199 
200 };
201 
202 }
203 
204 #endif	//  _SAX_CONVERTER_HXX
205