merged tag ooo/OOO330_m14
[LibreOffice.git] / framework / source / classes / converter.cxx
blobd70b099c9e03c6d6c28ec3b9ff1d096a6be71ca3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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>
33 namespace framework{
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.
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.
51 css::uno::Sequence< css::uno::Any > Converter::convert_seqProp2seqAny( const css::uno::Sequence< css::beans::PropertyValue >& lSource )
53 sal_Int32 nCount = lSource.getLength();
54 css::uno::Sequence< css::uno::Any > lDestination(nCount);
56 for (sal_Int32 nItem=0; nItem<nCount; ++nItem)
57 lDestination[nItem]<<=lSource[nItem];
59 return lDestination;
62 //-----------------------------------------------------------------------------
63 /**
64 * do the same like convert_seqProp2seqAny() before - but reverse.
65 * It try to unpack PropertyValue items from given Any's.
67 css::uno::Sequence< css::beans::PropertyValue > Converter::convert_seqAny2seqProp( const css::uno::Sequence< css::uno::Any >& lSource )
69 sal_Int32 nCount = lSource.getLength();
70 sal_Int32 nRealCount = 0;
71 css::uno::Sequence< css::beans::PropertyValue > lDestination(nCount);
73 for (sal_Int32 nItem=0; nItem<nCount; ++nItem)
75 if (lSource[nItem]>>=lDestination[nItem])
76 ++nRealCount;
79 if (nRealCount!=nCount)
80 lDestination.realloc(nRealCount);
82 return lDestination;
85 //-----------------------------------------------------------------------------
86 /**
87 * converts a sequence of NamedValue to a sequence of PropertyValue.
89 css::uno::Sequence< css::beans::PropertyValue > Converter::convert_seqNamedVal2seqPropVal( const css::uno::Sequence< css::beans::NamedValue >& lSource )
91 sal_Int32 nCount = lSource.getLength();
92 css::uno::Sequence< css::beans::PropertyValue > lDestination(nCount);
93 for (sal_Int32 nItem=0; nItem<nCount; ++nItem)
95 lDestination[nItem].Name = lSource[nItem].Name ;
96 lDestination[nItem].Value = lSource[nItem].Value;
98 return lDestination;
101 //-----------------------------------------------------------------------------
103 * converts a sequence of PropertyValue to a sequence of NamedValue.
105 css::uno::Sequence< css::beans::NamedValue > Converter::convert_seqPropVal2seqNamedVal( const css::uno::Sequence< css::beans::PropertyValue >& lSource )
107 sal_Int32 nCount = lSource.getLength();
108 css::uno::Sequence< css::beans::NamedValue > lDestination(nCount);
109 for (sal_Int32 nItem=0; nItem<nCount; ++nItem)
111 lDestination[nItem].Name = lSource[nItem].Name ;
112 lDestination[nItem].Value = lSource[nItem].Value;
114 return lDestination;
117 //-----------------------------------------------------------------------------
119 * converts a sequence of unicode strings into a vector of such items
121 OUStringList Converter::convert_seqOUString2OUStringList( const css::uno::Sequence< ::rtl::OUString >& lSource )
123 OUStringList lDestination;
124 sal_Int32 nCount = lSource.getLength();
126 for (sal_Int32 nItem=0; nItem<nCount; ++nItem )
128 lDestination.push_back(lSource[nItem]);
131 return lDestination;
134 //-----------------------------------------------------------------------------
136 * converts a vector of unicode strings into a sequence of such items
138 css::uno::Sequence< ::rtl::OUString > Converter::convert_OUStringList2seqOUString( const OUStringList& lSource )
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)
144 lDestination[nItem] = *pIterator;
145 ++nItem;
147 return lDestination;
150 //-----------------------------------------------------------------------------
152 * converts an unicode string hash to a sequence<PropertyValue>, where names and values match to key and values.
154 css::uno::Sequence< css::beans::PropertyValue > Converter::convert_OUStringHash2seqProp( const OUStringHash& lSource )
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)
161 pDestination[nItem].Name = pItem->first ;
162 pDestination[nItem].Value <<= pItem->second;
163 ++nItem;
165 return lDestination;
168 //-----------------------------------------------------------------------------
170 * converts a sequence<PropertyValue> to an unicode string hash, where keys and values match to names and values.
172 OUStringHash Converter::convert_seqProp2OUStringHash( const css::uno::Sequence< css::beans::PropertyValue >& lSource )
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)
179 pSource[nItem].Value >>= lDestination[pSource[nItem].Name];
181 return lDestination;
184 //-----------------------------------------------------------------------------
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"
190 @param sString
191 timestamp in string notation
193 @return timestamp in DateTime notation
195 DateTime Converter::convert_String2DateTime( /*IN*/ const ::rtl::OUString& sSource )
197 DateTime aStamp ;
198 sal_Int32 nIndex = 0;
200 sal_uInt16 nDay = (sal_uInt16)(sSource.getToken( 0, (sal_Unicode)'.', nIndex ).toInt32());
201 if( nIndex>0 )
203 sal_uInt16 nMonth = (sal_uInt16)(sSource.getToken( 0, (sal_Unicode)'.', nIndex ).toInt32());
204 if( nIndex>0 )
206 sal_uInt16 nYear = (sal_uInt16)(sSource.getToken( 0, (sal_Unicode)'/', nIndex ).toInt32());
207 if( nIndex>0 )
209 sal_uInt32 nHour = sSource.getToken( 0, (sal_Unicode)':', nIndex ).toInt32();
210 if( nIndex>0 )
212 sal_uInt32 nMin = sSource.getToken( 0, (sal_Unicode)':', nIndex ).toInt32();
213 if( nIndex>0 && nIndex<sSource.getLength() )
215 sal_uInt32 nSec = sSource.copy( nIndex, sSource.getLength()-nIndex ).toInt32();
217 Date aDate( nDay , nMonth, nYear );
218 Time aTime( nHour, nMin , nSec );
219 aStamp = DateTime( aDate, aTime );
225 return aStamp;
228 //-----------------------------------------------------------------------------
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"
234 @param aStamp
235 timestamp in DateTime notation
237 @return timestamp in String notation
239 ::rtl::OUString Converter::convert_DateTime2String( /*IN*/ const DateTime& aSource )
241 ::rtl::OUStringBuffer sBuffer(25);
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() );
255 return sBuffer.makeStringAndClear();
258 ::rtl::OUString Converter::convert_DateTime2ISO8601( const DateTime& aSource )
260 ::rtl::OUStringBuffer sBuffer(25);
262 sal_Int32 nYear = aSource.GetYear();
263 sal_Int32 nMonth = aSource.GetMonth();
264 sal_Int32 nDay = aSource.GetDay();
266 sal_Int32 nHour = aSource.GetHour();
267 sal_Int32 nMin = aSource.GetMin();
268 sal_Int32 nSec = aSource.GetSec();
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 );
281 sBuffer.appendAscii("-");
282 // write month formated as "MM"
283 if (nMonth<10)
284 sBuffer.appendAscii("0");
285 sBuffer.append( (sal_Int32)nMonth );
287 sBuffer.appendAscii("-");
288 // write day formated as "DD"
289 if (nDay<10)
290 sBuffer.appendAscii("0");
291 sBuffer.append( (sal_Int32)nDay );
293 sBuffer.appendAscii("T");
294 // write hours formated as "hh"
295 if (nHour<10)
296 sBuffer.appendAscii("0");
297 sBuffer.append( (sal_Int32)nHour );
299 sBuffer.appendAscii(":");
300 // write min formated as "mm"
301 if (nMin<10)
302 sBuffer.appendAscii("0");
303 sBuffer.append( (sal_Int32)nMin );
305 sBuffer.appendAscii(":");
306 // write sec formated as "ss"
307 if (nSec<10)
308 sBuffer.appendAscii("0");
309 sBuffer.append( (sal_Int32)nSec );
311 sBuffer.appendAscii("Z");
313 return sBuffer.makeStringAndClear();
316 } // namespace framework