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_comphelper.hxx"
30 
31 //_______________________________________________
32 // includes
33 #include <comphelper/sequenceashashmap.hxx>
34 
35 //_______________________________________________
36 // namespace
37 
38 namespace comphelper{
39 
40 namespace css = ::com::sun::star;
41 
42 //_______________________________________________
43 // definitions
44 
45 /*-----------------------------------------------
46     04.11.2003 09:29
47 -----------------------------------------------*/
48 SequenceAsHashMap::SequenceAsHashMap()
49     : SequenceAsHashMapBase()
50 {
51 }
52 
53 /*-----------------------------------------------
54     04.11.2003 08:30
55 -----------------------------------------------*/
56 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Any& aSource)
57 {
58     (*this) << aSource;
59 }
60 
61 //-----------------------------------------------
62 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::uno::Any >& lSource)
63 {
64     (*this) << lSource;
65 }
66 
67 /*-----------------------------------------------
68     04.11.2003 08:30
69 -----------------------------------------------*/
70 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
71 {
72     (*this) << lSource;
73 }
74 
75 /*-----------------------------------------------
76     04.11.2003 08:30
77 -----------------------------------------------*/
78 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::beans::NamedValue >& lSource)
79 {
80     (*this) << lSource;
81 }
82 
83 /*-----------------------------------------------
84     04.11.2003 09:04
85 -----------------------------------------------*/
86 SequenceAsHashMap::~SequenceAsHashMap()
87 {
88 }
89 
90 /*-----------------------------------------------
91     04.11.2003 10:21
92 -----------------------------------------------*/
93 void SequenceAsHashMap::operator<<(const css::uno::Any& aSource)
94 {
95     // An empty Any reset this instance!
96     if (!aSource.hasValue())
97     {
98         clear();
99         return;
100     }
101 
102     css::uno::Sequence< css::beans::NamedValue > lN;
103     if (aSource >>= lN)
104     {
105         (*this) << lN;
106         return;
107     }
108 
109     css::uno::Sequence< css::beans::PropertyValue > lP;
110     if (aSource >>= lP)
111     {
112         (*this) << lP;
113         return;
114     }
115 
116     throw css::beans::IllegalTypeException(
117             ::rtl::OUString::createFromAscii("Any contains wrong type."),
118             css::uno::Reference< css::uno::XInterface >());
119 }
120 
121 //-----------------------------------------------
122 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::uno::Any >& lSource)
123 {
124     sal_Int32 c = lSource.getLength();
125     sal_Int32 i = 0;
126 
127     for (i=0; i<c; ++i)
128     {
129         css::beans::PropertyValue lP;
130         if (lSource[i] >>= lP)
131         {
132             if (
133                 (!lP.Name.getLength()) ||
134                 (!lP.Value.hasValue())
135                )
136                 throw css::beans::IllegalTypeException(
137                         ::rtl::OUString::createFromAscii("PropertyValue struct contains no usefull informations."),
138                         css::uno::Reference< css::uno::XInterface >());
139             (*this)[lP.Name] = lP.Value;
140             continue;
141         }
142 
143         css::beans::NamedValue lN;
144         if (lSource[i] >>= lN)
145         {
146             if (
147                 (!lN.Name.getLength()) ||
148                 (!lN.Value.hasValue())
149                )
150                 throw css::beans::IllegalTypeException(
151                         ::rtl::OUString::createFromAscii("NamedValue struct contains no usefull informations."),
152                         css::uno::Reference< css::uno::XInterface >());
153             (*this)[lN.Name] = lN.Value;
154             continue;
155         }
156 
157         // ignore VOID Any ... but reject wrong filled ones!
158         if (lSource[i].hasValue())
159             throw css::beans::IllegalTypeException(
160                     ::rtl::OUString::createFromAscii("Any contains wrong type."),
161                     css::uno::Reference< css::uno::XInterface >());
162     }
163 }
164 
165 /*-----------------------------------------------
166     04.11.2003 08:30
167 -----------------------------------------------*/
168 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
169 {
170     clear();
171 
172           sal_Int32                  c       = lSource.getLength();
173     const css::beans::PropertyValue* pSource = lSource.getConstArray();
174 
175     for (sal_Int32 i=0; i<c; ++i)
176         (*this)[pSource[i].Name] = pSource[i].Value;
177 }
178 
179 /*-----------------------------------------------
180     04.11.2003 08:30
181 -----------------------------------------------*/
182 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::NamedValue >& lSource)
183 {
184     clear();
185 
186           sal_Int32               c       = lSource.getLength();
187     const css::beans::NamedValue* pSource = lSource.getConstArray();
188 
189     for (sal_Int32 i=0; i<c; ++i)
190         (*this)[pSource[i].Name] = pSource[i].Value;
191 }
192 
193 /*-----------------------------------------------
194     04.11.2003 08:30
195 -----------------------------------------------*/
196 void SequenceAsHashMap::operator>>(css::uno::Sequence< css::beans::PropertyValue >& lDestination) const
197 {
198     sal_Int32 c = (sal_Int32)size();
199     lDestination.realloc(c);
200     css::beans::PropertyValue* pDestination = lDestination.getArray();
201 
202     sal_Int32 i = 0;
203     for (const_iterator pThis  = begin();
204                         pThis != end()  ;
205                       ++pThis           )
206     {
207         pDestination[i].Name  = pThis->first ;
208         pDestination[i].Value = pThis->second;
209         ++i;
210     }
211 }
212 
213 /*-----------------------------------------------
214     04.11.2003 08:30
215 -----------------------------------------------*/
216 void SequenceAsHashMap::operator>>(css::uno::Sequence< css::beans::NamedValue >& lDestination) const
217 {
218     sal_Int32 c = (sal_Int32)size();
219     lDestination.realloc(c);
220     css::beans::NamedValue* pDestination = lDestination.getArray();
221 
222     sal_Int32 i = 0;
223     for (const_iterator pThis  = begin();
224                         pThis != end()  ;
225                       ++pThis           )
226     {
227         pDestination[i].Name  = pThis->first ;
228         pDestination[i].Value = pThis->second;
229         ++i;
230     }
231 }
232 
233 /*-----------------------------------------------
234     30.07.2007 14:10
235 -----------------------------------------------*/
236 const css::uno::Any SequenceAsHashMap::getAsConstAny(::sal_Bool bAsPropertyValueList) const
237 {
238     css::uno::Any aDestination;
239     if (bAsPropertyValueList)
240         aDestination = css::uno::makeAny(getAsConstPropertyValueList());
241     else
242         aDestination = css::uno::makeAny(getAsConstNamedValueList());
243     return aDestination;
244 }
245 
246 /*-----------------------------------------------
247     30.07.2007 14:10
248 -----------------------------------------------*/
249 const css::uno::Sequence< css::uno::Any > SequenceAsHashMap::getAsConstAnyList(::sal_Bool bAsPropertyValueList) const
250 {
251     ::sal_Int32                         i            = 0;
252     ::sal_Int32                         c            = (::sal_Int32)size();
253     css::uno::Sequence< css::uno::Any > lDestination(c);
254     css::uno::Any*                      pDestination = lDestination.getArray();
255 
256     for (const_iterator pThis  = begin();
257                         pThis != end()  ;
258                       ++pThis           )
259     {
260         if (bAsPropertyValueList)
261         {
262             css::beans::PropertyValue aProp;
263             aProp.Name      = pThis->first;
264             aProp.Value     = pThis->second;
265             pDestination[i] = css::uno::makeAny(aProp);
266         }
267         else
268         {
269             css::beans::NamedValue aProp;
270             aProp.Name      = pThis->first;
271             aProp.Value     = pThis->second;
272             pDestination[i] = css::uno::makeAny(aProp);
273         }
274 
275         ++i;
276     }
277 
278     return lDestination;
279 }
280 
281 /*-----------------------------------------------
282     04.11.2003 08:30
283 -----------------------------------------------*/
284 const css::uno::Sequence< css::beans::NamedValue > SequenceAsHashMap::getAsConstNamedValueList() const
285 {
286     css::uno::Sequence< css::beans::NamedValue > lReturn;
287     (*this) >> lReturn;
288     return lReturn;
289 }
290 
291 /*-----------------------------------------------
292     04.11.2003 08:30
293 -----------------------------------------------*/
294 const css::uno::Sequence< css::beans::PropertyValue > SequenceAsHashMap::getAsConstPropertyValueList() const
295 {
296     css::uno::Sequence< css::beans::PropertyValue > lReturn;
297     (*this) >> lReturn;
298     return lReturn;
299 }
300 
301 /*-----------------------------------------------
302     07.03.2007 12:45
303 -----------------------------------------------*/
304 sal_Bool SequenceAsHashMap::match(const SequenceAsHashMap& rCheck) const
305 {
306     const_iterator pCheck;
307     for (  pCheck  = rCheck.begin();
308            pCheck != rCheck.end()  ;
309          ++pCheck                  )
310     {
311         const ::rtl::OUString& sCheckName  = pCheck->first;
312         const css::uno::Any&   aCheckValue = pCheck->second;
313               const_iterator   pFound      = find(sCheckName);
314 
315         if (pFound == end())
316             return sal_False;
317 
318         const css::uno::Any& aFoundValue = pFound->second;
319         if (aFoundValue != aCheckValue)
320             return sal_False;
321     }
322 
323     return sal_True;
324 }
325 
326 /*-----------------------------------------------
327     30.07.2007 14:30
328 -----------------------------------------------*/
329 void SequenceAsHashMap::update(const SequenceAsHashMap& rUpdate)
330 {
331     const_iterator pUpdate;
332     for (  pUpdate  = rUpdate.begin();
333            pUpdate != rUpdate.end()  ;
334          ++pUpdate                   )
335     {
336         const ::rtl::OUString& sName  = pUpdate->first;
337         const css::uno::Any&   aValue = pUpdate->second;
338 
339         (*this)[sName] = aValue;
340     }
341 }
342 
343 /*-----------------------------------------------
344     04.11.2003 08:30
345 -----------------------------------------------*/
346 #if OSL_DEBUG_LEVEL > 1
347 void SequenceAsHashMap::dbg_dumpToFile(const char* pFileName,
348                                        const char* pComment ) const
349 {
350     if (!pFileName || !pComment)
351         return;
352 
353     FILE* pFile = fopen(pFileName, "a");
354     if (!pFile)
355         return;
356 
357     ::rtl::OUStringBuffer sBuffer(1000);
358     sBuffer.appendAscii("\n----------------------------------------\n");
359     sBuffer.appendAscii(pComment                                      );
360     sBuffer.appendAscii("\n----------------------------------------\n");
361     sal_Int32 i = 0;
362     for (const_iterator pIt  = begin();
363                         pIt != end()  ;
364                       ++pIt           )
365     {
366         sBuffer.appendAscii("["       );
367         sBuffer.append     (i++       );
368         sBuffer.appendAscii("] "      );
369         sBuffer.appendAscii("\""      );
370         sBuffer.append     (pIt->first);
371         sBuffer.appendAscii("\" = \"" );
372 
373         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xv;
374         ::rtl::OUString                                                       sv;
375         sal_Int32                                                             nv;
376         sal_Bool                                                              bv;
377 
378         if (pIt->second >>= sv)
379             sBuffer.append(sv);
380         else
381         if (pIt->second >>= nv)
382             sBuffer.append(nv);
383         else
384         if (pIt->second >>= bv)
385             sBuffer.appendAscii(bv ? "true" : "false");
386         else
387         if (pIt->second >>= xv)
388             sBuffer.appendAscii(xv.is() ? "object" : "null");
389         else
390             sBuffer.appendAscii("???");
391 
392         sBuffer.appendAscii("\"\n");
393     }
394 
395     fprintf(pFile, ::rtl::OUStringToOString(sBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8).getStr());
396     fclose(pFile);
397 }
398 #endif // OSL_DEBUG_LEVEL > 1
399 
400 } // namespace comphelper
401