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