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 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_framework.hxx" 30 #include <classes/converter.hxx> 31 #include <rtl/ustrbuf.hxx> 32 33 namespace framework{ 34 35 //----------------------------------------------------------------------------- 36 /** 37 * pack every property item of source list into an any entry of destination list 38 * Resulting list will have follow format then: "sequence< Any(PropertyValue) >". 39 * If one item couldn't be converted it will be ignored - means target list can 40 * be smaller then source list. Source list isn't changed anytime. 41 * 42 * algorithm: 43 * (a) reserve enough space on destination list for all possible entries of 44 * source list 45 * (b) try to pack every property of source into an any of destination list 46 * (b1) count successfully packed entries only 47 * (c) use this count of packed entries to resize destination list 48 * Because we getted enough space before - that will remove unused items 49 * of destination list at the end of it only. 50 */ 51 css::uno::Sequence< css::uno::Any > Converter::convert_seqProp2seqAny( const css::uno::Sequence< css::beans::PropertyValue >& lSource ) 52 { 53 sal_Int32 nCount = lSource.getLength(); 54 css::uno::Sequence< css::uno::Any > lDestination(nCount); 55 56 for (sal_Int32 nItem=0; nItem<nCount; ++nItem) 57 lDestination[nItem]<<=lSource[nItem]; 58 59 return lDestination; 60 } 61 62 //----------------------------------------------------------------------------- 63 /** 64 * do the same like convert_seqProp2seqAny() before - but reverse. 65 * It try to unpack PropertyValue items from given Any's. 66 */ 67 css::uno::Sequence< css::beans::PropertyValue > Converter::convert_seqAny2seqProp( const css::uno::Sequence< css::uno::Any >& lSource ) 68 { 69 sal_Int32 nCount = lSource.getLength(); 70 sal_Int32 nRealCount = 0; 71 css::uno::Sequence< css::beans::PropertyValue > lDestination(nCount); 72 73 for (sal_Int32 nItem=0; nItem<nCount; ++nItem) 74 { 75 if (lSource[nItem]>>=lDestination[nItem]) 76 ++nRealCount; 77 } 78 79 if (nRealCount!=nCount) 80 lDestination.realloc(nRealCount); 81 82 return lDestination; 83 } 84 85 //----------------------------------------------------------------------------- 86 /** 87 * converts a sequence of NamedValue to a sequence of PropertyValue. 88 */ 89 css::uno::Sequence< css::beans::PropertyValue > Converter::convert_seqNamedVal2seqPropVal( const css::uno::Sequence< css::beans::NamedValue >& lSource ) 90 { 91 sal_Int32 nCount = lSource.getLength(); 92 css::uno::Sequence< css::beans::PropertyValue > lDestination(nCount); 93 for (sal_Int32 nItem=0; nItem<nCount; ++nItem) 94 { 95 lDestination[nItem].Name = lSource[nItem].Name ; 96 lDestination[nItem].Value = lSource[nItem].Value; 97 } 98 return lDestination; 99 } 100 101 //----------------------------------------------------------------------------- 102 /** 103 * converts a sequence of PropertyValue to a sequence of NamedValue. 104 */ 105 css::uno::Sequence< css::beans::NamedValue > Converter::convert_seqPropVal2seqNamedVal( const css::uno::Sequence< css::beans::PropertyValue >& lSource ) 106 { 107 sal_Int32 nCount = lSource.getLength(); 108 css::uno::Sequence< css::beans::NamedValue > lDestination(nCount); 109 for (sal_Int32 nItem=0; nItem<nCount; ++nItem) 110 { 111 lDestination[nItem].Name = lSource[nItem].Name ; 112 lDestination[nItem].Value = lSource[nItem].Value; 113 } 114 return lDestination; 115 } 116 117 //----------------------------------------------------------------------------- 118 /** 119 * converts a sequence of unicode strings into a vector of such items 120 */ 121 OUStringList Converter::convert_seqOUString2OUStringList( const css::uno::Sequence< ::rtl::OUString >& lSource ) 122 { 123 OUStringList lDestination; 124 sal_Int32 nCount = lSource.getLength(); 125 126 for (sal_Int32 nItem=0; nItem<nCount; ++nItem ) 127 { 128 lDestination.push_back(lSource[nItem]); 129 } 130 131 return lDestination; 132 } 133 134 //----------------------------------------------------------------------------- 135 /** 136 * converts a vector of unicode strings into a sequence of such items 137 */ 138 css::uno::Sequence< ::rtl::OUString > Converter::convert_OUStringList2seqOUString( const OUStringList& lSource ) 139 { 140 css::uno::Sequence< ::rtl::OUString > lDestination(lSource.size()); 141 sal_uInt32 nItem = 0; 142 for (OUStringList::const_iterator pIterator=lSource.begin(); pIterator!=lSource.end(); ++pIterator) 143 { 144 lDestination[nItem] = *pIterator; 145 ++nItem; 146 } 147 return lDestination; 148 } 149 150 //----------------------------------------------------------------------------- 151 /** 152 * converts an unicode string hash to a sequence<PropertyValue>, where names and values match to key and values. 153 */ 154 css::uno::Sequence< css::beans::PropertyValue > Converter::convert_OUStringHash2seqProp( const OUStringHash& lSource ) 155 { 156 css::uno::Sequence< css::beans::PropertyValue > lDestination (lSource.size()); 157 css::beans::PropertyValue* pDestination = lDestination.getArray(); 158 sal_Int32 nItem = 0; 159 for (OUStringHash::const_iterator pItem=lSource.begin(); pItem!=lSource.end(); ++pItem) 160 { 161 pDestination[nItem].Name = pItem->first ; 162 pDestination[nItem].Value <<= pItem->second; 163 ++nItem; 164 } 165 return lDestination; 166 } 167 168 //----------------------------------------------------------------------------- 169 /** 170 * converts a sequence<PropertyValue> to an unicode string hash, where keys and values match to names and values. 171 */ 172 OUStringHash Converter::convert_seqProp2OUStringHash( const css::uno::Sequence< css::beans::PropertyValue >& lSource ) 173 { 174 OUStringHash lDestination; 175 sal_Int32 nCount = lSource.getLength(); 176 const css::beans::PropertyValue* pSource = lSource.getConstArray(); 177 for (sal_Int32 nItem=0; nItem<nCount; ++nItem) 178 { 179 pSource[nItem].Value >>= lDestination[pSource[nItem].Name]; 180 } 181 return lDestination; 182 } 183 184 //----------------------------------------------------------------------------- 185 /** 186 @short convert timestamp from String to tools::DateTime notation 187 @descr Format: "<day>.<month>.<year>/<hour>:<min>:<sec>" 188 e.g. : "1.11.2001/13:45:16" 189 190 @param sString 191 timestamp in string notation 192 193 @return timestamp in DateTime notation 194 */ 195 DateTime Converter::convert_String2DateTime( /*IN*/ const ::rtl::OUString& sSource ) 196 { 197 DateTime aStamp ; 198 sal_Int32 nIndex = 0; 199 200 sal_uInt16 nDay = (sal_uInt16)(sSource.getToken( 0, (sal_Unicode)'.', nIndex ).toInt32()); 201 if( nIndex>0 ) 202 { 203 sal_uInt16 nMonth = (sal_uInt16)(sSource.getToken( 0, (sal_Unicode)'.', nIndex ).toInt32()); 204 if( nIndex>0 ) 205 { 206 sal_uInt16 nYear = (sal_uInt16)(sSource.getToken( 0, (sal_Unicode)'/', nIndex ).toInt32()); 207 if( nIndex>0 ) 208 { 209 sal_uInt32 nHour = sSource.getToken( 0, (sal_Unicode)':', nIndex ).toInt32(); 210 if( nIndex>0 ) 211 { 212 sal_uInt32 nMin = sSource.getToken( 0, (sal_Unicode)':', nIndex ).toInt32(); 213 if( nIndex>0 && nIndex<sSource.getLength() ) 214 { 215 sal_uInt32 nSec = sSource.copy( nIndex, sSource.getLength()-nIndex ).toInt32(); 216 217 Date aDate( nDay , nMonth, nYear ); 218 Time aTime( nHour, nMin , nSec ); 219 aStamp = DateTime( aDate, aTime ); 220 } 221 } 222 } 223 } 224 } 225 return aStamp; 226 } 227 228 //----------------------------------------------------------------------------- 229 /** 230 @short convert timestamp from DateTime to String notation 231 @descr Format: "<day>.<month>.<year>/<hour>:<min>:<sec>" 232 e.g. : "1.11.2001/13:45:16" 233 234 @param aStamp 235 timestamp in DateTime notation 236 237 @return timestamp in String notation 238 */ 239 ::rtl::OUString Converter::convert_DateTime2String( /*IN*/ const DateTime& aSource ) 240 { 241 ::rtl::OUStringBuffer sBuffer(25); 242 243 sBuffer.append( (sal_Int32)aSource.GetDay() ); 244 sBuffer.append( (sal_Unicode)'.' ); 245 sBuffer.append( (sal_Int32)aSource.GetMonth() ); 246 sBuffer.append( (sal_Unicode)'.' ); 247 sBuffer.append( (sal_Int32)aSource.GetYear() ); 248 sBuffer.append( (sal_Unicode)'/' ); 249 sBuffer.append( (sal_Int32)aSource.GetHour() ); 250 sBuffer.append( (sal_Unicode)':' ); 251 sBuffer.append( (sal_Int32)aSource.GetMin() ); 252 sBuffer.append( (sal_Unicode)':' ); 253 sBuffer.append( (sal_Int32)aSource.GetSec() ); 254 255 return sBuffer.makeStringAndClear(); 256 } 257 258 ::rtl::OUString Converter::convert_DateTime2ISO8601( const DateTime& aSource ) 259 { 260 ::rtl::OUStringBuffer sBuffer(25); 261 262 sal_Int32 nYear = aSource.GetYear(); 263 sal_Int32 nMonth = aSource.GetMonth(); 264 sal_Int32 nDay = aSource.GetDay(); 265 266 sal_Int32 nHour = aSource.GetHour(); 267 sal_Int32 nMin = aSource.GetMin(); 268 sal_Int32 nSec = aSource.GetSec(); 269 270 // write year formated as "YYYY" 271 if (nYear<10) 272 sBuffer.appendAscii("000"); 273 else 274 if (nYear<100) 275 sBuffer.appendAscii("00"); 276 else 277 if (nYear<1000) 278 sBuffer.appendAscii("0"); 279 sBuffer.append( (sal_Int32)nYear ); 280 281 sBuffer.appendAscii("-"); 282 // write month formated as "MM" 283 if (nMonth<10) 284 sBuffer.appendAscii("0"); 285 sBuffer.append( (sal_Int32)nMonth ); 286 287 sBuffer.appendAscii("-"); 288 // write day formated as "DD" 289 if (nDay<10) 290 sBuffer.appendAscii("0"); 291 sBuffer.append( (sal_Int32)nDay ); 292 293 sBuffer.appendAscii("T"); 294 // write hours formated as "hh" 295 if (nHour<10) 296 sBuffer.appendAscii("0"); 297 sBuffer.append( (sal_Int32)nHour ); 298 299 sBuffer.appendAscii(":"); 300 // write min formated as "mm" 301 if (nMin<10) 302 sBuffer.appendAscii("0"); 303 sBuffer.append( (sal_Int32)nMin ); 304 305 sBuffer.appendAscii(":"); 306 // write sec formated as "ss" 307 if (nSec<10) 308 sBuffer.appendAscii("0"); 309 sBuffer.append( (sal_Int32)nSec ); 310 311 sBuffer.appendAscii("Z"); 312 313 return sBuffer.makeStringAndClear(); 314 } 315 316 } // namespace framework 317