merged tag ooo/OOO330_m14
[LibreOffice.git] / comphelper / source / misc / sequenceashashmap.cxx
bloba79ffc9037cc0dc0803f9e83aa5c5447c162cbea
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
31 //_______________________________________________
32 // includes
33 #include <comphelper/sequenceashashmap.hxx>
35 //_______________________________________________
36 // namespace
38 namespace comphelper{
40 namespace css = ::com::sun::star;
42 //_______________________________________________
43 // definitions
45 /*-----------------------------------------------
46 04.11.2003 09:29
47 -----------------------------------------------*/
48 SequenceAsHashMap::SequenceAsHashMap()
49 : SequenceAsHashMapBase()
53 /*-----------------------------------------------
54 04.11.2003 08:30
55 -----------------------------------------------*/
56 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Any& aSource)
58 (*this) << aSource;
61 //-----------------------------------------------
62 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::uno::Any >& lSource)
64 (*this) << lSource;
67 /*-----------------------------------------------
68 04.11.2003 08:30
69 -----------------------------------------------*/
70 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
72 (*this) << lSource;
75 /*-----------------------------------------------
76 04.11.2003 08:30
77 -----------------------------------------------*/
78 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::beans::NamedValue >& lSource)
80 (*this) << lSource;
83 /*-----------------------------------------------
84 04.11.2003 09:04
85 -----------------------------------------------*/
86 SequenceAsHashMap::~SequenceAsHashMap()
90 /*-----------------------------------------------
91 04.11.2003 10:21
92 -----------------------------------------------*/
93 void SequenceAsHashMap::operator<<(const css::uno::Any& aSource)
95 // An empty Any reset this instance!
96 if (!aSource.hasValue())
98 clear();
99 return;
102 css::uno::Sequence< css::beans::NamedValue > lN;
103 if (aSource >>= lN)
105 (*this) << lN;
106 return;
109 css::uno::Sequence< css::beans::PropertyValue > lP;
110 if (aSource >>= lP)
112 (*this) << lP;
113 return;
116 throw css::beans::IllegalTypeException(
117 ::rtl::OUString::createFromAscii("Any contains wrong type."),
118 css::uno::Reference< css::uno::XInterface >());
121 //-----------------------------------------------
122 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::uno::Any >& lSource)
124 sal_Int32 c = lSource.getLength();
125 sal_Int32 i = 0;
127 for (i=0; i<c; ++i)
129 css::beans::PropertyValue lP;
130 if (lSource[i] >>= lP)
132 if (
133 (!lP.Name.getLength()) ||
134 (!lP.Value.hasValue())
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;
143 css::beans::NamedValue lN;
144 if (lSource[i] >>= lN)
146 if (
147 (!lN.Name.getLength()) ||
148 (!lN.Value.hasValue())
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;
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 >());
165 /*-----------------------------------------------
166 04.11.2003 08:30
167 -----------------------------------------------*/
168 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
170 clear();
172 sal_Int32 c = lSource.getLength();
173 const css::beans::PropertyValue* pSource = lSource.getConstArray();
175 for (sal_Int32 i=0; i<c; ++i)
176 (*this)[pSource[i].Name] = pSource[i].Value;
179 /*-----------------------------------------------
180 04.11.2003 08:30
181 -----------------------------------------------*/
182 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::NamedValue >& lSource)
184 clear();
186 sal_Int32 c = lSource.getLength();
187 const css::beans::NamedValue* pSource = lSource.getConstArray();
189 for (sal_Int32 i=0; i<c; ++i)
190 (*this)[pSource[i].Name] = pSource[i].Value;
193 /*-----------------------------------------------
194 04.11.2003 08:30
195 -----------------------------------------------*/
196 void SequenceAsHashMap::operator>>(css::uno::Sequence< css::beans::PropertyValue >& lDestination) const
198 sal_Int32 c = (sal_Int32)size();
199 lDestination.realloc(c);
200 css::beans::PropertyValue* pDestination = lDestination.getArray();
202 sal_Int32 i = 0;
203 for (const_iterator pThis = begin();
204 pThis != end() ;
205 ++pThis )
207 pDestination[i].Name = pThis->first ;
208 pDestination[i].Value = pThis->second;
209 ++i;
213 /*-----------------------------------------------
214 04.11.2003 08:30
215 -----------------------------------------------*/
216 void SequenceAsHashMap::operator>>(css::uno::Sequence< css::beans::NamedValue >& lDestination) const
218 sal_Int32 c = (sal_Int32)size();
219 lDestination.realloc(c);
220 css::beans::NamedValue* pDestination = lDestination.getArray();
222 sal_Int32 i = 0;
223 for (const_iterator pThis = begin();
224 pThis != end() ;
225 ++pThis )
227 pDestination[i].Name = pThis->first ;
228 pDestination[i].Value = pThis->second;
229 ++i;
233 /*-----------------------------------------------
234 30.07.2007 14:10
235 -----------------------------------------------*/
236 const css::uno::Any SequenceAsHashMap::getAsConstAny(::sal_Bool bAsPropertyValueList) const
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;
246 /*-----------------------------------------------
247 30.07.2007 14:10
248 -----------------------------------------------*/
249 const css::uno::Sequence< css::uno::Any > SequenceAsHashMap::getAsConstAnyList(::sal_Bool bAsPropertyValueList) const
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();
256 for (const_iterator pThis = begin();
257 pThis != end() ;
258 ++pThis )
260 if (bAsPropertyValueList)
262 css::beans::PropertyValue aProp;
263 aProp.Name = pThis->first;
264 aProp.Value = pThis->second;
265 pDestination[i] = css::uno::makeAny(aProp);
267 else
269 css::beans::NamedValue aProp;
270 aProp.Name = pThis->first;
271 aProp.Value = pThis->second;
272 pDestination[i] = css::uno::makeAny(aProp);
275 ++i;
278 return lDestination;
281 /*-----------------------------------------------
282 04.11.2003 08:30
283 -----------------------------------------------*/
284 const css::uno::Sequence< css::beans::NamedValue > SequenceAsHashMap::getAsConstNamedValueList() const
286 css::uno::Sequence< css::beans::NamedValue > lReturn;
287 (*this) >> lReturn;
288 return lReturn;
291 /*-----------------------------------------------
292 04.11.2003 08:30
293 -----------------------------------------------*/
294 const css::uno::Sequence< css::beans::PropertyValue > SequenceAsHashMap::getAsConstPropertyValueList() const
296 css::uno::Sequence< css::beans::PropertyValue > lReturn;
297 (*this) >> lReturn;
298 return lReturn;
301 /*-----------------------------------------------
302 07.03.2007 12:45
303 -----------------------------------------------*/
304 sal_Bool SequenceAsHashMap::match(const SequenceAsHashMap& rCheck) const
306 const_iterator pCheck;
307 for ( pCheck = rCheck.begin();
308 pCheck != rCheck.end() ;
309 ++pCheck )
311 const ::rtl::OUString& sCheckName = pCheck->first;
312 const css::uno::Any& aCheckValue = pCheck->second;
313 const_iterator pFound = find(sCheckName);
315 if (pFound == end())
316 return sal_False;
318 const css::uno::Any& aFoundValue = pFound->second;
319 if (aFoundValue != aCheckValue)
320 return sal_False;
323 return sal_True;
326 /*-----------------------------------------------
327 30.07.2007 14:30
328 -----------------------------------------------*/
329 void SequenceAsHashMap::update(const SequenceAsHashMap& rUpdate)
331 const_iterator pUpdate;
332 for ( pUpdate = rUpdate.begin();
333 pUpdate != rUpdate.end() ;
334 ++pUpdate )
336 const ::rtl::OUString& sName = pUpdate->first;
337 const css::uno::Any& aValue = pUpdate->second;
339 (*this)[sName] = aValue;
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
350 if (!pFileName || !pComment)
351 return;
353 FILE* pFile = fopen(pFileName, "a");
354 if (!pFile)
355 return;
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 )
366 sBuffer.appendAscii("[" );
367 sBuffer.append (i++ );
368 sBuffer.appendAscii("] " );
369 sBuffer.appendAscii("\"" );
370 sBuffer.append (pIt->first);
371 sBuffer.appendAscii("\" = \"" );
373 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xv;
374 ::rtl::OUString sv;
375 sal_Int32 nv;
376 sal_Bool bv;
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("???");
392 sBuffer.appendAscii("\"\n");
395 fprintf(pFile, ::rtl::OUStringToOString(sBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8).getStr());
396 fclose(pFile);
398 #endif // OSL_DEBUG_LEVEL > 1
400 } // namespace comphelper