xref: /aoo42x/main/sax/inc/sax/tools/converter.hxx (revision 8d192041)
1*8d192041SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*8d192041SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*8d192041SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*8d192041SAndrew Rist  * distributed with this work for additional information
6*8d192041SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*8d192041SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*8d192041SAndrew Rist  * "License"); you may not use this file except in compliance
9*8d192041SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*8d192041SAndrew Rist  *
11*8d192041SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*8d192041SAndrew Rist  *
13*8d192041SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*8d192041SAndrew Rist  * software distributed under the License is distributed on an
15*8d192041SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*8d192041SAndrew Rist  * KIND, either express or implied.  See the License for the
17*8d192041SAndrew Rist  * specific language governing permissions and limitations
18*8d192041SAndrew Rist  * under the License.
19*8d192041SAndrew Rist  *
20*8d192041SAndrew Rist  *************************************************************/
21*8d192041SAndrew Rist 
22*8d192041SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _SAX_CONVERTER_HXX
25cdf0e10cSrcweir #define _SAX_CONVERTER_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sax/dllapi.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <sal/types.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.h>
32cdf0e10cSrcweir #include <com/sun/star/util/MeasureUnit.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace rtl
36cdf0e10cSrcweir {
37cdf0e10cSrcweir class OUString;
38cdf0e10cSrcweir class OUStringBuffer;
39cdf0e10cSrcweir }
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace com { namespace sun { namespace star {
42cdf0e10cSrcweir     namespace util {
43cdf0e10cSrcweir         struct Date;
44cdf0e10cSrcweir         struct DateTime;
45cdf0e10cSrcweir         struct Duration;
46cdf0e10cSrcweir     }
47cdf0e10cSrcweir } } }
48cdf0e10cSrcweir 
49cdf0e10cSrcweir namespace sax {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir /** the Converter converts values of various types from
52cdf0e10cSrcweir 	their internal represantation to the textual form used in xml
53cdf0e10cSrcweir 	and back.
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	All unit types are expressed as com::sun::star::util::MeasureUnit
56cdf0e10cSrcweir */
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 
59cdf0e10cSrcweir class SAX_DLLPUBLIC Converter
60cdf0e10cSrcweir {
61cdf0e10cSrcweir public:
62cdf0e10cSrcweir 	/** convert string to measure using optional min and max values*/
63cdf0e10cSrcweir 	static bool convertMeasure(	sal_Int32& rValue,
64cdf0e10cSrcweir 								const ::rtl::OUString& rString,
65cdf0e10cSrcweir 								sal_Int16 nTargetUnit = ::com::sun::star::util::MeasureUnit::MM_100TH,
66cdf0e10cSrcweir 								sal_Int32 nMin = SAL_MIN_INT32,
67cdf0e10cSrcweir 								sal_Int32 nMax = SAL_MAX_INT32 );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	/** convert measure to string */
70cdf0e10cSrcweir 	static void convertMeasure( ::rtl::OUStringBuffer& rBuffer,
71cdf0e10cSrcweir 								sal_Int32 nMeasure,
72cdf0e10cSrcweir 								sal_Int16 SourceUnit = ::com::sun::star::util::MeasureUnit::MM_100TH,
73cdf0e10cSrcweir 								sal_Int16 nTargetUnit = ::com::sun::star::util::MeasureUnit::INCH  );
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	/** convert string to boolean */
76cdf0e10cSrcweir 	static bool convertBool( bool& rBool,
77cdf0e10cSrcweir 							 const ::rtl::OUString& rString );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	/** convert boolean to string */
80cdf0e10cSrcweir 	static void convertBool( ::rtl::OUStringBuffer& rBuffer,
81cdf0e10cSrcweir 							 bool bValue );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	/** convert string to percent */
84cdf0e10cSrcweir 	static bool convertPercent( sal_Int32& rValue,
85cdf0e10cSrcweir 								const ::rtl::OUString& rString );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	/** convert percent to string */
88cdf0e10cSrcweir 	static void convertPercent( ::rtl::OUStringBuffer& rBuffer,
89cdf0e10cSrcweir 								sal_Int32 nValue );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	/** convert string to pixel measure unite */
92cdf0e10cSrcweir 	static bool convertMeasurePx( sal_Int32& rValue,
93cdf0e10cSrcweir 								  const ::rtl::OUString& rString );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	/** convert pixel measure unit to string */
96cdf0e10cSrcweir 	static void convertMeasurePx( ::rtl::OUStringBuffer& rBuffer,
97cdf0e10cSrcweir 								  sal_Int32 nValue );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	/** convert string to color */
100cdf0e10cSrcweir 	static bool convertColor( sal_Int32& rColor,
101cdf0e10cSrcweir 							  const ::rtl::OUString&rValue );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	/** convert color to string */
104cdf0e10cSrcweir 	static void convertColor( ::rtl::OUStringBuffer &rBuffer,
105cdf0e10cSrcweir 							  sal_Int32 nColor );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	/** convert number to string */
108cdf0e10cSrcweir 	static void convertNumber( ::rtl::OUStringBuffer& rBuffer,
109cdf0e10cSrcweir 							   sal_Int32 nNumber );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	/** convert string to number with optional min and max values */
112cdf0e10cSrcweir 	static bool convertNumber( sal_Int32& rValue,
113cdf0e10cSrcweir 							   const ::rtl::OUString& rString,
114cdf0e10cSrcweir 							   sal_Int32 nMin = SAL_MIN_INT32,
115cdf0e10cSrcweir 							   sal_Int32 nMax = SAL_MAX_INT32 );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	/** convert double number to string (using ::rtl::math) and
118cdf0e10cSrcweir 		DO convert from source unit to target unit */
119cdf0e10cSrcweir 	static void convertDouble( ::rtl::OUStringBuffer& rBuffer,
120cdf0e10cSrcweir 							   double fNumber,
121cdf0e10cSrcweir 							   bool bWriteUnits,
122cdf0e10cSrcweir 							   sal_Int16 nSourceUnit,
123cdf0e10cSrcweir 							   sal_Int16 nTargetUnit );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	/** convert double number to string (using ::rtl::math) without unit conversion */
126cdf0e10cSrcweir 	static void convertDouble( ::rtl::OUStringBuffer& rBuffer, double fNumber);
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	/** convert string to double number (using ::rtl::math) and DO convert from
129cdf0e10cSrcweir 		source unit to target unit. */
130cdf0e10cSrcweir 	static bool convertDouble(	double& rValue,
131cdf0e10cSrcweir 								const ::rtl::OUString& rString,
132cdf0e10cSrcweir 								sal_Int16 nSourceUnit,
133cdf0e10cSrcweir 								sal_Int16 nTargetUnit );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	/** convert string to double number (using ::rtl::math) without unit conversion */
136cdf0e10cSrcweir 	static bool convertDouble(double& rValue, const ::rtl::OUString& rString);
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	/** convert string to double number (using ::rtl::math) with unit conversion */
139cdf0e10cSrcweir 	static bool convertDouble(double& rValue, const ::rtl::OUString& rString, sal_Int16 nTargetUnit );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     /** convert double to ISO "duration" string; negative durations allowed */
142cdf0e10cSrcweir     static void convertDuration(::rtl::OUStringBuffer& rBuffer,
143cdf0e10cSrcweir                                 const double fTime);
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     /** convert util::Duration to ISO "duration" string */
146cdf0e10cSrcweir     static void convertDuration(::rtl::OUStringBuffer& rBuffer,
147cdf0e10cSrcweir                         const ::com::sun::star::util::Duration& rDuration);
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     /** convert ISO "duration" string to double; negative durations allowed */
150cdf0e10cSrcweir     static bool convertDuration(double & rfTime,
151cdf0e10cSrcweir                                 const ::rtl::OUString& rString);
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     /** convert ISO "duration" string to util::Duration */
154cdf0e10cSrcweir     static bool convertDuration(::com::sun::star::util::Duration& rDuration,
155cdf0e10cSrcweir                         const ::rtl::OUString& rString);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     /** convert util::Date to ISO "date" string */
158cdf0e10cSrcweir     static void convertDate( ::rtl::OUStringBuffer& rBuffer,
159cdf0e10cSrcweir                     const com::sun::star::util::Date& rDate );
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     /** convert util::DateTime to ISO "date" or "dateTime" string */
162cdf0e10cSrcweir 	static void convertDateTime( ::rtl::OUStringBuffer& rBuffer,
163cdf0e10cSrcweir 								const com::sun::star::util::DateTime& rDateTime,
164cdf0e10cSrcweir 		   						bool bAddTimeIf0AM = false );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     /** convert ISO "date" or "dateTime" string to util::DateTime */
167cdf0e10cSrcweir 	static bool convertDateTime( com::sun::star::util::DateTime& rDateTime,
168cdf0e10cSrcweir 								 const ::rtl::OUString& rString );
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     /** convert ISO "date" or "dateTime" string to util::DateTime or
171cdf0e10cSrcweir         util::Date */
172cdf0e10cSrcweir     static bool convertDateOrDateTime(
173cdf0e10cSrcweir                     com::sun::star::util::Date & rDate,
174cdf0e10cSrcweir                     com::sun::star::util::DateTime & rDateTime,
175cdf0e10cSrcweir                     bool & rbDateTime,
176cdf0e10cSrcweir                     const ::rtl::OUString & rString );
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 	/** gets the position of the first comma after npos in the string
179cdf0e10cSrcweir 		rStr. Commas inside '"' pairs are not matched */
180cdf0e10cSrcweir 	static sal_Int32 indexOfComma( const ::rtl::OUString& rStr,
181cdf0e10cSrcweir 								   sal_Int32 nPos );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	/** encodes the given byte sequence into Base64 */
184cdf0e10cSrcweir 	static void encodeBase64(rtl::OUStringBuffer& aStrBuffer, const com::sun::star::uno::Sequence<sal_Int8>& aPass);
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	// Decode a base 64 encoded string into a sequence of bytes. The first
187cdf0e10cSrcweir 	// version can be used for attribute values only, bacause it does not
188cdf0e10cSrcweir 	// return any chars left from conversion.
189cdf0e10cSrcweir 	// For text submitted throgh the SAX characters call, the later method
190cdf0e10cSrcweir 	// must be used!
191cdf0e10cSrcweir 	static void decodeBase64(com::sun::star::uno::Sequence<sal_Int8>& aPass, const rtl::OUString& sBuffer);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 	static sal_Int32 decodeBase64SomeChars(com::sun::star::uno::Sequence<sal_Int8>& aPass, const rtl::OUString& sBuffer);
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	static void clearUndefinedChars(rtl::OUString& rTarget, const rtl::OUString& rSource);
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	static double GetConversionFactor(::rtl::OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit);
198cdf0e10cSrcweir 	static sal_Int16 GetUnitFromString(const ::rtl::OUString& rString, sal_Int16 nDefaultUnit);
199cdf0e10cSrcweir 
200cdf0e10cSrcweir };
201cdf0e10cSrcweir 
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir #endif	//  _SAX_CONVERTER_HXX
205