1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: converter.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
33 #include <classes/converter.hxx>
34 #include <rtl/ustrbuf.hxx>
38 //-----------------------------------------------------------------------------
40 * pack every property item of source list into an any entry of destination list
41 * Resulting list will have follow format then: "sequence< Any(PropertyValue) >".
42 * If one item couldn't be converted it will be ignored - means target list can
43 * be smaller then source list. Source list isn't changed anytime.
46 * (a) reserve enough space on destination list for all possible entries of
48 * (b) try to pack every property of source into an any of destination list
49 * (b1) count successfully packed entries only
50 * (c) use this count of packed entries to resize destination list
51 * Because we getted enough space before - that will remove unused items
52 * of destination list at the end of it only.
54 css::uno::Sequence
< css::uno::Any
> Converter::convert_seqProp2seqAny( const css::uno::Sequence
< css::beans::PropertyValue
>& lSource
)
56 sal_Int32 nCount
= lSource
.getLength();
57 css::uno::Sequence
< css::uno::Any
> lDestination(nCount
);
59 for (sal_Int32 nItem
=0; nItem
<nCount
; ++nItem
)
60 lDestination
[nItem
]<<=lSource
[nItem
];
65 //-----------------------------------------------------------------------------
67 * do the same like convert_seqProp2seqAny() before - but reverse.
68 * It try to unpack PropertyValue items from given Any's.
70 css::uno::Sequence
< css::beans::PropertyValue
> Converter::convert_seqAny2seqProp( const css::uno::Sequence
< css::uno::Any
>& lSource
)
72 sal_Int32 nCount
= lSource
.getLength();
73 sal_Int32 nRealCount
= 0;
74 css::uno::Sequence
< css::beans::PropertyValue
> lDestination(nCount
);
76 for (sal_Int32 nItem
=0; nItem
<nCount
; ++nItem
)
78 if (lSource
[nItem
]>>=lDestination
[nItem
])
82 if (nRealCount
!=nCount
)
83 lDestination
.realloc(nRealCount
);
88 //-----------------------------------------------------------------------------
90 * converts a sequence of NamedValue to a sequence of PropertyValue.
92 css::uno::Sequence
< css::beans::PropertyValue
> Converter::convert_seqNamedVal2seqPropVal( const css::uno::Sequence
< css::beans::NamedValue
>& lSource
)
94 sal_Int32 nCount
= lSource
.getLength();
95 css::uno::Sequence
< css::beans::PropertyValue
> lDestination(nCount
);
96 for (sal_Int32 nItem
=0; nItem
<nCount
; ++nItem
)
98 lDestination
[nItem
].Name
= lSource
[nItem
].Name
;
99 lDestination
[nItem
].Value
= lSource
[nItem
].Value
;
104 //-----------------------------------------------------------------------------
106 * converts a sequence of PropertyValue to a sequence of NamedValue.
108 css::uno::Sequence
< css::beans::NamedValue
> Converter::convert_seqPropVal2seqNamedVal( const css::uno::Sequence
< css::beans::PropertyValue
>& lSource
)
110 sal_Int32 nCount
= lSource
.getLength();
111 css::uno::Sequence
< css::beans::NamedValue
> lDestination(nCount
);
112 for (sal_Int32 nItem
=0; nItem
<nCount
; ++nItem
)
114 lDestination
[nItem
].Name
= lSource
[nItem
].Name
;
115 lDestination
[nItem
].Value
= lSource
[nItem
].Value
;
120 //-----------------------------------------------------------------------------
122 * converts a sequence of unicode strings into a vector of such items
124 OUStringList
Converter::convert_seqOUString2OUStringList( const css::uno::Sequence
< ::rtl::OUString
>& lSource
)
126 OUStringList lDestination
;
127 sal_Int32 nCount
= lSource
.getLength();
129 for (sal_Int32 nItem
=0; nItem
<nCount
; ++nItem
)
131 lDestination
.push_back(lSource
[nItem
]);
137 //-----------------------------------------------------------------------------
139 * converts a vector of unicode strings into a sequence of such items
141 css::uno::Sequence
< ::rtl::OUString
> Converter::convert_OUStringList2seqOUString( const OUStringList
& lSource
)
143 css::uno::Sequence
< ::rtl::OUString
> lDestination(lSource
.size());
144 sal_uInt32 nItem
= 0;
145 for (OUStringList::const_iterator pIterator
=lSource
.begin(); pIterator
!=lSource
.end(); ++pIterator
)
147 lDestination
[nItem
] = *pIterator
;
153 //-----------------------------------------------------------------------------
155 * converts an unicode string hash to a sequence<PropertyValue>, where names and values match to key and values.
157 css::uno::Sequence
< css::beans::PropertyValue
> Converter::convert_OUStringHash2seqProp( const OUStringHash
& lSource
)
159 css::uno::Sequence
< css::beans::PropertyValue
> lDestination (lSource
.size());
160 css::beans::PropertyValue
* pDestination
= lDestination
.getArray();
162 for (OUStringHash::const_iterator pItem
=lSource
.begin(); pItem
!=lSource
.end(); ++pItem
)
164 pDestination
[nItem
].Name
= pItem
->first
;
165 pDestination
[nItem
].Value
<<= pItem
->second
;
171 //-----------------------------------------------------------------------------
173 * converts a sequence<PropertyValue> to an unicode string hash, where keys and values match to names and values.
175 OUStringHash
Converter::convert_seqProp2OUStringHash( const css::uno::Sequence
< css::beans::PropertyValue
>& lSource
)
177 OUStringHash lDestination
;
178 sal_Int32 nCount
= lSource
.getLength();
179 const css::beans::PropertyValue
* pSource
= lSource
.getConstArray();
180 for (sal_Int32 nItem
=0; nItem
<nCount
; ++nItem
)
182 pSource
[nItem
].Value
>>= lDestination
[pSource
[nItem
].Name
];
187 //-----------------------------------------------------------------------------
189 @short convert timestamp from String to tools::DateTime notation
190 @descr Format: "<day>.<month>.<year>/<hour>:<min>:<sec>"
191 e.g. : "1.11.2001/13:45:16"
194 timestamp in string notation
196 @return timestamp in DateTime notation
198 DateTime
Converter::convert_String2DateTime( /*IN*/ const ::rtl::OUString
& sSource
)
201 sal_Int32 nIndex
= 0;
203 sal_uInt16 nDay
= (sal_uInt16
)(sSource
.getToken( 0, (sal_Unicode
)'.', nIndex
).toInt32());
206 sal_uInt16 nMonth
= (sal_uInt16
)(sSource
.getToken( 0, (sal_Unicode
)'.', nIndex
).toInt32());
209 sal_uInt16 nYear
= (sal_uInt16
)(sSource
.getToken( 0, (sal_Unicode
)'/', nIndex
).toInt32());
212 sal_uInt32 nHour
= sSource
.getToken( 0, (sal_Unicode
)':', nIndex
).toInt32();
215 sal_uInt32 nMin
= sSource
.getToken( 0, (sal_Unicode
)':', nIndex
).toInt32();
216 if( nIndex
>0 && nIndex
<sSource
.getLength() )
218 sal_uInt32 nSec
= sSource
.copy( nIndex
, sSource
.getLength()-nIndex
).toInt32();
220 Date
aDate( nDay
, nMonth
, nYear
);
221 Time
aTime( nHour
, nMin
, nSec
);
222 aStamp
= DateTime( aDate
, aTime
);
231 //-----------------------------------------------------------------------------
233 @short convert timestamp from DateTime to String notation
234 @descr Format: "<day>.<month>.<year>/<hour>:<min>:<sec>"
235 e.g. : "1.11.2001/13:45:16"
238 timestamp in DateTime notation
240 @return timestamp in String notation
242 ::rtl::OUString
Converter::convert_DateTime2String( /*IN*/ const DateTime
& aSource
)
244 ::rtl::OUStringBuffer
sBuffer(25);
246 sBuffer
.append( (sal_Int32
)aSource
.GetDay() );
247 sBuffer
.append( (sal_Unicode
)'.' );
248 sBuffer
.append( (sal_Int32
)aSource
.GetMonth() );
249 sBuffer
.append( (sal_Unicode
)'.' );
250 sBuffer
.append( (sal_Int32
)aSource
.GetYear() );
251 sBuffer
.append( (sal_Unicode
)'/' );
252 sBuffer
.append( (sal_Int32
)aSource
.GetHour() );
253 sBuffer
.append( (sal_Unicode
)':' );
254 sBuffer
.append( (sal_Int32
)aSource
.GetMin() );
255 sBuffer
.append( (sal_Unicode
)':' );
256 sBuffer
.append( (sal_Int32
)aSource
.GetSec() );
258 return sBuffer
.makeStringAndClear();
261 ::rtl::OUString
Converter::convert_DateTime2ISO8601( const DateTime
& aSource
)
263 ::rtl::OUStringBuffer
sBuffer(25);
265 sal_Int32 nYear
= aSource
.GetYear();
266 sal_Int32 nMonth
= aSource
.GetMonth();
267 sal_Int32 nDay
= aSource
.GetDay();
269 sal_Int32 nHour
= aSource
.GetHour();
270 sal_Int32 nMin
= aSource
.GetMin();
271 sal_Int32 nSec
= aSource
.GetSec();
273 // write year formated as "YYYY"
275 sBuffer
.appendAscii("000");
278 sBuffer
.appendAscii("00");
281 sBuffer
.appendAscii("0");
282 sBuffer
.append( (sal_Int32
)nYear
);
284 sBuffer
.appendAscii("-");
285 // write month formated as "MM"
287 sBuffer
.appendAscii("0");
288 sBuffer
.append( (sal_Int32
)nMonth
);
290 sBuffer
.appendAscii("-");
291 // write day formated as "DD"
293 sBuffer
.appendAscii("0");
294 sBuffer
.append( (sal_Int32
)nDay
);
296 sBuffer
.appendAscii("T");
297 // write hours formated as "hh"
299 sBuffer
.appendAscii("0");
300 sBuffer
.append( (sal_Int32
)nHour
);
302 sBuffer
.appendAscii(":");
303 // write min formated as "mm"
305 sBuffer
.appendAscii("0");
306 sBuffer
.append( (sal_Int32
)nMin
);
308 sBuffer
.appendAscii(":");
309 // write sec formated as "ss"
311 sBuffer
.appendAscii("0");
312 sBuffer
.append( (sal_Int32
)nSec
);
314 sBuffer
.appendAscii("Z");
316 return sBuffer
.makeStringAndClear();
319 } // namespace framework