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