Update ooo320-m1
[ooovba.git] / comphelper / source / misc / sequenceashashmap.cxx
blob907c45e47b48b67a3f27ec6427446ec7f5e85faf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sequenceashashmap.cxx,v $
10 * $Revision: 1.8 $
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_comphelper.hxx"
34 //_______________________________________________
35 // includes
36 #include <comphelper/sequenceashashmap.hxx>
38 //_______________________________________________
39 // namespace
41 namespace comphelper{
43 namespace css = ::com::sun::star;
45 //_______________________________________________
46 // definitions
48 /*-----------------------------------------------
49 04.11.2003 09:29
50 -----------------------------------------------*/
51 SequenceAsHashMap::SequenceAsHashMap()
52 : SequenceAsHashMapBase()
56 /*-----------------------------------------------
57 04.11.2003 08:30
58 -----------------------------------------------*/
59 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Any& aSource)
61 (*this) << aSource;
64 //-----------------------------------------------
65 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::uno::Any >& lSource)
67 (*this) << lSource;
70 /*-----------------------------------------------
71 04.11.2003 08:30
72 -----------------------------------------------*/
73 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
75 (*this) << lSource;
78 /*-----------------------------------------------
79 04.11.2003 08:30
80 -----------------------------------------------*/
81 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::beans::NamedValue >& lSource)
83 (*this) << lSource;
86 /*-----------------------------------------------
87 04.11.2003 09:04
88 -----------------------------------------------*/
89 SequenceAsHashMap::~SequenceAsHashMap()
93 /*-----------------------------------------------
94 04.11.2003 10:21
95 -----------------------------------------------*/
96 void SequenceAsHashMap::operator<<(const css::uno::Any& aSource)
98 // An empty Any reset this instance!
99 if (!aSource.hasValue())
101 clear();
102 return;
105 css::uno::Sequence< css::beans::NamedValue > lN;
106 if (aSource >>= lN)
108 (*this) << lN;
109 return;
112 css::uno::Sequence< css::beans::PropertyValue > lP;
113 if (aSource >>= lP)
115 (*this) << lP;
116 return;
119 throw css::beans::IllegalTypeException(
120 ::rtl::OUString::createFromAscii("Any contains wrong type."),
121 css::uno::Reference< css::uno::XInterface >());
124 //-----------------------------------------------
125 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::uno::Any >& lSource)
127 sal_Int32 c = lSource.getLength();
128 sal_Int32 i = 0;
130 for (i=0; i<c; ++i)
132 css::beans::PropertyValue lP;
133 if (lSource[i] >>= lP)
135 if (
136 (!lP.Name.getLength()) ||
137 (!lP.Value.hasValue())
139 throw css::beans::IllegalTypeException(
140 ::rtl::OUString::createFromAscii("PropertyValue struct contains no usefull informations."),
141 css::uno::Reference< css::uno::XInterface >());
142 (*this)[lP.Name] = lP.Value;
143 continue;
146 css::beans::NamedValue lN;
147 if (lSource[i] >>= lN)
149 if (
150 (!lN.Name.getLength()) ||
151 (!lN.Value.hasValue())
153 throw css::beans::IllegalTypeException(
154 ::rtl::OUString::createFromAscii("NamedValue struct contains no usefull informations."),
155 css::uno::Reference< css::uno::XInterface >());
156 (*this)[lN.Name] = lN.Value;
157 continue;
160 // ignore VOID Any ... but reject wrong filled ones!
161 if (lSource[i].hasValue())
162 throw css::beans::IllegalTypeException(
163 ::rtl::OUString::createFromAscii("Any contains wrong type."),
164 css::uno::Reference< css::uno::XInterface >());
168 /*-----------------------------------------------
169 04.11.2003 08:30
170 -----------------------------------------------*/
171 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
173 clear();
175 sal_Int32 c = lSource.getLength();
176 const css::beans::PropertyValue* pSource = lSource.getConstArray();
178 for (sal_Int32 i=0; i<c; ++i)
179 (*this)[pSource[i].Name] = pSource[i].Value;
182 /*-----------------------------------------------
183 04.11.2003 08:30
184 -----------------------------------------------*/
185 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::NamedValue >& lSource)
187 clear();
189 sal_Int32 c = lSource.getLength();
190 const css::beans::NamedValue* pSource = lSource.getConstArray();
192 for (sal_Int32 i=0; i<c; ++i)
193 (*this)[pSource[i].Name] = pSource[i].Value;
196 /*-----------------------------------------------
197 04.11.2003 08:30
198 -----------------------------------------------*/
199 void SequenceAsHashMap::operator>>(css::uno::Sequence< css::beans::PropertyValue >& lDestination) const
201 sal_Int32 c = (sal_Int32)size();
202 lDestination.realloc(c);
203 css::beans::PropertyValue* pDestination = lDestination.getArray();
205 sal_Int32 i = 0;
206 for (const_iterator pThis = begin();
207 pThis != end() ;
208 ++pThis )
210 pDestination[i].Name = pThis->first ;
211 pDestination[i].Value = pThis->second;
212 ++i;
216 /*-----------------------------------------------
217 04.11.2003 08:30
218 -----------------------------------------------*/
219 void SequenceAsHashMap::operator>>(css::uno::Sequence< css::beans::NamedValue >& lDestination) const
221 sal_Int32 c = (sal_Int32)size();
222 lDestination.realloc(c);
223 css::beans::NamedValue* pDestination = lDestination.getArray();
225 sal_Int32 i = 0;
226 for (const_iterator pThis = begin();
227 pThis != end() ;
228 ++pThis )
230 pDestination[i].Name = pThis->first ;
231 pDestination[i].Value = pThis->second;
232 ++i;
236 /*-----------------------------------------------
237 30.07.2007 14:10
238 -----------------------------------------------*/
239 const css::uno::Any SequenceAsHashMap::getAsConstAny(::sal_Bool bAsPropertyValueList) const
241 css::uno::Any aDestination;
242 if (bAsPropertyValueList)
243 aDestination = css::uno::makeAny(getAsConstPropertyValueList());
244 else
245 aDestination = css::uno::makeAny(getAsConstNamedValueList());
246 return aDestination;
249 /*-----------------------------------------------
250 30.07.2007 14:10
251 -----------------------------------------------*/
252 const css::uno::Sequence< css::uno::Any > SequenceAsHashMap::getAsConstAnyList(::sal_Bool bAsPropertyValueList) const
254 ::sal_Int32 i = 0;
255 ::sal_Int32 c = (::sal_Int32)size();
256 css::uno::Sequence< css::uno::Any > lDestination(c);
257 css::uno::Any* pDestination = lDestination.getArray();
259 for (const_iterator pThis = begin();
260 pThis != end() ;
261 ++pThis )
263 if (bAsPropertyValueList)
265 css::beans::PropertyValue aProp;
266 aProp.Name = pThis->first;
267 aProp.Value = pThis->second;
268 pDestination[i] = css::uno::makeAny(aProp);
270 else
272 css::beans::NamedValue aProp;
273 aProp.Name = pThis->first;
274 aProp.Value = pThis->second;
275 pDestination[i] = css::uno::makeAny(aProp);
278 ++i;
281 return lDestination;
284 /*-----------------------------------------------
285 04.11.2003 08:30
286 -----------------------------------------------*/
287 const css::uno::Sequence< css::beans::NamedValue > SequenceAsHashMap::getAsConstNamedValueList() const
289 css::uno::Sequence< css::beans::NamedValue > lReturn;
290 (*this) >> lReturn;
291 return lReturn;
294 /*-----------------------------------------------
295 04.11.2003 08:30
296 -----------------------------------------------*/
297 const css::uno::Sequence< css::beans::PropertyValue > SequenceAsHashMap::getAsConstPropertyValueList() const
299 css::uno::Sequence< css::beans::PropertyValue > lReturn;
300 (*this) >> lReturn;
301 return lReturn;
304 /*-----------------------------------------------
305 07.03.2007 12:45
306 -----------------------------------------------*/
307 sal_Bool SequenceAsHashMap::match(const SequenceAsHashMap& rCheck) const
309 const_iterator pCheck;
310 for ( pCheck = rCheck.begin();
311 pCheck != rCheck.end() ;
312 ++pCheck )
314 const ::rtl::OUString& sCheckName = pCheck->first;
315 const css::uno::Any& aCheckValue = pCheck->second;
316 const_iterator pFound = find(sCheckName);
318 if (pFound == end())
319 return sal_False;
321 const css::uno::Any& aFoundValue = pFound->second;
322 if (aFoundValue != aCheckValue)
323 return sal_False;
326 return sal_True;
329 /*-----------------------------------------------
330 30.07.2007 14:30
331 -----------------------------------------------*/
332 void SequenceAsHashMap::update(const SequenceAsHashMap& rUpdate)
334 const_iterator pUpdate;
335 for ( pUpdate = rUpdate.begin();
336 pUpdate != rUpdate.end() ;
337 ++pUpdate )
339 const ::rtl::OUString& sName = pUpdate->first;
340 const css::uno::Any& aValue = pUpdate->second;
342 (*this)[sName] = aValue;
346 /*-----------------------------------------------
347 04.11.2003 08:30
348 -----------------------------------------------*/
349 #if OSL_DEBUG_LEVEL > 1
350 void SequenceAsHashMap::dbg_dumpToFile(const char* pFileName,
351 const char* pComment ) const
353 if (!pFileName || !pComment)
354 return;
356 FILE* pFile = fopen(pFileName, "a");
357 if (!pFile)
358 return;
360 ::rtl::OUStringBuffer sBuffer(1000);
361 sBuffer.appendAscii("\n----------------------------------------\n");
362 sBuffer.appendAscii(pComment );
363 sBuffer.appendAscii("\n----------------------------------------\n");
364 sal_Int32 i = 0;
365 for (const_iterator pIt = begin();
366 pIt != end() ;
367 ++pIt )
369 sBuffer.appendAscii("[" );
370 sBuffer.append (i++ );
371 sBuffer.appendAscii("] " );
372 sBuffer.appendAscii("\"" );
373 sBuffer.append (pIt->first);
374 sBuffer.appendAscii("\" = \"" );
376 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xv;
377 ::rtl::OUString sv;
378 sal_Int32 nv;
379 sal_Bool bv;
381 if (pIt->second >>= sv)
382 sBuffer.append(sv);
383 else
384 if (pIt->second >>= nv)
385 sBuffer.append(nv);
386 else
387 if (pIt->second >>= bv)
388 sBuffer.appendAscii(bv ? "true" : "false");
389 else
390 if (pIt->second >>= xv)
391 sBuffer.appendAscii(xv.is() ? "object" : "null");
392 else
393 sBuffer.appendAscii("???");
395 sBuffer.appendAscii("\"\n");
398 fprintf(pFile, ::rtl::OUStringToOString(sBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8).getStr());
399 fclose(pFile);
401 #endif // OSL_DEBUG_LEVEL > 1
403 } // namespace comphelper