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