1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef _SAX_CONVERTER_HXX 29 #define _SAX_CONVERTER_HXX 30 31 #include "sax/dllapi.h" 32 33 #include <sal/types.h> 34 35 #include <com/sun/star/uno/Sequence.h> 36 #include <com/sun/star/util/MeasureUnit.hpp> 37 38 39 namespace rtl 40 { 41 class OUString; 42 class OUStringBuffer; 43 } 44 45 namespace com { namespace sun { namespace star { 46 namespace util { 47 struct Date; 48 struct DateTime; 49 struct Duration; 50 } 51 } } } 52 53 namespace sax { 54 55 /** the Converter converts values of various types from 56 their internal represantation to the textual form used in xml 57 and back. 58 59 All unit types are expressed as com::sun::star::util::MeasureUnit 60 */ 61 62 63 class SAX_DLLPUBLIC Converter 64 { 65 public: 66 /** convert string to measure using optional min and max values*/ 67 static bool convertMeasure( sal_Int32& rValue, 68 const ::rtl::OUString& rString, 69 sal_Int16 nTargetUnit = ::com::sun::star::util::MeasureUnit::MM_100TH, 70 sal_Int32 nMin = SAL_MIN_INT32, 71 sal_Int32 nMax = SAL_MAX_INT32 ); 72 73 /** convert measure to string */ 74 static void convertMeasure( ::rtl::OUStringBuffer& rBuffer, 75 sal_Int32 nMeasure, 76 sal_Int16 SourceUnit = ::com::sun::star::util::MeasureUnit::MM_100TH, 77 sal_Int16 nTargetUnit = ::com::sun::star::util::MeasureUnit::INCH ); 78 79 /** convert string to boolean */ 80 static bool convertBool( bool& rBool, 81 const ::rtl::OUString& rString ); 82 83 /** convert boolean to string */ 84 static void convertBool( ::rtl::OUStringBuffer& rBuffer, 85 bool bValue ); 86 87 /** convert string to percent */ 88 static bool convertPercent( sal_Int32& rValue, 89 const ::rtl::OUString& rString ); 90 91 /** convert percent to string */ 92 static void convertPercent( ::rtl::OUStringBuffer& rBuffer, 93 sal_Int32 nValue ); 94 95 /** convert string to pixel measure unite */ 96 static bool convertMeasurePx( sal_Int32& rValue, 97 const ::rtl::OUString& rString ); 98 99 /** convert pixel measure unit to string */ 100 static void convertMeasurePx( ::rtl::OUStringBuffer& rBuffer, 101 sal_Int32 nValue ); 102 103 /** convert string to color */ 104 static bool convertColor( sal_Int32& rColor, 105 const ::rtl::OUString&rValue ); 106 107 /** convert color to string */ 108 static void convertColor( ::rtl::OUStringBuffer &rBuffer, 109 sal_Int32 nColor ); 110 111 /** convert number to string */ 112 static void convertNumber( ::rtl::OUStringBuffer& rBuffer, 113 sal_Int32 nNumber ); 114 115 /** convert string to number with optional min and max values */ 116 static bool convertNumber( sal_Int32& rValue, 117 const ::rtl::OUString& rString, 118 sal_Int32 nMin = SAL_MIN_INT32, 119 sal_Int32 nMax = SAL_MAX_INT32 ); 120 121 /** convert double number to string (using ::rtl::math) and 122 DO convert from source unit to target unit */ 123 static void convertDouble( ::rtl::OUStringBuffer& rBuffer, 124 double fNumber, 125 bool bWriteUnits, 126 sal_Int16 nSourceUnit, 127 sal_Int16 nTargetUnit ); 128 129 /** convert double number to string (using ::rtl::math) without unit conversion */ 130 static void convertDouble( ::rtl::OUStringBuffer& rBuffer, double fNumber); 131 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 ::rtl::OUString& rString, 136 sal_Int16 nSourceUnit, 137 sal_Int16 nTargetUnit ); 138 139 /** convert string to double number (using ::rtl::math) without unit conversion */ 140 static bool convertDouble(double& rValue, const ::rtl::OUString& rString); 141 142 /** convert string to double number (using ::rtl::math) with unit conversion */ 143 static bool convertDouble(double& rValue, const ::rtl::OUString& rString, sal_Int16 nTargetUnit ); 144 145 /** convert double to ISO "duration" string; negative durations allowed */ 146 static void convertDuration(::rtl::OUStringBuffer& rBuffer, 147 const double fTime); 148 149 /** convert util::Duration to ISO "duration" string */ 150 static void convertDuration(::rtl::OUStringBuffer& rBuffer, 151 const ::com::sun::star::util::Duration& rDuration); 152 153 /** convert ISO "duration" string to double; negative durations allowed */ 154 static bool convertDuration(double & rfTime, 155 const ::rtl::OUString& rString); 156 157 /** convert ISO "duration" string to util::Duration */ 158 static bool convertDuration(::com::sun::star::util::Duration& rDuration, 159 const ::rtl::OUString& rString); 160 161 /** convert util::Date to ISO "date" string */ 162 static void convertDate( ::rtl::OUStringBuffer& rBuffer, 163 const com::sun::star::util::Date& rDate ); 164 165 /** convert util::DateTime to ISO "date" or "dateTime" string */ 166 static void convertDateTime( ::rtl::OUStringBuffer& rBuffer, 167 const com::sun::star::util::DateTime& rDateTime, 168 bool bAddTimeIf0AM = false ); 169 170 /** convert ISO "date" or "dateTime" string to util::DateTime */ 171 static bool convertDateTime( com::sun::star::util::DateTime& rDateTime, 172 const ::rtl::OUString& rString ); 173 174 /** convert ISO "date" or "dateTime" string to util::DateTime or 175 util::Date */ 176 static bool convertDateOrDateTime( 177 com::sun::star::util::Date & rDate, 178 com::sun::star::util::DateTime & rDateTime, 179 bool & rbDateTime, 180 const ::rtl::OUString & rString ); 181 182 /** gets the position of the first comma after npos in the string 183 rStr. Commas inside '"' pairs are not matched */ 184 static sal_Int32 indexOfComma( const ::rtl::OUString& rStr, 185 sal_Int32 nPos ); 186 187 /** encodes the given byte sequence into Base64 */ 188 static void encodeBase64(rtl::OUStringBuffer& aStrBuffer, const com::sun::star::uno::Sequence<sal_Int8>& aPass); 189 190 // Decode a base 64 encoded string into a sequence of bytes. The first 191 // version can be used for attribute values only, bacause it does not 192 // return any chars left from conversion. 193 // For text submitted throgh the SAX characters call, the later method 194 // must be used! 195 static void decodeBase64(com::sun::star::uno::Sequence<sal_Int8>& aPass, const rtl::OUString& sBuffer); 196 197 static sal_Int32 decodeBase64SomeChars(com::sun::star::uno::Sequence<sal_Int8>& aPass, const rtl::OUString& sBuffer); 198 199 static void clearUndefinedChars(rtl::OUString& rTarget, const rtl::OUString& rSource); 200 201 static double GetConversionFactor(::rtl::OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit); 202 static sal_Int16 GetUnitFromString(const ::rtl::OUString& rString, sal_Int16 nDefaultUnit); 203 204 }; 205 206 } 207 208 #endif // _SAX_CONVERTER_HXX 209